{"id":27786335,"url":"https://github.com/clever/json-predicate-transformer","last_synced_at":"2025-04-30T15:59:47.119Z","repository":{"id":42621258,"uuid":"204981451","full_name":"Clever/json-predicate-transformer","owner":"Clever","description":"A simple utility for modifying JSON objects using a predicate-transformer paradigm","archived":false,"fork":false,"pushed_at":"2024-09-23T18:28:57.000Z","size":85,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":37,"default_branch":"master","last_synced_at":"2025-03-17T16:53:16.149Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Clever.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-28T16:57:36.000Z","updated_at":"2024-09-23T18:29:01.000Z","dependencies_parsed_at":"2023-01-31T20:46:03.748Z","dependency_job_id":null,"html_url":"https://github.com/Clever/json-predicate-transformer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Clever%2Fjson-predicate-transformer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Clever%2Fjson-predicate-transformer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Clever%2Fjson-predicate-transformer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Clever%2Fjson-predicate-transformer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Clever","download_url":"https://codeload.github.com/Clever/json-predicate-transformer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251737988,"owners_count":21635715,"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":[],"created_at":"2025-04-30T15:59:44.824Z","updated_at":"2025-04-30T15:59:47.109Z","avatar_url":"https://github.com/Clever.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-predicate-transformer\n\nA simple utility for modifying JSON objects using a predicate-transformer paradigm\n\n## Usage\n\n```js\n  import { predicateTransform } from \"json-predicate-transformer\"\n\n  const preTransformedObject = {\n    foo: {\n      bar: 123,\n      xyz: 200,\n      dontTouchMe: 0,\n    },\n    baz: \"hello world\",\n  };\n\n  const result = predicateTransform(preTransformedObject, {\n    keyHandlers: [\n      {\n          predicate: (path) =\u003e path === \"foo.xyz\",\n          transformer: (path, value) =\u003e `key-${path}-${value}`\n      }\n    ],\n    valueHandlers: [\n      {\n        predicate: (_, value) =\u003e value \u003e 100,\n        transformer: (_, value) =\u003e value * 2,\n      },\n      {\n        predicate: (_, value) =\u003e typeof value === \"string\",\n        transformer: () =\u003e \"[string]\",\n      },\n    ],\n  });\n\n  console.log(result)\n  // {\n  //     foo: {\n  //         bar: 246,\n  //         \"key-xyz-200\": 200,\n  //         dontTouchMe: 0,\n  //     },\n  //     baz: \"[string]\"\n  // }\n```\n\n## API\n\n\u003e This library is built in Typescript and includes type declarations so those are the authoritative and easiest to use source for the API.\n\n### `predicateTransform(blob: object | any[], options: TransformerOptions): object | any[]`\nThe main transformer function.\n\n### Important Types\n```js\ninterface TransformerOptions {\n  keyHandlers?: []HandlerConfig,\n  valueHandlers?: []HandlerConfig\n}\n\ninterface HandlerConfig {\n  predicate: (path: string, value: any) =\u003e boolean,\n  transformer: (path: string, value: any) =\u003e any\n}\n```\n\n## FAQ\n\n### What is a `HandlerConfig`?\nA `HandlerConfig` is the most fundamental building block for using `json-predicate-transformer`. A `HandlerConfig` is an object with two properties: a `predicate` function and `transformer` function. \n\nA `predicate` has the function signature `(path: string, value: any) =\u003e boolean`.\n\nA `transformer` has the function signature `(path: string, value: any) =\u003e any`.\n\nYou can specify any number of `HandlerConfig`s for `keyHandlers` and `valueHandlers` and they will be applied (in the provided order) on every JSON key or value, respectively, e.g:\n```js\nif (predicate(...)) {\n  newValue = transformer(...)\n}\n```\nSee the example usage at the top for an idea of what this means.\n\n### What is the difference between `keyHandlers` vs. `valueHandlers`?\nKey handlers allow you to predicate-transform the **keys** of a JSON object while value handlers allow you to predicate-transform the **values**.\n\nKey handlers and value handlers will both receive the same parameters to their `predicate` and `transformer` functions.\n\nAssume the following input to `predicateTransform`:\n```js\n{\n  foo: {\n    bar: 123\n  }\n}\n```\n\nFor both key and value handlers, the `path` parameter will be the dotpath to the key being processed, e.g. `\"foo.bar\"`, while the `value` parameter will be the value at that path, e.g. `123`.\n\nThe key difference between them is simply which value the `transformer`'s output is applied to. Assume the following super-simple `HandlerConfig`\n```js\n{ predicate: () =\u003e true, transformer: () =\u003e \"ZZZ\" }\n```\n\nIf this was provided in `keyHandlers` with the above object as input, it would result in\n```js\n{\n  ZZZ: {\n    ZZZ: 123\n  }\n}\n```\nwhereas if it was provided in `valueHandlers`, it would result in\n```js\n{\n  foo: {\n    bar: \"ZZZ\"\n  }\n}\n```\n\n### What's a \"dotpath\"?\n\nA dotpath is simply notation you can use to refer to objects nested within a JSON object.\n```js\n{\n  foo: {\n    bar: [\n      { abc: 1, def: 2 },\n      { xyz: 3, uvw: 4 }\n    ],\n    baz: 100\n  }\n}\n```\n\nFor example, You can access the `1` using `foo.bar.abc`, the `100` using `foo.baz`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclever%2Fjson-predicate-transformer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclever%2Fjson-predicate-transformer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclever%2Fjson-predicate-transformer/lists"}