* 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)
| 542 | */ |
| 543 | |
| 544 | function 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 |
no outgoing calls
no test coverage detected
searching dependent graphs…