-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
58 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# omit | ||
|
||
删除对象指定属性。 | ||
|
||
> 注意:此方法不会改变源对象。 | ||
## 基本用法 | ||
|
||
传入一个对象和属性名数组,返回删除属性后的对象。 | ||
|
||
```ts | ||
import { omit } from '@renzp/utils' | ||
|
||
const a = { a: 1, b: 2 }; | ||
const c = omit(a, "a"); // c = { b: 2 } a = { a: 1, b: 2 } | ||
``` | ||
|
||
## 参数 | ||
|
||
|
||
| 参数 | 说明 | 类型 | 默认值 | 是否必填 | | ||
| ------ | ---------- | ------------------ | ------ | -------- | | ||
| target | 源对象 | `T` | - | 是 | | ||
| keys | 属性名数组 | `Array<K keyof T>` | - | 是 | | ||
|
||
|
||
## 返回 | ||
|
||
| 参数 | 说明 | 类型 | | ||
| ------ | ------------------ | ------------ | | ||
| target | 删除指定属性的对象 | `Omit<T, K>` | |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './merge' | ||
export * from './omit' | ||
export * from './pick' | ||
export * from './removeKey' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { _po } from '../_base' | ||
import type { ExpandRecursively } from '../types' | ||
|
||
/** | ||
* 删除对象指定属性 | ||
* | ||
* 此方法不会改变源对象 | ||
* @param target 源对象 | ||
* @param keys 属性数组 | ||
* @returns 如果源对象是对象则返回删除属性后的对象,否则原样返回 | ||
* | ||
* @example | ||
* const a = { a: 1, b: 2 }; | ||
* const c = omit(a, "a"); // c = { b: 2 } a = { a: 1, b: 2 } | ||
*/ | ||
export const omit = <T extends Record<PropertyKey, any>, K extends keyof T>( | ||
target: T, | ||
keys: Array<K>, | ||
): ExpandRecursively<Omit<T, K>> => { | ||
return _po(target, keys, 'omit') as unknown as ExpandRecursively<Omit<T, K>> | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { expect, test } from 'bun:test' | ||
import { removeKey } from '../../src' | ||
import { omit } from '../../src' | ||
|
||
test('移除对象属性', () => { | ||
const a = { a: 1, b: 2 } | ||
const b = removeKey(a, 'a') | ||
const b = omit(a, ['a']) | ||
expect(a).toEqual({ a: 1, b: 2 }) | ||
expect(b).toEqual({ b: 2 }) | ||
}) | ||
|
||
test('传入的不是对象', () => { | ||
expect(removeKey([], 'a' as any)).toEqual([]) | ||
expect(omit([], 'a' as any)).toEqual([]) | ||
}) |