something is working

This commit is contained in:
quoije
2022-09-15 01:55:36 -04:00
parent a0aa251c27
commit f38e69883c
1123 changed files with 251731 additions and 11 deletions
+39
View File
@@ -0,0 +1,39 @@
'use strict';
const MongooseError = require('../../error/mongooseError');
const isMongooseObject = require('../isMongooseObject');
const setDottedPath = require('../path/setDottedPath');
const util = require('util');
/**
* Given an object that may contain dotted paths, flatten the paths out.
* For example: `flattenObjectWithDottedPaths({ a: { 'b.c': 42 } })` => `{ a: { b: { c: 42 } } }`
*/
module.exports = function flattenObjectWithDottedPaths(obj) {
if (obj == null || typeof obj !== 'object' || Array.isArray(obj)) {
return;
}
// Avoid Mongoose docs, like docs and maps, because these may cause infinite recursion
if (isMongooseObject(obj)) {
return;
}
const keys = Object.keys(obj);
for (const key of keys) {
const val = obj[key];
if (key.indexOf('.') !== -1) {
try {
delete obj[key];
setDottedPath(obj, key, val);
} catch (err) {
if (!(err instanceof TypeError)) {
throw err;
}
throw new MongooseError(`Conflicting dotted paths when setting document path, key: "${key}", value: ${util.inspect(val)}`);
}
continue;
}
flattenObjectWithDottedPaths(obj[key]);
}
};
+18
View File
@@ -0,0 +1,18 @@
'use strict';
const dotRE = /\./g;
module.exports = function parentPaths(path) {
if (path.indexOf('.') === -1) {
return [path];
}
const pieces = path.split(dotRE);
const len = pieces.length;
const ret = new Array(len);
let cur = '';
for (let i = 0; i < len; ++i) {
cur += (cur.length !== 0) ? '.' + pieces[i] : pieces[i];
ret[i] = cur;
}
return ret;
};
+33
View File
@@ -0,0 +1,33 @@
'use strict';
const specialProperties = require('../specialProperties');
module.exports = function setDottedPath(obj, path, val) {
if (path.indexOf('.') === -1) {
if (specialProperties.has(path)) {
return;
}
obj[path] = val;
return;
}
const parts = path.split('.');
const last = parts.pop();
let cur = obj;
for (const part of parts) {
if (specialProperties.has(part)) {
continue;
}
if (cur[part] == null) {
cur[part] = {};
}
cur = cur[part];
}
if (!specialProperties.has(last)) {
cur[last] = val;
}
};