Installations
npm install ts-option
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
12.12.0
NPM Version
6.11.3
Score
74.1
Supply Chain
98.9
Quality
75.5
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (99.8%)
JavaScript (0.2%)
Developer
shogogg
Download Statistics
Total Downloads
674,682
Last Day
87
Last Week
476
Last Month
2,324
Last Year
39,886
GitHub Statistics
90 Stars
49 Commits
6 Forks
3 Watching
14 Branches
7 Contributors
Bundle Size
3.94 kB
Minified
965.00 B
Minified + Gzipped
Package Meta Information
Latest Version
2.1.0
Package Id
ts-option@2.1.0
Unpacked Size
28.27 kB
Size
5.59 kB
File Count
6
NPM Version
6.11.3
Node Version
12.12.0
Total Downloads
Cumulative downloads
Total Downloads
674,682
Last day
14.5%
87
Compared to previous day
Last week
-5.2%
476
Compared to previous week
Last month
-61.4%
2,324
Compared to previous month
Last year
9.9%
39,886
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
ts-option
Scala like Option
type for TypeScript/JavaScript.
Install
# yarn
$ yarn add ts-option
# npm
$ npm install --save ts-option
Usage
TypeScript
import {Option, option, some, none} from "ts-option";
let a: Option<number> = option(1); // Some(1)
let c: Option<number> = some(2); // Some(2)
let b: Option<number> = option(null); // None
let d: Option<number> = none; // None
JavaScript (ES Modules)
import {Option, option, some, none} from "ts-option";
let a = option(1); // Some(1)
let c = some(2); // Some(2)
let b = option(null); // None
let d = none; // None
API
option<A>(value?: A | null): Option<A>
Create an Option
instance from a value.
It returns Some<A>
when the value is not null/undefined, otherwise returns None
.
some<A>(value: A): Option<A>
Create an Some
instance from a value.
It returns Some<A>
even if the value is null or undefined.
If strict null checks are enabled in your tsconfig.json, undefined and null won't be permitted here.
none: Option<never>
The None
type singleton object.
option<A>(...).exists(p: (_: A) => boolean): boolean
Returns true if the option is non-empty and the predicate p returns true when applied to the option's value.
option<A>(...).filter(p: (_: A) => boolean): Option<A>
Returns the option if it is non-empty and applying the predicate p to the option's value returns true.
option<A>(...).filterNot(p: (_: A) => boolean): Option<A>
Returns the option if it is non-empty and applying the predicate p to the option's value returns false.
option<A>(...).flatMap<B>(f: (_: A) => Option<B>): Option<B>
Returns the result of applying f to the option's value if the option is non-empty, otherwise returns None
.
option<A>(...).fold<B>(ifEmpty: () => B)(f: (_: A) => B): B
Returns the result of applying f to the option's value if the option is non-empty, otherwise returns ifEmpty
value.
option<A>(...).forAll(p: (_: A) => boolean): boolean
Tests whether a predicate holds for all elements of the option.
option<A>(...).forComprehension(...fns: (a: any) => Option<any>): Option<any>
Performs a for-comprehension like operation using the given list of functions. For example:
const nestedOptions = some({
anOption: some({
anotherOption: some({
finalValue: true
})
})
});
const result = nestedOptions.forComprehension(
obj => obj.anOption,
anOption => anOption.anotherOption,
anotherOption => anotherOption.finalValue
);
console.log(`${result}`) // Some(true)
As with the Scala for comprehension the result of each function is flat-mapped with the next, except for the last, which is mapped.
Please note that there are currently some limitations:
- Filtering must be done manually
- There is no shared scope between functions
- The result type is always
Option<any>
option<A>(...).forEach(f: (_: A) => any): void
Apply the given procedure f to the option's value if it is non-empty, otherwise do nothing.
option<A>(...).get: A
Returns the option's value if the option is non-empty, otherwise throws an error.
option<A>(...).getOrElse(defaultValue: () => A): A
Returns the option's value if the option is non-empty, otherwise return the result of evaluating defaultValue.
option<A>(...).getOrElseValue(defaultValue: A): A
Returns the option's value if the option is non-empty, otherwise return the defaultValue.
option<A>(...).isDefined: boolean
Returns true if the option's value is non-empty, false otherwise.
option<A>(...).isEmpty: boolean
Returns true if the option is an instance of None
, false otherwise.
option<A>(...).map<B>(f: (_: A) => B): Option<B>
Builds a new option by applying a function to all elements of this option.
option<A>(...).match<B>(matcher: { some: (_: A) => B, none: () => B }): B
Scala's "Pattern Match" like signature. Returns the value that the function some
returns if the option is non-empty,
otherwise returns the value that function none
returns.
let s: Option<number> = some(9);
let n: Option<number> = none;
let a: number = s.match({
some: x => x * 9,
none: () => -1,
});
let b: number = n.match({
some: x => x * 9,
none: () => -1,
});
console.log(a); // 81
console.log(b); // -1
option<A>(...).nonEmpty: boolean
Returns true if the option is an instance of Some
, false otherwise.
option<A>(...).orElse(alternative: () => Option<A>): Option<A>
Returns the option itself if it is non-empty, otherwise return the result of evaluating alternative.
option<A>(...).orElseValue(alternative: Option<A>): Option<A>
Returns the option itself if it is non-empty, otherwise return the alternative.
option<A>(...).orNull: A | null
Returns the option's value if it is non-empty, or null if it is empty.
option<A>(...).orUndefined: A | undefined
Returns the option's value if it is non-empty, or undefined if it is empty.
option<A>(...).toArray: Array<A>
Converts the option to an array.
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 3/21 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 12 are checked with a SAST tool
Reason
47 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv
- Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8
- Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65
- Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
1.9
/10
Last Scanned on 2025-01-13
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to ts-option
ts-command-line-args
A Typescript wrapper around command-line-args with additional support for markdown usage guide generation
ts-results
A typescript implementation of Rust's Result and Option objects.
ts-results-es
A typescript implementation of Rust's Result and Option objects.
@mobily/ts-belt
🔧 Fast, modern, and practical utility library for FP in TypeScript.