all files / scour/utilities/ normalize_keypath.js

100% Statements 14/14
100% Branches 12/12
100% Functions 1/1
100% Lines 9/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21                179× 170× 139× 129× 116×   31× 60×      
/**
 * Internal: normalizes a keypath, allowing dot syntax, and normalizing them
 * all to strings.
 *
 *     normalizeKeypath('user.12.name')  // => ['user', '12', 'name']
 *     normalizeKeypath(['user', 12])    // => ['user', 12]
 */
 
module.exports = function normalizeKeypath (keypath, isArguments) {
  if (typeof keypath === 'string') {
    return keypath.split('.')
  } else if (isArguments && keypath.length === 1) {
    if (Array.isArray(keypath[0])) return keypath[0].map((k) => '' + k)
    if (typeof keypath[0] === 'number') return [ '' + keypath[0] ]
    return ('' + keypath[0]).split('.')
  } else {
    if (isArguments) keypath = Array.prototype.slice.call(keypath)
    return keypath.map((k) => '' + k)
  }
}