Permalink
Browse files

feat(core): add Option.orUndefined, Try.orUndefined (#68)

  • Loading branch information...
alexandru committed Oct 19, 2017
1 parent df0f41a commit 6f551d86e0d9682f11331a93ede20e7a771db000
View
@@ -25,7 +25,7 @@
"babel-preset-es2015-rollup": "^3.0.0",
"commitizen": "^2.9.6",
"cz-conventional-changelog": "^2.0.0",
"flow-bin": "^0.57.1",
"flow-bin": "^0.57.3",
"fs-extra": "^4.0.1",
"husky": "^0.14.3",
"jsverify": "^0.8.2",
@@ -75,6 +75,7 @@ declare export class Option<+A> {
orElse<AA>(fallback: Option<AA>): Option<A | AA>;
orElseL<AA>(thunk: () => Option<AA>): Option<A | AA>;
orNull(): A | null;
orUndefined(): A | void;
chain<B>(f: (a: A) => Option<B>): Option<B>;
contains(elem: A): boolean;
@@ -130,6 +131,7 @@ declare export class Try<+A> {
orElse<AA>(fallback: Try<AA>): Try<A | AA>;
orElseL<AA>(thunk: () => Try<AA>): Try<A | AA>;
orNull(): A | null;
orUndefined(): A | void;
isSuccess(): boolean;
isFailure(): boolean;
@@ -550,10 +550,18 @@ export class Option<A> implements std.IEquals<Option<A>> {
/**
* Returns the option's value if the option is nonempty, otherwise
* return `null`.
* ```
*/
orNull(): A | null {
if (!this._isEmpty) return this._ref
else return null
return !this._isEmpty ? this._ref : null
}
/**
* Returns the option's value if the option is nonempty, otherwise
* return `undefined`.
*/
orUndefined(): A | undefined {
return !this._isEmpty ? this._ref : undefined
}
/**
@@ -1146,8 +1154,26 @@ export class Try<A> implements std.IEquals<Try<A>> {
* ```
*/
orNull(): A | null {
if (this._isSuccess) return this._successRef
return null
return this._isSuccess ? this._successRef : null
}
/**
* Returns the current value if it's a [[Success]], or
* if the source is a [[Failure]] then return `undefined`.
*
* ```typescript
* Success(10).orUndefined() // 10
* Failure("error").orUndefined() // undefined
* ```
*
* This can be useful for use-cases such as:
*
* ```typescript
* Try.of(() => dict.user.profile.name).orUndefined()
* ```
*/
orUndefined(): A | undefined {
return this._isSuccess ? this._successRef : undefined
}
/**
@@ -92,9 +92,12 @@ const r18: Option<number> =
const r19: Option<number> =
Option.of(1).orElseL(() => Option.of(2))
const r20: number | null =
const rel1: number | null =
Option.of(1).orNull()
const rel2: number | void =
Option.of(1).orUndefined()
// Covariance again
const optStr1: Option<string> = Option.some("")
const optStr2: Option<string> = Option.none()
@@ -51,3 +51,6 @@ const r29: Try<Object> = dogTry.orElseL(() => Success(new Human()))
const fail1: Try<number> = Try.raise("error")
const fail2: Try<number> = Try.failure("error")
const rel1: Dog | null = dogTry.orNull()
const rel2: Dog | void = dogTry.orUndefined()
@@ -96,6 +96,17 @@ describe("Option", () => {
})
})
describe("#orUndefined", () => {
jv.property("equivalence with #get if nonempty",
inst.arbOptNonempty,
opt => is(opt.orUndefined(), opt.get())
)
it("should return undefined in case the option is empty", () => {
assert.equal(Option.empty<number>().orUndefined(), undefined)
})
})
describe("#orElse", () => {
jv.property("mirrors the source if nonempty",
inst.arbOptNonempty,
@@ -273,12 +273,24 @@ describe("Try #orNull", () => {
b => is(Try.success(b).orNull(), b)
)
jv.property("failure(a).orNull() == null",
jv.property("failure(a).orNull() == undefined",
inst.arbAny, jv.string,
(a, b) => is(Try.failure(a).orNull(), null)
)
})
describe("Try #orUndefined", () => {
jv.property("success(b).orUndefined() == b",
inst.arbAny,
b => is(Try.success(b).orUndefined(), b)
)
jv.property("failure(a).orUndefined() == undefined",
inst.arbAny, jv.string,
(a, b) => is(Try.failure(a).orUndefined(), undefined)
)
})
describe("Try #orElse", () => {
jv.property("fa.orElse(fb) == fa for success",
inst.arbSuccess, inst.arbTry,
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 6f551d8

Please sign in to comment.