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

Function scopePaths

lib/schema/documentArray.js:578–607  ·  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

576 */
577
578function scopePaths(array, fields, init) {
579 if (!(init && fields)) {
580 return undefined;
581 }
582
583 const path = array.path + '.';
584 const keys = Object.keys(fields);
585 let i = keys.length;
586 const selected = {};
587 let hasKeys;
588 let key;
589 let sub;
590
591 while (i--) {
592 key = keys[i];
593 if (key.startsWith(path)) {
594 sub = key.substring(path.length);
595 if (sub === '$') {
596 continue;
597 }
598 if (sub.startsWith('$.')) {
599 sub = sub.substring(2);
600 }
601 hasKeys || (hasKeys = true);
602 selected[sub] = fields[key];
603 }
604 }
605
606 return hasKeys && selected || undefined;
607}
608
609/*!
610 * ignore

Callers 1

documentArray.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected