gx
chenyc
2025-02-12 ea42ff3ebee1eeb3fb29423aa848a249441db81c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
 
module.exports = function normalizeRefPath(refPath, doc, populatedPath) {
  if (refPath == null) {
    return refPath;
  }
 
  if (typeof refPath === 'function') {
    refPath = refPath.call(doc, doc, populatedPath);
  }
 
  // If populated path has numerics, the end `refPath` should too. For example,
  // if populating `a.0.b` instead of `a.b` and `b` has `refPath = a.c`, we
  // should return `a.0.c` for the refPath.
  const hasNumericProp = /(\.\d+$|\.\d+\.)/g;
 
  if (hasNumericProp.test(populatedPath)) {
    const chunks = populatedPath.split(hasNumericProp);
 
    if (chunks[chunks.length - 1] === '') {
      throw new Error('Can\'t populate individual element in an array');
    }
 
    let _refPath = '';
    let _remaining = refPath;
    // 2nd, 4th, etc. will be numeric props. For example: `[ 'a', '.0.', 'b' ]`
    for (let i = 0; i < chunks.length; i += 2) {
      const chunk = chunks[i];
      if (_remaining.startsWith(chunk + '.')) {
        _refPath += _remaining.substr(0, chunk.length) + chunks[i + 1];
        _remaining = _remaining.substr(chunk.length + 1);
      } else if (i === chunks.length - 1) {
        _refPath += _remaining;
        _remaining = '';
        break;
      } else {
        throw new Error('Could not normalize ref path, chunk ' + chunk + ' not in populated path');
      }
    }
 
    return _refPath;
  }
 
  return refPath;
};