UNPKG

3.23 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = exports.pick = void 0;
7
8var _getArrayIndex = _interopRequireDefault(require("./utils/get-array-index"));
9
10var _getKey = _interopRequireDefault(require("./utils/get-key"));
11
12var _is = _interopRequireDefault(require("./utils/is"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
17
18function _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."); }
19
20function _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); }
21
22function _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; }
23
24function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
25
26function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
27
28/**
29 * Pick
30 * @template T, S
31 * @description Reads value from object using dot notation path as key
32 * @param {string} path
33 * @param {T} source
34 * @returns {T} value
35 */
36var pick = function pick(source, path) {
37 if (_is.default.nullOrUndefined(path) || !path.trim()) {
38 throw new SyntaxError("A dot notation path was expected, but instead got \"".concat(path, "\""));
39 } // eslint-disable-next-line prefer-const
40
41
42 var _ref = (0, _getKey.default)(path),
43 _ref2 = _slicedToArray(_ref, 2),
44 current = _ref2[0],
45 remaining = _ref2[1];
46
47 var match = (0, _getArrayIndex.default)(current.toString());
48
49 if (match) {
50 var index = match[1];
51
52 if (!index) {
53 throw new SyntaxError("An array index was expected but nothing was found at \"".concat(path, "\""));
54 }
55
56 if (Number.isNaN(+index)) {
57 throw new TypeError("Array index must a positive integer \"".concat(index, "\""));
58 }
59
60 if (+index < 0) {
61 throw new RangeError("Array index must be equal or greater than 0, but instead got \"".concat(index, "\""));
62 }
63
64 current = +index;
65 }
66
67 if (!remaining || !source[current]) {
68 return source[current];
69 }
70
71 return pick(source[current], remaining);
72};
73
74exports.pick = pick;
75var _default = pick;
76exports.default = _default;
\No newline at end of file