{"id":22619568,"url":"https://github.com/evitanrelta/predicate-hof","last_synced_at":"2025-04-11T15:20:55.432Z","repository":{"id":54346667,"uuid":"522135306","full_name":"EvitanRelta/predicate-hof","owner":"EvitanRelta","description":"TypeScript higher-order-functions to combine or negate predicates without evaluating them.","archived":false,"fork":false,"pushed_at":"2022-09-22T00:44:35.000Z","size":162,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-25T11:22:06.641Z","etag":null,"topics":["higher-order-functions","javascript","node-js","nodejs","predicate","predicate-logic","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/predicate-hof","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EvitanRelta.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-08-07T06:32:13.000Z","updated_at":"2023-09-02T18:22:45.000Z","dependencies_parsed_at":"2022-08-13T12:50:26.009Z","dependency_job_id":null,"html_url":"https://github.com/EvitanRelta/predicate-hof","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvitanRelta%2Fpredicate-hof","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvitanRelta%2Fpredicate-hof/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvitanRelta%2Fpredicate-hof/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvitanRelta%2Fpredicate-hof/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EvitanRelta","download_url":"https://codeload.github.com/EvitanRelta/predicate-hof/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248429129,"owners_count":21101786,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["higher-order-functions","javascript","node-js","nodejs","predicate","predicate-logic","typescript"],"created_at":"2024-12-08T22:06:09.476Z","updated_at":"2025-04-11T15:20:55.394Z","avatar_url":"https://github.com/EvitanRelta.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Predicate HOF _\u003csup\u003e\u003csub\u003e(High-Order-Functions)\u003c/sub\u003e\u003c/sup\u003e_\n\nA collections of higher-order-functions to combine and negate predicates, without evaluating said predicates.\n\n**\u003cins\u003eFully written\u003c/ins\u003e** in TypeScript, and no dependencies.\n\nCurrently, the functions available are:\n\n-   `not`, \u0026nbsp; \u0026nbsp; \u0026hairsp;\u0026hairsp;\u0026hairsp; \u0026nbsp; negates the predicate.\n-   `all`, \u0026nbsp; \u0026nbsp; \u0026hairsp;\u0026hairsp;\u0026hairsp; \u0026nbsp; true only if **_ALL_** the predicates returns true.\n-   `any`, \u0026nbsp; \u0026nbsp; \u0026hairsp;\u0026hairsp;\u0026hairsp; \u0026nbsp; true if **_ANY_** the predicate returns true.\n-   `none`,\u0026thinsp;\u0026thinsp;\u0026thinsp;\u0026thinsp;\u0026thinsp;\u0026thinsp;\u0026thinsp;\u0026hairsp;\u0026hairsp;\u0026hairsp;\u0026hairsp; true only if **_NONE_** of the predicates return true.\n-   `onlyOne`,\u0026thinsp;\u0026thinsp;\u0026hairsp;\u0026hairsp;\u0026hairsp;true if **_ONLY ONE_** of the predicates return true.\n\n\u003cbr\u003eThey all work with predicates taking in **\u003cins\u003eany number\u003c/ins\u003e** of arguments, as long as all the predicates take in the same _(or subtype/supertype)_ parameters for example:\n\n```typescript\ninterface Parent {\n    x: number\n}\ninterface Child extends Parent {\n    y: string\n}\n\nconst parentPredicate = (x: Parent) =\u003e true\nconst childPredicate = (x: Child) =\u003e true\n\n// Both works:\nall(parentPredicate, childPredicate)\nall(childPredicate, parentPredicate)\n```\n\n\u003e If you're getting type errors, check the [**\"Fixing type errors\"**](#fixing-type-errors) section below\n\n## \u003cbr\u003eInstall\n\n```bash\nnpm i predicate-hof\n```\n\n## \u003cbr\u003eExamples\n\nI didn't define the predicates below for simplicity-sake, so you can just use your imagination on how they work :)\n\n```typescript\nimport { all } from 'predicate-hof'\n\nconst canBuyAGun = all(isAbove18, hasNoCriminalRecord, hasMoney)\ncanBuyAGun(bob) // true,  bob is cool\ncanBuyAGun(brokeAssTimmy) // false, go get a job Timmy\n```\n\n```typescript\nimport { any } from 'predicate-hof'\n\nconst toCallCops = any(hasDoneSmthBad, hasStolenMyPurse, isBlack)\ntoCallCops(bob) // false, bob is still cool\ntoCallCops(obama) // true,  he didn't do anything wrong but...\n```\n\n```typescript\nimport { not } from 'predicate-hof'\n\nisAlive(bob) // true, always alive in my heart\nnot(isAlive)(johnFKennedy) // true\n\nwasAssassinated(johnFKennedy) // true, rip Mr President :(\nnot(wasAssassinated)(epstein) // :^)\n```\n\n### \u003cbr\u003eHere's examples for binary-predicates\n\n```typescript\nimport { any } from 'predicate-hof'\n\nisLessThan(1, 3) // true, 1 \u003c 3\nisEqual(1, 1) // true, 1 === 1\n\nconst isLessThanOrEqual = any(isLessThan, isEqual)\nisLessThanOrEqual(1, 5) // true, 1 \u003c= 5\nisLessThanOrEqual(1, 1) // true, 1 \u003c= 1\n```\n\n## \u003cbr\u003eFixing type errors\n\nIf the predicate parameters don't match exactly, TypeScript might complain:\n\n```typescript\nconst numOnly = (n: number) =\u003e true\nconst numAndStr = (n: number, str: string) =\u003e true\n\nall(numOnly, numAndStr) // numAndStr gives type error\n```\n\n\u003cbr\u003eA fix would be to ensure the predicate that accepts the most parameters is the first to be passed:\n\n```typescript\n// 'numAndStr' takes more parameters,\n// so it should be at the front.\nall(numAndStr, numOnly)\n```\n\n\u003cbr\u003eIf that doesn't work, or is not possible to do, you can specify the generic type _(which is an array of the parameter type(s))_:\n\n```typescript\n// number is 1st argument\n// string is 2nd argument\nall\u003c[number, string]\u003e(numOnly, numAndStr)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevitanrelta%2Fpredicate-hof","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevitanrelta%2Fpredicate-hof","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevitanrelta%2Fpredicate-hof/lists"}