@consolidados/results
Version:
Result types, ease and simple
1 lines • 6.48 kB
Source Map (JSON)
{"version":3,"sources":["../../src/option/__internal__/return-types/some.ts","../../src/option/__internal__/return-types/none.ts","../../src/option/option.ts"],"names":["Some","None"],"mappings":";;;AAOO,IAAM,IAAA,GAAN,MAAM,KAAQ,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKnB,YAAoB,KAAU,EAAA;AAAV,IAAA,IAAA,CAAA,KAAA,GAAA,KAAA;AAAA;AAAY;AAAA;AAAA;AAAA;AAAA,EAMhC,MAA0B,GAAA;AACxB,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,MAAuB,GAAA;AACrB,IAAO,OAAA,KAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,MAAY,GAAA;AACV,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAO,EAAgC,EAAA;AACrC,IAAA,OAAO,IAAI,KAAA,CAAK,EAAG,CAAA,IAAA,CAAK,KAAK,CAAC,CAAA;AAAA;AAChC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QAAW,EAAwC,EAAA;AACjD,IAAO,OAAA,EAAA,CAAG,KAAK,KAAK,CAAA;AAAA;AACtB;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,SAAS,CAAS,EAAA;AAChB,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AAEhB,CAAA;;;AC5DO,IAAM,IAAA,GAAN,MAAM,KAAK,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKhB,MAA8B,GAAA;AAC5B,IAAO,OAAA,KAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,MAAuB,GAAA;AACrB,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,MAAgB,GAAA;AACd,IAAM,MAAA,IAAI,MAAM,+BAA+B,CAAA;AAAA;AACjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAO,GAAqC,EAAA;AAC1C,IAAA,OAAO,IAAI,KAAK,EAAA;AAAA;AAClB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QAAW,GAA6C,EAAA;AACtD,IAAA,OAAO,IAAI,KAAK,EAAA;AAAA;AAClB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,SAAY,YAAoB,EAAA;AAC9B,IAAO,OAAA,YAAA;AAAA;AAEX,CAAA;;;AC5CA,SAASA,MAAQ,KAAuB,EAAA;AACvC,EAAO,OAAA,IAAI,KAAS,KAAK,CAAA;AAC1B;AAUA,SAASC,KAAiB,GAAA;AACzB,EAAA,OAAO,IAAI,IAAS,EAAA;AACrB;AAEC,MAAA,CAAe,IAAOD,GAAAA,KAAAA;AACtB,MAAA,CAAe,IAAOC,GAAAA,KAAAA","file":"option.cjs","sourcesContent":["import type { None } from \"./none\";\nimport type { Option } from \"./option\";\n\n/**\n * Represents a `Some` option, which holds a value.\n * @template T The type of the value held by this `Some` instance.\n */\nexport class Some<T> {\n /**\n * Creates a new `Some` option with the given value.\n * @param value The value to be wrapped in the `Some` option.\n */\n constructor(private value: T) { }\n\n /**\n * Checks if this option is a `Some`.\n * @returns `true` if this option is a `Some`, otherwise `false`.\n */\n isSome(): this is Some<T> {\n return true;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `false` because this is a `Some`.\n */\n isNone(): this is None {\n return false;\n }\n\n /**\n * Unwraps the value held by this `Some` option.\n * @returns The value held by this `Some` option.\n */\n unwrap(): T {\n return this.value;\n }\n\n /**\n * Applies a transformation function to the value held by this `Some` option and returns a new `Option` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Some` option containing the transformed value.\n */\n map<U>(fn: (value: T) => U): Option<U> {\n return new Some(fn(this.value));\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value held by this `Some` option.\n * @template U The type of the value in the resulting `Option`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(fn: (value: T) => Option<U>): Option<U> {\n return fn(this.value);\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the default value provided.\n * @param _ A default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOr(_: T): T {\n return this.value;\n }\n}\n","import type { Some } from \"./some\";\nimport type { Option } from \"./option\";\n\n/**\n * Represents a `None` option, which holds no value.\n */\nexport class None {\n /**\n * Checks if this option is a `Some`.\n * @returns `false` because this is a `None`.\n */\n isSome(): this is Some<never> {\n return false;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `true` because this is a `None`.\n */\n isNone(): this is None {\n return true;\n }\n\n /**\n * Attempts to unwrap the value from this `None` option.\n * @throws An error because `None` has no value.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on a None value\");\n }\n\n /**\n * Applies a transformation function to the value (which does not exist) of this `None` option.\n * @template U The type of the value that would have been returned.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns A new `None` option.\n */\n map<U>(_fn: (value: never) => U): Option<U> {\n return new None();\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.\n * @template U The type of the value in the resulting `Option`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns A new `None` option.\n */\n flatMap<U>(_fn: (value: never) => Option<U>): Option<U> {\n return new None();\n }\n\n /**\n * Returns the default value provided, since `None` has no value.\n * @template T The type of the default value.\n * @param defaultValue The value to return.\n * @returns The default value provided.\n */\n unwrapOr<T>(defaultValue: T): T {\n return defaultValue;\n }\n}\n","import {\n\tSome as SomeType,\n\tNone as NoneType,\n\ttype Option,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Some` instance, representing an `Option` with a value.\n * @template T The type of the value contained in the `Some`.\n * @param value The value to wrap in the `Some` instance.\n * @returns A `Some` instance containing the given value.\n * @example\n * const option = Some(42);\n * console.log(option.isSome()); // true\n * console.log(option.unwrap()); // 42\n */\nfunction Some<T>(value: T): SomeType<T> {\n\treturn new SomeType(value);\n}\n\n/**\n * Creates a new `None` instance, representing an `Option` with no value.\n * @returns A `None` instance.\n * @example\n * const option = None();\n * console.log(option.isNone()); // true\n * console.log(option.unwrap()); // throws Error: \"Called unwrap on a None value\"\n */\nfunction None(): NoneType {\n\treturn new NoneType();\n}\n\n(global as any).Some = Some;\n(global as any).None = None;\n\nexport { None, Some, Option };\n"]}