@uform/utils
Advanced tools
Comparing version 0.2.3 to 0.3.0
"use strict"; | ||
exports.__esModule = true; | ||
exports.getPathSegments = getPathSegments; | ||
exports.parsePaths = exports.parseDestruct = exports.parseDesturctPath = exports.existIn = exports.deleteIn = exports.setIn = exports.getIn = void 0; | ||
var _types = require("./types"); | ||
var _array = require("./array"); | ||
var _lru = require("./lru"); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = require("@uform/types"); | ||
var array_1 = require("./array"); | ||
var lru_1 = require("./lru"); | ||
function whitespace(c) { | ||
return c === ' ' || c === '\n' || c === '\t' || c === '\f' || c === '\r'; | ||
return c === ' ' || c === '\n' || c === '\t' || c === '\f' || c === '\r'; | ||
} | ||
function toString(val) { | ||
if (!val) return ''; | ||
if ((0, _types.isArr)(val)) { | ||
return val.join('.'); | ||
} | ||
return (0, _types.isStr)(val) ? val : ''; | ||
if (!val) { | ||
return ''; | ||
} | ||
if (types_1.isArr(val)) { | ||
return val.join('.'); | ||
} | ||
return types_1.isStr(val) ? val : ''; | ||
} | ||
var PathCache = new _lru.LRUMap(1000); | ||
const PathCache = new lru_1.LRUMap(1000); | ||
function getPathSegments(path) { | ||
if ((0, _types.isArr)(path)) return path; | ||
if ((0, _types.isStr)(path) && path) { | ||
var cached = PathCache.get(path); | ||
if (cached) return cached; | ||
var pathArr = path.split('.'); | ||
var parts = []; | ||
for (var i = 0; i < pathArr.length; i++) { | ||
var p = pathArr[i]; | ||
while (p[p.length - 1] === '\\' && pathArr[i + 1] !== undefined) { | ||
p = p.slice(0, -1) + '.'; | ||
p += pathArr[++i]; | ||
} | ||
parts.push(p); | ||
if (types_1.isArr(path)) { | ||
return path; | ||
} | ||
PathCache.set(path, parts); | ||
return parts; | ||
} | ||
if ((0, _types.isNum)(path)) return [path]; | ||
return []; | ||
if (types_1.isStr(path) && path) { | ||
const cached = PathCache.get(path); | ||
if (cached) { | ||
return cached; | ||
} | ||
const pathArr = path.split('.'); | ||
const parts = []; | ||
for (let i = 0; i < pathArr.length; i++) { | ||
let p = pathArr[i]; | ||
while (p[p.length - 1] === '\\' && pathArr[i + 1] !== undefined) { | ||
p = p.slice(0, -1) + '.'; | ||
p += pathArr[++i]; | ||
} | ||
parts.push(p); | ||
} | ||
PathCache.set(path, parts); | ||
return parts; | ||
} | ||
if (types_1.isNum(path)) { | ||
return [path]; | ||
} | ||
return []; | ||
} | ||
var DestructTokenizer = | ||
/*#__PURE__*/ | ||
function () { | ||
function DestructTokenizer(text, cbs) { | ||
this.text = text; | ||
this.index = 0; | ||
this.cbs = cbs; | ||
this.state = this.processNameStart; | ||
this.declareNameStart = 0; | ||
this.declareNameEnd = 0; | ||
this.nbraceCount = 0; | ||
this.nbracketCount = 0; | ||
} | ||
var _proto = DestructTokenizer.prototype; | ||
_proto["goto"] = function goto(name) { | ||
this.state = this.StateMap[name]; | ||
}; | ||
_proto.parse = function parse() { | ||
var _char = ''; | ||
var prev = ''; | ||
var l = this.text.length; | ||
for (; this.index < l; this.index++) { | ||
_char = this.text.charAt(this.index); | ||
this.EOF = l - 1 === this.index; | ||
this.state(_char, prev); | ||
prev = _char; | ||
exports.getPathSegments = getPathSegments; | ||
class DestructTokenizer { | ||
constructor(text, handlers) { | ||
this.text = text; | ||
this.index = 0; | ||
this.handlers = handlers; | ||
this.state = this.processNameStart; | ||
this.declareNameStart = 0; | ||
this.declareNameEnd = 0; | ||
this.nbraceCount = 0; | ||
this.nbracketCount = 0; | ||
} | ||
}; | ||
_proto.processNameStart = function processNameStart(_char2) { | ||
if (_char2 === '{' || _char2 === '[') { | ||
this.state = this.processDestructStart; | ||
this.index--; | ||
} else if (!whitespace(_char2)) { | ||
this.declareNameStart = this.index; | ||
this.state = this.processName; | ||
parse() { | ||
let char = ''; | ||
let prev = ''; | ||
const l = this.text.length; | ||
for (; this.index < l; this.index++) { | ||
char = this.text.charAt(this.index); | ||
this.EOF = l - 1 === this.index; | ||
this.state(char, prev); | ||
prev = char; | ||
} | ||
} | ||
}; | ||
_proto.processName = function processName(_char3, prev) { | ||
if (whitespace(_char3)) { | ||
this.declareNameEnd = this.index; | ||
this.cbs.name(this.getName()); | ||
} else if (this.EOF) { | ||
this.declareNameEnd = this.index + 1; | ||
this.cbs.name(this.getName()); | ||
processNameStart(char) { | ||
if (char === '{' || char === '[') { | ||
this.state = this.processDestructStart; | ||
this.index--; | ||
} | ||
else if (!whitespace(char)) { | ||
this.declareNameStart = this.index; | ||
this.state = this.processName; | ||
} | ||
} | ||
}; | ||
_proto.processDestructStart = function processDestructStart(_char4) { | ||
if (_char4 === '{') { | ||
this.nbraceCount++; | ||
this.cbs.destructObjectStart(); | ||
} else if (_char4 === '[') { | ||
this.nbracketCount++; | ||
this.cbs.destructArrayStart(); | ||
} else if (!whitespace(_char4)) { | ||
this.state = this.processDestructKey; | ||
this.destructKeyStart = this.index; | ||
this.index--; | ||
processName(char, prev) { | ||
if (whitespace(char)) { | ||
this.declareNameEnd = this.index; | ||
this.handlers.name(this.getName()); | ||
} | ||
else if (this.EOF) { | ||
this.declareNameEnd = this.index + 1; | ||
this.handlers.name(this.getName()); | ||
} | ||
} | ||
}; | ||
_proto.processDestructKey = function processDestructKey(_char5, prev) { | ||
if (_char5 === '}') { | ||
this.nbraceCount--; | ||
if (this.nbraceCount || this.nbracketCount) { | ||
this.state = this.processDestructStart; | ||
} | ||
if (!whitespace(prev)) { | ||
this.destructKey = this.text.substring(this.destructKeyStart, this.index); | ||
} | ||
this.cbs.destructKey(this.destructKey); | ||
this.cbs.destructObjectEnd(); | ||
if (!this.nbraceCount && !this.nbracketCount) { | ||
this.index = this.text.length; | ||
} | ||
} else if (_char5 === ']') { | ||
this.nbracketCount--; | ||
if (this.nbraceCount || this.nbracketCount) { | ||
this.state = this.processDestructStart; | ||
} | ||
if (!whitespace(prev)) { | ||
this.destructKey = this.text.substring(this.destructKeyStart, this.index); | ||
} | ||
this.cbs.destructKey(this.destructKey); | ||
this.cbs.destructArrayEnd(); | ||
if (!this.nbraceCount && !this.nbracketCount) { | ||
this.index = this.text.length; | ||
} | ||
} else if (whitespace(_char5) || _char5 === ':' || _char5 === ',') { | ||
if (!whitespace(prev)) { | ||
this.destructKey = this.text.substring(this.destructKeyStart, this.index); | ||
} | ||
if (!whitespace(_char5)) { | ||
this.state = this.processDestructStart; | ||
this.cbs.destructKey(this.destructKey, _char5 === ':'); | ||
} | ||
processDestructStart(char) { | ||
if (char === '{') { | ||
this.nbraceCount++; | ||
this.handlers.destructObjectStart(); | ||
} | ||
else if (char === '[') { | ||
this.nbracketCount++; | ||
this.handlers.destructArrayStart(); | ||
} | ||
else if (!whitespace(char)) { | ||
this.state = this.processDestructKey; | ||
this.destructKeyStart = this.index; | ||
this.index--; | ||
} | ||
} | ||
}; | ||
_proto.getName = function getName() { | ||
return this.text.substring(this.declareNameStart, this.declareNameEnd); | ||
}; | ||
return DestructTokenizer; | ||
}(); | ||
var parseDestruct = function parseDestruct(string) { | ||
if (!(0, _types.isStr)(string)) return string; | ||
var destruct; | ||
var stack = []; | ||
var token = ''; | ||
var realKey = ''; | ||
var lastDestruct; | ||
var root; | ||
new DestructTokenizer(string, { | ||
name: function name(key) { | ||
root = key; | ||
}, | ||
destructKey: function destructKey(key, readyReplace) { | ||
if (!key) return; | ||
token = key; | ||
if (readyReplace) { | ||
realKey = key; | ||
lastDestruct = destruct; | ||
return; | ||
} | ||
if ((0, _types.isArr)(destruct)) { | ||
destruct.push(key); | ||
} else if ((0, _types.isPlainObj)(destruct)) { | ||
destruct[realKey && lastDestruct === destruct ? realKey : key] = key; | ||
} | ||
realKey = ''; | ||
lastDestruct = destruct; | ||
}, | ||
destructArrayStart: function destructArrayStart() { | ||
if (!destruct) { | ||
root = []; | ||
destruct = root; | ||
} else { | ||
destruct = []; | ||
} | ||
var tail = stack[stack.length - 1]; | ||
if ((0, _types.isPlainObj)(tail)) { | ||
tail[token] = destruct; | ||
} else if ((0, _types.isArr)(tail)) { | ||
tail.push(destruct); | ||
} | ||
stack.push(destruct); | ||
}, | ||
destructObjectStart: function destructObjectStart() { | ||
if (!destruct) { | ||
root = {}; | ||
destruct = root; | ||
} else { | ||
destruct = {}; | ||
} | ||
var tail = stack[stack.length - 1]; | ||
if ((0, _types.isPlainObj)(tail)) { | ||
tail[token] = destruct; | ||
} else if ((0, _types.isArr)(tail)) { | ||
tail.push(destruct); | ||
} | ||
stack.push(destruct); | ||
}, | ||
destructArrayEnd: function destructArrayEnd() { | ||
stack.pop(); | ||
destruct = stack[stack.length - 1]; | ||
}, | ||
destructObjectEnd: function destructObjectEnd() { | ||
stack.pop(); | ||
destruct = stack[stack.length - 1]; | ||
processDestructKey(char, prev) { | ||
if (char === '}') { | ||
this.nbraceCount--; | ||
if (this.nbraceCount || this.nbracketCount) { | ||
this.state = this.processDestructStart; | ||
} | ||
if (!whitespace(prev)) { | ||
this.destructKey = this.text.substring(this.destructKeyStart, this.index); | ||
} | ||
this.handlers.destructKey(this.destructKey); | ||
this.handlers.destructObjectEnd(); | ||
if (!this.nbraceCount && !this.nbracketCount) { | ||
this.index = this.text.length; | ||
} | ||
} | ||
else if (char === ']') { | ||
this.nbracketCount--; | ||
if (this.nbraceCount || this.nbracketCount) { | ||
this.state = this.processDestructStart; | ||
} | ||
if (!whitespace(prev)) { | ||
this.destructKey = this.text.substring(this.destructKeyStart, this.index); | ||
} | ||
this.handlers.destructKey(this.destructKey); | ||
this.handlers.destructArrayEnd(); | ||
if (!this.nbraceCount && !this.nbracketCount) { | ||
this.index = this.text.length; | ||
} | ||
} | ||
else if (whitespace(char) || char === ':' || char === ',') { | ||
if (!whitespace(prev)) { | ||
this.destructKey = this.text.substring(this.destructKeyStart, this.index); | ||
} | ||
if (!whitespace(char)) { | ||
this.state = this.processDestructStart; | ||
this.handlers.destructKey(this.destructKey, char === ':'); | ||
} | ||
} | ||
} | ||
}).parse(); | ||
return root; | ||
getName() { | ||
return this.text.substring(this.declareNameStart, this.declareNameEnd); | ||
} | ||
} | ||
const parseDestruct = (str) => { | ||
if (!types_1.isStr(str)) { | ||
return str; | ||
} | ||
let destruct; | ||
const stack = []; | ||
let token = ''; | ||
let realKey = ''; | ||
let lastDestruct; | ||
let root; | ||
new DestructTokenizer(str, { | ||
name(key) { | ||
root = key; | ||
}, | ||
destructKey(key, readyReplace) { | ||
if (!key) { | ||
return; | ||
} | ||
token = key; | ||
if (readyReplace) { | ||
realKey = key; | ||
lastDestruct = destruct; | ||
return; | ||
} | ||
if (types_1.isArr(destruct)) { | ||
; | ||
destruct.push(key); | ||
} | ||
else if (types_1.isPlainObj(destruct)) { | ||
destruct[realKey && lastDestruct === destruct ? realKey : key] = key; | ||
} | ||
realKey = ''; | ||
lastDestruct = destruct; | ||
}, | ||
destructArrayStart() { | ||
if (!destruct) { | ||
root = []; | ||
destruct = root; | ||
} | ||
else { | ||
destruct = []; | ||
} | ||
const tail = stack[stack.length - 1]; | ||
if (types_1.isPlainObj(tail)) { | ||
tail[token] = destruct; | ||
} | ||
else if (types_1.isArr(tail)) { | ||
tail.push(destruct); | ||
} | ||
stack.push(destruct); | ||
}, | ||
destructObjectStart() { | ||
if (!destruct) { | ||
root = {}; | ||
destruct = root; | ||
} | ||
else { | ||
destruct = {}; | ||
} | ||
const tail = stack[stack.length - 1]; | ||
if (types_1.isPlainObj(tail)) { | ||
tail[token] = destruct; | ||
} | ||
else if (types_1.isArr(tail)) { | ||
tail.push(destruct); | ||
} | ||
stack.push(destruct); | ||
}, | ||
destructArrayEnd() { | ||
stack.pop(); | ||
destruct = stack[stack.length - 1]; | ||
}, | ||
destructObjectEnd() { | ||
stack.pop(); | ||
destruct = stack[stack.length - 1]; | ||
} | ||
}).parse(); | ||
return root; | ||
}; | ||
exports.parseDestruct = parseDestruct; | ||
var traverse = function traverse(obj, callback) { | ||
var _traverse = function _traverse(obj, path, callback) { | ||
if ((0, _types.isStr)(obj)) return callback(obj, obj); | ||
(0, _array.each)(obj, function (item, key) { | ||
var _path = path.concat(key); | ||
if ((0, _types.isArr)(item) || (0, _types.isPlainObj)(item)) { | ||
_traverse(item, _path, callback); | ||
} else { | ||
callback(_path, item); | ||
} | ||
}); | ||
}; | ||
return _traverse(obj, [], callback); | ||
const traverse = (obj, callback) => { | ||
const internalTraverse = (internalObj, path) => { | ||
if (types_1.isStr(internalObj)) { | ||
return callback(internalObj, internalObj); | ||
} | ||
array_1.each(internalObj, (item, key) => { | ||
const newPath = path.concat(key); | ||
if (types_1.isArr(item) || types_1.isPlainObj(item)) { | ||
internalTraverse(item, newPath); | ||
} | ||
else { | ||
callback(newPath, item); | ||
} | ||
}); | ||
}; | ||
return internalTraverse(obj, []); | ||
}; | ||
var mapReduce = function mapReduce(obj, callback) { | ||
var _traverse = function _traverse(obj, path, callback) { | ||
return (0, _array.map)(obj, function (item, key) { | ||
var _path = path.concat(key); | ||
if ((0, _types.isArr)(item) || (0, _types.isPlainObj)(item)) { | ||
return _traverse(item, _path, callback); | ||
} else { | ||
return callback(_path, _path.slice(0, _path.length - 1).concat(item)); | ||
} | ||
}); | ||
}; | ||
return _traverse(obj, [], callback); | ||
const mapReduce = (obj, callback) => { | ||
const internalTraverse = (internalObj, path) => { | ||
return array_1.map(internalObj, (item, key) => { | ||
const newPath = path.concat(key); | ||
if (types_1.isArr(item) || types_1.isPlainObj(item)) { | ||
return internalTraverse(item, newPath); | ||
} | ||
else { | ||
return callback(newPath, newPath.slice(0, newPath.length - 1).concat(item)); | ||
} | ||
}); | ||
}; | ||
return internalTraverse(obj, []); | ||
}; | ||
var parseDesturctPath = function parseDesturctPath(path) { | ||
var _path = getPathSegments(path); | ||
var lastKey = _path[_path.length - 1]; | ||
var startPath = _path.slice(0, _path.length - 1); | ||
var destruct = parseDestruct(lastKey); | ||
return { | ||
path: _path, | ||
lastKey: lastKey, | ||
startPath: startPath, | ||
destruct: destruct | ||
}; | ||
const parseDesturctPath = (path) => { | ||
const newPath = getPathSegments(path); | ||
const lastKey = newPath[newPath.length - 1]; | ||
const startPath = newPath.slice(0, newPath.length - 1); | ||
const destruct = parseDestruct(lastKey); | ||
return { | ||
path: newPath, | ||
lastKey, | ||
startPath, | ||
destruct | ||
}; | ||
}; | ||
exports.parseDesturctPath = parseDesturctPath; | ||
var parsePaths = function parsePaths(path) { | ||
var result = []; | ||
var parsed = parseDesturctPath(path); | ||
if ((0, _types.isStr)(parsed.destruct)) { | ||
return path; | ||
} else if (parsed.destruct) { | ||
traverse(parsed.destruct, function (path, key) { | ||
result.push({ | ||
path: parsed.startPath.concat(path), | ||
startPath: parsed.startPath, | ||
endPath: path, | ||
key: key | ||
}); | ||
}); | ||
return result; | ||
} else { | ||
return path; | ||
} | ||
}; | ||
exports.parsePaths = parsePaths; | ||
var resolveGetIn = function resolveGetIn(get) { | ||
var cache = new Map(); | ||
return function (obj, path, value) { | ||
var ast = null; | ||
if (!(ast = cache.get(path))) { | ||
ast = parseDesturctPath(path); | ||
cache.set(path, ast); | ||
const parsePaths = (path) => { | ||
const result = []; | ||
const parsed = parseDesturctPath(path); | ||
if (types_1.isStr(parsed.destruct)) { | ||
return path; | ||
} | ||
if (!(0, _types.isArr)(ast.destruct) && !(0, _types.isPlainObj)(ast.destruct)) { | ||
return get(obj, path, value); | ||
else if (parsed.destruct) { | ||
traverse(parsed.destruct, (internalPath, key) => { | ||
result.push({ | ||
path: parsed.startPath.concat(internalPath), | ||
startPath: parsed.startPath, | ||
endPath: internalPath, | ||
key | ||
}); | ||
}); | ||
return result; | ||
} | ||
return mapReduce(ast.destruct, function (path, key) { | ||
return get(obj, ast.startPath.concat(key)); | ||
}); | ||
}; | ||
}; | ||
var resolveUpdateIn = function resolveUpdateIn(update, getIn) { | ||
var cache = new Map(); | ||
return function (obj, path, value) { | ||
var paths = []; | ||
if (!(paths = cache.get(path))) { | ||
paths = parsePaths(path); | ||
cache.set(path, paths); | ||
else { | ||
return path; | ||
} | ||
if (!(0, _types.isArr)(paths)) return update(obj, path, value); | ||
if (paths && paths.length) { | ||
(0, _array.each)(paths, function (_ref) { | ||
var path = _ref.path, | ||
key = _ref.key, | ||
startPath = _ref.startPath, | ||
endPath = _ref.endPath; | ||
update(obj, startPath.concat(key), getIn(value, endPath)); | ||
}); | ||
} | ||
return obj; | ||
}; | ||
}; | ||
var resolveExistIn = function resolveExistIn(has) { | ||
var cache = new Map(); | ||
return function (obj, path) { | ||
var paths = []; | ||
if (!(paths = cache.get(path))) { | ||
paths = parsePaths(path); | ||
cache.set(path, paths); | ||
exports.parsePaths = parsePaths; | ||
const resolveGetIn = (get) => { | ||
const cache = new Map(); | ||
return (obj, path, value) => { | ||
let ast = null; | ||
if (!cache.get(path)) { | ||
ast = parseDesturctPath(path); | ||
cache.set(path, ast); | ||
} | ||
else { | ||
ast = cache.get(path); | ||
} | ||
if (!types_1.isArr(ast.destruct) && !types_1.isPlainObj(ast.destruct)) { | ||
return get(obj, path, value); | ||
} | ||
return mapReduce(ast.destruct, (mapPath, key) => { | ||
return get(obj, ast.startPath.concat(key)); | ||
}); | ||
}; | ||
}; | ||
const resolveUpdateIn = (update, internalGetIn) => { | ||
const cache = new Map(); | ||
return (obj, path, value) => { | ||
let paths = []; | ||
if (!cache.get(path)) { | ||
paths = parsePaths(path); | ||
cache.set(path, paths); | ||
} | ||
else { | ||
paths = cache.get(path); | ||
} | ||
if (!types_1.isArr(paths)) { | ||
return update(obj, path, value); | ||
} | ||
if (paths && paths.length) { | ||
array_1.each(paths, ({ mapPath, key, startPath, endPath }) => { | ||
update(obj, startPath.concat(key), internalGetIn(value, endPath)); | ||
}); | ||
} | ||
return obj; | ||
}; | ||
}; | ||
const resolveExistIn = (has) => { | ||
const cache = new Map(); | ||
return (obj, path) => { | ||
let paths = []; | ||
if (!cache.get(path)) { | ||
paths = parsePaths(path); | ||
cache.set(path, paths); | ||
} | ||
else { | ||
paths = cache.get(path); | ||
} | ||
if (!types_1.isArr(paths)) { | ||
return has(obj, path); | ||
} | ||
if (paths && paths.length) { | ||
return array_1.every(paths, ({ startPath, key }) => { | ||
return has(obj, startPath.concat(key)); | ||
}); | ||
} | ||
return false; | ||
}; | ||
}; | ||
function _getIn(obj, path, value) { | ||
if (!types_1.isObj(obj) || !path) { | ||
return obj; | ||
} | ||
if (!(0, _types.isArr)(paths)) { | ||
return has(obj, path); | ||
path = toString(path); | ||
if (path in obj) { | ||
return obj[path]; | ||
} | ||
if (paths && paths.length) { | ||
return (0, _array.every)(paths, function (_ref2) { | ||
var startPath = _ref2.startPath, | ||
key = _ref2.key; | ||
return has(obj, startPath.concat(key)); | ||
}); | ||
const pathArr = getPathSegments(path); | ||
for (let i = 0; i < pathArr.length; i++) { | ||
if (!Object.prototype.propertyIsEnumerable.call(obj, pathArr[i])) { | ||
return value; | ||
} | ||
obj = obj[pathArr[i]]; | ||
if (obj === undefined || obj === null) { | ||
if (i !== pathArr.length - 1) { | ||
return value; | ||
} | ||
break; | ||
} | ||
} | ||
return false; | ||
}; | ||
}; | ||
function _getIn(obj, path, value) { | ||
if (!(0, _types.isObj)(obj) || !path) { | ||
return obj; | ||
} | ||
path = toString(path); | ||
if (path in obj) { | ||
return obj[path]; | ||
} | ||
var pathArr = getPathSegments(path); | ||
for (var i = 0; i < pathArr.length; i++) { | ||
if (!Object.prototype.propertyIsEnumerable.call(obj, pathArr[i])) { | ||
return value; | ||
} | ||
obj = obj[pathArr[i]]; | ||
if (obj === undefined || obj === null) { | ||
// `obj` is either `undefined` or `null` so we want to stop the loop, and | ||
// if this is not the last bit of the path, and | ||
// if it did't return `undefined` | ||
// it would return `null` if `obj` is `null` | ||
// but we want `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied value, not `null` | ||
if (i !== pathArr.length - 1) { | ||
return value; | ||
} | ||
break; | ||
} | ||
} | ||
return obj; | ||
} | ||
function _setIn(obj, path, value) { | ||
if (!(0, _types.isObj)(obj) || !path) { | ||
return; | ||
} | ||
path = toString(path); | ||
if (path in obj) { | ||
obj[path] = value; | ||
return; | ||
} | ||
var pathArr = getPathSegments(path); | ||
for (var i = 0; i < pathArr.length; i++) { | ||
var p = pathArr[i]; | ||
if (!(0, _types.isObj)(obj[p])) { | ||
if (obj[p] === undefined && value === undefined) return; | ||
obj[p] = {}; | ||
if (!types_1.isObj(obj) || !path) { | ||
return; | ||
} | ||
if (i === pathArr.length - 1) { | ||
obj[p] = value; | ||
path = toString(path); | ||
if (path in obj) { | ||
obj[path] = value; | ||
return; | ||
} | ||
obj = obj[p]; | ||
} | ||
const pathArr = getPathSegments(path); | ||
for (let i = 0; i < pathArr.length; i++) { | ||
const p = pathArr[i]; | ||
if (!types_1.isObj(obj[p])) { | ||
if (obj[p] === undefined && value === undefined) { | ||
return; | ||
} | ||
obj[p] = /^\d+$/.test(pathArr[i + 1 + '']) ? [] : {}; | ||
} | ||
if (i === pathArr.length - 1) { | ||
obj[p] = value; | ||
} | ||
obj = obj[p]; | ||
} | ||
} | ||
function _deleteIn(obj, path) { | ||
if (!(0, _types.isObj)(obj) || !path) { | ||
return; | ||
} | ||
path = toString(path); | ||
if (path in obj) { | ||
delete obj[path]; | ||
return; | ||
} | ||
var pathArr = getPathSegments(path); | ||
for (var i = 0; i < pathArr.length; i++) { | ||
var p = pathArr[i]; | ||
if (i === pathArr.length - 1) { | ||
if ((0, _types.isArr)(obj)) { | ||
obj.splice(p, 1); | ||
} else { | ||
delete obj[p]; | ||
} | ||
return; | ||
if (!types_1.isObj(obj) || !path) { | ||
return; | ||
} | ||
obj = obj[p]; | ||
if (!(0, _types.isObj)(obj)) { | ||
return; | ||
path = toString(path); | ||
if (path in obj) { | ||
delete obj[path]; | ||
return; | ||
} | ||
} | ||
const pathArr = getPathSegments(path); | ||
for (let i = 0; i < pathArr.length; i++) { | ||
const p = pathArr[i]; | ||
if (i === pathArr.length - 1) { | ||
if (types_1.isArr(obj)) { | ||
obj.splice(p, 1); | ||
} | ||
else { | ||
delete obj[p]; | ||
} | ||
return; | ||
} | ||
obj = obj[p]; | ||
if (!types_1.isObj(obj)) { | ||
return; | ||
} | ||
} | ||
} | ||
function _existIn(obj, path) { | ||
if (!(0, _types.isObj)(obj) || !path) { | ||
return false; | ||
} | ||
path = toString(path); | ||
if (path in obj) { | ||
return true; | ||
} | ||
var pathArr = getPathSegments(path); | ||
for (var i = 0; i < pathArr.length; i++) { | ||
if ((0, _types.isObj)(obj)) { | ||
if (!(pathArr[i] in obj)) { | ||
if (!types_1.isObj(obj) || !path) { | ||
return false; | ||
} | ||
obj = obj[pathArr[i]]; | ||
} else { | ||
return false; | ||
} | ||
} | ||
return true; | ||
path = toString(path); | ||
if (path in obj) { | ||
return true; | ||
} | ||
const pathArr = getPathSegments(path); | ||
for (let i = 0; i < pathArr.length; i++) { | ||
if (types_1.isObj(obj)) { | ||
if (!(pathArr[i] in obj)) { | ||
return false; | ||
} | ||
obj = obj[pathArr[i]]; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
var getIn = resolveGetIn(_getIn); | ||
exports.getIn = getIn; | ||
var setIn = resolveUpdateIn(_setIn, getIn); | ||
exports.setIn = setIn; | ||
var deleteIn = resolveUpdateIn(_deleteIn, getIn); | ||
exports.deleteIn = deleteIn; | ||
var existIn = resolveExistIn(_existIn); | ||
exports.existIn = existIn; | ||
exports.getIn = resolveGetIn(_getIn); | ||
exports.setIn = resolveUpdateIn(_setIn, exports.getIn); | ||
exports.deleteIn = resolveUpdateIn(_deleteIn, exports.getIn); | ||
exports.existIn = resolveExistIn(_existIn); |
204
lib/array.js
"use strict"; | ||
exports.__esModule = true; | ||
exports.includes = exports.find = exports.findIndex = exports.some = exports.every = exports.reduce = exports.map = exports.each = exports.toArr = void 0; | ||
var _types = require("./types"); | ||
var toArr = function toArr(val) { | ||
return (0, _types.isArr)(val) ? val : val ? [val] : []; | ||
}; | ||
exports.toArr = toArr; | ||
var each = function each(val, callback, revert) { | ||
if ((0, _types.isArr)(val)) { | ||
if (revert) { | ||
for (var i = val.length - 1; i >= 0; i--) { | ||
if (callback(val[i], i) === false) { | ||
return; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = require("@uform/types"); | ||
exports.toArr = (val) => (types_1.isArr(val) ? val : val ? [val] : []); | ||
function each(val, iterator, revert) { | ||
if (types_1.isArr(val) || types_1.isStr(val)) { | ||
if (revert) { | ||
for (let i = val.length - 1; i >= 0; i--) { | ||
if (iterator(val[i], i) === false) { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
for (var _i = 0, length = val.length; _i < length; _i++) { | ||
if (callback(val[_i], _i) === false) { | ||
return; | ||
else { | ||
for (let i = 0, length = val.length; i < length; i++) { | ||
if (iterator(val[i], i) === false) { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
for (var key in val) { | ||
if (Object.hasOwnProperty.call(val, key)) { | ||
if (callback(val[key], key) === false) { | ||
return; | ||
else if (types_1.isObj(val)) { | ||
let key; | ||
for (key in val) { | ||
if (Object.hasOwnProperty.call(val, key)) { | ||
if (iterator(val[key], key) === false) { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
exports.each = each; | ||
var map = function map(val, callback, revert) { | ||
var res = (0, _types.isArr)(val) ? [] : {}; | ||
each(val, function (item, key) { | ||
var value = callback(item, key); | ||
if ((0, _types.isArr)(res)) { | ||
res.push(value); | ||
} else { | ||
res[key] = value; | ||
} | ||
}, revert); | ||
return res; | ||
}; | ||
function map(val, iterator, revert) { | ||
const res = types_1.isArr(val) || types_1.isStr(val) ? [] : {}; | ||
each(val, (item, key) => { | ||
const value = iterator(item, key); | ||
if (types_1.isArr(res)) { | ||
; | ||
res.push(value); | ||
} | ||
else { | ||
res[key] = value; | ||
} | ||
}, revert); | ||
return res; | ||
} | ||
exports.map = map; | ||
var reduce = function reduce(val, callback, initial, revert) { | ||
var res = initial; | ||
each(val, function (item, key) { | ||
res = callback(res, item, key); | ||
}, revert); | ||
return res; | ||
}; | ||
function reduce(val, iterator, accumulator, revert) { | ||
let result = accumulator; | ||
each(val, (item, key) => { | ||
result = iterator(result, item, key); | ||
}, revert); | ||
return result; | ||
} | ||
exports.reduce = reduce; | ||
var every = function every(val, callback, revert) { | ||
var res = true; | ||
each(val, function (item, key) { | ||
if (!callback(item, key)) { | ||
res = false; | ||
return false; | ||
} | ||
}, revert); | ||
return res; | ||
}; | ||
function every(val, iterator, revert) { | ||
let res = true; | ||
each(val, (item, key) => { | ||
if (!iterator(item, key)) { | ||
res = false; | ||
return false; | ||
} | ||
}, revert); | ||
return res; | ||
} | ||
exports.every = every; | ||
var some = function some(val, callback, revert) { | ||
var res = false; | ||
each(val, function (item, key) { | ||
if (callback(item, key)) { | ||
res = true; | ||
return false; | ||
} | ||
}, revert); | ||
return res; | ||
}; | ||
function some(val, iterator, revert) { | ||
let res = false; | ||
each(val, (item, key) => { | ||
if (iterator(item, key)) { | ||
res = true; | ||
return false; | ||
} | ||
}, revert); | ||
return res; | ||
} | ||
exports.some = some; | ||
var findIndex = function findIndex(val, callback, revert) { | ||
var res = -1; | ||
each(val, function (item, key) { | ||
if (callback(item, key)) { | ||
res = key; | ||
return false; | ||
} | ||
}, revert); | ||
return res; | ||
}; | ||
function findIndex(val, iterator, revert) { | ||
let res = -1; | ||
each(val, (item, key) => { | ||
if (iterator(item, key)) { | ||
res = key; | ||
return false; | ||
} | ||
}, revert); | ||
return res; | ||
} | ||
exports.findIndex = findIndex; | ||
var find = function find(val, callback, revert) { | ||
var res; | ||
each(val, function (item, key) { | ||
if (callback(item, key)) { | ||
res = item; | ||
return false; | ||
} | ||
}, revert); | ||
return res; | ||
}; | ||
function find(val, iterator, revert) { | ||
let res; | ||
each(val, (item, key) => { | ||
if (iterator(item, key)) { | ||
res = item; | ||
return false; | ||
} | ||
}, revert); | ||
return res; | ||
} | ||
exports.find = find; | ||
var includes = function includes(val, searchElement, revert) { | ||
return some(val, function (item) { | ||
return item === searchElement; | ||
}, revert); | ||
}; | ||
exports.includes = includes; | ||
function includes(val, searchElement, revert) { | ||
return some(val, item => item === searchElement, revert); | ||
} | ||
exports.includes = includes; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
exports.__esModule = true; | ||
exports.Broadcast = void 0; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _array = require("./array"); | ||
var _types = require("./types"); | ||
var Broadcast = | ||
/*#__PURE__*/ | ||
function () { | ||
function Broadcast() { | ||
(0, _defineProperty2["default"])(this, "entries", []); | ||
(0, _defineProperty2["default"])(this, "buffer", []); | ||
} | ||
var _proto = Broadcast.prototype; | ||
_proto.subscribe = function subscribe(subscriber, subscription) { | ||
var _this = this; | ||
if (!(0, _types.isFn)(subscriber)) return function () {}; | ||
var index = this.entries.length; | ||
this.entries.push({ | ||
subscriber: subscriber, | ||
subscription: subscription | ||
}); | ||
this.flushBuffer(this.entries[index]); | ||
return function () { | ||
_this.entries.splice(index, 1); | ||
}; | ||
}; | ||
_proto.unsubscribe = function unsubscribe() { | ||
this.entries.length = 0; | ||
this.buffer.length = 0; | ||
}; | ||
_proto.flushBuffer = function flushBuffer(_ref) { | ||
var subscriber = _ref.subscriber, | ||
subscription = _ref.subscription; | ||
(0, _array.each)(this.buffer, function (_ref2) { | ||
var payload = _ref2.payload, | ||
filter = _ref2.filter; | ||
if ((0, _types.isFn)(filter)) { | ||
var notification; | ||
if (notification = filter(payload, subscription)) { | ||
subscriber(notification); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = require("@uform/types"); | ||
var array_1 = require("./array"); | ||
const noop = () => undefined; | ||
class Broadcast { | ||
constructor() { | ||
this.entries = []; | ||
this.buffer = []; | ||
} | ||
subscribe(subscriber, subscription) { | ||
if (!types_1.isFn(subscriber)) { | ||
return noop; | ||
} | ||
} else { | ||
subscriber(payload, subscription); | ||
} | ||
}); | ||
}; | ||
_proto.notify = function notify(payload, filter) { | ||
if (this.length === 0) { | ||
this.buffer.push({ | ||
payload: payload, | ||
filter: filter | ||
}); | ||
return; | ||
const index = this.entries.length; | ||
this.entries.push({ | ||
subscriber, | ||
subscription | ||
}); | ||
this.flushBuffer(this.entries[index]); | ||
return () => { | ||
this.entries.splice(index, 1); | ||
}; | ||
} | ||
(0, _array.each)(this.entries, function (_ref3) { | ||
var subscriber = _ref3.subscriber, | ||
subscription = _ref3.subscription; | ||
if ((0, _types.isFn)(filter)) { | ||
var notification; | ||
if (notification = filter(payload, subscription)) { | ||
subscriber(notification); | ||
unsubscribe() { | ||
this.entries.length = 0; | ||
this.buffer.length = 0; | ||
} | ||
flushBuffer({ subscriber, subscription }) { | ||
array_1.each(this.buffer, ({ payload, filter }) => { | ||
if (types_1.isFn(filter)) { | ||
const notification = filter(payload, subscription); | ||
if (notification !== undefined) { | ||
subscriber(notification); | ||
} | ||
} | ||
else { | ||
subscriber(payload, subscription); | ||
} | ||
}); | ||
} | ||
notify(payload, filter) { | ||
if (this.length === 0) { | ||
this.buffer.push({ payload, filter }); | ||
return; | ||
} | ||
} else { | ||
subscriber(payload, subscription); | ||
} | ||
}); | ||
this.buffer.length = 0; | ||
}; | ||
return Broadcast; | ||
}(); | ||
exports.Broadcast = Broadcast; | ||
array_1.each(this.entries, ({ subscriber, subscription }) => { | ||
if (types_1.isFn(filter)) { | ||
const notification = filter(payload, subscription); | ||
if (notification !== undefined) { | ||
subscriber(notification); | ||
} | ||
} | ||
else { | ||
subscriber(payload, subscription); | ||
} | ||
}); | ||
this.buffer.length = 0; | ||
} | ||
} | ||
exports.Broadcast = Broadcast; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.lowercase = exports.camelCase = void 0; | ||
var camelCase = require('camel-case'); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var camelCase = require("camel-case"); | ||
exports.camelCase = camelCase; | ||
var lowercase = function lowercase(str) { | ||
return String(str || '').toLowerCase(); | ||
}; | ||
exports.lowercase = lowercase; | ||
exports.lowercase = (str) => String(str || '').toLowerCase(); |
134
lib/clone.js
"use strict"; | ||
exports.__esModule = true; | ||
exports.clone = void 0; | ||
var _types = require("./types"); | ||
var self = void 0 || global || window; | ||
var NATIVE_KEYS = [['Map', function (map) { | ||
return new Map(map); | ||
}], ['WeakMap', function (map) { | ||
return new WeakMap(map); | ||
}], ['WeakSet', function (set) { | ||
return new WeakSet(set); | ||
}], ['Set', function (set) { | ||
return new Set(set); | ||
}], 'FileList', 'File', 'URL', 'RegExp', ['Promise', function (promise) { | ||
return new Promise(function (resolve, reject) { | ||
return promise.then(resolve, reject); | ||
}); | ||
}]]; | ||
var isNativeObject = function isNativeObject(values) { | ||
for (var i = 0; i < NATIVE_KEYS.length; i++) { | ||
var item = NATIVE_KEYS[i]; | ||
if (Array.isArray(item) && item[0]) { | ||
if (self[item[0]] && values instanceof self[item[0]]) { | ||
return item[1] ? item[1] : item[0]; | ||
} | ||
} else { | ||
if (self[item] && values instanceof self[item]) { | ||
return item; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = require("@uform/types"); | ||
const self = this || global || window; | ||
const NATIVE_KEYS = [ | ||
['Map', (map) => new Map(map)], | ||
['WeakMap', (map) => new WeakMap(map)], | ||
['WeakSet', (set) => new WeakSet(set)], | ||
['Set', (set) => new Set(set)], | ||
['Date', (date) => new Date(date)], | ||
'FileList', | ||
'File', | ||
'URL', | ||
'RegExp', | ||
[ | ||
'Promise', | ||
(promise) => new Promise((resolve, reject) => promise.then(resolve, reject)) | ||
] | ||
]; | ||
const isNativeObject = (values) => { | ||
for (let i = 0; i < NATIVE_KEYS.length; i++) { | ||
const item = NATIVE_KEYS[i]; | ||
if (Array.isArray(item) && item[0]) { | ||
if (self[item[0]] && | ||
values instanceof self[item[0]]) { | ||
return item[1] ? item[1] : item[0]; | ||
} | ||
} | ||
else { | ||
if (self[item] && values instanceof self[item]) { | ||
return item; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
var clone = function clone(values, filter) { | ||
var _nativeClone; | ||
if (Array.isArray(values)) { | ||
return values.map(function (item) { | ||
return clone(item, filter); | ||
}); | ||
} else if (_nativeClone = isNativeObject(values)) { | ||
return (0, _types.isFn)(_nativeClone) ? _nativeClone(values) : values; | ||
} else if (typeof values === 'object' && !!values) { | ||
if ('$$typeof' in values && '_owner' in values) { | ||
return values; | ||
exports.clone = (values, filter) => { | ||
let nativeClone; | ||
if (Array.isArray(values)) { | ||
return values.map(item => exports.clone(item, filter)); | ||
} | ||
var res = {}; | ||
for (var key in values) { | ||
if (Object.hasOwnProperty.call(values, key)) { | ||
if ((0, _types.isFn)(filter)) { | ||
if (filter(values[key], key)) { | ||
res[key] = clone(values[key], filter); | ||
} else { | ||
res[key] = values[key]; | ||
} | ||
} else { | ||
res[key] = clone(values[key], filter); | ||
else if (isNativeObject(values)) { | ||
nativeClone = isNativeObject(values); | ||
return types_1.isFn(nativeClone) ? nativeClone(values) : values; | ||
} | ||
else if (typeof values === 'object' && !!values) { | ||
if ('$$typeof' in values && '_owner' in values) { | ||
return values; | ||
} | ||
} | ||
if (Object.getOwnPropertySymbols(values || {}).length) { | ||
return values; | ||
} | ||
const res = {}; | ||
for (const key in values) { | ||
if (Object.hasOwnProperty.call(values, key)) { | ||
if (types_1.isFn(filter)) { | ||
if (filter(values[key], key)) { | ||
res[key] = exports.clone(values[key], filter); | ||
} | ||
else { | ||
res[key] = values[key]; | ||
} | ||
} | ||
else { | ||
res[key] = exports.clone(values[key], filter); | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
return res; | ||
} else { | ||
return values; | ||
} | ||
else { | ||
return values; | ||
} | ||
}; | ||
exports.clone = clone; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.isEqual = void 0; | ||
var _types = require("./types"); | ||
var isArray = _types.isArr; | ||
var keyList = Object.keys; | ||
var hasProp = Object.prototype.hasOwnProperty; | ||
/* eslint-disable */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = require("@uform/types"); | ||
const isArray = types_1.isArr; | ||
const keyList = Object.keys; | ||
const hasProp = Object.prototype.hasOwnProperty; | ||
function equal(a, b, filter) { | ||
// fast-deep-equal index.js 2.0.1 | ||
if (a === b) return true; | ||
if (a && b && typeof a === 'object' && typeof b === 'object') { | ||
var arrA = isArray(a); | ||
var arrB = isArray(b); | ||
var i; | ||
var length; | ||
var key; | ||
if (arrA && arrB) { | ||
length = a.length; | ||
if (length !== b.length) return false; | ||
for (i = length; i-- !== 0;) { | ||
if (!equal(a[i], b[i], filter)) return false; | ||
} | ||
return true; | ||
if (a === b) { | ||
return true; | ||
} | ||
if (arrA != arrB) return false; | ||
var dateA = a instanceof Date; | ||
var dateB = b instanceof Date; | ||
if (dateA != dateB) return false; | ||
if (dateA && dateB) return a.getTime() == b.getTime(); | ||
var regexpA = a instanceof RegExp; | ||
var regexpB = b instanceof RegExp; | ||
if (regexpA != regexpB) return false; | ||
if (regexpA && regexpB) return a.toString() == b.toString(); | ||
var urlA = a instanceof URL; | ||
var urlB = b instanceof URL; | ||
if (urlA && urlB) return urlA.href == urlB.href; | ||
var keys = keyList(a); | ||
length = keys.length; | ||
if (length !== keyList(b).length) return false; | ||
for (i = length; i-- !== 0;) { | ||
if (!hasProp.call(b, keys[i])) return false; | ||
} // end fast-deep-equal | ||
// Custom handling for React | ||
for (i = length; i-- !== 0;) { | ||
key = keys[i]; | ||
if (key === '_owner' && a.$$typeof) { | ||
// React-specific: avoid traversing React elements' _owner. | ||
// _owner contains circular references | ||
// and is not needed when comparing the actual elements (and not their owners) | ||
// .$$typeof and ._store on just reasonable markers of a react element | ||
continue; | ||
} else { | ||
if ((0, _types.isFn)(filter)) { | ||
if (filter({ | ||
a: a[key], | ||
b: b[key] | ||
}, key)) { | ||
if (!equal(a[key], b[key], filter)) return false; | ||
} | ||
} else { | ||
// all other properties should be traversed as usual | ||
if (!equal(a[key], b[key], filter)) return false; | ||
if (a && b && typeof a === 'object' && typeof b === 'object') { | ||
const arrA = isArray(a); | ||
const arrB = isArray(b); | ||
let i; | ||
let length; | ||
let key; | ||
if (arrA && arrB) { | ||
length = a.length; | ||
if (length !== b.length) { | ||
return false; | ||
} | ||
for (i = length; i-- !== 0;) { | ||
if (!equal(a[i], b[i], filter)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} // fast-deep-equal index.js 2.0.1 | ||
return true; | ||
} | ||
if (a && b && typeof a === 'function' && typeof b === 'function') { | ||
return a.toString() === b.toString(); | ||
} | ||
return a !== a && b !== b; | ||
} // end fast-deep-equal | ||
var isEqual = function exportedEqual(a, b, filter) { | ||
try { | ||
return equal(a, b, filter); | ||
} catch (error) { | ||
if (error.message && error.message.match(/stack|recursion/i) || error.number === -2146828260) { | ||
// warn on circular references, don't crash | ||
// browsers give this different errors name and messages: | ||
// chrome/safari: "RangeError", "Maximum call stack size exceeded" | ||
// firefox: "InternalError", too much recursion" | ||
// edge: "Error", "Out of stack space" | ||
console.warn('Warning: react-fast-compare does not handle circular references.', error.name, error.message); | ||
return false; | ||
} // some other error. we should definitely know about these | ||
throw error; | ||
} | ||
if (arrA !== arrB) { | ||
return false; | ||
} | ||
const dateA = a instanceof Date; | ||
const dateB = b instanceof Date; | ||
if (dateA !== dateB) { | ||
return false; | ||
} | ||
if (dateA && dateB) { | ||
return a.getTime() === b.getTime(); | ||
} | ||
const regexpA = a instanceof RegExp; | ||
const regexpB = b instanceof RegExp; | ||
if (regexpA !== regexpB) { | ||
return false; | ||
} | ||
if (regexpA && regexpB) { | ||
return a.toString() === b.toString(); | ||
} | ||
const urlA = a instanceof URL; | ||
const urlB = b instanceof URL; | ||
if (urlA && urlB) { | ||
return a.href === b.href; | ||
} | ||
const keys = keyList(a); | ||
length = keys.length; | ||
if (length !== keyList(b).length) { | ||
return false; | ||
} | ||
for (i = length; i-- !== 0;) { | ||
if (!hasProp.call(b, keys[i])) { | ||
return false; | ||
} | ||
} | ||
for (i = length; i-- !== 0;) { | ||
key = keys[i]; | ||
if (key === '_owner' && a.$$typeof) { | ||
continue; | ||
} | ||
else { | ||
if (types_1.isFn(filter)) { | ||
if (filter({ a: a[key], b: b[key] }, key)) { | ||
if (!equal(a[key], b[key], filter)) { | ||
return false; | ||
} | ||
} | ||
} | ||
else { | ||
if (!equal(a[key], b[key], filter)) { | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
if (a && b && typeof a === 'function' && typeof b === 'function') { | ||
return a.toString() === b.toString(); | ||
} | ||
return a !== a && b !== b; | ||
} | ||
exports.isEqual = function exportedEqual(a, b, filter) { | ||
try { | ||
return equal(a, b, filter); | ||
} | ||
catch (error) { | ||
if ((error.message && error.message.match(/stack|recursion/i)) || | ||
error.number === -2146828260) { | ||
console.warn('Warning: react-fast-compare does not handle circular references.', error.name, error.message); | ||
return false; | ||
} | ||
throw error; | ||
} | ||
}; | ||
exports.isEqual = isEqual; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.defer = void 0; | ||
var defer = function defer() { | ||
var _resolve; | ||
var _reject; | ||
var promise = new Promise(function (resolve, reject) { | ||
_resolve = resolve; | ||
_reject = reject; | ||
}); | ||
return { | ||
promise: promise, | ||
resolve: _resolve, | ||
reject: _reject | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.defer = () => { | ||
let internalResolve; | ||
let internalReject; | ||
const promise = new Promise((resolve, reject) => { | ||
internalResolve = resolve; | ||
internalReject = reject; | ||
}); | ||
return { | ||
promise, | ||
resolve: internalResolve, | ||
reject: internalReject | ||
}; | ||
}; | ||
exports.defer = defer; |
"use strict"; | ||
exports.__esModule = true; | ||
var _accessor = require("./accessor"); | ||
Object.keys(_accessor).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
exports[key] = _accessor[key]; | ||
}); | ||
var _array = require("./array"); | ||
Object.keys(_array).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
exports[key] = _array[key]; | ||
}); | ||
var _compare = require("./compare"); | ||
Object.keys(_compare).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
exports[key] = _compare[key]; | ||
}); | ||
var _broadcast = require("./broadcast"); | ||
Object.keys(_broadcast).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
exports[key] = _broadcast[key]; | ||
}); | ||
var _types = require("./types"); | ||
Object.keys(_types).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
exports[key] = _types[key]; | ||
}); | ||
var _clone = require("./clone"); | ||
Object.keys(_clone).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
exports[key] = _clone[key]; | ||
}); | ||
var _schema = require("./schema"); | ||
Object.keys(_schema).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
exports[key] = _schema[key]; | ||
}); | ||
var _lru = require("./lru"); | ||
Object.keys(_lru).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
exports[key] = _lru[key]; | ||
}); | ||
var _isEmpty = require("./isEmpty"); | ||
Object.keys(_isEmpty).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
exports[key] = _isEmpty[key]; | ||
}); | ||
var _case = require("./case"); | ||
Object.keys(_case).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
exports[key] = _case[key]; | ||
}); | ||
var _defer = require("./defer"); | ||
Object.keys(_defer).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
exports[key] = _defer[key]; | ||
}); | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./accessor")); | ||
__export(require("./array")); | ||
__export(require("./compare")); | ||
__export(require("./broadcast")); | ||
__export(require("@uform/types")); | ||
__export(require("./clone")); | ||
__export(require("./schema")); | ||
__export(require("./lru")); | ||
__export(require("./isEmpty")); | ||
__export(require("./case")); | ||
__export(require("./defer")); | ||
__export(require("./stringLength")); |
"use strict"; | ||
exports.__esModule = true; | ||
exports.isEmpty = isEmpty; | ||
/** | ||
* copyright by https://github.com/ianstormtaylor/is-empty | ||
*/ | ||
/** | ||
* Has own property. | ||
* | ||
* @type {Function} | ||
*/ | ||
var has = Object.prototype.hasOwnProperty; | ||
/** | ||
* To string. | ||
* | ||
* @type {Function} | ||
*/ | ||
var toString = Object.prototype.toString; | ||
/** | ||
* Test whether a value is "empty". | ||
* | ||
* @param {Mixed} val | ||
* @return {Boolean} | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const has = Object.prototype.hasOwnProperty; | ||
const toString = Object.prototype.toString; | ||
function isEmpty(val) { | ||
// Null and Undefined... | ||
if (val == null) return true; // Booleans... | ||
if (typeof val === 'boolean') return false; // Numbers... | ||
if (typeof val === 'number') return false; // Strings... | ||
if (typeof val === 'string') return val.length === 0; // Functions... | ||
if (typeof val === 'function') return val.length === 0; // Arrays... | ||
if (Array.isArray(val)) { | ||
if (val.length === 0) return true; | ||
for (var i = 0; i < val.length; i++) { | ||
if (val[i] !== undefined && val[i] !== null && val[i] !== '' && val[i] !== 0) { | ||
if (val == null) { | ||
return true; | ||
} | ||
if (typeof val === 'boolean') { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} // Errors... | ||
if (val instanceof Error) return val.message === ''; // Objects... | ||
if (val.toString === toString) { | ||
switch (val.toString()) { | ||
// Maps, Sets, Files and Errors... | ||
case '[object File]': | ||
case '[object Map]': | ||
case '[object Set]': | ||
{ | ||
return val.size === 0; | ||
if (typeof val === 'number') { | ||
return false; | ||
} | ||
if (typeof val === 'string') { | ||
return val.length === 0; | ||
} | ||
if (typeof val === 'function') { | ||
return val.length === 0; | ||
} | ||
if (Array.isArray(val)) { | ||
if (val.length === 0) { | ||
return true; | ||
} | ||
// Plain objects... | ||
case '[object Object]': | ||
{ | ||
for (var key in val) { | ||
if (has.call(val, key)) return false; | ||
} | ||
return true; | ||
for (let i = 0; i < val.length; i++) { | ||
if (val[i] !== undefined && | ||
val[i] !== null && | ||
val[i] !== '' && | ||
val[i] !== 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} // Anything else... | ||
return false; | ||
} | ||
if (val instanceof Error) { | ||
return val.message === ''; | ||
} | ||
if (val.toString === toString) { | ||
switch (val.toString()) { | ||
case '[object File]': | ||
case '[object Map]': | ||
case '[object Set]': { | ||
return val.size === 0; | ||
} | ||
case '[object Object]': { | ||
for (const key in val) { | ||
if (has.call(val, key)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
exports.isEmpty = isEmpty; |
515
lib/lru.js
"use strict"; | ||
exports.__esModule = true; | ||
exports.LRUMap = LRUMap; | ||
/** | ||
* A doubly linked list-based Least Recently Used (LRU) cache. Will keep most | ||
* recently used items while discarding least recently used items when its limit | ||
* is reached. | ||
* | ||
* Licensed under MIT. Copyright (c) 2010 Rasmus Andersson <http://hunch.se/> | ||
* See README.md for details. | ||
* | ||
* Illustration of the design: | ||
* | ||
* entry entry entry entry | ||
* ______ ______ ______ ______ | ||
* | head |.newer => | |.newer => | |.newer => | tail | | ||
* | A | | B | | C | | D | | ||
* |______| <= older.|______| <= older.|______| <= older.|______| | ||
* | ||
* removed <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- added | ||
*/ | ||
/* eslint-disable */ | ||
var NEWER = Symbol('newer'); | ||
var OLDER = Symbol('older'); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const NEWER = Symbol('newer'); | ||
const OLDER = Symbol('older'); | ||
function LRUMap(limit, entries) { | ||
if (typeof limit !== 'number') { | ||
// called as (entries) | ||
entries = limit; | ||
limit = 0; | ||
} | ||
this.size = 0; | ||
this.limit = limit; | ||
this.oldest = this.newest = undefined; | ||
this._keymap = new Map(); | ||
if (entries) { | ||
this.assign(entries); | ||
if (limit < 1) { | ||
this.limit = this.size; | ||
if (typeof limit !== 'number') { | ||
entries = limit; | ||
limit = 0; | ||
} | ||
} | ||
this.size = 0; | ||
this.limit = limit; | ||
this.oldest = this.newest = undefined; | ||
this._keymap = new Map(); | ||
if (entries) { | ||
this.assign(entries); | ||
if (limit < 1) { | ||
this.limit = this.size; | ||
} | ||
} | ||
} | ||
exports.LRUMap = LRUMap; | ||
function Entry(key, value) { | ||
this.key = key; | ||
this.value = value; | ||
this[NEWER] = undefined; | ||
this[OLDER] = undefined; | ||
this.key = key; | ||
this.value = value; | ||
this[NEWER] = undefined; | ||
this[OLDER] = undefined; | ||
} | ||
LRUMap.prototype._markEntryAsUsed = function (entry) { | ||
if (entry === this.newest) { | ||
// Already the most recenlty used entry, so no need to update the list | ||
return; | ||
} // HEAD--------------TAIL | ||
// <.older .newer> | ||
// <--- add direction -- | ||
// A B C <D> E | ||
if (entry[NEWER]) { | ||
if (entry === this.oldest) { | ||
this.oldest = entry[NEWER]; | ||
if (entry === this.newest) { | ||
return; | ||
} | ||
entry[NEWER][OLDER] = entry[OLDER]; // C <-- E. | ||
} | ||
if (entry[OLDER]) { | ||
entry[OLDER][NEWER] = entry[NEWER]; // C. --> E | ||
} | ||
entry[NEWER] = undefined; // D --x | ||
entry[OLDER] = this.newest; // D. --> E | ||
if (this.newest) { | ||
this.newest[NEWER] = entry; // E. <-- D | ||
} | ||
this.newest = entry; | ||
if (entry[NEWER]) { | ||
if (entry === this.oldest) { | ||
this.oldest = entry[NEWER]; | ||
} | ||
entry[NEWER][OLDER] = entry[OLDER]; | ||
} | ||
if (entry[OLDER]) { | ||
entry[OLDER][NEWER] = entry[NEWER]; | ||
} | ||
entry[NEWER] = undefined; | ||
entry[OLDER] = this.newest; | ||
if (this.newest) { | ||
this.newest[NEWER] = entry; | ||
} | ||
this.newest = entry; | ||
}; | ||
LRUMap.prototype.assign = function (entries) { | ||
var entry; | ||
var limit = this.limit || Number.MAX_VALUE; | ||
this._keymap.clear(); | ||
var it = entries[Symbol.iterator](); | ||
for (var itv = it.next(); !itv.done; itv = it.next()) { | ||
var e = new Entry(itv.value[0], itv.value[1]); | ||
this._keymap.set(e.key, e); | ||
if (!entry) { | ||
this.oldest = e; | ||
} else { | ||
entry[NEWER] = e; | ||
e[OLDER] = entry; | ||
let entry; | ||
let limit = this.limit || Number.MAX_VALUE; | ||
this._keymap.clear(); | ||
const it = entries[Symbol.iterator](); | ||
for (let itv = it.next(); !itv.done; itv = it.next()) { | ||
const e = new Entry(itv.value[0], itv.value[1]); | ||
this._keymap.set(e.key, e); | ||
if (!entry) { | ||
this.oldest = e; | ||
} | ||
else { | ||
entry[NEWER] = e; | ||
e[OLDER] = entry; | ||
} | ||
entry = e; | ||
if (limit-- === 0) { | ||
throw new Error('overflow'); | ||
} | ||
} | ||
entry = e; | ||
if (limit-- === 0) { | ||
throw new Error('overflow'); | ||
} | ||
} | ||
this.newest = entry; | ||
this.size = this._keymap.size; | ||
this.newest = entry; | ||
this.size = this._keymap.size; | ||
}; | ||
LRUMap.prototype.get = function (key) { | ||
// First, find our cache entry | ||
var entry = this._keymap.get(key); | ||
if (!entry) return; // Not cached. Sorry. | ||
// As <key> was found in the cache, register it as being requested recently | ||
this._markEntryAsUsed(entry); | ||
return entry.value; | ||
const entry = this._keymap.get(key); | ||
if (!entry) { | ||
return; | ||
} | ||
this._markEntryAsUsed(entry); | ||
return entry.value; | ||
}; | ||
LRUMap.prototype.set = function (key, value) { | ||
var entry = this._keymap.get(key); | ||
if (entry) { | ||
// update existing | ||
entry.value = value; | ||
this._markEntryAsUsed(entry); | ||
let entry = this._keymap.get(key); | ||
if (entry) { | ||
entry.value = value; | ||
this._markEntryAsUsed(entry); | ||
return this; | ||
} | ||
this._keymap.set(key, (entry = new Entry(key, value))); | ||
if (this.newest) { | ||
this.newest[NEWER] = entry; | ||
entry[OLDER] = this.newest; | ||
} | ||
else { | ||
this.oldest = entry; | ||
} | ||
this.newest = entry; | ||
++this.size; | ||
if (this.size > this.limit) { | ||
this.shift(); | ||
} | ||
return this; | ||
} // new entry | ||
this._keymap.set(key, entry = new Entry(key, value)); | ||
if (this.newest) { | ||
// link previous tail to the new tail (entry) | ||
this.newest[NEWER] = entry; | ||
entry[OLDER] = this.newest; | ||
} else { | ||
// we're first in -- yay | ||
this.oldest = entry; | ||
} // add new entry to the end of the linked list -- it's now the freshest entry. | ||
this.newest = entry; | ||
++this.size; | ||
if (this.size > this.limit) { | ||
// we hit the limit -- remove the head | ||
this.shift(); | ||
} | ||
return this; | ||
}; | ||
LRUMap.prototype.shift = function () { | ||
// todo: handle special case when limit == 1 | ||
var entry = this.oldest; | ||
if (entry) { | ||
if (this.oldest[NEWER]) { | ||
// advance the list | ||
this.oldest = this.oldest[NEWER]; | ||
this.oldest[OLDER] = undefined; | ||
} else { | ||
// the cache is exhausted | ||
this.oldest = undefined; | ||
this.newest = undefined; | ||
} // Remove last strong reference to <entry> and remove links from the purged | ||
// entry being returned: | ||
entry[NEWER] = entry[OLDER] = undefined; | ||
this._keymap["delete"](entry.key); | ||
--this.size; | ||
return [entry.key, entry.value]; | ||
} | ||
}; // ---------------------------------------------------------------------------- | ||
// Following code is optional and can be removed without breaking the core | ||
// functionality. | ||
const entry = this.oldest; | ||
if (entry) { | ||
if (this.oldest[NEWER]) { | ||
this.oldest = this.oldest[NEWER]; | ||
this.oldest[OLDER] = undefined; | ||
} | ||
else { | ||
this.oldest = undefined; | ||
this.newest = undefined; | ||
} | ||
entry[NEWER] = entry[OLDER] = undefined; | ||
this._keymap.delete(entry.key); | ||
--this.size; | ||
return [entry.key, entry.value]; | ||
} | ||
}; | ||
LRUMap.prototype.find = function (key) { | ||
var e = this._keymap.get(key); | ||
return e ? e.value : undefined; | ||
const e = this._keymap.get(key); | ||
return e ? e.value : undefined; | ||
}; | ||
LRUMap.prototype.has = function (key) { | ||
return this._keymap.has(key); | ||
return this._keymap.has(key); | ||
}; | ||
LRUMap.prototype['delete'] = function (key) { | ||
var entry = this._keymap.get(key); | ||
if (!entry) return; | ||
this._keymap["delete"](entry.key); | ||
if (entry[NEWER] && entry[OLDER]) { | ||
// relink the older entry with the newer entry | ||
entry[OLDER][NEWER] = entry[NEWER]; | ||
entry[NEWER][OLDER] = entry[OLDER]; | ||
} else if (entry[NEWER]) { | ||
// remove the link to us | ||
entry[NEWER][OLDER] = undefined; // link the newer entry to head | ||
this.oldest = entry[NEWER]; | ||
} else if (entry[OLDER]) { | ||
// remove the link to us | ||
entry[OLDER][NEWER] = undefined; // link the newer entry to head | ||
this.newest = entry[OLDER]; | ||
} else { | ||
// if(entry[OLDER] === undefined && entry.newer === undefined) { | ||
this.oldest = this.newest = undefined; | ||
} | ||
this.size--; | ||
return entry.value; | ||
LRUMap.prototype.delete = function (key) { | ||
const entry = this._keymap.get(key); | ||
if (!entry) { | ||
return; | ||
} | ||
this._keymap.delete(entry.key); | ||
if (entry[NEWER] && entry[OLDER]) { | ||
entry[OLDER][NEWER] = entry[NEWER]; | ||
entry[NEWER][OLDER] = entry[OLDER]; | ||
} | ||
else if (entry[NEWER]) { | ||
entry[NEWER][OLDER] = undefined; | ||
this.oldest = entry[NEWER]; | ||
} | ||
else if (entry[OLDER]) { | ||
entry[OLDER][NEWER] = undefined; | ||
this.newest = entry[OLDER]; | ||
} | ||
else { | ||
this.oldest = this.newest = undefined; | ||
} | ||
this.size--; | ||
return entry.value; | ||
}; | ||
LRUMap.prototype.clear = function () { | ||
// Not clearing links should be safe, as we don't expose live links to user | ||
this.oldest = this.newest = undefined; | ||
this.size = 0; | ||
this._keymap.clear(); | ||
this.oldest = this.newest = undefined; | ||
this.size = 0; | ||
this._keymap.clear(); | ||
}; | ||
function EntryIterator(oldestEntry) { | ||
this.entry = oldestEntry; | ||
this.entry = oldestEntry; | ||
} | ||
EntryIterator.prototype[Symbol.iterator] = function () { | ||
return this; | ||
return this; | ||
}; | ||
EntryIterator.prototype.next = function () { | ||
var ent = this.entry; | ||
if (ent) { | ||
this.entry = ent[NEWER]; | ||
return { | ||
done: false, | ||
value: [ent.key, ent.value] | ||
}; | ||
} else { | ||
return { | ||
done: true, | ||
value: undefined | ||
}; | ||
} | ||
const ent = this.entry; | ||
if (ent) { | ||
this.entry = ent[NEWER]; | ||
return { done: false, value: [ent.key, ent.value] }; | ||
} | ||
else { | ||
return { done: true, value: undefined }; | ||
} | ||
}; | ||
function KeyIterator(oldestEntry) { | ||
this.entry = oldestEntry; | ||
this.entry = oldestEntry; | ||
} | ||
KeyIterator.prototype[Symbol.iterator] = function () { | ||
return this; | ||
return this; | ||
}; | ||
KeyIterator.prototype.next = function () { | ||
var ent = this.entry; | ||
if (ent) { | ||
this.entry = ent[NEWER]; | ||
return { | ||
done: false, | ||
value: ent.key | ||
}; | ||
} else { | ||
return { | ||
done: true, | ||
value: undefined | ||
}; | ||
} | ||
const ent = this.entry; | ||
if (ent) { | ||
this.entry = ent[NEWER]; | ||
return { done: false, value: ent.key }; | ||
} | ||
else { | ||
return { done: true, value: undefined }; | ||
} | ||
}; | ||
function ValueIterator(oldestEntry) { | ||
this.entry = oldestEntry; | ||
this.entry = oldestEntry; | ||
} | ||
ValueIterator.prototype[Symbol.iterator] = function () { | ||
return this; | ||
return this; | ||
}; | ||
ValueIterator.prototype.next = function () { | ||
var ent = this.entry; | ||
if (ent) { | ||
this.entry = ent[NEWER]; | ||
return { | ||
done: false, | ||
value: ent.value | ||
}; | ||
} else { | ||
return { | ||
done: true, | ||
value: undefined | ||
}; | ||
} | ||
const ent = this.entry; | ||
if (ent) { | ||
this.entry = ent[NEWER]; | ||
return { done: false, value: ent.value }; | ||
} | ||
else { | ||
return { done: true, value: undefined }; | ||
} | ||
}; | ||
LRUMap.prototype.keys = function () { | ||
return new KeyIterator(this.oldest); | ||
return new KeyIterator(this.oldest); | ||
}; | ||
LRUMap.prototype.values = function () { | ||
return new ValueIterator(this.oldest); | ||
return new ValueIterator(this.oldest); | ||
}; | ||
LRUMap.prototype.entries = function () { | ||
return this; | ||
return this; | ||
}; | ||
LRUMap.prototype[Symbol.iterator] = function () { | ||
return new EntryIterator(this.oldest); | ||
return new EntryIterator(this.oldest); | ||
}; | ||
LRUMap.prototype.forEach = function (fun, thisObj) { | ||
if (typeof thisObj !== 'object') { | ||
thisObj = this; | ||
} | ||
var entry = this.oldest; | ||
while (entry) { | ||
fun.call(thisObj, entry.value, entry.key, this); | ||
entry = entry[NEWER]; | ||
} | ||
if (typeof thisObj !== 'object') { | ||
thisObj = this; | ||
} | ||
let entry = this.oldest; | ||
while (entry) { | ||
fun.call(thisObj, entry.value, entry.key, this); | ||
entry = entry[NEWER]; | ||
} | ||
}; | ||
/** Returns a JSON (array) representation */ | ||
LRUMap.prototype.toJSON = function () { | ||
var s = new Array(this.size); | ||
var i = 0; | ||
var entry = this.oldest; | ||
while (entry) { | ||
s[i++] = { | ||
key: entry.key, | ||
value: entry.value | ||
}; | ||
entry = entry[NEWER]; | ||
} | ||
return s; | ||
const s = new Array(this.size); | ||
let i = 0; | ||
let entry = this.oldest; | ||
while (entry) { | ||
s[i++] = { key: entry.key, value: entry.value }; | ||
entry = entry[NEWER]; | ||
} | ||
return s; | ||
}; | ||
/** Returns a String representation */ | ||
LRUMap.prototype.toString = function () { | ||
var s = ''; | ||
var entry = this.oldest; | ||
while (entry) { | ||
s += String(entry.key) + ':' + entry.value; | ||
entry = entry[NEWER]; | ||
if (entry) { | ||
s += ' < '; | ||
let s = ''; | ||
let entry = this.oldest; | ||
while (entry) { | ||
s += String(entry.key) + ':' + entry.value; | ||
entry = entry[NEWER]; | ||
if (entry) { | ||
s += ' < '; | ||
} | ||
} | ||
} | ||
return s; | ||
}; | ||
return s; | ||
}; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.caculateSchemaInitialValues = exports.registerVirtualboxFlag = exports.isVirtualBox = exports.schemaIs = exports.getSchemaNodeFromPath = void 0; | ||
var _array = require("./array"); | ||
var _accessor = require("./accessor"); | ||
var _types = require("./types"); | ||
var _isEmpty = require("./isEmpty"); | ||
var numberRE = /^\d+$/; | ||
var VIRTUAL_BOXES = {}; | ||
var getSchemaNodeFromPath = function getSchemaNodeFromPath(schema, path) { | ||
var res = schema; | ||
var suc = 0; | ||
path = (0, _array.toArr)(path); | ||
for (var i = 0; i < path.length; i++) { | ||
var key = path[i]; | ||
if (res && !(0, _isEmpty.isEmpty)(res.properties)) { | ||
res = res.properties[key]; | ||
suc++; | ||
} else if (res && !(0, _isEmpty.isEmpty)(res.items) && numberRE.test(key)) { | ||
res = res.items; | ||
suc++; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var array_1 = require("./array"); | ||
var accessor_1 = require("./accessor"); | ||
var types_1 = require("@uform/types"); | ||
var isEmpty_1 = require("./isEmpty"); | ||
const numberRE = /^\d+$/; | ||
const VIRTUAL_BOXES = {}; | ||
exports.getSchemaNodeFromPath = (schema, path) => { | ||
let res = schema; | ||
let suc = 0; | ||
path = array_1.toArr(path); | ||
for (let i = 0; i < path.length; i++) { | ||
const key = path[i]; | ||
if (res && !isEmpty_1.isEmpty(res.properties)) { | ||
res = res.properties[key]; | ||
suc++; | ||
} | ||
else if (res && !isEmpty_1.isEmpty(res.items) && numberRE.test(key)) { | ||
res = res.items; | ||
suc++; | ||
} | ||
} | ||
} | ||
return suc === path.length ? res : undefined; | ||
return suc === path.length ? res : undefined; | ||
}; | ||
exports.getSchemaNodeFromPath = getSchemaNodeFromPath; | ||
var schemaIs = function schemaIs(schema, type) { | ||
return schema && schema.type === type; | ||
exports.schemaIs = (schema, type) => { | ||
return schema && schema.type === type; | ||
}; | ||
exports.schemaIs = schemaIs; | ||
var isVirtualBox = function isVirtualBox(name) { | ||
return !!VIRTUAL_BOXES[name]; | ||
exports.isVirtualBox = (name) => { | ||
return !!VIRTUAL_BOXES[name]; | ||
}; | ||
exports.isVirtualBox = isVirtualBox; | ||
var registerVirtualboxFlag = function registerVirtualboxFlag(name) { | ||
VIRTUAL_BOXES[name] = true; | ||
exports.registerVirtualboxFlag = (name) => { | ||
VIRTUAL_BOXES[name] = true; | ||
}; | ||
exports.registerVirtualboxFlag = registerVirtualboxFlag; | ||
var isVirtualBoxSchema = function isVirtualBoxSchema(schema) { | ||
return isVirtualBox(schema['type']) || isVirtualBox(schema['x-component']); | ||
const isVirtualBoxSchema = (schema) => { | ||
return exports.isVirtualBox(schema.type) || exports.isVirtualBox(schema['x-component']); | ||
}; | ||
var schemaTraverse = function schemaTraverse(schema, callback, path, schemaPath) { | ||
if (path === void 0) { | ||
path = []; | ||
} | ||
if (schemaPath === void 0) { | ||
schemaPath = []; | ||
} | ||
if (schema) { | ||
if (isVirtualBoxSchema(schema)) { | ||
path = path.slice(0, path.length - 1); | ||
const schemaTraverse = (schema, callback, path = [], schemaPath = []) => { | ||
if (schema) { | ||
if (isVirtualBoxSchema(schema)) { | ||
path = path.slice(0, path.length - 1); | ||
} | ||
callback(schema, { path, schemaPath }); | ||
if (exports.schemaIs(schema, 'object') || schema.properties) { | ||
array_1.each(schema.properties, (subSchema, key) => { | ||
schemaTraverse(subSchema, callback, path.concat(key), schemaPath.concat(key)); | ||
}); | ||
} | ||
else if (exports.schemaIs(schema, 'array') || schema.items) { | ||
if (schema.items) { | ||
callback(schema.items, key => { | ||
schemaTraverse(schema.items, callback, path.concat(key), schemaPath.concat(key)); | ||
}, path); | ||
} | ||
} | ||
} | ||
callback(schema, { | ||
path: path, | ||
schemaPath: schemaPath | ||
}; | ||
exports.caculateSchemaInitialValues = (schema, initialValues, callback) => { | ||
initialValues = initialValues || schema.default || {}; | ||
schemaTraverse(schema, (subSchema, $path, parentPath) => { | ||
const defaultValue = subSchema.default; | ||
if (types_1.isFn($path) && parentPath) { | ||
array_1.each(array_1.toArr(accessor_1.getIn(initialValues, parentPath)), (value, index) => { | ||
$path(index); | ||
}); | ||
} | ||
else if ($path) { | ||
const isVirtualBoxInstance = isVirtualBoxSchema(subSchema); | ||
const name = isVirtualBoxInstance | ||
? $path.schemaPath.join('.') | ||
: $path.path.join('.'); | ||
const path = isVirtualBoxInstance ? $path.schemaPath : $path.path; | ||
const schemaPath = $path.schemaPath; | ||
const initialValue = accessor_1.getIn(initialValues, name); | ||
const value = !isEmpty_1.isEmpty(initialValue) ? initialValue : defaultValue; | ||
if (!isEmpty_1.isEmpty(value)) { | ||
accessor_1.setIn(initialValues, name, value); | ||
} | ||
if (types_1.isFn(callback)) { | ||
const newPath = { | ||
name, | ||
path, | ||
schemaPath | ||
}; | ||
callback(newPath, subSchema, value); | ||
} | ||
} | ||
}); | ||
if (schemaIs(schema, 'object') || schema.properties) { | ||
(0, _array.each)(schema.properties, function (schema, key) { | ||
schemaTraverse(schema, callback, path.concat(key), schemaPath.concat(key)); | ||
}); | ||
} else if (schemaIs(schema, 'array') || schema.items) { | ||
if (schema.items) { | ||
callback(schema.items, function (index) { | ||
schemaTraverse(schema.items, callback, path.concat(index), schemaPath.concat(index)); | ||
}, path); | ||
} | ||
} | ||
} | ||
return initialValues; | ||
}; | ||
var caculateSchemaInitialValues = function caculateSchemaInitialValues(schema, initialValues, callback) { | ||
initialValues = initialValues || schema["default"] || {}; | ||
schemaTraverse(schema, function (schema, $path, parentPath) { | ||
var defaultValue = schema["default"]; | ||
if ((0, _types.isFn)($path) && parentPath) { | ||
(0, _array.each)((0, _array.toArr)((0, _accessor.getIn)(initialValues, parentPath)), function (value, index) { | ||
$path(index); | ||
}); | ||
} else if ($path) { | ||
var _isVirtualBox = isVirtualBoxSchema(schema); | ||
var name = _isVirtualBox ? $path.schemaPath.join('.') : $path.path.join('.'); | ||
var path = _isVirtualBox ? $path.schemaPath : $path.path; | ||
var schemaPath = $path.schemaPath; | ||
var initialValue = (0, _accessor.getIn)(initialValues, name); | ||
var value = !(0, _isEmpty.isEmpty)(initialValue) ? initialValue : defaultValue; | ||
if (!(0, _isEmpty.isEmpty)(value)) { | ||
(0, _accessor.setIn)(initialValues, name, value); | ||
} | ||
if ((0, _types.isFn)(callback)) { | ||
var _path = { | ||
name: name, | ||
path: path, | ||
schemaPath: schemaPath | ||
}; | ||
callback(_path, schema, value); | ||
} | ||
} | ||
}); | ||
return initialValues; | ||
}; | ||
exports.caculateSchemaInitialValues = caculateSchemaInitialValues; |
{ | ||
"name": "@uform/utils", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"license": "MIT", | ||
"main": "lib", | ||
"types": "lib/index.d.ts", | ||
"repository": { | ||
@@ -20,3 +21,3 @@ "type": "git", | ||
}, | ||
"gitHead": "4819ac65b7097283c7b4e9589b30d69897b5c826", | ||
"gitHead": "24956f7c858a183300c95953b806c3ad84eda162", | ||
"peerDependencies": { | ||
@@ -26,4 +27,5 @@ "@babel/runtime": "^7.4.4" | ||
"dependencies": { | ||
"@uform/types": "^0.3.0", | ||
"camel-case": "^3.0.0" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
82991
40
2820
3
1
+ Added@uform/types@^0.3.0
+ Added@uform/types@0.3.11(transitive)
+ Addedrxjs@6.6.7(transitive)
+ Addedtslib@1.14.1(transitive)