From 6768c0567580bbc480705fde2068ca2c80bcc053 Mon Sep 17 00:00:00 2001 From: Kobi Date: Fri, 22 May 2020 14:21:20 +0300 Subject: [PATCH] create nome --- package.json | 4 ++-- src/option/none.ts | 12 ++++++++---- src/option/optionOf.ts | 2 +- src/option/some.ts | 2 +- test/option.test.ts | 13 ++++++------- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 3c90fce..5ecc58b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/option/none.ts b/src/option/none.ts index 885821a..7d3cb61 100644 --- a/src/option/none.ts +++ b/src/option/none.ts @@ -7,7 +7,11 @@ import { OptionBase } from "./optionBase" export interface None extends OptionBase {} export class None implements OptionBase { - static none = new None() + static noneInstance = new None() + + static none(): Option { + return None.noneInstance as Option + } isEmpty = true @@ -20,11 +24,11 @@ export class None implements OptionBase { } map(fn: (t: T) => Nullable): Option { - return None.none as Option + return None.none() } flatMap(fn: (t: T) => Option): Option { - return None.none as Option + return None.none() } orElse(alternative: () => Option): Option { @@ -32,7 +36,7 @@ export class None implements OptionBase { } filter(predicate: (t: T) => boolean): Option { - return None.none as Option + return None.none() } } diff --git a/src/option/optionOf.ts b/src/option/optionOf.ts index 5b1c4f5..499542a 100644 --- a/src/option/optionOf.ts +++ b/src/option/optionOf.ts @@ -4,5 +4,5 @@ import { Option } from "./option" import { Some } from "./some" export function optionOf(t: Nullable): Option { - return t == null ? (None.none as Option) : new Some(t) + return t == null ? None.none() : new Some(t) } diff --git a/src/option/some.ts b/src/option/some.ts index 14a74cf..d957dfc 100644 --- a/src/option/some.ts +++ b/src/option/some.ts @@ -42,7 +42,7 @@ export class Some implements OptionBase { return this } - return None.none as Option + return None.none() } } diff --git a/test/option.test.ts b/test/option.test.ts index 2b7dfcc..a9e077b 100644 --- a/test/option.test.ts +++ b/test/option.test.ts @@ -20,16 +20,15 @@ describe("option", function () { }) it("Option.none should be safe", () => { - const none = None.none as Option - 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 // some.map(x => x.toString()).should.eq(Option.of(null)) // some.flatMap(x => Option.of(x!.toString())).should.eq(Option.of(null)) @@ -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", () => { @@ -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") }) })