UNPKG

@bitty/predicate

Version:

Predicate helper functions and types for TypeScript.

73 lines 3.91 kB
{ "name": "@bitty/predicate", "version": "0.2.0", "description": "Predicate helper functions and types for TypeScript.", "sideEffects": false, "cdn": "./dist/main.umd.min.js", "main": "./dist/main.js", "types": "./types/main.d.ts", "unpkg": "./dist/main.umd.min.js", "module": "./dist/main.esm.js", "jsdelivr": "./dist/main.umd.min.js", "umd:main": "./dist/main.umd.js", "exports": { ".": [ { "import": "./dist/main.mjs", "require": "./dist/main.js", "default": "./dist/main.js" }, "./dist/main.js" ] }, "files": [ "dist/", "types/" ], "keywords": [ "predicate", "type", "types", "type-guard", "type-predicate", "type-helper", "typescript", "typescript-types", "fp", "functional programming", "function", "functional" ], "repository": { "type": "git", "url": "git+https://github.com/VitorLuizC/bitty.git", "directory": "packages/predicate" }, "author": { "url": "https://vitorluizc.github.io/", "name": "Vitor Luiz Cavalcanti", "email": "vitorluizc@outlook.com" }, "bugs": { "url": "https://github.com/VitorLuizC/bitty/issues" }, "homepage": "https://github.com/VitorLuizC/bitty/tree/master/packages/predicate", "license": "MIT", "devDependencies": { "ava": "^4.0.1", "prettier": "^2.5.1", "rollup": "^2.63.0", "rollup-plugin-terser": "^7.0.2", "typescript": "^4.5.4" }, "scripts": { "test": "pnpm run test:code-style && pnpm run test:unit", "test:transpile": "tsc --project ./tsconfig.test.json", "test:unit": "pnpm run test:transpile && ava", "test:code-style": "prettier --check \"./src/**/*.ts\"", "build": "pnpm run build:transpile && pnpm run build:bundle", "build:transpile": "tsc --project ./tsconfig.build.json", "build:bundle": "rollup --config rollup.config.js" }, "readme": "# `@bitty/predicate`\n\n[![Bundle minified size](https://badgen.net/bundlephobia/min/@bitty/predicate)](https://bundlephobia.com/result?p=@bitty/predicate)\n[![Bundle minified and gzipped size](https://badgen.net/bundlephobia/minzip/@bitty/predicate)](https://bundlephobia.com/result?p=@bitty/predicate)\n\n`Predicate` and `Refinement` types for TypeScript.\n\n- 🏷 Safe:\n - JSDocs and type declarations for IDEs and editor's autocomplete/intellisense.\n - Made with TypeScript as strict as possible.\n - Unit tests with AVA.\n\n## Installation\n\nThis library is published in the NPM registry and can be installed using any compatible package manager.\n\n```sh\nnpm install @bitty/predicate --save\n\n# For Yarn, use the command below.\nyarn add @bitty/predicate\n```\n\n## Getting Stated\n\nIt exports `Predicate` and `Refinement` types.\n\n- `Predicate` is a function that receives a value and returns a boolean.\n\n ```ts\n import type { Predicate } from '@bitty/predicate';\n\n function checkAll<T = unknown>(predicates: Predicate<T>[]): Predicate<T> {\n return (value) => predicates.every((predicate) => predicate(value));\n }\n ```\n\n- `Refinement` is similar to `Predicate`, but uses [TypeScript `is` type guard](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards).\n\n ```ts\n import { Refinement } from '@bitty/predicate';\n\n const isNumber: Refinement<unknown, number> = (value) =>\n Number.isFinite(value);\n //=> Was typed as `(value: unknown) => value is number`.\n\n const value: number | string = Math.random() > 0.5 ? 100 : 'A text.';\n\n if (isNumber(value)) {\n console.log(value.toFixed(2));\n //=> Okay, because type of `value` was refined to `number`.\n }\n console.log(value.toFixed(2));\n //=> Property 'toFixed' does not exist on type 'string | number'.\n // Property 'toFixed' does not exist on type 'string'.\n ```\n\n## License\n\nReleased under [MIT License](./LICENSE).\n" }