From 2e3bf6b32461931b58dcf3e0d129a26fd424ee61 Mon Sep 17 00:00:00 2001 From: mym0404 Date: Thu, 22 Feb 2024 00:07:06 +0900 Subject: [PATCH] Release 1.0.21 --- README.md | 1 + package.json | 2 +- src/internal/removeValueByKeyInObject.test.ts | 16 ++++++++++++ src/internal/removeValueByKeyInObject.ts | 26 +++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/internal/removeValueByKeyInObject.test.ts create mode 100644 src/internal/removeValueByKeyInObject.ts diff --git a/README.md b/README.md index eac45a7..28902b1 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ npm install @mj-studio/js-util * `groupByArray` : group object as arrays with key a provider callback * `groupByObject` : group object as objects with key a provider callback * `doBatch` : with list, do something with batched manner and return results of callback as a list +* And.. other things! #### Promise helper * `withTimeout` : set max running time of promise, if exceeds it will reject. diff --git a/package.json b/package.json index 00eed05..af37ef0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mj-studio/js-util", - "version": "1.0.19", + "version": "1.0.21", "description": "Custom JavaScript Utilities for MJ Studio", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/internal/removeValueByKeyInObject.test.ts b/src/internal/removeValueByKeyInObject.test.ts new file mode 100644 index 0000000..423e8f4 --- /dev/null +++ b/src/internal/removeValueByKeyInObject.test.ts @@ -0,0 +1,16 @@ +import { removeValueByKeyInObject } from './removeValueByKeyInObject'; + +it('simple case', () => { + const ret = removeValueByKeyInObject({ a: 1, b: 'string', c: {} }, 'c'); + expect(ret).toEqual({ + b: 'string', + a: 1, + }); +}); + +it('complex case', () => { + const ret = removeValueByKeyInObject({ a: 1, b: 'string', c: {} }, ['a', 'b']); + expect(ret).toEqual({ + c: {}, + }); +}); diff --git a/src/internal/removeValueByKeyInObject.ts b/src/internal/removeValueByKeyInObject.ts new file mode 100644 index 0000000..f9225a3 --- /dev/null +++ b/src/internal/removeValueByKeyInObject.ts @@ -0,0 +1,26 @@ +import is from './is'; + +export function removeValueByKeyInObject>( + v: T, + key: (string | number) | (string | number)[], +): T { + if (!is.object(v)) { + return v; + } + + const ret = {}; + + Object.entries(v).forEach(([k, value]) => { + if (is.array(key)) { + if (!key.includes(k)) { + ret[k] = value; + } + } else { + if (k !== key) { + ret[k] = value; + } + } + }); + + return ret as T; +}