UNPKG

@cookbook/dot-notation

Version:

Object readings and complex transformations using dot notation syntax.

37 lines (27 loc) 1.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } /** * Get initial key from dot notation path * @description returns first key defined in dot notation and the remaining path * @param {string} value * @returns {[string, string | undefined]} - returns key, remaining dot notation path and isArray, */ var getKey = function getKey(value) { var _value$split$filter = value.split(getKey.regexp).filter(Boolean), _value$split$filter2 = _toArray(_value$split$filter), current = _value$split$filter2[0], remaining = _value$split$filter2.slice(1); return [current, remaining.length ? remaining.join('.') : undefined]; }; getKey.regexp = /\.|(\[[^\]]*])|(\[-*\d*])/; var _default = getKey; exports.default = _default;