feat: omit function

This commit is contained in:
Rewrite0
2023-08-10 12:13:04 +08:00
parent 9e03735074
commit 74f8dcdf83
2 changed files with 34 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
import { expect, it } from 'vitest';
import { omit } from './omit';
it('test omit', () => {
const obj = {
a: 1,
b: 2,
c: 3,
d: 4,
};
expect(omit(obj, ['a'])).toStrictEqual({
b: 2,
c: 3,
d: 4,
});
expect(omit(obj, ['b', 'c'])).toStrictEqual({
a: 1,
d: 4,
});
});

12
webui/src/utils/omit.ts Normal file
View File

@@ -0,0 +1,12 @@
export function omit<T extends { [k: string]: any }>(
obj: T,
omitKeys: Array<keyof T>
) {
return Object.keys(obj).reduce((acc, key) => {
if (omitKeys.includes(key)) {
return acc;
} else {
return { ...acc, [key]: obj[key] };
}
}, {});
}