SIGN IN SIGN UP

A modern JavaScript utility library delivering modularity, performance, & extras.

0 0 2065 JavaScript
2016-05-18 16:00:35 -07:00
import arrayMap from './_arrayMap.js';
import copyArray from './_copyArray.js';
import isArray from './isArray.js';
import isSymbol from './isSymbol.js';
import stringToPath from './_stringToPath.js';
import toKey from './_toKey.js';
2016-11-14 20:56:46 -08:00
import toString from './toString.js';
2015-09-17 17:52:09 -07:00
/**
* Converts `value` to a property path array.
*
* @static
* @memberOf _
2016-03-26 00:00:36 -07:00
* @since 4.0.0
2015-09-17 17:52:09 -07:00
* @category Util
* @param {*} value The value to convert.
* @returns {Array} Returns the new property path array.
* @example
*
* _.toPath('a.b.c');
* // => ['a', 'b', 'c']
*
* _.toPath('a[0].b.c');
* // => ['a', '0', 'b', 'c']
*/
function toPath(value) {
2016-03-26 00:00:36 -07:00
if (isArray(value)) {
2016-04-10 22:55:59 -07:00
return arrayMap(value, toKey);
2016-03-26 00:00:36 -07:00
}
2016-11-14 20:56:46 -08:00
return isSymbol(value) ? [value] : copyArray(stringToPath(toString(value)));
2015-09-17 17:52:09 -07:00
}
export default toPath;