{"id":20420332,"url":"https://github.com/deepraining/json-refactor","last_synced_at":"2026-04-20T06:33:03.217Z","repository":{"id":57285110,"uuid":"83432516","full_name":"deepraining/json-refactor","owner":"deepraining","description":"基于指定的规则对 Json 数据结构进行重构. Refactor JSON object structure based on provided rules.","archived":false,"fork":false,"pushed_at":"2019-07-24T02:32:43.000Z","size":118,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-09-29T12:54:56.830Z","etag":null,"topics":["format","json","refactor","rule","structure"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/deepraining.png","metadata":{"files":{"readme":"README.en.md","changelog":"CHANGELOG.md","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":"2017-02-28T12:56:41.000Z","updated_at":"2021-11-01T07:37:53.000Z","dependencies_parsed_at":"2022-09-19T23:21:18.367Z","dependency_job_id":null,"html_url":"https://github.com/deepraining/json-refactor","commit_stats":null,"previous_names":["senntyou/json-refactor"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/deepraining/json-refactor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepraining%2Fjson-refactor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepraining%2Fjson-refactor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepraining%2Fjson-refactor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepraining%2Fjson-refactor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deepraining","download_url":"https://codeload.github.com/deepraining/json-refactor/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepraining%2Fjson-refactor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32036395,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T00:18:06.643Z","status":"online","status_checked_at":"2026-04-20T02:00:06.527Z","response_time":94,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["format","json","refactor","rule","structure"],"created_at":"2024-11-15T06:42:29.089Z","updated_at":"2026-04-20T06:33:03.201Z","avatar_url":"https://github.com/deepraining.png","language":"JavaScript","readme":"# json-refactor\n\n[中文文档](./README.md)\n\nRefactor JSON object structure based on provided rules.\n\n## quick start\n\n```\nnpm install json-refactor --save\n```\n\n## how to use\n\n```\nimport refactor from 'json-refactor';\n\nconst result = refactor(target, rules, clone);\n```\n\n- `target`: target to be refactored\n- `rules`: rules to refactor\n- `clone`: default `false`, whether to clone a copy of target. By default, the original target will be modified and return. If `true`, a new cloned target will be formatted and return, and the original target will not be modified.\n\n## rules\n\nThe `to key` to `from key` hash map.\n\n### 1. base\n\n`target`: `{a: 1, b: 2}`\n\n`rules`: `{aaa: 'a', bbb: 'b'}`\n\n`result`: `{aaa: 1, bbb: 2}`\n\n### 2. rules should have the same structure with target, including array\n\n`target`: `[{a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}]`\n\n`rules`: `[{aaa: 'a', bbb: 'b'}]`\n\n`result`: `[{aaa: 1, bbb: 2}, {aaa: 3, bbb: 4}, {aaa: 5, bbb: 6}]`\n\n### 3. support `.` semantic\n\n`target`: `{a: {a: {a: 1}}}`\n\n`rules`: `{aaa: 'a.a.a'}`\n\n`result`: `{aaa: 1}`\n\n### 4. make a new key, and keep on formatting the new key\n\n`target`: `{a: {a: {a: 1}}}`\n\n`rules`: `{aaa: 'a', _aaa: {aaa: 'a', _aaa: {aaa: 'a'}}}`\n\n`result`: `{aaa: {aaa: {aaa: 1}}}`\n\n### 5. take an operator to original value\n\nUse `|` to concat `from key` and `operator`, and you can add multiple operators.\n\n`target`: `{a: 1, b: '234', c: '1.22', d: '0.01'}`\n\n`rules`: `{aaa: 'a|bool', bbb: 'b|int', ccc: 'c|float', ddd: 'd|int|bool'}`\n\n`result`: `{aaa: true, bbb: 234, ccc: 1.22, ddd: false}`\n\n## api\n\n### `refactor.set`: set the default config values\n\n```\nrefactor.set({\n  keepOnHandling: '_',\n  operatorDelimiter: '|'\n});\n```\n\n- `keepOnHandling`: make a new key, and keep on formatting the new key\n\n  - `type`: `string`\n  - `default`: `_`\n\n- `operatorDelimiter`: delimiter of operators\n  - `type`: `string`\n  - `default`: `|`\n\n### `refactor.register`: register operators\n\n```\n// register one operator\nrefactor.register(test, handler);\nrefactor.register({test, handler});\n\n// register multiple operators\nrefactor.register([{test1, handler1}, {test2, handler2}, ...]);\n```\n\n- `test`: to match the operator\n\n  - `type`: `string/RegExp`\n  - `example`: `int`, `float`, `bool`, `string`, `/^slice!0!10/`\n\n- `handler`: handle the original value and return a new value\n  - `type`: `function`\n  - `example`: `(value, operator) =\u003e newValue`\n  - `parameters`: `value, operator`\n    - `value`: original value to be handled\n    - `operator`: operator matched\n\n## built-in operators\n\n### 1. `int`\n\nGet an integer value.\n\n### 2. `float`\n\nGet a float value.\n\n### 3. `bool`\n\nGet a bool value.\n\n### 4. `string`\n\nGet a string value.\n\n### 5. `sum`\n\nGet a sum value specified by a key of each element, within an array.\n\n`format`: `sum!key`\n\n`target`: `{oldKey: [{a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}]}`\n\n`rules`: `{newKey: 'oldKey|sum!a'}`\n\n`result`: `{newKey: 9}`\n\n### 6. `average`\n\nGet an average value specified by a key of each element, within an array.\n\n`format`: `average!key`\n\n`target`: `{oldKey: [{a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}]}`\n\n`rules`: `{newKey: 'oldKey|average!a'}`\n\n`result`: `{newKey: 3}`\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepraining%2Fjson-refactor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeepraining%2Fjson-refactor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepraining%2Fjson-refactor/lists"}