MCPcopy Create free account
hub / github.com/Automattic/mongoose / scopePaths

Function scopePaths

lib/schema/documentarray.js:543–572  ·  view source on GitHub ↗

* Scopes paths selected in a query to this array. * Necessary for proper default application of subdocument values. * * @param {DocumentArrayPath} array the array to scope `fields` paths * @param {Object|undefined} fields the root fields selected in the query * @param {Boolean|undefined} init i

(array, fields, init)

Source from the content-addressed store, hash-verified

541 */
542
543function scopePaths(array, fields, init) {
544 if (!(init && fields)) {
545 return undefined;
546 }
547
548 const path = array.path + '.';
549 const keys = Object.keys(fields);
550 let i = keys.length;
551 const selected = {};
552 let hasKeys;
553 let key;
554 let sub;
555
556 while (i--) {
557 key = keys[i];
558 if (key.startsWith(path)) {
559 sub = key.substring(path.length);
560 if (sub === '$') {
561 continue;
562 }
563 if (sub.startsWith('$.')) {
564 sub = sub.substring(2);
565 }
566 hasKeys || (hasKeys = true);
567 selected[sub] = fields[key];
568 }
569 }
570
571 return hasKeys && selected || undefined;
572}
573
574/*!
575 * ignore

Callers 1

documentarray.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected