Skip to content

Commit

Permalink
feat(hooks): 新添加useOmit钩子
Browse files Browse the repository at this point in the history
新添加`useOmit`钩子,实现在对象中删除指定属性并返回剩余属性的功能。更新包括文档、示例和实现代码。
  • Loading branch information
Null committed Sep 4, 2024
1 parent b158567 commit 36ff0e4
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 42 deletions.
27 changes: 14 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
## 1.0.0 (2024-09-04)

* chore: update changelog ([b12d9fe](https://github.com/zjydipingxian/hooks-encode/commit/b12d9fe))
* chore: update changelog ([1dddd5f](https://github.com/zjydipingxian/hooks-encode/commit/1dddd5f))
* chore: update changelog ([5e540cd](https://github.com/zjydipingxian/hooks-encode/commit/5e540cd))
* chore: update changelog ([8a54fec](https://github.com/zjydipingxian/hooks-encode/commit/8a54fec))
* chore: update changelog ([217c8c1](https://github.com/zjydipingxian/hooks-encode/commit/217c8c1))
* chore: update changelog ([e8e8ae2](https://github.com/zjydipingxian/hooks-encode/commit/e8e8ae2))
* chore: update changelog ([3a0256d](https://github.com/zjydipingxian/hooks-encode/commit/3a0256d))
* chore: update changelog ([a574a8a](https://github.com/zjydipingxian/hooks-encode/commit/a574a8a))
* chore: update changelog ([b719a22](https://github.com/zjydipingxian/hooks-encode/commit/b719a22))
* chore: update changelog ([ce2dd13](https://github.com/zjydipingxian/hooks-encode/commit/ce2dd13))
* chore(hooks): 1.3.0 版本发布并添加新 Hooks ([02efbe7](https://github.com/zjydipingxian/hooks-encode/commit/02efbe7))
* chore(packages): 更新变更日志并升级hooks包版本 ([66ec3d0](https://github.com/zjydipingxian/hooks-encode/commit/66ec3d0))
* chore(release): 优化发布脚本并更新包json ([ac20ebf](https://github.com/zjydipingxian/hooks-encode/commit/ac20ebf))
* feat: 发布`zhongjiayao_v3_hooks``1.0.0`版本,设置访问级别为public并更新内部依赖性。移除直接发布命令,使用changeset进行发布 ([6ed8ddf](https://github.com/zjydipingxian/hooks-encode/commit/6ed8ddf))
* feat: 优化代码结构并增强功能 ([1f988d0](https://github.com/zjydipingxian/hooks-encode/commit/1f988d0))
* feat: 重命名watch.js为cli/watch.mjs并更新相关引用 ([900b9fe](https://github.com/zjydipingxian/hooks-encode/commit/900b9fe))
Expand All @@ -27,6 +14,7 @@
* feat(hooks): 添加 usePermission, useMounted 和 useSupported 钩子 ([e42dda9](https://github.com/zjydipingxian/hooks-encode/commit/e42dda9))
* feat(hooks): 添加 useResizeObserver 钩子以监听元素尺寸变化 ([9d71b99](https://github.com/zjydipingxian/hooks-encode/commit/9d71b99))
* feat(hooks): 添加 useWindowSize 钩子并更新相关路由 ([1166b6b](https://github.com/zjydipingxian/hooks-encode/commit/1166b6b))
* feat(hooks): 添加usePick钩子 ([b158567](https://github.com/zjydipingxian/hooks-encode/commit/b158567))
* feat(hooks): 新增useClipboard钩子以支持剪贴板操作 ([ec0fe1a](https://github.com/zjydipingxian/hooks-encode/commit/ec0fe1a))
* feat(hooks): hooks包版本更新至0.0.8 ([7f1ccc7](https://github.com/zjydipingxian/hooks-encode/commit/7f1ccc7))
* feat(husky): 提交钩子中的 changelog 生成命令 ([0950815](https://github.com/zjydipingxian/hooks-encode/commit/0950815))
Expand All @@ -42,6 +30,19 @@
* feat(useTitle): 添加 useTitle 钩子并进行文档更新 ([199f50e](https://github.com/zjydipingxian/hooks-encode/commit/199f50e))
* feat(useVirtualList): 添加虚拟列表钩子以处理大型数据集 ([7292b7c](https://github.com/zjydipingxian/hooks-encode/commit/7292b7c))
* feat(zhongjiayao_v3_hooks): 新增 useOnline Hook 以检查浏览器在线状态 ([37af4bc](https://github.com/zjydipingxian/hooks-encode/commit/37af4bc))
* chore: update changelog ([b12d9fe](https://github.com/zjydipingxian/hooks-encode/commit/b12d9fe))
* chore: update changelog ([1dddd5f](https://github.com/zjydipingxian/hooks-encode/commit/1dddd5f))
* chore: update changelog ([5e540cd](https://github.com/zjydipingxian/hooks-encode/commit/5e540cd))
* chore: update changelog ([8a54fec](https://github.com/zjydipingxian/hooks-encode/commit/8a54fec))
* chore: update changelog ([217c8c1](https://github.com/zjydipingxian/hooks-encode/commit/217c8c1))
* chore: update changelog ([e8e8ae2](https://github.com/zjydipingxian/hooks-encode/commit/e8e8ae2))
* chore: update changelog ([3a0256d](https://github.com/zjydipingxian/hooks-encode/commit/3a0256d))
* chore: update changelog ([a574a8a](https://github.com/zjydipingxian/hooks-encode/commit/a574a8a))
* chore: update changelog ([b719a22](https://github.com/zjydipingxian/hooks-encode/commit/b719a22))
* chore: update changelog ([ce2dd13](https://github.com/zjydipingxian/hooks-encode/commit/ce2dd13))
* chore(hooks): 1.3.0 版本发布并添加新 Hooks ([02efbe7](https://github.com/zjydipingxian/hooks-encode/commit/02efbe7))
* chore(packages): 更新变更日志并升级hooks包版本 ([66ec3d0](https://github.com/zjydipingxian/hooks-encode/commit/66ec3d0))
* chore(release): 优化发布脚本并更新包json ([ac20ebf](https://github.com/zjydipingxian/hooks-encode/commit/ac20ebf))
* test(hooks): 添加对useBoolean和useToggle和useTitle的单元测试 ([8951ad9](https://github.com/zjydipingxian/hooks-encode/commit/8951ad9))
* test(hooks): 添加useDebounce和useThrottle的测试 ([818815c](https://github.com/zjydipingxian/hooks-encode/commit/818815c))
* test(hooks): 添加useDebounce和useThrottle的测试 ([2d5539a](https://github.com/zjydipingxian/hooks-encode/commit/2d5539a))
Expand Down
34 changes: 5 additions & 29 deletions docs/.vitepress/generate-router.js
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;
19 changes: 19 additions & 0 deletions docs/useOmit/demo/index.vue
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>
9 changes: 9 additions & 0 deletions docs/useOmit/index.md
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/) 一致
2 changes: 2 additions & 0 deletions docs/usePick/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# usePick

通俗的说法就是:在已经定义的对象中选取一些

## 基本使用

<preview path="./demo/index.vue" title="基本使用" description='usePick'></preview>
Expand Down
2 changes: 2 additions & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import usePermission from './usePermission';
import useMounted from './useMounted';
import useSupported from './useSupported';
import usePick from './usePick';
import useOmit from './useOmit';
export {
useBoolean,
useToggle,
Expand Down Expand Up @@ -51,4 +52,5 @@ export {
useMounted,
useSupported,
usePick,
useOmit,
};
19 changes: 19 additions & 0 deletions packages/hooks/src/useOmit/demo/index.vue
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>
9 changes: 9 additions & 0 deletions packages/hooks/src/useOmit/index.md
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/) 一致
32 changes: 32 additions & 0 deletions packages/hooks/src/useOmit/index.ts
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;
3 changes: 3 additions & 0 deletions packages/hooks/src/useOmit/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "Worker"
}
2 changes: 2 additions & 0 deletions packages/hooks/src/usePick/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# usePick

通俗的说法就是:在已经定义的对象中选取一些

## 基本使用

<preview path="./demo/index.vue" title="基本使用" description='usePick'></preview>
Expand Down

0 comments on commit 36ff0e4

Please sign in to comment.