deno.land / x / mongoose@6.7.5 / lib / helpers / path / flattenObjectWithDottedPaths.js

flattenObjectWithDottedPaths.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'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]); }};
mongoose

Version Info

Tagged at
a year ago