Added "is" and "isinstance"

This commit is contained in:
Michaël Lemaire 2019-10-17 22:51:42 +02:00
parent 515612f089
commit 9121402cfb
7 changed files with 795 additions and 122 deletions

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}

View file

@ -82,6 +82,23 @@ bool({x: 1}); // => true
a === identity(a) // => true
```
**is** and **isinstance** checks for strict equality and inheritance:
```typescript
const f = is(8);
f(8) // => true
f(5 + 3) // => true
f("8") // => false
f(null) // => false
class A { }
class A1 extends A { }
class A2 extends A { }
class B { }
const f: any[] = [5, null, undefined, new A, new A1, new B, new A2];
const result: A[] = f.filter(isinstance(A)); // => [f[3], f[4], f[6]]
```
**nn**, **nu** and **nnu** checks at run-time for null or undefined:
```typescript

833
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{
"name": "tk-functional",
"version": "0.1.5",
"version": "0.2.0",
"description": "Typescript/Javascript helpers for functional-style programming",
"source": "src/index.ts",
"main": "dist/tk-functional.umd.js",
@ -18,7 +18,8 @@
"prepublishOnly": "npm test",
"dev": "run-p dev:*",
"dev:test": "jest --watchAll",
"dev:build": "microbundle watch -f modern,umd"
"dev:build": "microbundle watch -f modern,umd",
"dev:serve": "live-server --host=localhost --port=5000 --no-browser dist"
},
"author": {
"name": "Michaël Lemaire",
@ -37,7 +38,7 @@
"typescript"
],
"devDependencies": {
"tk-base": "^0.2.1"
"tk-base": "^0.2.3"
},
"dependencies": {}
}
}

View file

@ -1,4 +1,4 @@
import { always, and, attr, bool, cmp, fmap, identity, never, nn, nnu, nop, nu, or, partial, pipe } from "./index";
import { always, and, attr, bool, cmp, fmap, identity, is, isinstance, never, nn, nnu, nop, nu, or, partial, pipe } from "./index";
describe(nop, () => {
it("does nothing", () => {
@ -74,6 +74,38 @@ describe(or, () => {
});
});
describe(is, () => {
it("checks for equality", () => {
const f = is(5);
expect(f(5)).toBe(true);
expect(f(4)).toBe(false);
expect(f("a" as any)).toBe(false);
expect(f(null as any)).toBe(false);
expect(f(undefined as any)).toBe(false);
});
it("applies strict equality", () => {
const obj: { x: number } = { x: 1 };
let f = is(obj);
expect(f(obj)).toBe(true);
expect(f({ x: 1 })).toBe(false);
expect(f({ y: 1 } as any)).toBe(false);
});
});
describe(isinstance, () => {
it("checks for subclasses", () => {
class A { }
class A1 extends A { }
class A2 extends A { }
class B { }
const f: any[] = [5, null, undefined, new A, new A1, new B, new A2];
const result = f.filter(isinstance(A));
expect(result).toEqual([f[3], f[4], f[6]]);
});
});
describe(pipe, () => {
it("pipes two functions", () => {
const f = pipe((x: number) => x * 2, (x: number) => x + 1);

View file

@ -77,6 +77,20 @@ export function or<A extends any[]>(...predicates: ((...args: A) => boolean)[]):
}
}
/**
* Get a function to check the strict equality with a reference
*/
export function is<T, S extends T>(ref: T): (input: S) => input is Extract<T, S> {
return (x): x is Extract<T, S> => x === ref;
}
/**
* Get a function to check that inputs are instances of a given type
*/
export function isinstance<T, S extends T>(ref: { new(...args: any[]): T }): (input: S) => input is Extract<T, S> {
return (x): x is Extract<T, S> => x instanceof ref;
}
/**
* Pipe the result of a function as first parameter of another, to create a new function
*/

View file

@ -12,5 +12,10 @@
"dom",
"es6"
]
}
},
"exclude": [
"node_modules",
"dist",
"src/**/*.test.ts"
]
}