@forminator/option
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -1,6 +0,8 @@ | ||
export declare function catchNoneError<R>(fn: () => R): Option_2<R>; | ||
export declare function catchNoneError<R>(fn: () => R): Option_2<R & Defined>; | ||
export declare type Defined = {} | null; | ||
declare const FORMINATOR_OPTION: unique symbol; | ||
export declare function fromOption<Value>( | ||
export declare function fromOption<Value extends Defined>( | ||
option: Option_2<Value>, | ||
@@ -11,13 +13,15 @@ ): Value | undefined; | ||
value: Value | undefined, | ||
): Option_2<Value>; | ||
): Option_2<Value & Defined>; | ||
export declare function isNoneError(e: any): e is NoneError; | ||
export declare function isOption(value: any): value is Option_2<unknown>; | ||
export declare function isOption( | ||
value: any, | ||
): value is Option_2<unknown & Defined>; | ||
export declare type None<Value> = { | ||
export declare type None<Value extends Defined> = { | ||
some: false; | ||
} & OptionFns<Value>; | ||
export declare function none<Value>(): None<Value>; | ||
export declare function none<Value extends Defined>(): None<Value>; | ||
@@ -28,6 +32,6 @@ export declare class NoneError extends Error { | ||
declare type Option_2<Value> = Some<Value> | None<Value>; | ||
declare type Option_2<Value extends Defined> = Some<Value> | None<Value>; | ||
export { Option_2 as Option }; | ||
export declare interface OptionFns<Value> { | ||
export declare interface OptionFns<Value extends Defined> { | ||
readonly [FORMINATOR_OPTION]: true; | ||
@@ -37,3 +41,3 @@ unwrap(this: Option_2<Value>): Value; | ||
or(this: Option_2<Value>, value: Option_2<Value>): Option_2<Value>; | ||
map<T>(this: Option_2<Value>, fn: (value: Value) => T): Option_2<T>; | ||
map<T>(this: Option_2<Value>, fn: (value: Value) => T): Option_2<T & Defined>; | ||
isSome(this: Option_2<Value>): this is Some<Value>; | ||
@@ -43,3 +47,3 @@ isNone(this: Option_2<Value>): this is None<Value>; | ||
export declare type Some<Value> = { | ||
export declare type Some<Value extends Defined> = { | ||
some: true; | ||
@@ -49,3 +53,3 @@ value: Value; | ||
export declare function some<Value extends {} | null>( | ||
export declare function some<Value extends Defined>( | ||
value: Value, | ||
@@ -52,0 +56,0 @@ ): Some<Value> & OptionFns<Value>; |
{ | ||
"name": "@forminator/option", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"files": [ | ||
@@ -5,0 +5,0 @@ "src", |
@@ -11,3 +11,4 @@ export { | ||
NoneError, | ||
type Defined, | ||
} from './option'; | ||
export type { Option, OptionFns, Some, None } from './option'; |
import { | ||
catchNoneError, | ||
Defined, | ||
fromOption, | ||
@@ -122,3 +123,3 @@ intoOption, | ||
if (isOption(o)) { | ||
const s: Option<unknown> = o; | ||
const s: Option<Defined> = o; | ||
} | ||
@@ -125,0 +126,0 @@ if (isOption(o)) { |
import { FirmMap } from './firm-map'; | ||
export const FORMINATOR_OPTION = Symbol('FORMINATOR_OPTION'); | ||
export type Some<Value> = { | ||
export type Defined = {} | null; | ||
export type Some<Value extends Defined> = { | ||
some: true; | ||
value: Value; | ||
} & OptionFns<Value>; | ||
export type None<Value> = { | ||
export type None<Value extends Defined> = { | ||
some: false; | ||
} & OptionFns<Value>; | ||
export type Option<Value> = Some<Value> | None<Value>; | ||
export type Option<Value extends Defined> = Some<Value> | None<Value>; | ||
export interface OptionFns<Value> { | ||
export interface OptionFns<Value extends Defined> { | ||
readonly [FORMINATOR_OPTION]: true; | ||
@@ -23,3 +23,3 @@ | ||
map<T>(this: Option<Value>, fn: (value: Value) => T): Option<T>; | ||
map<T>(this: Option<Value>, fn: (value: Value) => T): Option<T & Defined>; | ||
@@ -31,3 +31,3 @@ isSome(this: Option<Value>): this is Some<Value>; | ||
function getOptionFns<Value>(): OptionFns<Value> { | ||
function getOptionFns<Value extends Defined>(): OptionFns<Value> { | ||
return { | ||
@@ -58,3 +58,3 @@ [FORMINATOR_OPTION]: true, | ||
function createSome<Value extends {} | null>( | ||
function createSome<Value extends Defined>( | ||
value: Value, | ||
@@ -68,3 +68,3 @@ ): Some<Value> & OptionFns<Value> { | ||
function createNone<Value>(): None<Value> { | ||
function createNone<Value extends Defined>(): None<Value> { | ||
return Object.assign(Object.create(optionFns), { some: false }); | ||
@@ -77,3 +77,3 @@ } | ||
export function some<Value extends {} | null>( | ||
export function some<Value extends Defined>( | ||
value: Value, | ||
@@ -87,11 +87,11 @@ ): Some<Value> & OptionFns<Value> { | ||
export function none<Value>(): None<Value> { | ||
export function none<Value extends Defined>(): None<Value> { | ||
return theNone; | ||
} | ||
export function isOption(value: any): value is Option<unknown> { | ||
export function isOption(value: any): value is Option<unknown & Defined> { | ||
return !!(value && value[FORMINATOR_OPTION]); | ||
} | ||
function isSome<Value>( | ||
function isSome<Value extends Defined>( | ||
option: Option<Value>, | ||
@@ -102,3 +102,5 @@ ): option is Some<Value> & OptionFns<Value> { | ||
function isNone<Value>(option: Option<Value>): option is None<Value> { | ||
function isNone<Value extends Defined>( | ||
option: Option<Value>, | ||
): option is None<Value> { | ||
return !option.some; | ||
@@ -116,3 +118,3 @@ } | ||
function unwrap<Value>(option: Option<Value>): Value { | ||
function unwrap<Value extends Defined>(option: Option<Value>): Value { | ||
if (isSome(option)) { | ||
@@ -124,3 +126,6 @@ return option.value; | ||
function unwrap_or<Value>(option: Option<Value>, value: Value): Value { | ||
function unwrap_or<Value extends Defined>( | ||
option: Option<Value>, | ||
value: Value, | ||
): Value { | ||
if (isSome(option)) { | ||
@@ -132,3 +137,6 @@ return option.value; | ||
function or<Value>(option: Option<Value>, value: Option<Value>): Option<Value> { | ||
function or<Value extends Defined>( | ||
option: Option<Value>, | ||
value: Option<Value>, | ||
): Option<Value> { | ||
if (isSome(option)) { | ||
@@ -148,3 +156,3 @@ return option; | ||
export function catchNoneError<R>(fn: () => R): Option<R> { | ||
export function catchNoneError<R>(fn: () => R): Option<R & Defined> { | ||
try { | ||
@@ -160,3 +168,6 @@ return intoOption(fn()); | ||
function mapOption<V, T>(option: Option<V>, fn: (v: V) => T): Option<T> { | ||
function mapOption<V extends Defined, T>( | ||
option: Option<V>, | ||
fn: (v: V) => T, | ||
): Option<T & Defined> { | ||
if (isNone(option)) { | ||
@@ -168,3 +179,5 @@ return none(); | ||
export function intoOption<Value>(value: Value | undefined): Option<Value> { | ||
export function intoOption<Value>( | ||
value: Value | undefined, | ||
): Option<Value & Defined> { | ||
if (value === undefined) { | ||
@@ -176,4 +189,6 @@ return none(); | ||
export function fromOption<Value>(option: Option<Value>): Value | undefined { | ||
export function fromOption<Value extends Defined>( | ||
option: Option<Value>, | ||
): Value | undefined { | ||
return option.isSome() ? option.value : undefined; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23214
700