Skip to content

Commit

Permalink
feat(utils): add vue utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kirklin committed Aug 14, 2023
1 parent 48f801e commit e07ad7d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/web/utils/src/vue/createInjectionKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { InjectionKey } from "vue";

/**
* Creates an injection key for use with the provide/inject API.
* 创建一个用于 provide/inject API 的注入键。
*
* @template T - The type of value associated with the injection key.
* 与注入键关联的值的类型。
* @param {string} key - The unique identifier for the injection key.
* 用于标识注入键的唯一标识符。
* @returns {InjectionKey<T>} Returns the injection key.
* 返回注入键。
*/
export function createInjectionKey<T>(key: string): InjectionKey<T> {
return key as any;
}
54 changes: 54 additions & 0 deletions packages/web/utils/src/vue/flattenVNodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
Comment,
Fragment,
type VNode,
type VNodeChild,
createTextVNode,
} from "vue";

/**
* Flattens an array of VNode children.
* 将 VNode 子数组展平。
*
* @param {VNodeChild[]} vNodes - The array of VNode children to flatten.
* 要展平的 VNode 子数组。
* @param {boolean} filterCommentNode - Whether to filter out comment nodes. Default is true.
* 是否过滤掉注释节点。默认为 true。
* @param {VNode[]} result - The array to store the flattened VNodes.
* 用于存储展平后的 VNodes 的数组。
* @returns {VNode[]} Returns an array of flattened VNodes.
* 返回展平后的 VNode 数组。
*/
export function flattenVNodes(
vNodes: VNodeChild[],
filterCommentNode = true,
result: VNode[] = [],
): VNode[] {
vNodes.forEach((vNode) => {
if (vNode === null) {
return;
}
if (typeof vNode !== "object") {
if (typeof vNode === "string" || typeof vNode === "number") {
result.push(createTextVNode(String(vNode)));
}
return;
}
if (Array.isArray(vNode)) {
flattenVNodes(vNode, filterCommentNode, result);
return;
}
if (vNode.type === Fragment) {
if (vNode.children === null) {
return;
}
if (Array.isArray(vNode.children)) {
flattenVNodes(vNode.children, filterCommentNode, result);
}
// rawSlot
} else if (vNode.type !== Comment) {
result.push(vNode);
}
});
return result;
}
2 changes: 2 additions & 0 deletions packages/web/utils/src/vue/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./createInjectionKey";
export * from "./flattenVNodes";
export * from "./install";

2 comments on commit e07ad7d

@vercel
Copy link

@vercel vercel bot commented on e07ad7d Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

celeris-web – ./apps/admin

celeris-web.vercel.app
celeris-web-git-master-kirklin.vercel.app
celeris-web-kirklin.vercel.app

@vercel
Copy link

@vercel vercel bot commented on e07ad7d Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

celeris-web-api – ./services/admin

celeris-web-api.vercel.app
celeris-web-api-kirklin.vercel.app
celeris-web-api-git-master-kirklin.vercel.app

Please sign in to comment.