MCPcopy
hub / github.com/Automattic/mongoose / scopePaths

Function scopePaths

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

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

Callers 1

documentArray.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…