Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"compile:es": "tsc --build tsconfig.prod.es.json",
"test-all": "npm run test && npm run test:es",
"test": "npm run test:cjs && npm run test:es",
"test:cjs": "mocha 'test/**/*.ts*' --require=ts-node/register --require mocha-clean",
"test:es": "export TS_NODE_PROJECT=tsconfig.es.json && mocha 'test/**/*.ts*' --require=ts-node/register --require mocha-clean",
"test:cjs": "mocha \"test/**/*.ts*\" --require=ts-node/register --require mocha-clean",
"test:es": "export TS_NODE_PROJECT=tsconfig.es.json && mocha \"test/**/*.ts*\" --require=ts-node/register --require mocha-clean",
"watch": "tsc -p tsconfig.prod.json -w",
"coverage": "nyc --extension=.ts --extension=.tsx --include=src/**/*.ts* --all --reporter=html mocha --require=ts-node/register --require source-map-support/register --recursive 'test/**/*.ts*'",
"check-coverage": "rm -rf .nyc_output && npm run coverage && nyc check-coverage",
Expand Down
12 changes: 8 additions & 4 deletions src/option/none.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { OptionBase } from "./optionBase"
export interface None<T> extends OptionBase<T> {}

export class None<T> implements OptionBase<T> {
static none = new None<unknown>()
static noneInstance = new None<unknown>()

static none<T>(): Option<T> {
return None.noneInstance as Option<T>
}

isEmpty = true

Expand All @@ -20,19 +24,19 @@ export class None<T> implements OptionBase<T> {
}

map<E>(fn: (t: T) => Nullable<E>): Option<E> {
return None.none as Option<E>
return None.none()
}

flatMap<E>(fn: (t: T) => Option<E>): Option<E> {
return None.none as Option<E>
return None.none()
}

orElse<E>(alternative: () => Option<E>): Option<E | T> {
return alternative()
}

filter(predicate: (t: T) => boolean): Option<T> {
return None.none as Option<T>
return None.none()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/option/optionOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { Option } from "./option"
import { Some } from "./some"

export function optionOf<T>(t: Nullable<T>): Option<T> {
return t == null ? (None.none as Option<T>) : new Some(t)
return t == null ? None.none() : new Some(t)
}
2 changes: 1 addition & 1 deletion src/option/some.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Some<T> implements OptionBase<T> {
return this
}

return None.none as Option<T>
return None.none()
}
}

Expand Down
13 changes: 6 additions & 7 deletions test/option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ describe("option", function () {
})

it("Option.none should be safe", () => {
const none = None.none as Option<any>
none.map(x => x.toString()).should.eq(Option.of(null))
none.flatMap(x => Option.of(x.toString())).should.eq(Option.of(null))
None.none<{}>().map(x => x.toString()).should.eq(Option.of(null))
None.none<{}>().flatMap(x => Option.of(x.toString())).should.eq(Option.of(null))
})

it("Some(null) should be safe", () => {
Option.of("value")
.map(() => null)
.map(() => null)
.should.eq(None.none)
.should.eq(None.none())
// const some = new Some(null) as Option<any>
// some.map(x => x.toString()).should.eq(Option.of(null))
// some.flatMap(x => Option.of(x!.toString())).should.eq(Option.of(null))
Expand All @@ -39,7 +38,7 @@ describe("option", function () {
Option.of({ a: null as any })
.map(e => e.a)
.map(e => e.b)
.should.eq(None.none)
.should.eq(None.none())
})

it("Option.isNone", () => {
Expand All @@ -59,13 +58,13 @@ describe("option", function () {
})

it("filter", () => {
None.none.filter(() => true).should.eq(None.none)
None.none().filter(() => true).should.eq(None.none())
Option.of("result").filter(() => false)
Option.of("result").filter(r => r.startsWith("re")).get!.should.eq("result")
})

it("orElse", () => {
expect(None.none.orElse(() => Option.of("else")).get).to.eq("else")
expect(None.none().orElse(() => Option.of("else")).get).to.eq("else")
expect(Option.of("result").orElse(() => Option.of("else")).get).to.eq("result")
})
})