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

Function scopePaths

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

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

Callers 1

documentArray.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected