├── .editorconfig ├── .eslintrc ├── .github └── workflows │ ├── publish_to_npm.yml │ └── validate_pull_request.yml ├── .gitignore ├── .prettierrc ├── .vscode └── settings.json ├── LICENSE ├── README.md ├── bun.lockb ├── bunfig.toml ├── package.json ├── src ├── index.ts ├── remove │ ├── README.md │ ├── index.ts │ └── remove.test.ts └── replace │ ├── README.md │ ├── index.ts │ └── replace.test.ts ├── tsconfig.json └── tsconfig.types.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = spaces 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es2021": true, 4 | "node": true 5 | }, 6 | "extends": [ 7 | "eslint:recommended", 8 | "plugin:@typescript-eslint/eslint-recommended", 9 | "plugin:@typescript-eslint/recommended", 10 | "plugin:prettier/recommended" 11 | ], 12 | "parser": "@typescript-eslint/parser", 13 | "parserOptions": { 14 | "ecmaVersion": 12, 15 | "sourceType": "module" 16 | }, 17 | "plugins": [ 18 | "@typescript-eslint", 19 | "prettier" 20 | ], 21 | "rules": { 22 | "@typescript-eslint/explicit-module-boundary-types": "off", 23 | "@typescript-eslint/no-non-null-assertion": "off" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /.github/workflows/publish_to_npm.yml: -------------------------------------------------------------------------------- 1 | name: Publish to NPM 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | concurrency: ${{ github.workflow }}-${{ github.ref }} 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v3 14 | - uses: oven-sh/setup-bun@v1 15 | 16 | - run: bun install 17 | - run: bun run build 18 | 19 | - run: echo //registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }} > .npmrc 20 | 21 | - name: Authenticate with Registry 22 | run: | 23 | echo "registry=https://registry.npmjs.org/" >> .npmrc 24 | echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc 25 | env: 26 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 27 | 28 | - name: Release package 29 | run: bun run publish:npm 30 | env: 31 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 32 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 33 | -------------------------------------------------------------------------------- /.github/workflows/validate_pull_request.yml: -------------------------------------------------------------------------------- 1 | name: Validade Pull Request 2 | 3 | on: [pull_request] 4 | 5 | jobs: 6 | validate_and_test: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v3 10 | - uses: oven-sh/setup-bun@v1 11 | 12 | - name: Install 13 | run: bun install 14 | 15 | - name: Linting 16 | run: bun run lint 17 | 18 | - name: Typecheck 19 | run: bun run typecheck 20 | 21 | - name: Test 22 | run: bun run test:ci 23 | 24 | - name: Build 25 | run: bun run build 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | build 3 | .env 4 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "none", 3 | "semi": false, 4 | "singleQuote": true, 5 | "overrides": [ 6 | { 7 | "files": "*.ts", 8 | "options": { 9 | "printWidth": 120 10 | } 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.codeActionsOnSave": { 3 | "source.fixAll.eslint": "explicit" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2023 João Paulo Moraes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![json-object-keys](https://user-images.githubusercontent.com/16243531/222727947-360bc403-92b8-4dc2-8d1b-586215b49a2f.jpg) 2 | 3 | # json-object-keys | ⚠️ WIP ⚠️ 4 | 5 | Manage complex object keys in depth. 6 | 7 | ## Table of contents 8 | 9 | - [json-object-keys | ⚠️ WIP ⚠️](#json-object-keys--️-wip-️) 10 | - [Table of contents](#table-of-contents) 11 | - [Install](#install) 12 | - [Bun](#bun) 13 | - [PNPM](#pnpm) 14 | - [NPM](#npm) 15 | - [Yarn](#yarn) 16 | - [Remove](#remove) 17 | - [Remove an unique key](#remove-an-unique-key) 18 | - [Remove multiple keys](#remove-multiple-keys) 19 | - [Replace](#replace) 20 | - [Replace an unique key](#replace-an-unique-key) 21 | - [Replace multiple keys](#replace-multiple-keys) 22 | - [License](#license) 23 | 24 | ## Install 25 | 26 | ### Bun 27 | 28 | ```sh 29 | bun add json-object-keys 30 | ``` 31 | 32 | ### PNPM 33 | 34 | ```sh 35 | pnpm add json-object-keys 36 | ``` 37 | 38 | ### NPM 39 | 40 | ```sh 41 | npm install json-object-keys 42 | ``` 43 | 44 | ### Yarn 45 | 46 | ```sh 47 | yarn add json-object-keys 48 | ``` 49 | 50 | ## Remove 51 | 52 | ### Remove an unique key 53 | 54 | ```ts 55 | import { remove } from 'json-object-keys' 56 | 57 | remove( 58 | { 59 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 60 | name: 'John Doe', 61 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 62 | email: 'john@doe.com', 63 | age: 29, 64 | ... 65 | }, 66 | 'category' 67 | ) 68 | ``` 69 | 70 | Output 71 | 72 | ```diff 73 | { 74 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 75 | name: 'John Doe', 76 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 77 | email: 'john@doe.com', 78 | age: 29 79 | } 80 | ``` 81 | 82 | ### Remove multiple keys 83 | 84 | ```ts 85 | import { remove } from 'json-object-keys' 86 | 87 | remove( 88 | { 89 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 90 | name: 'John Doe', 91 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 92 | email: 'john@doe.com', 93 | age: 29 94 | }, 95 | ['category', 'email'] 96 | ) 97 | ``` 98 | 99 | Output 100 | 101 | ```diff 102 | { 103 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 104 | name: 'John Doe', 105 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 106 | - email: 'john@doe.com', 107 | age: 29 108 | } 109 | ``` 110 | 111 | ## Replace 112 | 113 | ### Replace an unique key 114 | 115 | ```ts 116 | import { replace } from 'json-object-keys' 117 | 118 | replace({ id: 1, foo: 2, baz: 3 }, { foo: 'bar' }) 119 | ``` 120 | 121 | Output 122 | 123 | ```diff 124 | { 125 | id: 1, 126 | + bar: 2, 127 | baz: 3 128 | } 129 | ``` 130 | 131 | ### Replace multiple keys 132 | 133 | ```ts 134 | import { replace } from 'json-object-keys' 135 | 136 | replace( 137 | { 138 | uuid: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 139 | name: 'Grandchild 1', 140 | children: [ 141 | { 142 | uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 143 | name: 'Great Grandchild 1' 144 | }, 145 | { 146 | uuid: 'c579a0b8-c2c7-44e2-a7d9-2edba8f7b472', 147 | name: 'Great Grandchild 2' 148 | } 149 | ] 150 | }, { uuid: 'id', children: 'node' }) 151 | ``` 152 | 153 | Output 154 | 155 | ```diff 156 | { 157 | + id: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 158 | name: 'Grandchild 1', 159 | + node: [ 160 | { 161 | + id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 162 | name: 'Great Grandchild 1' 163 | }, 164 | { 165 | + id: 'c579a0b8-c2c7-44e2-a7d9-2edba8f7b472', 166 | name: 'Great Grandchild 2' 167 | } 168 | ] 169 | } 170 | ``` 171 | 172 | ## License 173 | 174 | This project is licensed under the MIT License. 175 | -------------------------------------------------------------------------------- /bun.lockb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joaopaulomoraes/json-object-keys/ff8b89baca593eb1a47a3f1b129e270ba70cef25/bun.lockb -------------------------------------------------------------------------------- /bunfig.toml: -------------------------------------------------------------------------------- 1 | [install] 2 | registry = { url = "https://registry.npmjs.org", token = "$npm_token" } 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-object-keys", 3 | "version": "0.1.25", 4 | "description": "Manage complex object keys in depth", 5 | "author": { 6 | "name": "João Paulo Moraes", 7 | "email": "joaopaulomoraes@outlook.com", 8 | "url": "https://github.com/joaopaulomoraes" 9 | }, 10 | "contributors": [ 11 | { 12 | "name": "Fabio Ramos", 13 | "email": "fabioalvesramos12@gmail.com", 14 | "url": "https://github.com/fabioramos12" 15 | }, 16 | { 17 | "name": "Filipe Boechat", 18 | "email": "filipeboechatsampaio@hotmail.com", 19 | "url": "https://github.com/fboechats" 20 | } 21 | ], 22 | "repository": { 23 | "type": "git", 24 | "url": "git+https://github.com/joaopaulomoraes/json-object-keys.git" 25 | }, 26 | "bugs": { 27 | "url": "https://github.com/joaopaulomoraes/json-object-keys/issues", 28 | "email": "joaopaulomoraes@outlook.com" 29 | }, 30 | "main": "build/commonjs/src/index.js", 31 | "module": "build/esm/src/index.js", 32 | "types": "build/types/index.d.ts", 33 | "scripts": { 34 | "lint": "eslint src --max-warnings=0", 35 | "typecheck": "tsc --project tsconfig.json --noEmit", 36 | "format": "prettier src --write \"**/*.+(js|ts|json)\"", 37 | "build": "bun build --minify index.ts --outdir build/umd --target node && bun run build:commonjs && bun run build:declaration", 38 | "build:commonjs": "bun build --minify index.ts --outdir build/commonjs --target browser", 39 | "build:declaration": "tsc --emitDeclarationOnly --project tsconfig.types.json", 40 | "dev": "bun --watch src/index.ts", 41 | "test": "bun test", 42 | "test:watch": "bun --watch test", 43 | "test:ci": "bun test", 44 | "publish:npm": "pnpm publish --access public --no-git-checks" 45 | }, 46 | "keywords": [ 47 | "json", 48 | "objects", 49 | "keys", 50 | "values", 51 | "remove", 52 | "replace" 53 | ], 54 | "license": "MIT", 55 | "devDependencies": { 56 | "@typescript-eslint/eslint-plugin": "^6.7.2", 57 | "@typescript-eslint/parser": "^6.7.2", 58 | "bun-types": "^1.0.2", 59 | "eslint": "^8.43.0", 60 | "eslint-config-prettier": "^8.8.0", 61 | "eslint-plugin-import": "^2.27.5", 62 | "eslint-plugin-prettier": "^4.2.1", 63 | "pnpm": "^8.7.6", 64 | "prettier": "^2.8.8", 65 | "tsc-alias": "^1.8.6", 66 | "typescript": "^5.1.3" 67 | }, 68 | "files": [ 69 | "build" 70 | ], 71 | "type": "module" 72 | } 73 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './remove' 2 | export * from './replace' 3 | -------------------------------------------------------------------------------- /src/remove/README.md: -------------------------------------------------------------------------------- 1 | # Remove 2 | 3 | ## Quickstart 4 | 5 | ### Remove an unique key 6 | 7 | ```ts 8 | import { remove } from 'json-object-keys' 9 | 10 | remove( 11 | { 12 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 13 | name: 'John Doe', 14 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 15 | email: 'john@doe.com', 16 | age: 29 17 | }, 18 | 'category' 19 | ) 20 | ``` 21 | 22 | Output 23 | 24 | ```diff 25 | { 26 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 27 | name: 'John Doe', 28 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 29 | email: 'john@doe.com', 30 | age: 29 31 | } 32 | ``` 33 | 34 | ```ts 35 | import { remove } from 'json-object-keys' 36 | 37 | remove( 38 | { 39 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 40 | name: 'John Doe', 41 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 42 | email: 'john@doe.com', 43 | age: 29, 44 | node: [ 45 | { 46 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 47 | name: 'John Doe', 48 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 49 | email: 'john@doe.com', 50 | age: 29, 51 | node: { 52 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 53 | name: 'John Doe', 54 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 55 | email: 'john@doe.com', 56 | age: 29, 57 | node: { 58 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 59 | name: 'John Doe', 60 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 61 | email: 'john@doe.com', 62 | age: 29 63 | } 64 | } 65 | }, 66 | { 67 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 68 | name: 'John Doe', 69 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 70 | email: 'john@doe.com', 71 | age: 29, 72 | node: { 73 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 74 | name: 'John Doe', 75 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 76 | email: 'john@doe.com', 77 | age: 29 78 | } 79 | }, 80 | { 81 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 82 | name: 'John Doe', 83 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 84 | email: 'john@doe.com', 85 | age: 29, 86 | node: { 87 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 88 | name: 'John Doe', 89 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 90 | email: 'john@doe.com', 91 | age: 29, 92 | node: { 93 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 94 | name: 'John Doe', 95 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 96 | email: 'john@doe.com', 97 | age: 29 98 | } 99 | } 100 | } 101 | ] 102 | }, 103 | 'id' 104 | ) 105 | ``` 106 | 107 | Output 108 | 109 | ```diff 110 | { 111 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 112 | name: 'John Doe', 113 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 114 | email: 'john@doe.com', 115 | age: 29, 116 | node: [ 117 | { 118 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 119 | name: 'John Doe', 120 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 121 | email: 'john@doe.com', 122 | age: 29, 123 | node: { 124 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 125 | name: 'John Doe', 126 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 127 | email: 'john@doe.com', 128 | age: 29, 129 | node: { 130 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 131 | name: 'John Doe', 132 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 133 | email: 'john@doe.com', 134 | age: 29 135 | } 136 | } 137 | }, 138 | { 139 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 140 | name: 'John Doe', 141 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 142 | email: 'john@doe.com', 143 | age: 29, 144 | node: { 145 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 146 | name: 'John Doe', 147 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 148 | email: 'john@doe.com', 149 | age: 29 150 | } 151 | }, 152 | { 153 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 154 | name: 'John Doe', 155 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 156 | email: 'john@doe.com', 157 | age: 29, 158 | node: { 159 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 160 | name: 'John Doe', 161 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 162 | email: 'john@doe.com', 163 | age: 29, 164 | node: { 165 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 166 | name: 'John Doe', 167 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 168 | email: 'john@doe.com', 169 | age: 29 170 | } 171 | } 172 | } 173 | ] 174 | } 175 | ``` 176 | 177 | ### Remove multiple keys 178 | 179 | ```ts 180 | import { remove } from 'json-object-keys' 181 | 182 | remove( 183 | { 184 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 185 | name: 'John Doe', 186 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 187 | email: 'john@doe.com', 188 | age: 29 189 | }, 190 | ['category', 'email'] 191 | ) 192 | ``` 193 | 194 | Output 195 | 196 | ```diff 197 | { 198 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 199 | name: 'John Doe', 200 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 201 | - email: 'john@doe.com', 202 | age: 29 203 | } 204 | ``` 205 | 206 | ```ts 207 | import { remove } from 'json-object-keys' 208 | 209 | remove( 210 | { 211 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 212 | name: 'John Doe', 213 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 214 | email: 'john@doe.com', 215 | age: 29, 216 | node: [ 217 | { 218 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 219 | name: 'John Doe', 220 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 221 | email: 'john@doe.com', 222 | age: 29, 223 | node: { 224 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 225 | name: 'John Doe', 226 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 227 | email: 'john@doe.com', 228 | age: 29, 229 | node: { 230 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 231 | name: 'John Doe', 232 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 233 | email: 'john@doe.com', 234 | age: 29 235 | } 236 | } 237 | }, 238 | { 239 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 240 | name: 'John Doe', 241 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 242 | email: 'john@doe.com', 243 | age: 29, 244 | node: { 245 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 246 | name: 'John Doe', 247 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 248 | email: 'john@doe.com', 249 | age: 29 250 | } 251 | }, 252 | { 253 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 254 | name: 'John Doe', 255 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 256 | email: 'john@doe.com', 257 | age: 29, 258 | node: { 259 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 260 | name: 'John Doe', 261 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 262 | email: 'john@doe.com', 263 | age: 29, 264 | node: { 265 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 266 | name: 'John Doe', 267 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 268 | email: 'john@doe.com', 269 | age: 29 270 | } 271 | } 272 | } 273 | ] 274 | }, 275 | ['email', 'category', 'age'] 276 | ) 277 | ``` 278 | 279 | Output 280 | 281 | ```diff 282 | { 283 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 284 | name: 'John Doe', 285 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 286 | - email: 'john@doe.com', 287 | - age: 29, 288 | node: [ 289 | { 290 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 291 | name: 'John Doe', 292 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 293 | - email: 'john@doe.com', 294 | - age: 29, 295 | node: { 296 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 297 | name: 'John Doe', 298 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 299 | - email: 'john@doe.com', 300 | - age: 29, 301 | node: { 302 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 303 | name: 'John Doe', 304 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 305 | - email: 'john@doe.com', 306 | - age: 29 307 | } 308 | } 309 | }, 310 | { 311 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 312 | name: 'John Doe', 313 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 314 | - email: 'john@doe.com', 315 | - age: 29, 316 | node: { 317 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 318 | name: 'John Doe', 319 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 320 | - email: 'john@doe.com', 321 | - age: 29 322 | } 323 | }, 324 | { 325 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 326 | name: 'John Doe', 327 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 328 | - email: 'john@doe.com', 329 | - age: 29, 330 | node: { 331 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 332 | name: 'John Doe', 333 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 334 | - email: 'john@doe.com', 335 | - age: 29, 336 | node: { 337 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 338 | name: 'John Doe', 339 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 340 | - email: 'john@doe.com', 341 | - age: 29 342 | } 343 | } 344 | } 345 | ] 346 | } 347 | ``` 348 | -------------------------------------------------------------------------------- /src/remove/index.ts: -------------------------------------------------------------------------------- 1 | type JSObject = { 2 | [key: string | number | symbol]: unknown 3 | } 4 | 5 | /** 6 | * Remove a key or multiple keys from object 7 | * @param object - An object 8 | * @param keys - A single key or an array of keys 9 | * @returns A new object without the given keys 10 | */ 11 | export function remove(object: O, keys: K | K[]): Omit { 12 | if (typeof keys === 'string') return withoutKey(object, keys) 13 | 14 | if (typeof keys === 'object') return withoutKeys(object, keys) 15 | 16 | return object 17 | } 18 | 19 | function withoutKey(object: O, key: K) { 20 | const regex = new RegExp(`"${key}":[^,}]+,?|,?"${key}":[^,}]+`, 'g') 21 | return withRegex(object, regex) 22 | } 23 | 24 | function withoutKeys(object: O, keys: K[]) { 25 | const params = keys.join('|') 26 | const regex = new RegExp(`"(${params})":[^,}]+,?|,?"(${params})":[^,}]+`, 'g') 27 | return withRegex(object, regex) 28 | } 29 | 30 | function withRegex(object: JSObject, regex: RegExp) { 31 | const stringify = JSON.stringify(object).replace(regex, '') 32 | return JSON.parse(stringify) 33 | } 34 | -------------------------------------------------------------------------------- /src/remove/remove.test.ts: -------------------------------------------------------------------------------- 1 | import { expect, it, describe } from 'bun:test' 2 | import { remove } from '.' 3 | 4 | const user = { 5 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 6 | name: 'John Doe', 7 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 8 | email: 'john@doe.com', 9 | age: 29 10 | } 11 | 12 | const userNodes = { 13 | ...user, 14 | node: [ 15 | { 16 | ...user, 17 | node: { 18 | ...user, 19 | node: { 20 | ...user 21 | } 22 | } 23 | }, 24 | { 25 | ...user, 26 | node: { 27 | ...user 28 | } 29 | }, 30 | { 31 | ...user, 32 | node: { 33 | ...user, 34 | node: { 35 | ...user 36 | } 37 | } 38 | } 39 | ] 40 | } 41 | 42 | describe('remove', () => { 43 | it('should remove the id from user', () => { 44 | expect(remove(user, 'id')).toStrictEqual({ 45 | name: 'John Doe', 46 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 47 | email: 'john@doe.com', 48 | age: 29 49 | }) 50 | }) 51 | 52 | it('should remove the category from user', () => { 53 | expect(remove(user, 'category')).toStrictEqual({ 54 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 55 | name: 'John Doe', 56 | email: 'john@doe.com', 57 | age: 29 58 | }) 59 | }) 60 | 61 | it('should remove category and email from user', () => { 62 | expect(remove(user, ['category', 'email'])).toStrictEqual({ 63 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 64 | name: 'John Doe', 65 | age: 29 66 | }) 67 | }) 68 | 69 | it('should remove age from user', () => { 70 | expect(remove(user, 'age')).toStrictEqual({ 71 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 72 | name: 'John Doe', 73 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 74 | email: 'john@doe.com' 75 | }) 76 | }) 77 | 78 | it('should remove id and age from user', () => { 79 | expect(remove(user, ['id', 'age'])).toStrictEqual({ 80 | name: 'John Doe', 81 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 82 | email: 'john@doe.com' 83 | }) 84 | }) 85 | 86 | it('should remove id from userNodes', () => { 87 | expect(remove(userNodes, 'id')).toStrictEqual({ 88 | name: 'John Doe', 89 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 90 | email: 'john@doe.com', 91 | age: 29, 92 | node: [ 93 | { 94 | name: 'John Doe', 95 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 96 | email: 'john@doe.com', 97 | age: 29, 98 | node: { 99 | name: 'John Doe', 100 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 101 | email: 'john@doe.com', 102 | age: 29, 103 | node: { 104 | name: 'John Doe', 105 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 106 | email: 'john@doe.com', 107 | age: 29 108 | } 109 | } 110 | }, 111 | { 112 | name: 'John Doe', 113 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 114 | email: 'john@doe.com', 115 | age: 29, 116 | node: { 117 | name: 'John Doe', 118 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 119 | email: 'john@doe.com', 120 | age: 29 121 | } 122 | }, 123 | { 124 | name: 'John Doe', 125 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 126 | email: 'john@doe.com', 127 | age: 29, 128 | node: { 129 | name: 'John Doe', 130 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 131 | email: 'john@doe.com', 132 | age: 29, 133 | node: { 134 | name: 'John Doe', 135 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 136 | email: 'john@doe.com', 137 | age: 29 138 | } 139 | } 140 | } 141 | ] 142 | }) 143 | }) 144 | 145 | it('should remove email, category and age from userNodes', () => { 146 | expect(remove(userNodes, ['email', 'category', 'age'])).toStrictEqual({ 147 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 148 | name: 'John Doe', 149 | node: [ 150 | { 151 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 152 | name: 'John Doe', 153 | node: { 154 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 155 | name: 'John Doe', 156 | node: { 157 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 158 | name: 'John Doe' 159 | } 160 | } 161 | }, 162 | { 163 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 164 | name: 'John Doe', 165 | node: { 166 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 167 | name: 'John Doe' 168 | } 169 | }, 170 | { 171 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 172 | name: 'John Doe', 173 | node: { 174 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 175 | name: 'John Doe', 176 | node: { 177 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 178 | name: 'John Doe' 179 | } 180 | } 181 | } 182 | ] 183 | }) 184 | }) 185 | }) 186 | -------------------------------------------------------------------------------- /src/replace/README.md: -------------------------------------------------------------------------------- 1 | # Replace 2 | 3 | ## Quickstart 4 | 5 | ### Replace an unique key 6 | 7 | ```ts 8 | import { replace } from 'json-object-keys' 9 | 10 | replace( 11 | { 12 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 13 | name: 'John Doe', 14 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 15 | email: 'john@doe.com', 16 | age: 29 17 | }, 18 | { 19 | id: 'uuid', 20 | } 21 | ) 22 | ``` 23 | 24 | Output 25 | 26 | ```diff 27 | { 28 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 29 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 30 | name: 'John Doe', 31 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 32 | email: 'john@doe.com', 33 | age: 29 34 | } 35 | ``` 36 | 37 | ```ts 38 | import { replace } from 'json-object-keys' 39 | 40 | replace( 41 | { 42 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 43 | name: 'John Doe', 44 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 45 | email: 'john@doe.com', 46 | age: 29, 47 | node: [ 48 | { 49 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 50 | name: 'John Doe', 51 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 52 | email: 'john@doe.com', 53 | age: 29, 54 | node: { 55 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 56 | name: 'John Doe', 57 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 58 | email: 'john@doe.com', 59 | age: 29, 60 | node: { 61 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 62 | name: 'John Doe', 63 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 64 | email: 'john@doe.com', 65 | age: 29 66 | } 67 | } 68 | }, 69 | { 70 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 71 | name: 'John Doe', 72 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 73 | email: 'john@doe.com', 74 | age: 29, 75 | node: { 76 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 77 | name: 'John Doe', 78 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 79 | email: 'john@doe.com', 80 | age: 29 81 | } 82 | }, 83 | { 84 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 85 | name: 'John Doe', 86 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 87 | email: 'john@doe.com', 88 | age: 29, 89 | node: { 90 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 91 | name: 'John Doe', 92 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 93 | email: 'john@doe.com', 94 | age: 29, 95 | node: { 96 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 97 | name: 'John Doe', 98 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 99 | email: 'john@doe.com', 100 | age: 29 101 | } 102 | } 103 | } 104 | ] 105 | }, 106 | { 107 | id: 'uuid', 108 | } 109 | ) 110 | ``` 111 | 112 | Output 113 | 114 | ```diff 115 | { 116 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 117 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 118 | name: 'John Doe', 119 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 120 | email: 'john@doe.com', 121 | age: 29, 122 | node: [ 123 | { 124 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 125 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 126 | name: 'John Doe', 127 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 128 | email: 'john@doe.com', 129 | age: 29, 130 | node: { 131 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 132 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 133 | name: 'John Doe', 134 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 135 | email: 'john@doe.com', 136 | age: 29, 137 | node: { 138 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 139 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 140 | name: 'John Doe', 141 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 142 | email: 'john@doe.com', 143 | age: 29 144 | } 145 | } 146 | }, 147 | { 148 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 149 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 150 | name: 'John Doe', 151 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 152 | email: 'john@doe.com', 153 | age: 29, 154 | node: { 155 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 156 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 157 | name: 'John Doe', 158 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 159 | email: 'john@doe.com', 160 | age: 29 161 | } 162 | }, 163 | { 164 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 165 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 166 | name: 'John Doe', 167 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 168 | email: 'john@doe.com', 169 | age: 29, 170 | node: { 171 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 172 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 173 | name: 'John Doe', 174 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 175 | email: 'john@doe.com', 176 | age: 29, 177 | node: { 178 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 179 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 180 | name: 'John Doe', 181 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 182 | email: 'john@doe.com', 183 | age: 29 184 | } 185 | } 186 | } 187 | ] 188 | } 189 | ``` 190 | 191 | ### Replace multiple keys 192 | 193 | ```ts 194 | import { replace } from 'json-object-keys' 195 | 196 | replace( 197 | { 198 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 199 | name: 'John Doe', 200 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 201 | email: 'john@doe.com', 202 | age: 29 203 | }, 204 | { 205 | id: 'uuid', 206 | category: 'tag', 207 | } 208 | ) 209 | ``` 210 | 211 | Output 212 | 213 | ```diff 214 | { 215 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 216 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 217 | name: 'John Doe', 218 | - category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 219 | + tag: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 220 | email: 'john@doe.com', 221 | age: 29 222 | } 223 | ``` 224 | 225 | ```ts 226 | import { replace } from 'json-object-keys' 227 | 228 | replace( 229 | { 230 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 231 | name: 'John Doe', 232 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 233 | email: 'john@doe.com', 234 | age: 29, 235 | node: [ 236 | { 237 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 238 | name: 'John Doe', 239 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 240 | email: 'john@doe.com', 241 | age: 29, 242 | node: { 243 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 244 | name: 'John Doe', 245 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 246 | email: 'john@doe.com', 247 | age: 29, 248 | node: { 249 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 250 | name: 'John Doe', 251 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 252 | email: 'john@doe.com', 253 | age: 29 254 | } 255 | } 256 | }, 257 | { 258 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 259 | name: 'John Doe', 260 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 261 | email: 'john@doe.com', 262 | age: 29, 263 | node: { 264 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 265 | name: 'John Doe', 266 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 267 | email: 'john@doe.com', 268 | age: 29 269 | } 270 | }, 271 | { 272 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 273 | name: 'John Doe', 274 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 275 | email: 'john@doe.com', 276 | age: 29, 277 | node: { 278 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 279 | name: 'John Doe', 280 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 281 | email: 'john@doe.com', 282 | age: 29, 283 | node: { 284 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 285 | name: 'John Doe', 286 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 287 | email: 'john@doe.com', 288 | age: 29 289 | } 290 | } 291 | } 292 | ] 293 | }, 294 | { 295 | id: 'uuid', 296 | node: 'children', 297 | name: 'fullName' 298 | } 299 | ) 300 | ``` 301 | 302 | Output 303 | 304 | ```diff 305 | { 306 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 307 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 308 | - name: 'John Doe', 309 | + fullName: 'John Doe', 310 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 311 | email: 'john@doe.com', 312 | age: 29, 313 | - node: [ 314 | + children: [ 315 | { 316 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 317 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 318 | - name: 'John Doe', 319 | + fullName: 'John Doe', 320 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 321 | email: 'john@doe.com', 322 | age: 29, 323 | - node: { 324 | + children: { 325 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 326 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 327 | - name: 'John Doe', 328 | + fullName: 'John Doe', 329 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 330 | email: 'john@doe.com', 331 | age: 29, 332 | - node: { 333 | + children: { 334 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 335 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 336 | - name: 'John Doe', 337 | + fullName: 'John Doe', 338 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 339 | email: 'john@doe.com', 340 | age: 29 341 | } 342 | } 343 | }, 344 | { 345 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 346 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 347 | - name: 'John Doe', 348 | + fullName: 'John Doe', 349 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 350 | email: 'john@doe.com', 351 | age: 29, 352 | - node: { 353 | + children: { 354 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 355 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 356 | - name: 'John Doe', 357 | + fullName: 'John Doe', 358 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 359 | email: 'john@doe.com', 360 | age: 29 361 | } 362 | }, 363 | { 364 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 365 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 366 | - name: 'John Doe', 367 | + fullName: 'John Doe', 368 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 369 | email: 'john@doe.com', 370 | age: 29, 371 | - node: { 372 | + children: { 373 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 374 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 375 | - name: 'John Doe', 376 | + fullName: 'John Doe', 377 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 378 | email: 'john@doe.com', 379 | age: 29, 380 | - node: { 381 | + children: { 382 | - id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 383 | + uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 384 | - name: 'John Doe', 385 | + fullName: 'John Doe', 386 | category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 387 | email: 'john@doe.com', 388 | age: 29 389 | } 390 | } 391 | } 392 | ] 393 | } 394 | ``` 395 | -------------------------------------------------------------------------------- /src/replace/index.ts: -------------------------------------------------------------------------------- 1 | type JSObject = { 2 | [key: string | number | symbol]: unknown 3 | } 4 | 5 | type Replace> = { 6 | [K in keyof O as K extends keyof R ? NonNullable : K]: O[K] 7 | } 8 | 9 | /** 10 | * Replace a key or multiple keys from object 11 | * @param object - An object 12 | * @param replacement - An object of replacement keys 13 | * @returns A new object replaced with the given keys 14 | */ 15 | export function replace>( 16 | object: O, 17 | replacement: R 18 | ): Replace { 19 | const regex = new RegExp(`(${Object.keys(replacement).join('|')})`, 'g') 20 | const stringify = JSON.stringify(object).replace(regex, (match) => replacement[match] ?? match) 21 | return JSON.parse(stringify) 22 | } 23 | -------------------------------------------------------------------------------- /src/replace/replace.test.ts: -------------------------------------------------------------------------------- 1 | import { expect, it, describe } from 'bun:test' 2 | import { replace } from '.' 3 | 4 | describe('replace', () => { 5 | it('should replace foo with bar', () => { 6 | expect(replace({ id: 1, foo: 2, baz: 3 }, { foo: 'bar' })).toStrictEqual({ id: 1, bar: 2, baz: 3 }) 7 | }) 8 | 9 | it('should replace a complex object', () => { 10 | expect( 11 | replace( 12 | { 13 | uuid: 'a78e3a45-3b3c-4f5d-85d6-9e6e1b2dcb45', 14 | name: 'Large Object', 15 | children: [ 16 | { 17 | uuid: 'c828d77d-aa2c-4af6-a4a6-4d4bbd7f8bf5', 18 | name: 'Child 1', 19 | children: [ 20 | { 21 | uuid: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 22 | name: 'Grandchild 1', 23 | children: [ 24 | { 25 | uuid: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 26 | name: 'Great Grandchild 1' 27 | }, 28 | { 29 | uuid: 'c579a0b8-c2c7-44e2-a7d9-2edba8f7b472', 30 | name: 'Great Grandchild 2' 31 | } 32 | ] 33 | }, 34 | { 35 | uuid: 'a3a7aee0-bfe6-47f6-8d90-a7e2c2fc1e35', 36 | name: 'Grandchild 2' 37 | } 38 | ] 39 | }, 40 | { 41 | uuid: '3b0f3ea3-af52-4f03-9f54-4a4a7f2f299a', 42 | name: 'Child 2', 43 | children: [ 44 | { 45 | uuid: '6a45df6a-54d7-4abf-9db9-9fc2fdff6f79', 46 | name: 'Grandchild 3', 47 | children: [ 48 | { 49 | uuid: '417f6e08-a0d3-405f-8b63-2fa701d9717b', 50 | name: 'Great Grandchild 3' 51 | }, 52 | { 53 | uuid: '25a6a035-c95e-4df7-a92d-40f59c9f6ff2', 54 | name: 'Great Grandchild 4' 55 | } 56 | ] 57 | }, 58 | { 59 | uuid: 'f4e4f54d-4d4b-4fb7-bc51-d097d08f0a78', 60 | name: 'Grandchild 4' 61 | } 62 | ] 63 | } 64 | ] 65 | }, 66 | { uuid: 'id', children: 'node' } 67 | ) 68 | ).toStrictEqual({ 69 | id: 'a78e3a45-3b3c-4f5d-85d6-9e6e1b2dcb45', 70 | name: 'Large Object', 71 | node: [ 72 | { 73 | id: 'c828d77d-aa2c-4af6-a4a6-4d4bbd7f8bf5', 74 | name: 'Child 1', 75 | node: [ 76 | { 77 | id: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2', 78 | name: 'Grandchild 1', 79 | node: [ 80 | { 81 | id: 'e059d01a-7082-4b63-9c70-997491cdcf7c', 82 | name: 'Great Grandchild 1' 83 | }, 84 | { 85 | id: 'c579a0b8-c2c7-44e2-a7d9-2edba8f7b472', 86 | name: 'Great Grandchild 2' 87 | } 88 | ] 89 | }, 90 | { 91 | id: 'a3a7aee0-bfe6-47f6-8d90-a7e2c2fc1e35', 92 | name: 'Grandchild 2' 93 | } 94 | ] 95 | }, 96 | { 97 | id: '3b0f3ea3-af52-4f03-9f54-4a4a7f2f299a', 98 | name: 'Child 2', 99 | node: [ 100 | { 101 | id: '6a45df6a-54d7-4abf-9db9-9fc2fdff6f79', 102 | name: 'Grandchild 3', 103 | node: [ 104 | { 105 | id: '417f6e08-a0d3-405f-8b63-2fa701d9717b', 106 | name: 'Great Grandchild 3' 107 | }, 108 | { 109 | id: '25a6a035-c95e-4df7-a92d-40f59c9f6ff2', 110 | name: 'Great Grandchild 4' 111 | } 112 | ] 113 | }, 114 | { 115 | id: 'f4e4f54d-4d4b-4fb7-bc51-d097d08f0a78', 116 | name: 'Grandchild 4' 117 | } 118 | ] 119 | } 120 | ] 121 | }) 122 | }) 123 | }) 124 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "esnext", 4 | "module": "esnext", 5 | "rootDir": "src", 6 | "baseUrl": "src", 7 | "allowJs": true, 8 | "esModuleInterop": true, 9 | "forceConsistentCasingInFileNames": true, 10 | "strict": true, 11 | "moduleResolution": "bundler", 12 | "strictNullChecks": true, 13 | "isolatedModules": true, 14 | "noEmit": true, 15 | "allowImportingTsExtensions": true, 16 | "moduleDetection": "force", 17 | "types": ["bun-types"], 18 | }, 19 | "include": ["src/**/*.ts"], 20 | "exclude": ["node_modules", "build"] 21 | } 22 | -------------------------------------------------------------------------------- /tsconfig.types.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "esnext", 4 | "module": "esnext", 5 | "rootDir": "src", 6 | "baseUrl": "src", 7 | "declaration": true, 8 | "declarationDir": "build/types", 9 | "esModuleInterop": true, 10 | "forceConsistentCasingInFileNames": true, 11 | "strict": true, 12 | "moduleResolution": "bundler", 13 | "isolatedModules": true, 14 | "noEmit": false, 15 | "moduleDetection": "force", 16 | }, 17 | "include": ["src/**/*.ts"], 18 | "exclude": ["node_modules", "build", "src/**/*.test.ts"] 19 | } 20 | --------------------------------------------------------------------------------