Skip to content

Commit

Permalink
feat: adds isJustOf
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Mar 21, 2021
1 parent f39f0a5 commit ca6a3ce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/__tests__/maybe.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { maybe } from "..";
import { isstring, maybe } from "..";
import { bind } from "../applicative";

describe(`maybe`, () => {
test(`isJustOf`, () => {
const isjustStr = maybe.isJustOf(isstring);

expect(isjustStr(10)).toBe(false);
expect(isjustStr(maybe.just(10))).toBe(false);
expect(isjustStr(maybe.just('abc'))).toBe(true);
expect(isjustStr(maybe.nothing())).toBe(false);
});

test(`frompredicate`, () => {
const isgreaterthan10 = (value: number) => value > 10;
const fn = maybe.frompredicate(isgreaterthan10);
Expand Down
33 changes: 31 additions & 2 deletions src/maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { createMap, createMapOr, createMapOrElse } from "./mappables";
import { Predicate } from "./predicate";
import {
isTagged,
isTaggedOf,
isTaggedWith,
tagged,
Tagged,
TaggedFactory,
} from "./tagged-type";
import { anyof, combine } from "./typeguards";
import { anyof, combine, Typeguard } from "./typeguards";
import {
createUnwrap,
createUnwrapOr,
Expand Down Expand Up @@ -69,9 +70,37 @@ export const isMaybe = combine<Maybe>(

/**
* Checks if a type is `Just<unknown>`
* @since 2.0.0
*
* @example
* ```ts
* import { maybe } from 'tiinvo';
*
* maybe.isJust(10) // false
* maybe.isJust(maybe.just(10)) // true
* maybe.isJust(maybe.nothing()) // false
* ```
*/
export const isJust = combine<Just>(isTagged, hasjusttag);

/**
* Checks if a type is `Just<T>`
* @since 2.14.0
*
* @example
* ```ts
* import { maybe, isstring } from 'tiinvo';
*
* const isjustStr = maybe.isJustOf(isstring);
*
* isjustStr(10) // false
* isjustStr(maybe.just(10)) // false
* isjustStr(maybe.just('abc')) // true
* isjustStr(maybe.nothing()) // false
* ```
*/
export const isJustOf = <T>(type: Typeguard<T>) => isTaggedOf(JUSTTAG, type);

/**
* Checks if a type if `Nothing`
*/
Expand All @@ -95,7 +124,7 @@ export const just = <T>(value: T) =>

/**
* Creates a `Maybe<T>` tagged type. If the value is truthy, it
* creates a `Just<T>`, otherwise creates `Nothing`
* will be a `Just<T>`, otherwise it will be `Nothing`
*/
export const maybe = <T>(arg?: T) => (arg ? just(arg) : nothing());

Expand Down

0 comments on commit ca6a3ce

Please sign in to comment.