veffect
Version:
powerful TypeScript validation library built on the robust foundation of Effect combining exceptional type safety, high performance, and developer experience. Taking inspiration from Effect's functional principles, VEffect delivers a balanced approach tha
208 lines (207 loc) • 6.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.patch = exports.makeUpdateRight = exports.makeUpdateLeft = exports.makeSetRight = exports.makeSetLeft = exports.makeAndThen = exports.empty = exports.diff = exports.combine = exports.OrPatchTypeId = void 0;
var Chunk = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../../Chunk.js"));
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../../Either.js"));
var Equal = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../../Equal.js"));
var Dual = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../../Function.js"));
var _data = /*#__PURE__*/require("../data.js");
function _getRequireWildcardCache(e) {
if ("function" != typeof WeakMap) return null;
var r = new WeakMap(),
t = new WeakMap();
return (_getRequireWildcardCache = function (e) {
return e ? t : r;
})(e);
}
function _interopRequireWildcard(e, r) {
if (!r && e && e.__esModule) return e;
if (null === e || "object" != typeof e && "function" != typeof e) return {
default: e
};
var t = _getRequireWildcardCache(r);
if (t && t.has(e)) return t.get(e);
var n = {
__proto__: null
},
a = Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) {
var i = a ? Object.getOwnPropertyDescriptor(e, u) : null;
i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u];
}
return n.default = e, t && t.set(e, n), n;
}
/** @internal */
const OrPatchTypeId = exports.OrPatchTypeId = /*#__PURE__*/Symbol.for("effect/DifferOrPatch");
function variance(a) {
return a;
}
/** @internal */
const PatchProto = {
..._data.Structural.prototype,
[OrPatchTypeId]: {
_Value: variance,
_Key: variance,
_Patch: variance
}
};
const EmptyProto = /*#__PURE__*/Object.assign( /*#__PURE__*/Object.create(PatchProto), {
_tag: "Empty"
});
const _empty = /*#__PURE__*/Object.create(EmptyProto);
/** @internal */
const empty = () => _empty;
exports.empty = empty;
const AndThenProto = /*#__PURE__*/Object.assign( /*#__PURE__*/Object.create(PatchProto), {
_tag: "AndThen"
});
/** @internal */
const makeAndThen = (first, second) => {
const o = Object.create(AndThenProto);
o.first = first;
o.second = second;
return o;
};
exports.makeAndThen = makeAndThen;
const SetLeftProto = /*#__PURE__*/Object.assign( /*#__PURE__*/Object.create(PatchProto), {
_tag: "SetLeft"
});
/** @internal */
const makeSetLeft = value => {
const o = Object.create(SetLeftProto);
o.value = value;
return o;
};
exports.makeSetLeft = makeSetLeft;
const SetRightProto = /*#__PURE__*/Object.assign( /*#__PURE__*/Object.create(PatchProto), {
_tag: "SetRight"
});
/** @internal */
const makeSetRight = value => {
const o = Object.create(SetRightProto);
o.value = value;
return o;
};
exports.makeSetRight = makeSetRight;
const UpdateLeftProto = /*#__PURE__*/Object.assign( /*#__PURE__*/Object.create(PatchProto), {
_tag: "UpdateLeft"
});
/** @internal */
const makeUpdateLeft = patch => {
const o = Object.create(UpdateLeftProto);
o.patch = patch;
return o;
};
exports.makeUpdateLeft = makeUpdateLeft;
const UpdateRightProto = /*#__PURE__*/Object.assign( /*#__PURE__*/Object.create(PatchProto), {
_tag: "UpdateRight"
});
/** @internal */
const makeUpdateRight = patch => {
const o = Object.create(UpdateRightProto);
o.patch = patch;
return o;
};
/** @internal */
exports.makeUpdateRight = makeUpdateRight;
const diff = options => {
switch (options.oldValue._tag) {
case "Left":
{
switch (options.newValue._tag) {
case "Left":
{
const valuePatch = options.left.diff(options.oldValue.left, options.newValue.left);
if (Equal.equals(valuePatch, options.left.empty)) {
return empty();
}
return makeUpdateLeft(valuePatch);
}
case "Right":
{
return makeSetRight(options.newValue.right);
}
}
}
case "Right":
{
switch (options.newValue._tag) {
case "Left":
{
return makeSetLeft(options.newValue.left);
}
case "Right":
{
const valuePatch = options.right.diff(options.oldValue.right, options.newValue.right);
if (Equal.equals(valuePatch, options.right.empty)) {
return empty();
}
return makeUpdateRight(valuePatch);
}
}
}
}
};
/** @internal */
exports.diff = diff;
const combine = exports.combine = /*#__PURE__*/Dual.dual(2, (self, that) => makeAndThen(self, that));
/** @internal */
const patch = exports.patch = /*#__PURE__*/Dual.dual(2, (self, {
left,
oldValue,
right
}) => {
if (self._tag === "Empty") {
return oldValue;
}
let patches = Chunk.of(self);
let result = oldValue;
while (Chunk.isNonEmpty(patches)) {
const head = Chunk.headNonEmpty(patches);
const tail = Chunk.tailNonEmpty(patches);
switch (head._tag) {
case "Empty":
{
patches = tail;
break;
}
case "AndThen":
{
patches = Chunk.prepend(head.first)(Chunk.prepend(head.second)(tail));
break;
}
case "UpdateLeft":
{
if (result._tag === "Left") {
result = E.left(left.patch(head.patch, result.left));
}
patches = tail;
break;
}
case "UpdateRight":
{
if (result._tag === "Right") {
result = E.right(right.patch(head.patch, result.right));
}
patches = tail;
break;
}
case "SetLeft":
{
result = E.left(head.value);
patches = tail;
break;
}
case "SetRight":
{
result = E.right(head.value);
patches = tail;
break;
}
}
}
return result;
});
//# sourceMappingURL=orPatch.js.map