-
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.
新添加`useOmit`钩子,实现在对象中删除指定属性并返回剩余属性的功能。更新包括文档、示例和实现代码。
- Loading branch information
Null
committed
Sep 4, 2024
1 parent
b158567
commit 36ff0e4
Showing
11 changed files
with
116 additions
and
42 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
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,31 +1,7 @@ | ||
const Router = { | ||
Dom: [ | ||
'useClickAway', | ||
'useClipboard', | ||
'useDocumentVisibility', | ||
'useElementBounding', | ||
'useEventListener', | ||
'useFullscreen', | ||
'useHover', | ||
'usePermission', | ||
'useRect', | ||
'useResizeObserver', | ||
'useTitle', | ||
'useVirtualList', | ||
'useWindowSize', | ||
], | ||
State: [ | ||
'useBoolean', | ||
'useDebounce', | ||
'useLocalStorage', | ||
'useMounted', | ||
'useOnline', | ||
'useSessionStorage', | ||
'useSupported', | ||
'useThrottle', | ||
'useToggle', | ||
], | ||
Worker: ['useCountDown', 'usePick'], | ||
Effect: ['useDebounceFn', 'useThrottleFn'], | ||
Dom: ['useClickAway', 'useClipboard', 'useDocumentVisibility', 'useElementBounding', 'useEventListener', 'useFullscreen', 'useHover', 'usePermission', 'useRect', 'useResizeObserver', 'useTitle', 'useVirtualList', 'useWindowSize'], | ||
State: ['useBoolean', 'useDebounce', 'useLocalStorage', 'useMounted', 'useOnline', 'useSessionStorage', 'useSupported', 'useThrottle', 'useToggle'], | ||
Worker: ['useCountDown', "useOmit", 'usePick'], | ||
Effect: ['useDebounceFn', 'useThrottleFn'] | ||
}; | ||
export default Router; | ||
export default Router; |
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,19 @@ | ||
<template> | ||
<div>{{ userWithoutIdAndEmail }}</div> | ||
</template> | ||
|
||
<script setup> | ||
import { useOmit } from 'zhongjiayao_v3_hooks'; | ||
// 示例用法 | ||
const user = { | ||
id: 1, | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
age: 30, | ||
}; | ||
const omittedKeys = ['id', 'email']; | ||
const userWithoutIdAndEmail = useOmit(user, omittedKeys); | ||
</script> |
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,9 @@ | ||
# useOmit | ||
|
||
在已经定义的对象中删除一些,留下剩下的 `omit<T,keys>` | ||
|
||
<preview path="./demo/index.vue" title="基本使用" description='useOmit'></preview> | ||
|
||
## API | ||
|
||
使用方法和 [usePick](/usePick/) 一致 |
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
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,19 @@ | ||
<template> | ||
<div>{{ userWithoutIdAndEmail }}</div> | ||
</template> | ||
|
||
<script setup> | ||
import { useOmit } from 'zhongjiayao_v3_hooks'; | ||
// 示例用法 | ||
const user = { | ||
id: 1, | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
age: 30, | ||
}; | ||
const omittedKeys = ['id', 'email']; | ||
const userWithoutIdAndEmail = useOmit(user, omittedKeys); | ||
</script> |
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,9 @@ | ||
# useOmit | ||
|
||
在已经定义的对象中删除一些,留下剩下的 `omit<T,keys>` | ||
|
||
<preview path="./demo/index.vue" title="基本使用" description='useOmit'></preview> | ||
|
||
## API | ||
|
||
使用方法和 [usePick](/usePick/) 一致 |
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,32 @@ | ||
import { Ref, unref } from 'vue'; | ||
|
||
function useOmit<T extends object, K extends keyof T>(obj: Ref<T> | T, keys: K[] | string): Omit<T, K> { | ||
const result: Partial<T> = {}; | ||
|
||
let keySet: Set<K>; | ||
|
||
if (typeof keys === 'string') { | ||
// 将字符串转换为 Set 并进行类型验证 | ||
keySet = new Set(keys.split(',').map((key) => key.trim() as K)); | ||
} else { | ||
keySet = new Set(keys); | ||
} | ||
|
||
// 使用 unref 处理 obj | ||
const unrefOmit = unref(obj); | ||
|
||
// 检查 obj 是否为对象 | ||
if (typeof unrefOmit !== 'object' || unrefOmit === null) { | ||
throw new Error('The provided object is not valid.'); | ||
} | ||
|
||
// 使用 Set 进行高效查找 | ||
for (const key in unrefOmit) { | ||
if (!keySet.has(key as unknown as K)) { | ||
result[key] = unrefOmit[key]; | ||
} | ||
} | ||
|
||
return result as Omit<T, K>; | ||
} | ||
export default useOmit; |
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,3 @@ | ||
{ | ||
"type": "Worker" | ||
} |
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