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

263 lines (262 loc) 8.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.unsafeMake = exports.unsafeGet = exports.pick = exports.omit = exports.merge = exports.make = exports.isTag = exports.isContext = exports.getOption = exports.get = exports.empty = exports.add = exports.Tag = exports.GenericTag = void 0; var internal = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./internal/context.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; } const TagTypeId = internal.TagTypeId; /** * Creates a new `Tag` instance with an optional key parameter. * * @param key - A key that will be used to compare tags. * * @example * import * as Context from "effect/Context" * * assert.strictEqual(Context.GenericTag("PORT").key === Context.GenericTag("PORT").key, true) * * @since 2.0.0 * @category constructors */ const GenericTag = exports.GenericTag = internal.makeGenericTag; const TypeId = internal.TypeId; /** * @since 2.0.0 * @category constructors */ const unsafeMake = exports.unsafeMake = internal.makeContext; /** * Checks if the provided argument is a `Context`. * * @param input - The value to be checked if it is a `Context`. * * @example * import * as Context from "effect/Context" * * assert.strictEqual(Context.isContext(Context.empty()), true) * * @since 2.0.0 * @category guards */ const isContext = exports.isContext = internal.isContext; /** * Checks if the provided argument is a `Tag`. * * @param input - The value to be checked if it is a `Tag`. * * @example * import * as Context from "effect/Context" * * assert.strictEqual(Context.isTag(Context.GenericTag("Tag")), true) * * @since 2.0.0 * @category guards */ const isTag = exports.isTag = internal.isTag; /** * Returns an empty `Context`. * * @example * import * as Context from "effect/Context" * * assert.strictEqual(Context.isContext(Context.empty()), true) * * @since 2.0.0 * @category constructors */ const empty = exports.empty = internal.empty; /** * Creates a new `Context` with a single service associated to the tag. * * @example * import * as Context from "effect/Context" * * const Port = Context.GenericTag<{ PORT: number }>("Port") * * const Services = Context.make(Port, { PORT: 8080 }) * * assert.deepStrictEqual(Context.get(Services, Port), { PORT: 8080 }) * * @since 2.0.0 * @category constructors */ const make = exports.make = internal.make; /** * Adds a service to a given `Context`. * * @example * import * as Context from "effect/Context" * import { pipe } from "effect/Function" * * const Port = Context.GenericTag<{ PORT: number }>("Port") * const Timeout = Context.GenericTag<{ TIMEOUT: number }>("Timeout") * * const someContext = Context.make(Port, { PORT: 8080 }) * * const Services = pipe( * someContext, * Context.add(Timeout, { TIMEOUT: 5000 }) * ) * * assert.deepStrictEqual(Context.get(Services, Port), { PORT: 8080 }) * assert.deepStrictEqual(Context.get(Services, Timeout), { TIMEOUT: 5000 }) * * @since 2.0.0 */ const add = exports.add = internal.add; /** * Get a service from the context that corresponds to the given tag. * * @param self - The `Context` to search for the service. * @param tag - The `Tag` of the service to retrieve. * * @example * import * as Context from "effect/Context" * import { pipe } from "effect/Function" * * const Port = Context.GenericTag<{ PORT: number }>("Port") * const Timeout = Context.GenericTag<{ TIMEOUT: number }>("Timeout") * * const Services = pipe( * Context.make(Port, { PORT: 8080 }), * Context.add(Timeout, { TIMEOUT: 5000 }) * ) * * assert.deepStrictEqual(Context.get(Services, Timeout), { TIMEOUT: 5000 }) * * @since 2.0.0 * @category getters */ const get = exports.get = internal.get; /** * Get a service from the context that corresponds to the given tag. * This function is unsafe because if the tag is not present in the context, a runtime error will be thrown. * * For a safer version see {@link getOption}. * * @param self - The `Context` to search for the service. * @param tag - The `Tag` of the service to retrieve. * * @example * import * as Context from "effect/Context" * * const Port = Context.GenericTag<{ PORT: number }>("Port") * const Timeout = Context.GenericTag<{ TIMEOUT: number }>("Timeout") * * const Services = Context.make(Port, { PORT: 8080 }) * * assert.deepStrictEqual(Context.unsafeGet(Services, Port), { PORT: 8080 }) * assert.throws(() => Context.unsafeGet(Services, Timeout)) * * @since 2.0.0 * @category unsafe */ const unsafeGet = exports.unsafeGet = internal.unsafeGet; /** * Get the value associated with the specified tag from the context wrapped in an `Option` object. If the tag is not * found, the `Option` object will be `None`. * * @param self - The `Context` to search for the service. * @param tag - The `Tag` of the service to retrieve. * * @example * import * as Context from "effect/Context" * import * as O from "effect/Option" * * const Port = Context.GenericTag<{ PORT: number }>("Port") * const Timeout = Context.GenericTag<{ TIMEOUT: number }>("Timeout") * * const Services = Context.make(Port, { PORT: 8080 }) * * assert.deepStrictEqual(Context.getOption(Services, Port), O.some({ PORT: 8080 })) * assert.deepStrictEqual(Context.getOption(Services, Timeout), O.none()) * * @since 2.0.0 * @category getters */ const getOption = exports.getOption = internal.getOption; /** * Merges two `Context`s, returning a new `Context` containing the services of both. * * @param self - The first `Context` to merge. * @param that - The second `Context` to merge. * * @example * import * as Context from "effect/Context" * * const Port = Context.GenericTag<{ PORT: number }>("Port") * const Timeout = Context.GenericTag<{ TIMEOUT: number }>("Timeout") * * const firstContext = Context.make(Port, { PORT: 8080 }) * const secondContext = Context.make(Timeout, { TIMEOUT: 5000 }) * * const Services = Context.merge(firstContext, secondContext) * * assert.deepStrictEqual(Context.get(Services, Port), { PORT: 8080 }) * assert.deepStrictEqual(Context.get(Services, Timeout), { TIMEOUT: 5000 }) * * @since 2.0.0 */ const merge = exports.merge = internal.merge; /** * Returns a new `Context` that contains only the specified services. * * @param self - The `Context` to prune services from. * @param tags - The list of `Tag`s to be included in the new `Context`. * * @example * import * as Context from "effect/Context" * import { pipe } from "effect/Function" * import * as O from "effect/Option" * * const Port = Context.GenericTag<{ PORT: number }>("Port") * const Timeout = Context.GenericTag<{ TIMEOUT: number }>("Timeout") * * const someContext = pipe( * Context.make(Port, { PORT: 8080 }), * Context.add(Timeout, { TIMEOUT: 5000 }) * ) * * const Services = pipe(someContext, Context.pick(Port)) * * assert.deepStrictEqual(Context.getOption(Services, Port), O.some({ PORT: 8080 })) * assert.deepStrictEqual(Context.getOption(Services, Timeout), O.none()) * * @since 2.0.0 */ const pick = exports.pick = internal.pick; /** * @since 2.0.0 */ const omit = exports.omit = internal.omit; /** * @since 2.0.0 * @category constructors */ const Tag = exports.Tag = internal.Tag; //# sourceMappingURL=Context.js.map