Skip to content

Commit

Permalink
Release 1.0.21
Browse files Browse the repository at this point in the history
  • Loading branch information
mym0404 committed Feb 21, 2024
1 parent 78765dc commit 2e3bf6b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 16 additions & 0 deletions src/internal/removeValueByKeyInObject.test.ts
Original file line number Diff line number Diff line change
@@ -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: {},
});
});
26 changes: 26 additions & 0 deletions src/internal/removeValueByKeyInObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import is from './is';

export function removeValueByKeyInObject<T extends Record<string | number, any>>(
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;
}

0 comments on commit 2e3bf6b

Please sign in to comment.