pathington
Version:
Custom identity functions for composability
128 lines (116 loc) • 3.46 kB
JavaScript
// constants
import { CACHE, DOTTY_WITH_BRACKETS_SYNTAX, MAX_CACHE_SIZE, NUMBER, QUOTED_KEY, VALID_KEY, WHITE_SPACE } from './constants';
/**
* @function isNumericKey
*
* @description
* is the key passed a numeric string
*
* @param {string} key the key to test
* @returns {boolean} is the key passed a numeric string
*/
export var isNumericKey = function isNumericKey(key) {
return !!(key && key.length) && NUMBER.test(key);
};
/**
* @function isQuotedKey
*
* @description
* is the key passed a quoted key
*
* @param {string} key the key to test
* @returns {boolean} is the key a quoted key
*/
export var isQuotedKey = function isQuotedKey(key) {
return QUOTED_KEY.test(key);
};
/**
* @function map
*
* @description
* map the array to a new array based on fn
*
* @param {Array<*>} array the array to map
* @param {function} fn the function to call with each iteration value
* @returns {Array<*>} the mapped array
*/
export var map = function map(array, fn) {
var length = array.length;
var mapped = [];
for (var index = 0; index < length; index++) {
mapped[index] = fn(array[index]);
}
return mapped;
};
/**
* @function shouldBeInBrackets
*
* @description
* should the key passed be encased in brackets when in the path string
*
* @param {*} key the key that is being added to the path string
* @returns {boolean} should the key be in brackets
*/
export var shouldBeInBrackets = function shouldBeInBrackets(key) {
return typeof key === 'number' || isNumericKey(key) || isQuotedKey(key);
};
/**
* @function shouldBeInQuotes
*
* @description
* should the key passed be encased in quotes when in the path string
*
* @param {*} key the key that is being added to the path string
* @returns {boolean} should the key be in quotes
*/
export var shouldBeInQuotes = function shouldBeInQuotes(key) {
return WHITE_SPACE.test(key) || !VALID_KEY.test(key);
};
/**
* @function createGetNormalizedCreateKey
*
* @description
* get the normalized path string based on the quote and key passed
*
* @param {string} [quote="] the quote string to use
* @returns {function(string, *): string}
*/
export var createGetNormalizedCreateKey = function createGetNormalizedCreateKey(quote) {
return function (existingString, key) {
var normalizedKey = shouldBeInQuotes(key) ? "" + quote + key + quote : key;
return existingString + (shouldBeInBrackets(normalizedKey) ? "[" + normalizedKey + "]" : "." + normalizedKey);
};
};
/**
* @function getNormalizedParseKey
*
* @description
* get the key as a number if parseable, or as a quoted string if applicable
*
* @param {string} key the key to try to parse
* @returns {number|string} the parsed key
*/
export var getNormalizedParseKey = function getNormalizedParseKey(key) {
var cleanKey = isQuotedKey(key) ? key.slice(1, key.length - 1) : key;
return isNumericKey(cleanKey) ? +cleanKey : cleanKey;
};
/**
* @function parsePath
*
* @description
* parse the path, memoizing the results
*
* @param {string} path the path to parse
* @returns {Array<number|string>} the parsed path
*/
export var parseStringPath = function parseStringPath(path) {
if (CACHE.results[path]) {
return CACHE.results[path];
}
if (CACHE.size > MAX_CACHE_SIZE) {
CACHE.clear();
}
CACHE.results[path] = path ? map(path.match(DOTTY_WITH_BRACKETS_SYNTAX), getNormalizedParseKey) : [path];
CACHE.size++;
return CACHE.results[path];
};