UNPKG

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

265 lines (264 loc) 7.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.unsafeStruct = exports.unsafeArray = exports.tuple = exports.taggedEnum = exports.tagged = exports.struct = exports.case = exports.array = exports.TaggedError = exports.TaggedClass = exports.Structural = exports.Error = exports.Class = void 0; var core = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./internal/core.js")); var internal = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./internal/data.js")); var _effectable = /*#__PURE__*/require("./internal/effectable.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; } /** * @example * import * as Data from "effect/Data" * import * as Equal from "effect/Equal" * * const alice = Data.struct({ name: "Alice", age: 30 }) * * const bob = Data.struct({ name: "Bob", age: 40 }) * * assert.deepStrictEqual(Equal.equals(alice, alice), true) * assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true) * * assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false) * assert.deepStrictEqual(Equal.equals(alice, bob), false) * * @category constructors * @since 2.0.0 */ const struct = exports.struct = internal.struct; /** * @category constructors * @since 2.0.0 */ const unsafeStruct = as => Object.setPrototypeOf(as, _effectable.StructuralPrototype); /** * @example * import * as Data from "effect/Data" * import * as Equal from "effect/Equal" * * const alice = Data.tuple("Alice", 30) * * const bob = Data.tuple("Bob", 40) * * assert.deepStrictEqual(Equal.equals(alice, alice), true) * assert.deepStrictEqual(Equal.equals(alice, Data.tuple("Alice", 30)), true) * * assert.deepStrictEqual(Equal.equals(alice, ["Alice", 30]), false) * assert.deepStrictEqual(Equal.equals(alice, bob), false) * * @category constructors * @since 2.0.0 */ exports.unsafeStruct = unsafeStruct; const tuple = (...as) => unsafeArray(as); /** * @example * import * as Data from "effect/Data" * import * as Equal from "effect/Equal" * * const alice = Data.struct({ name: "Alice", age: 30 }) * const bob = Data.struct({ name: "Bob", age: 40 }) * * const persons = Data.array([alice, bob]) * * assert.deepStrictEqual( * Equal.equals( * persons, * Data.array([ * Data.struct({ name: "Alice", age: 30 }), * Data.struct({ name: "Bob", age: 40 }) * ]) * ), * true * ) * * @category constructors * @since 2.0.0 */ exports.tuple = tuple; const array = as => unsafeArray(as.slice(0)); /** * @category constructors * @since 2.0.0 */ exports.array = array; const unsafeArray = as => Object.setPrototypeOf(as, internal.ArrayProto); exports.unsafeArray = unsafeArray; const _case = () => args => args === undefined ? Object.create(_effectable.StructuralPrototype) : struct(args); exports.case = _case; /** * Provides a tagged constructor for the specified `Case`. * * @example * import * as Data from "effect/Data" * * interface Person { * readonly _tag: "Person" // the tag * readonly name: string * } * * const Person = Data.tagged<Person>("Person") * * const mike = Person({ name: "Mike" }) * * assert.deepEqual(mike, { _tag: "Person", name: "Mike" }) * * @since 2.0.0 * @category constructors */ const tagged = tag => args => { const value = args === undefined ? Object.create(_effectable.StructuralPrototype) : struct(args); value._tag = tag; return value; }; /** * Provides a constructor for a Case Class. * * @example * import * as Data from "effect/Data" * import * as Equal from "effect/Equal" * * class Person extends Data.Class<{ readonly name: string }> {} * * // Creating instances of Person * const mike1 = new Person({ name: "Mike" }) * const mike2 = new Person({ name: "Mike" }) * const john = new Person({ name: "John" }) * * // Checking equality * assert.deepStrictEqual(Equal.equals(mike1, mike2), true) * assert.deepStrictEqual(Equal.equals(mike1, john), false) * * @since 2.0.0 * @category constructors */ exports.tagged = tagged; const Class = exports.Class = internal.Structural; /** * Provides a Tagged constructor for a Case Class. * * @example * import * as Data from "effect/Data" * import * as Equal from "effect/Equal" * * class Person extends Data.TaggedClass("Person")<{ readonly name: string }> {} * * // Creating instances of Person * const mike1 = new Person({ name: "Mike" }) * const mike2 = new Person({ name: "Mike" }) * const john = new Person({ name: "John" }) * * // Checking equality * assert.deepStrictEqual(Equal.equals(mike1, mike2), true) * assert.deepStrictEqual(Equal.equals(mike1, john), false) * * assert.deepStrictEqual(mike1._tag, "Person") * * @since 2.0.0 * @category constructors */ const TaggedClass = tag => { class Base extends Class { _tag = tag; } return Base; }; /** * @since 2.0.0 * @category constructors */ exports.TaggedClass = TaggedClass; const Structural = exports.Structural = internal.Structural; /** * Create a constructor for a tagged union of `Data` structs. * * You can also pass a `TaggedEnum.WithGenerics` if you want to add generics to * the constructor. * * @example * import * as Data from "effect/Data" * * const { BadRequest, NotFound } = Data.taggedEnum< * | { readonly _tag: "BadRequest"; readonly status: 400; readonly message: string } * | { readonly _tag: "NotFound"; readonly status: 404; readonly message: string } * >() * * const notFound = NotFound({ status: 404, message: "Not Found" }) * * @example * import * as Data from "effect/Data" * * type MyResult<E, A> = Data.TaggedEnum<{ * Failure: { readonly error: E } * Success: { readonly value: A } * }> * interface MyResultDefinition extends Data.TaggedEnum.WithGenerics<2> { * readonly taggedEnum: MyResult<this["A"], this["B"]> * } * const { Failure, Success } = Data.taggedEnum<MyResultDefinition>() * * const success = Success({ value: 1 }) * * @category constructors * @since 2.0.0 */ const taggedEnum = () => new Proxy({}, { get(_target, tag, _receiver) { return tagged(tag); } }); /** * Provides a constructor for a Case Class. * * @since 2.0.0 * @category constructors */ exports.taggedEnum = taggedEnum; const Error = exports.Error = /*#__PURE__*/function () { return class Base extends core.YieldableError { constructor(args) { super(); if (args) { Object.assign(this, args); } } }; }(); /** * @since 2.0.0 * @category constructors */ const TaggedError = tag => { class Base extends Error { _tag = tag; } ; Base.prototype.name = tag; return Base; }; exports.TaggedError = TaggedError; //# sourceMappingURL=Data.js.map