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