UNPKG

flowjv

Version:

Flow based approach to JSON validation!

149 lines (148 loc) 5.18 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from) { for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) to[j] = from[i]; return to; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Immutable = exports.insertIndex = exports.unset = exports.get = exports.set = exports.normalizeKeyPath = exports.stringToKeyPath = void 0; var stringToKeyPath = function (path) { return exports.normalizeKeyPath(path.split(".")); }; exports.stringToKeyPath = stringToKeyPath; var normalizeKeyPath = function (keyPath) { return keyPath .map(function (key) { return (isNaN(parseInt(key + "")) ? key : parseInt(key + "")); }) .filter(function (v) { return (v + "").trim() !== ""; }); }; exports.normalizeKeyPath = normalizeKeyPath; var set = function (obj, keyPath, value) { var _a; keyPath = exports.normalizeKeyPath(keyPath); if (keyPath.length === 0) { return value; } var key = keyPath.shift(); if (typeof key === "string") { if (!obj) { obj = {}; } return __assign(__assign({}, obj), (_a = {}, _a[key] = exports.set(obj[key], keyPath, value), _a)); } else if (typeof key === "number") { if (obj == null) { obj = []; } if (!(obj instanceof Array)) { throw { msg: "object should be of type array.", obj: obj }; } obj[key] = exports.set(obj[key], keyPath, value); return __spreadArray([], __read(obj)); } throw new Error("Key should be one of either string|number."); }; exports.set = set; var get = function (obj, keyPath, defaultValue) { keyPath = exports.normalizeKeyPath(keyPath); var result = keyPath.reduce(function (agg, key) { return agg === null || agg === void 0 ? void 0 : agg[key]; }, obj); return result == null ? defaultValue : result; }; exports.get = get; var unset = function (obj, keyPath) { var _a; keyPath = exports.normalizeKeyPath(keyPath); if (keyPath.length === 0 || obj == null) return obj; if (keyPath.length === 1) { var key_1 = keyPath[0]; if (typeof key_1 === "string") { var newobj = __assign({}, obj); delete newobj[key_1]; return !!obj[key_1] ? newobj : obj; } else { if (!(obj instanceof Array)) { throw new Error("obj should be of type Array."); } return obj.length >= key_1 ? obj.filter(function (v, i) { return i !== key_1; }) : obj; } } var key = keyPath.shift(); if (key == null) { throw new Error("Key cannot be undefined."); } var updatedObject = exports.unset(obj[key], keyPath); // if object updates... if (obj[key] !== updatedObject) { if (obj instanceof Array) { var newobj = __spreadArray([], __read(obj)); newobj[key] = updatedObject; return newobj; } else { return __assign(__assign({}, obj), (_a = {}, _a[key] = updatedObject, _a)); } } return obj; }; exports.unset = unset; function insertIndex(obj, keyPath_, value) { var keyPath = __spreadArray([], __read(keyPath_)); var index = keyPath.pop(); var arr = exports.get(obj, keyPath, []); if (!Array.isArray(arr)) { throw new Error("Value is not an array at path " + keyPath.join(".") + "."); } if (typeof index !== "number") { throw new Error("Index provided for array is not number."); } var newArr = __spreadArray([], __read(arr)); newArr.splice(index, 0, value); return exports.set(obj, keyPath, newArr); } exports.insertIndex = insertIndex; var Immutable = function (obj) { return { get: function (keyPath, defaultValue) { return exports.Immutable(exports.get(obj, keyPath, defaultValue)); }, set: function (keyPath, value) { return exports.Immutable(exports.set(obj, keyPath, value)); }, unset: function (keyPath) { return exports.Immutable(exports.unset(obj, keyPath)); }, insertIndex: function (keyPath) { }, value: function () { return obj; }, }; }; exports.Immutable = Immutable;