UNPKG

@metaplex-foundation/umi-options

Version:

A TypeScript implementation of Rust Options

1 lines 3.4 kB
{"version":3,"file":"common.cjs","sources":["../../src/common.ts"],"sourcesContent":["/**\n * Defines a type `T` that can also be `null`.\n * @category Utils — Options\n */\nexport type Nullable<T> = T | null;\n\n/**\n * Defines a type `T` that can also be `null` or `undefined`.\n * @category Utils — Options\n */\nexport type Nullish<T> = T | null | undefined;\n\n/**\n * An implementation of the Rust Option type in JavaScript.\n * It can be one of the following:\n * - <code>{@link Some}<T></code>: Meaning there is a value of type T.\n * - <code>{@link None}</code>: Meaning there is no value.\n *\n * @category Utils — Options\n */\nexport type Option<T> = Some<T> | None;\n\n/**\n * Defines a looser type that can be used when serializing an {@link Option}.\n * This allows us to pass null or the Option value directly whilst still\n * supporting the Option type for use-cases that need more type safety.\n *\n * @category Utils — Options\n */\nexport type OptionOrNullable<T> = Option<T> | Nullable<T>;\n\n/**\n * Represents an option of type `T` that has a value.\n *\n * @see {@link Option}\n * @category Utils — Options\n */\nexport type Some<T> = { __option: 'Some'; value: T };\n\n/**\n * Represents an option of type `T` that has no value.\n *\n * @see {@link Option}\n * @category Utils — Options\n */\nexport type None = { __option: 'None' };\n\n/**\n * Creates a new {@link Option} of type `T` that has a value.\n *\n * @see {@link Option}\n * @category Utils — Options\n */\nexport const some = <T>(value: T): Option<T> => ({ __option: 'Some', value });\n\n/**\n * Creates a new {@link Option} of type `T` that has no value.\n *\n * @see {@link Option}\n * @category Utils — Options\n */\nexport const none = <T>(): Option<T> => ({ __option: 'None' });\n\n/**\n * Whether the given data is an {@link Option}.\n * @category Utils — Options\n */\nexport const isOption = <T = unknown>(input: any): input is Option<T> =>\n input &&\n typeof input === 'object' &&\n '__option' in input &&\n ((input.__option === 'Some' && 'value' in input) ||\n input.__option === 'None');\n\n/**\n * Whether the given {@link Option} is a {@link Some}.\n * @category Utils — Options\n */\nexport const isSome = <T>(option: Option<T>): option is Some<T> =>\n option.__option === 'Some';\n\n/**\n * Whether the given {@link Option} is a {@link None}.\n * @category Utils — Options\n */\nexport const isNone = <T>(option: Option<T>): option is None =>\n option.__option === 'None';\n"],"names":["some","value","__option","none","isOption","input","isSome","option","isNone"],"mappings":";;;;AAAA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACaA,MAAAA,IAAI,GAAOC,KAAQ,KAAiB;AAAEC,EAAAA,QAAQ,EAAE,MAAM;AAAED,EAAAA,KAAAA;AAAM,CAAC,EAAC;;AAE7E;AACA;AACA;AACA;AACA;AACA;AACO,MAAME,IAAI,GAAG,OAAqB;AAAED,EAAAA,QAAQ,EAAE,MAAA;AAAO,CAAC,EAAC;;AAE9D;AACA;AACA;AACA;AACaE,MAAAA,QAAQ,GAAiBC,KAAU,IAC9CA,KAAK,IACL,OAAOA,KAAK,KAAK,QAAQ,IACzB,UAAU,IAAIA,KAAK,KACjBA,KAAK,CAACH,QAAQ,KAAK,MAAM,IAAI,OAAO,IAAIG,KAAK,IAC7CA,KAAK,CAACH,QAAQ,KAAK,MAAM,EAAC;;AAE9B;AACA;AACA;AACA;AACO,MAAMI,MAAM,GAAOC,MAAiB,IACzCA,MAAM,CAACL,QAAQ,KAAK,OAAM;;AAE5B;AACA;AACA;AACA;AACO,MAAMM,MAAM,GAAOD,MAAiB,IACzCA,MAAM,CAACL,QAAQ,KAAK;;;;;;;;"}