pathington
Version:
Custom identity functions for composability
66 lines (52 loc) • 1.64 kB
JavaScript
exports.__esModule = true;
exports.parse = exports.create = void 0;
var _constants = require("./constants");
var _utils = require("./utils");
// constants
// utils
var isArray = Array.isArray;
/**
* @function create
*
* @description
* create a new path string based on the path and quote passed
*
* @param {Array<number|string>} path the path to convert to a string
* @param {string} [quote="] the quote string to use when quoting keys
* @returns {string} the path string
*/
var create = function create(path, quote) {
if (quote === void 0) {
quote = '"';
}
if (!isArray(path)) {
throw new ReferenceError('path passed must be an array');
}
if (!_constants.VALID_QUOTES.test(quote)) {
throw new SyntaxError("quote " + quote + " passed is invalid, must be \", `, or '.");
}
var pathString = path.reduce((0, _utils.createGetNormalizedCreateKey)(quote), '');
return pathString[0] === '.' ? pathString.slice(1) : pathString;
};
/**
* @function parse
*
* @description
* the path parsed into a valid array of keys / indices
*
* @param {Array<number|string>|number|string} path the path to parse
* @returns {Array<number|string>} the parsed path
*/
exports.create = create;
var parse = function parse(path) {
if (typeof path === 'string') {
return (0, _utils.parseStringPath)(path);
}
if (isArray(path)) {
return (0, _utils.map)(path, _utils.getNormalizedParseKey);
}
var normalizedParseKey = (0, _utils.getNormalizedParseKey)(path);
return [typeof normalizedParseKey === 'number' ? normalizedParseKey : "" + normalizedParseKey];
};
exports.parse = parse;
;