diff --git a/src/__tests__/maybe.test.ts b/src/__tests__/maybe.test.ts index 53c5241..ae120e2 100644 --- a/src/__tests__/maybe.test.ts +++ b/src/__tests__/maybe.test.ts @@ -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); diff --git a/src/maybe.ts b/src/maybe.ts index fc7850d..3479ac0 100644 --- a/src/maybe.ts +++ b/src/maybe.ts @@ -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, @@ -69,9 +70,37 @@ export const isMaybe = combine( /** * Checks if a type is `Just` + * @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(isTagged, hasjusttag); +/** + * Checks if a type is `Just` + * @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 = (type: Typeguard) => isTaggedOf(JUSTTAG, type); + /** * Checks if a type if `Nothing` */ @@ -95,7 +124,7 @@ export const just = (value: T) => /** * Creates a `Maybe` tagged type. If the value is truthy, it - * creates a `Just`, otherwise creates `Nothing` + * will be a `Just`, otherwise it will be `Nothing` */ export const maybe = (arg?: T) => (arg ? just(arg) : nothing());