@sinclair/typebox
Version:
JSONSchema Type Builder with Static Type Resolution for TypeScript
192 lines (184 loc) • 7.36 kB
JavaScript
;
/*--------------------------------------------------------------------------
@sinclair/typebox/value
The MIT License (MIT)
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeltaValue = exports.EditType = void 0;
const reflect_1 = require("./reflect");
const pointer_1 = require("./pointer");
const clone_1 = require("./clone");
var FlatValue;
(function (FlatValue) {
function* Undefined(key, value) {
yield [key, { type: 'undefined', value }];
}
function* Null(key, value) {
yield [key, { type: 'null', value }];
}
function* Function(key, value) {
yield [key, { type: 'function', value }];
}
function* Object(key, value) {
yield [key, { type: 'object', value }];
for (const entry of globalThis.Object.entries(value)) {
yield* Visit(`${key}/${entry[0]}`, entry[1]);
}
}
function* Array(key, value) {
yield [key, { type: 'array', value }];
for (let i = 0; i < value.length; i++) {
yield* Visit(`${key}/${i}`, value[i]);
}
}
function* BigInt(key, value) {
yield [key, { type: 'bigint', value }];
}
function* Symbol(key, value) {
yield [key, { type: 'symbol', value }];
}
function* String(key, value) {
yield [key, { type: 'string', value }];
}
function* Boolean(key, value) {
yield [key, { type: 'boolean', value }];
}
function* Number(key, value) {
yield [key, { type: 'number', value }];
}
function* Visit(path, value) {
switch ((0, reflect_1.Reflect)(value)) {
case 'array':
return yield* Array(path, value);
case 'bigint':
return yield* BigInt(path, value);
case 'boolean':
return yield* Boolean(path, value);
case 'function':
return yield* Function(path, value);
case 'null':
return yield* Null(path, value);
case 'number':
return yield* Number(path, value);
case 'object':
return yield* Object(path, value);
case 'string':
return yield* String(path, value);
case 'symbol':
return yield* Symbol(path, value);
case 'undefined':
return yield* Undefined(path, value);
}
}
function* Create(value) {
return yield* Visit('', value);
}
FlatValue.Create = Create;
})(FlatValue || (FlatValue = {}));
var EditType;
(function (EditType) {
EditType[EditType["Delete"] = 0] = "Delete";
EditType[EditType["Update"] = 1] = "Update";
EditType[EditType["Insert"] = 2] = "Insert";
})(EditType = exports.EditType || (exports.EditType = {}));
var DeltaValue;
(function (DeltaValue) {
function IsRootUpdate(edits) {
return edits.length > 0 && edits[0][0] === EditType.Update && edits[0][1] === '';
}
function IsNullUpdate(edits) {
return edits.length === 0;
}
function* Updates(mapA, mapB) {
for (const [keyB, entryB] of mapB) {
if (!mapA.has(keyB))
continue;
const entryA = mapA.get(keyB);
if (entryA.value === entryB.value)
continue;
if (entryA.type === 'object' && entryB.type === 'object')
continue;
if (entryA.type === 'array' && entryB.type === 'array')
continue;
yield [EditType.Update, keyB, entryB.value];
}
}
function* Inserts(mapA, mapB, updates) {
const discards = [];
for (const [keyB, entryB] of mapB) {
if (discards.some((ignore) => keyB.indexOf(ignore) === 0))
continue;
if (updates.some((update) => keyB.indexOf(update[1]) === 0))
continue;
if (mapA.has(keyB))
continue;
if (entryB.type === 'object' || entryB.type === 'array')
discards.push(keyB);
yield [EditType.Insert, keyB, entryB.value];
}
}
function* Deletes(mapA, mapB) {
const discards = [];
for (const [keyA, entryA] of mapA) {
if (discards.some((discard) => keyA.indexOf(discard) === 0))
continue;
if (mapB.has(keyA))
continue;
if (entryA.type === 'object' || entryA.type === 'array')
discards.push(keyA);
yield [EditType.Delete, keyA];
}
}
function Diff(valueA, valueB) {
const mapA = new Map(FlatValue.Create(valueA));
const mapB = new Map(FlatValue.Create(valueB));
const updates = [...Updates(mapA, mapB)];
const inserts = [...Inserts(mapA, mapB, updates)];
const deletes = [...Deletes(mapA, mapB)].reverse();
return [...updates, ...inserts, ...deletes];
}
DeltaValue.Diff = Diff;
function Edit(valueA, operations) {
if (IsRootUpdate(operations)) {
return clone_1.CloneValue.Create(operations[0][2]);
}
if (IsNullUpdate(operations)) {
return clone_1.CloneValue.Create(valueA);
}
const clone = clone_1.CloneValue.Create(valueA);
for (const operation of operations) {
switch (operation[0]) {
case EditType.Insert: {
pointer_1.Pointer.Set(clone, operation[1], operation[2]);
break;
}
case EditType.Update: {
pointer_1.Pointer.Set(clone, operation[1], operation[2]);
break;
}
case EditType.Delete: {
pointer_1.Pointer.Delete(clone, operation[1]);
break;
}
}
}
return clone;
}
DeltaValue.Edit = Edit;
})(DeltaValue = exports.DeltaValue || (exports.DeltaValue = {}));