(values, path, doc, schematype)
| 30 | const numberRE = /^\d+$/; |
| 31 | |
| 32 | function MongooseArray(values, path, doc, schematype) { |
| 33 | let __array; |
| 34 | |
| 35 | if (Array.isArray(values)) { |
| 36 | const len = values.length; |
| 37 | |
| 38 | // Perf optimizations for small arrays: much faster to use `...` than `for` + `push`, |
| 39 | // but large arrays may cause stack overflows. And for arrays of length 0/1, just |
| 40 | // modifying the array is faster. Seems small, but adds up when you have a document |
| 41 | // with thousands of nested arrays. |
| 42 | if (len === 0) { |
| 43 | __array = new Array(); |
| 44 | } else if (len === 1) { |
| 45 | __array = new Array(1); |
| 46 | __array[0] = values[0]; |
| 47 | } else if (len < 10000) { |
| 48 | __array = new Array(); |
| 49 | _basePush.apply(__array, values); |
| 50 | } else { |
| 51 | __array = new Array(); |
| 52 | for (let i = 0; i < len; ++i) { |
| 53 | _basePush.call(__array, values[i]); |
| 54 | } |
| 55 | } |
| 56 | } else { |
| 57 | __array = []; |
| 58 | } |
| 59 | |
| 60 | const internals = { |
| 61 | [arrayAtomicsSymbol]: {}, |
| 62 | [arrayAtomicsBackupSymbol]: void 0, |
| 63 | [arrayPathSymbol]: path, |
| 64 | [arraySchemaSymbol]: schematype, |
| 65 | [arrayParentSymbol]: void 0, |
| 66 | isMongooseArray: true, |
| 67 | isMongooseArrayProxy: true, |
| 68 | __array: __array |
| 69 | }; |
| 70 | |
| 71 | if (values && values[arrayAtomicsSymbol] != null) { |
| 72 | internals[arrayAtomicsSymbol] = values[arrayAtomicsSymbol]; |
| 73 | } |
| 74 | |
| 75 | if (doc?.$__) { |
| 76 | internals[arrayParentSymbol] = doc; |
| 77 | internals[arraySchemaSymbol] = schematype || doc.schema.path(path); |
| 78 | } |
| 79 | |
| 80 | const proxy = new Proxy(__array, { |
| 81 | get: function(target, prop) { |
| 82 | if (Object.hasOwn(internals, prop)) { |
| 83 | return internals[prop]; |
| 84 | } |
| 85 | if (Object.hasOwn(mongooseArrayMethods, prop)) { |
| 86 | return mongooseArrayMethods[prop]; |
| 87 | } |
| 88 | if (schematype?.virtuals && Object.hasOwn(schematype.virtuals, prop)) { |
| 89 | return schematype.virtuals[prop].applyGetters(undefined, target); |
no test coverage detected
searching dependent graphs…