UNPKG

pathington

Version:

Custom identity functions for composability

390 lines (351 loc) 14.2 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define("pathington", [], factory); else if(typeof exports === 'object') exports["pathington"] = factory(); else root["pathington"] = factory(); })(window, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); /******/ } /******/ }; /******/ /******/ // define __esModule on exports /******/ __webpack_require__.r = function(exports) { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ /******/ // create a fake namespace object /******/ // mode & 1: value is a module id, require it /******/ // mode & 2: merge all properties of value into the ns /******/ // mode & 4: return value when already ns object /******/ // mode & 8|1: behave like require /******/ __webpack_require__.t = function(value, mode) { /******/ if(mode & 1) value = __webpack_require__(value); /******/ if(mode & 8) return value; /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; /******/ var ns = Object.create(null); /******/ __webpack_require__.r(ns); /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); /******/ return ns; /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = "./src/index.js"); /******/ }) /************************************************************************/ /******/ ({ /***/ "./src/constants.js": /*!**************************!*\ !*** ./src/constants.js ***! \**************************/ /*! exports provided: CACHE, DOTTY_WITH_BRACKETS_SYNTAX, MAX_CACHE_SIZE, NUMBER, QUOTED_KEY, VALID_QUOTES, VALID_KEY, WHITE_SPACE */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; __webpack_require__.r(__webpack_exports__); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "CACHE", function() { return CACHE; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "DOTTY_WITH_BRACKETS_SYNTAX", function() { return DOTTY_WITH_BRACKETS_SYNTAX; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "MAX_CACHE_SIZE", function() { return MAX_CACHE_SIZE; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "NUMBER", function() { return NUMBER; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "QUOTED_KEY", function() { return QUOTED_KEY; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "VALID_QUOTES", function() { return VALID_QUOTES; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "VALID_KEY", function() { return VALID_KEY; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "WHITE_SPACE", function() { return WHITE_SPACE; }); /** * @constant {Object} CACHE * * @property {function} clear clear the cache results * @property {Object} results the map of path => array results * @property {number} size the size of the cache */ var CACHE = { clear: function clear() { CACHE.results = {}; CACHE.size = 0; }, results: {}, size: 0 }; /** * @constant {RegExp} DOTTY_WITH_BRACKETS_SYNTAX */ var DOTTY_WITH_BRACKETS_SYNTAX = /"[^"]+"|`[^`]+`|'[^']+'|[^.[\]]+/g; /** * @constant {number} MAX_CACHE_SIZE */ var MAX_CACHE_SIZE = 500; /** * @constant {RegExp} NUMBER */ var NUMBER = /^\d+$/i; /** * @constant {RegExp} QUOTED_KEY */ var QUOTED_KEY = /^"[^"]+"|`[^`]+`|'[^']+'$/; /** * @constant {Array<string>} VALID_QUOTES */ var VALID_QUOTES = /^["'`]{1}$/; /** * @constant {RegExp} VALID_KEY */ var VALID_KEY = /^\d+$|^[a-zA-Z_$][\w$]+$/; /** * @constant {RegExp} WHITE_SPACE */ var WHITE_SPACE = /\s/; /***/ }), /***/ "./src/index.js": /*!**********************!*\ !*** ./src/index.js ***! \**********************/ /*! exports provided: create, parse */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; __webpack_require__.r(__webpack_exports__); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "create", function() { return create; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "parse", function() { return parse; }); /* harmony import */ var _constants__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./constants */ "./src/constants.js"); /* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils */ "./src/utils.js"); // 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__WEBPACK_IMPORTED_MODULE_0__["VALID_QUOTES"].test(quote)) { throw new SyntaxError("quote " + quote + " passed is invalid, must be \", `, or '."); } var pathString = path.reduce(Object(_utils__WEBPACK_IMPORTED_MODULE_1__["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 */ var parse = function parse(path) { if (typeof path === 'string') { return Object(_utils__WEBPACK_IMPORTED_MODULE_1__["parseStringPath"])(path); } if (isArray(path)) { return Object(_utils__WEBPACK_IMPORTED_MODULE_1__["map"])(path, _utils__WEBPACK_IMPORTED_MODULE_1__["getNormalizedParseKey"]); } var normalizedParseKey = Object(_utils__WEBPACK_IMPORTED_MODULE_1__["getNormalizedParseKey"])(path); return [typeof normalizedParseKey === 'number' ? normalizedParseKey : "" + normalizedParseKey]; }; /***/ }), /***/ "./src/utils.js": /*!**********************!*\ !*** ./src/utils.js ***! \**********************/ /*! exports provided: isNumericKey, isQuotedKey, map, shouldBeInBrackets, shouldBeInQuotes, createGetNormalizedCreateKey, getNormalizedParseKey, parseStringPath */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; __webpack_require__.r(__webpack_exports__); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "isNumericKey", function() { return isNumericKey; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "isQuotedKey", function() { return isQuotedKey; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "map", function() { return map; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "shouldBeInBrackets", function() { return shouldBeInBrackets; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "shouldBeInQuotes", function() { return shouldBeInQuotes; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "createGetNormalizedCreateKey", function() { return createGetNormalizedCreateKey; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "getNormalizedParseKey", function() { return getNormalizedParseKey; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "parseStringPath", function() { return parseStringPath; }); /* harmony import */ var _constants__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./constants */ "./src/constants.js"); // 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 */ var isNumericKey = function isNumericKey(key) { return !!(key && key.length) && _constants__WEBPACK_IMPORTED_MODULE_0__["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 */ var isQuotedKey = function isQuotedKey(key) { return _constants__WEBPACK_IMPORTED_MODULE_0__["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 */ 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 */ 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 */ var shouldBeInQuotes = function shouldBeInQuotes(key) { return _constants__WEBPACK_IMPORTED_MODULE_0__["WHITE_SPACE"].test(key) || !_constants__WEBPACK_IMPORTED_MODULE_0__["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} */ 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 */ 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 */ var parseStringPath = function parseStringPath(path) { if (_constants__WEBPACK_IMPORTED_MODULE_0__["CACHE"].results[path]) { return _constants__WEBPACK_IMPORTED_MODULE_0__["CACHE"].results[path]; } if (_constants__WEBPACK_IMPORTED_MODULE_0__["CACHE"].size > _constants__WEBPACK_IMPORTED_MODULE_0__["MAX_CACHE_SIZE"]) { _constants__WEBPACK_IMPORTED_MODULE_0__["CACHE"].clear(); } _constants__WEBPACK_IMPORTED_MODULE_0__["CACHE"].results[path] = path ? map(path.match(_constants__WEBPACK_IMPORTED_MODULE_0__["DOTTY_WITH_BRACKETS_SYNTAX"]), getNormalizedParseKey) : [path]; _constants__WEBPACK_IMPORTED_MODULE_0__["CACHE"].size++; return _constants__WEBPACK_IMPORTED_MODULE_0__["CACHE"].results[path]; }; /***/ }) /******/ }); }); //# sourceMappingURL=pathington.js.map