* 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)
| 583 | */ |
| 584 | |
| 585 | function 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 |