Added always and never

This commit is contained in:
Michaël Lemaire 2019-09-24 23:39:04 +02:00
parent 48e1c6365a
commit 079a9dc5ae
4 changed files with 36 additions and 3 deletions

View file

@ -30,6 +30,13 @@ new ConstructorWithMandatoryCallback(nop)
a === identity(a) // => true
```
**always** and **never** return true and false respectively
```typescript
always() // => true
never() // => false
```
**partial** applies a partial configuration object as first argument of compatible functions:
```typescript

View file

@ -1,6 +1,6 @@
{
"name": "tk-functional",
"version": "0.1.2",
"version": "0.1.3",
"description": "Typescript/Javascript helpers for functional-style programming",
"source": "src/index.ts",
"main": "dist/tk-functional.js",
@ -26,4 +26,4 @@
"tk-base": "^0.1.7"
},
"dependencies": {}
}
}

View file

@ -1,4 +1,4 @@
import { partial, cmp, nop, identity } from "./index";
import { always, cmp, identity, never, nop, partial } from "./index";
describe("nop", () => {
it("does nothing", () => {
@ -35,3 +35,19 @@ describe("cmp", () => {
expect(["c", "a", "b"].sort(cmp())).toEqual(["a", "b", "c"]);
});
});
describe("always", () => {
it("returns true", () => {
expect(always()).toBe(true);
expect(always(true)).toBe(true);
expect(always(false)).toBe(true);
});
});
describe("never", () => {
it("returns false", () => {
expect(never()).toBe(false);
expect(never(true)).toBe(false);
expect(never(false)).toBe(false);
});
});

View file

@ -37,3 +37,13 @@ export function cmp(args?: Partial<cmpArgs>): (a: any, b: any) => number {
}
}
}
/**
* Predicate that always returns true
*/
export const always = (...args: any[]) => true
/**
* Predicate that always returns false
*/
export const never = (...args: any[]) => false