Skip to content

Commit

Permalink
Merge pull request #86 from swan-io/from-predicate
Browse files Browse the repository at this point in the history
Add Option.fromPredicate
  • Loading branch information
bloodyowl authored Oct 22, 2024
2 parents b9628ad + 32cf1e1 commit 9818bcf
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
70 changes: 70 additions & 0 deletions docs/docs/option.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,76 @@ option.tapSome(console.log).map((x) => x * 2);

## Statics

### Option.fromNullable(value)

```ts
fromNullable(nullable: A | null | undefined): Option<A>
```

Creates an option from a nullable value, excluding `null` & `undefined`

```ts title="Examples"
Option.fromNullable(null);
// Option.None()

Option.fromNullable(undefined);
// Option.None()

Option.fromNullable(1);
// Option.Some(1)
```

### Option.fromNull(value)

```ts
fromNull(nullable: A | null): Option<A>
```

Creates an option from a nullable value, excluding `null`

```ts title="Examples"
Option.fromNull(null);
// Option.None()

Option.fromNull(undefined);
// Option.Some(undefined)

Option.fromNull(1);
// Option.Some(1)
```

### Option.fromUndefined(value)

```ts
fromUndefined(nullable: A | undefined): Option<A>
```

Creates an option from a nullable value, excluding `undefined`

```ts title="Examples"
Option.fromUndefined(null);
// Option.Some(null)

Option.fromUndefined(undefined);
// Option.None()

Option.fromUndefined(1);
// Option.Some(1)
```

### Option.fromPredicate(value, predicate)

```ts
fromPredicate(value: A, f: (value: A) => boolean): Option<A>
```

Creates an option from a value and a predicate. Will return `Some(value)` if predicate returns `true`, `None` if `false`

```ts title="Examples"
Option.fromPredicate(value, (value) => value % 2 === 0);
// Option<number> where `number` is even
```

### Option.isOption(value)

```ts
Expand Down
17 changes: 17 additions & 0 deletions docs/docs/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ result.tapError(console.log).map((x) => x * 2);

## Statics

### Result.fromPredicate(value, predicate, error)

```ts
fromPredicate(value: A, f: (value: A) => boolean, errorIfFalse: E): Result<A, E>
```

Creates an option from a value and a predicate. Will return `Ok(value)` if predicate returns `true`, `Error(errorIfFalse)` if `false`

```ts title="Examples"
Result.fromPredicate(
value,
(value) => value % 2 === 0,
new Error("Odd number"),
);
// Ok<number> if `number` is even, Error<Error> if odd
```

### Result.isResult(value)

```ts
Expand Down
23 changes: 23 additions & 0 deletions src/OptionResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ class __Option<A> {
: Option.Some<A>(nullable);
};

/**
* Create an Option from a value & predicate
*/

static fromPredicate<A, B extends A>(
value: A,
predicate: (value: A) => value is B,
): Option<B>;
static fromPredicate<A>(
value: A,
predicate: (value: A) => boolean,
): Option<A>;
static fromPredicate<A>(
value: A,
predicate: (value: A) => boolean,
): Option<A> {
if (predicate(value)) {
return Option.Some(value);
} else {
return NONE as Option<A>;
}
}

/**
* Turns an array of options into an option of array
*/
Expand Down
5 changes: 5 additions & 0 deletions test/Option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ test("Option.fromUndefined", () => {
expect(Option.fromUndefined(undefined)).toEqual(Option.None());
});

test("Option.fromPredicate", () => {
expect(Option.fromPredicate(1, (x) => x === 1)).toEqual(Option.Some(1));
expect(Option.fromPredicate(1, (x) => x === 2)).toEqual(Option.None());
});

test("Option.equals", () => {
expect(Option.equals(Option.None(), Option.None(), (a, b) => a === b)).toBe(
true,
Expand Down

0 comments on commit 9818bcf

Please sign in to comment.