UNPKG

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