UNPKG

shineout

Version:
167 lines (164 loc) • 6.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.flatten = flatten; exports.getSthByName = exports.flattenArray = void 0; exports.insertPoint = insertPoint; exports.insertValue = insertValue; exports.removeSthByName = void 0; exports.spliceValue = spliceValue; exports.unflatten = unflatten; var _is = require("./is"); var _clone = require("./clone"); function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } // https://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects function insertPoint(name) { var reg = /(\[\d+\])/gi; return name.replace(reg, function (s, m, i) { return s.replace(m, i === 0 ? m : ".".concat(m)); }); } function flatten(data, skipArray) { if ((0, _is.isEmpty)(data)) return data; var result = {}; function recurse(cur, prop) { if (Object(cur) !== cur || typeof cur === 'function' || cur instanceof Date || cur instanceof Error || skipArray && Array.isArray(cur)) { if (!(cur === undefined && /\[\d+\]$/.test(prop))) { result[prop] = cur; } } else if (Array.isArray(cur)) { if (cur.length === 0) { result[prop] = []; } else { for (var i = 0, l = cur.length; i < l; i++) { recurse(cur[i], prop ? "".concat(prop, "[").concat(i, "]") : "[".concat(i, "]")); } } } else { var empty = true; if (_typeof(cur) === 'object') { // eslint-disable-next-line for (var p in cur) { empty = false; recurse(cur[p], prop ? "".concat(prop, ".").concat(p) : p); } if (empty) { result[prop] = {}; } } } } recurse(data, ''); return result; } function unflatten(rawdata) { if (Object(rawdata) !== rawdata || (0, _is.isEmpty)(rawdata) || Array.isArray(rawdata)) { return rawdata; } var data = _objectSpread({}, rawdata); var result = {}; // let { cur, prop, idx, last, temp, match } = {} var cur; var prop; var idx; var last; var temp; var match; // eslint-disable-next-line Object.keys(data).sort().forEach(function (p) { var pathWithPoint = insertPoint(p); cur = result; prop = ''; last = 0; do { idx = pathWithPoint.indexOf('.', last); temp = pathWithPoint.substring(last, idx !== -1 ? idx : undefined); match = /^\[(\d+)\]$/.exec(temp); cur = cur[prop] || (cur[prop] = match ? [] : {}); prop = match ? match[1] : temp; last = idx + 1; } while (idx >= 0); cur[prop] = (0, _clone.deepClone)(data[p]); }); return result['']; } function insertValue(obj, name, index, value) { Object.keys(obj).filter(function (n) { return n.indexOf("".concat(name, "[")) === 0; }).sort().reverse().forEach(function (n) { // const reg = new RegExp(`${name}\\[(\\d+)\\]`) var reg = new RegExp("".concat(name.replace(/\[/g, '\\[').replace(/\]/g, '\\]'), "\\[(\\d+)\\]")); var match = reg.exec(n); var i = parseInt(match[1], 10); if (i < index) return; var newName = n.replace(reg, "".concat(name, "[").concat(i + 1, "]")); if (obj[n]) obj[newName] = obj[n]; delete obj[n]; }); var newValue = flatten(_defineProperty({}, "".concat(name, "[").concat(index, "]"), value)); Object.keys(newValue).forEach(function (k) { if (newValue[k] !== undefined) obj[k] = newValue[k]; }); } function spliceValue(obj, name, index) { var names = Object.keys(obj).filter(function (n) { return n === name || n.indexOf("".concat(name, "[")) === 0; }).sort(); names.forEach(function (n) { if (n === name && !Array.isArray(obj[name])) return; if (n === name) { obj[name].splice(index, 1); return; } var reg = new RegExp("".concat(name.replace(/\[/g, '\\[').replace(/\]/g, '\\]'), "\\[(\\d+)\\]")); var match = reg.exec(n); var i = parseInt(match[1], 10); if (i < index) return; if (i === index) { delete obj[n]; return; } var newName = n.replace(reg, "".concat(name, "[").concat(i - 1, "]")); obj[newName] = obj[n]; delete obj[n]; }); } var isNameWithPath = function isNameWithPath(name, path) { if (name.indexOf(path) !== 0) return false; var remain = name.replace(path, '')[0]; return [undefined, '[', '.'].includes(remain); }; var getSthByName = exports.getSthByName = function getSthByName(name, source) { if (source[name]) return source[name]; var result = unflatten(source); name = insertPoint(name); name.split('.').forEach(function (n) { var match = /^\[(\d+)\]$/.exec(n); // eslint-disable-next-line if (match) n = match[1]; if (result) result = result[n];else result = undefined; }); // get from form-error if (!result && (0, _is.isObject)(source[''])) result = source[''][name]; return result; }; var removeSthByName = exports.removeSthByName = function removeSthByName(name, source) { var match = /(.*)\[(\d+)\]$/.exec(name); if (match) { spliceValue(source, match[1], parseInt(match[2], 10)); } else { Object.keys(source).forEach(function (n) { if (isNameWithPath(n, name)) delete source[n]; }); } }; var flattenArray = exports.flattenArray = function flattenArray(arr1) { return arr1.reduce(function (acc, val) { return Array.isArray(val) ? acc.concat(flattenArray(val)) : acc.concat(val); }, []); };