1 | 'use strict';
|
2 |
|
3 |
|
4 | const cloneRegExp = require('regexp-clone');
|
5 | const Decimal = require('../types/decimal128');
|
6 | const ObjectId = require('../types/objectid');
|
7 | const specialProperties = require('./specialProperties');
|
8 | const isMongooseObject = require('./isMongooseObject');
|
9 | const getFunctionName = require('./getFunctionName');
|
10 | const isBsonType = require('./isBsonType');
|
11 | const isObject = require('./isObject');
|
12 | const symbols = require('./symbols');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function clone(obj, options, isArrayChild) {
|
30 | if (obj == null) {
|
31 | return obj;
|
32 | }
|
33 |
|
34 | if (Array.isArray(obj)) {
|
35 | return cloneArray(obj, options);
|
36 | }
|
37 |
|
38 | if (isMongooseObject(obj)) {
|
39 |
|
40 |
|
41 | if (options && options._skipSingleNestedGetters && obj.$isSingleNested) {
|
42 | options = Object.assign({}, options, { getters: false });
|
43 | }
|
44 | if (options && options.json && typeof obj.toJSON === 'function') {
|
45 | return obj.toJSON(options);
|
46 | }
|
47 | return obj.toObject(options);
|
48 | }
|
49 |
|
50 | if (obj.constructor) {
|
51 | switch (getFunctionName(obj.constructor)) {
|
52 | case 'Object':
|
53 | return cloneObject(obj, options, isArrayChild);
|
54 | case 'Date':
|
55 | return new obj.constructor(+obj);
|
56 | case 'RegExp':
|
57 | return cloneRegExp(obj);
|
58 | default:
|
59 |
|
60 | break;
|
61 | }
|
62 | }
|
63 |
|
64 | if (obj instanceof ObjectId) {
|
65 | return new ObjectId(obj.id);
|
66 | }
|
67 |
|
68 | if (isBsonType(obj, 'Decimal128')) {
|
69 | if (options && options.flattenDecimals) {
|
70 | return obj.toJSON();
|
71 | }
|
72 | return Decimal.fromString(obj.toString());
|
73 | }
|
74 |
|
75 | if (!obj.constructor && isObject(obj)) {
|
76 |
|
77 | return cloneObject(obj, options, isArrayChild);
|
78 | }
|
79 |
|
80 | if (obj[symbols.schemaTypeSymbol]) {
|
81 | return obj.clone();
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | if (options && options.bson && typeof obj.toBSON === 'function') {
|
88 | return obj;
|
89 | }
|
90 |
|
91 | if (obj.valueOf != null) {
|
92 | return obj.valueOf();
|
93 | }
|
94 |
|
95 | return cloneObject(obj, options, isArrayChild);
|
96 | }
|
97 | module.exports = clone;
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | function cloneObject(obj, options, isArrayChild) {
|
104 | const minimize = options && options.minimize;
|
105 | const ret = {};
|
106 | let hasKeys;
|
107 |
|
108 | for (const k in obj) {
|
109 | if (specialProperties.has(k)) {
|
110 | continue;
|
111 | }
|
112 |
|
113 |
|
114 | const val = clone(obj[k], options);
|
115 |
|
116 | if (!minimize || (typeof val !== 'undefined')) {
|
117 | if (minimize === false && typeof val === 'undefined') {
|
118 | delete ret[k];
|
119 | } else {
|
120 | hasKeys || (hasKeys = true);
|
121 | ret[k] = val;
|
122 | }
|
123 | }
|
124 | }
|
125 |
|
126 | return minimize && !isArrayChild ? hasKeys && ret : ret;
|
127 | }
|
128 |
|
129 | function cloneArray(arr, options) {
|
130 | const ret = [];
|
131 |
|
132 | for (const item of arr) {
|
133 | ret.push(clone(item, options, true));
|
134 | }
|
135 |
|
136 | return ret;
|
137 | } |
\ | No newline at end of file |