monastery 3.0.20 → 3.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +2 -0
- package/lib/util.js +23 -20
- package/package.json +1 -1
- package/test/util.js +3 -2
package/changelog.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [3.0.21](https://github.com/boycce/monastery/compare/3.0.20...3.0.21) (2024-05-07)
|
|
6
|
+
|
|
5
7
|
### [3.0.20](https://github.com/boycce/monastery/compare/3.0.19...3.0.20) (2024-05-05)
|
|
6
8
|
|
|
7
9
|
### [3.0.19](https://github.com/boycce/monastery/compare/3.0.18...3.0.19) (2024-05-05)
|
package/lib/util.js
CHANGED
|
@@ -193,7 +193,8 @@ module.exports = {
|
|
|
193
193
|
* @param {object}
|
|
194
194
|
* @return promise(data)
|
|
195
195
|
*/
|
|
196
|
-
|
|
196
|
+
let data = this.parseDotNotation(obj)
|
|
197
|
+
return this.parseFormData(data)
|
|
197
198
|
},
|
|
198
199
|
|
|
199
200
|
parseDotNotation: function(obj) {
|
|
@@ -201,18 +202,20 @@ module.exports = {
|
|
|
201
202
|
* Mutates dot notation objects into a deep object
|
|
202
203
|
* @param {object}
|
|
203
204
|
*/
|
|
204
|
-
|
|
205
|
+
if (!Object.keys(obj).find(o => o.indexOf('.') !== -1)) return obj
|
|
206
|
+
let objCopy = this.deepCopy(obj) // maybe convert to JSON.parse(JSON.stringify(obj))
|
|
207
|
+
|
|
205
208
|
for (let key in obj) {
|
|
206
209
|
if (key.indexOf('.') !== -1) {
|
|
207
|
-
|
|
210
|
+
setup(key, obj[key], obj)
|
|
208
211
|
} else {
|
|
209
212
|
// Ordinary values may of been updated by the bracket notation values, we are
|
|
210
213
|
// reassigning, trying to preserve the order of keys (not always guaranteed in for loops)
|
|
211
|
-
obj[key] =
|
|
214
|
+
obj[key] = objCopy[key]
|
|
212
215
|
}
|
|
213
216
|
}
|
|
214
217
|
return obj
|
|
215
|
-
function
|
|
218
|
+
function setup(str, val, obj) {
|
|
216
219
|
let parentObj = obj
|
|
217
220
|
let grandparentObj = obj
|
|
218
221
|
let keys = str.split(/\./)
|
|
@@ -233,7 +236,6 @@ module.exports = {
|
|
|
233
236
|
},
|
|
234
237
|
|
|
235
238
|
parseFormData: function(obj) {
|
|
236
|
-
let original = this.deepCopy(obj)
|
|
237
239
|
/**
|
|
238
240
|
* Mutates FormData (bracket notation) objects into a deep object
|
|
239
241
|
* @param {object}
|
|
@@ -242,21 +244,22 @@ module.exports = {
|
|
|
242
244
|
* E.g. ['user']['petnames'][0]
|
|
243
245
|
* E.g. ['users'][0]['name']
|
|
244
246
|
*/
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
setup(key)
|
|
252
|
-
} else {
|
|
253
|
-
// Ordinary values may of been updated by the bracket notation values, we are
|
|
254
|
-
// reassigning, trying to preserve the order of keys (not always guaranteed in for loops)
|
|
255
|
-
obj[key] = original[key]
|
|
256
|
-
}
|
|
247
|
+
if (!Object.keys(obj).find(o => o.indexOf('[') !== -1)) return obj
|
|
248
|
+
let objCopy = this.deepCopy(obj) // maybe convert to JSON.parse(JSON.stringify(obj))
|
|
249
|
+
|
|
250
|
+
for (let key in obj) {
|
|
251
|
+
if (key.match(/\[\]\[/i)) {
|
|
252
|
+
throw new Error(`Monastery: Array items in bracket notation need array indexes "${key}", e.g. users[0][name]`)
|
|
257
253
|
}
|
|
258
|
-
|
|
259
|
-
|
|
254
|
+
if (key.indexOf('[') !== -1) {
|
|
255
|
+
setup(key)
|
|
256
|
+
} else {
|
|
257
|
+
// Ordinary values may of been updated by the bracket notation values, we are
|
|
258
|
+
// reassigning, trying to preserve the order of keys (not always guaranteed in for loops)
|
|
259
|
+
obj[key] = objCopy[key]
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return obj
|
|
260
263
|
function setup(path) {
|
|
261
264
|
let parent = obj
|
|
262
265
|
let grandparent = obj
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "monastery",
|
|
3
3
|
"description": "⛪ A simple, straightforward MongoDB ODM",
|
|
4
4
|
"author": "Ricky Boyce",
|
|
5
|
-
"version": "3.0.
|
|
5
|
+
"version": "3.0.21",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:boycce/monastery",
|
|
8
8
|
"homepage": "https://boycce.github.io/monastery/",
|
package/test/util.js
CHANGED
|
@@ -26,8 +26,9 @@ test('util > formdata', async () => {
|
|
|
26
26
|
{ 'first': 'Bruce', 'last': 'Lee' },
|
|
27
27
|
],
|
|
28
28
|
})
|
|
29
|
-
expect(util.parseFormData({ 'users[][\'name\']': 'Martin' })).
|
|
30
|
-
|
|
29
|
+
expect(() => util.parseFormData({ 'users[][\'name\']': 'Martin' })).toThrow(
|
|
30
|
+
'Monastery: Array items in bracket notation need array indexes "users[][\'name\']", e.g. users[0][name]'
|
|
31
|
+
)
|
|
31
32
|
})
|
|
32
33
|
|
|
33
34
|
test('util > isId', async () => {
|