Skip to content

Commit

Permalink
feat(object): 将removeKey更名为omit
Browse files Browse the repository at this point in the history
  • Loading branch information
renzp94 committed Jun 11, 2024
1 parent 9f664bf commit c0d0cd7
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 63 deletions.
31 changes: 31 additions & 0 deletions docs/pages/object/omit.md
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>` |
30 changes: 0 additions & 30 deletions docs/pages/object/removeKey.md

This file was deleted.

4 changes: 2 additions & 2 deletions src/array/flatten.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isArray, isUnDef } from '../is'
import { removeKey } from '../object'
import { omit } from '../object'

export interface FlattenOptions {
// 如果是对象数组,指定深度扁平化的属性
Expand Down Expand Up @@ -61,7 +61,7 @@ export const flatten: Flatten = (list, options) => {
(prev: Parameters<Flatten>[0], curr: Parameters<Flatten>[0][0]) => {
let values = isArray(curr) ? curr : [curr]
if (isCanDeep) {
const target: Parameters<Flatten>[0][0] = removeKey(curr, deepKey)
const target: Parameters<Flatten>[0][0] = omit(curr, [deepKey])
values = [target, ...(curr[deepKey] ? _flatten(curr[deepKey]) : [])]
}

Expand Down
2 changes: 1 addition & 1 deletion src/object/index.ts
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'
21 changes: 21 additions & 0 deletions src/object/omit.ts
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>>
}
27 changes: 0 additions & 27 deletions src/object/removeKey.ts

This file was deleted.

6 changes: 3 additions & 3 deletions test/object/removeKey.test.ts → test/object/omit.test.ts
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([])
})

0 comments on commit c0d0cd7

Please sign in to comment.