@pawover/kit 0.0.0-alpha.3 → 0.0.0-alpha.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/enums.d.ts +25 -0
  2. package/dist/enums.d.ts.map +1 -0
  3. package/dist/enums.js +25 -0
  4. package/dist/enums.js.map +1 -0
  5. package/dist/hooks-alova.d.ts +23 -0
  6. package/dist/hooks-alova.d.ts.map +1 -0
  7. package/dist/hooks-alova.js +39 -0
  8. package/dist/hooks-alova.js.map +1 -0
  9. package/dist/hooks-react.d.ts +89 -0
  10. package/dist/hooks-react.d.ts.map +1 -0
  11. package/dist/hooks-react.js +334 -0
  12. package/dist/hooks-react.js.map +1 -0
  13. package/dist/index.d.ts +2388 -0
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +1361 -2
  16. package/dist/index.js.map +1 -0
  17. package/dist/patches-fetchEventSource.d.ts +806 -0
  18. package/dist/patches-fetchEventSource.d.ts.map +1 -0
  19. package/dist/patches-fetchEventSource.js +313 -0
  20. package/dist/patches-fetchEventSource.js.map +1 -0
  21. package/dist/vite.d.ts +13 -0
  22. package/dist/vite.d.ts.map +1 -0
  23. package/dist/vite.js +22 -0
  24. package/dist/vite.js.map +1 -0
  25. package/dist/zod.d.ts +105 -0
  26. package/dist/zod.d.ts.map +1 -0
  27. package/dist/zod.js +138 -0
  28. package/dist/zod.js.map +1 -0
  29. package/metadata.json +157 -0
  30. package/package.json +57 -35
  31. package/dist/enums/index.js +0 -20
  32. package/dist/hooks/react/index.js +0 -5
  33. package/dist/hooks/react/useCreation.js +0 -11
  34. package/dist/hooks/react/useLatest.js +0 -6
  35. package/dist/hooks/react/useMount.js +0 -29
  36. package/dist/hooks/react/useResponsive.js +0 -59
  37. package/dist/hooks/react/useUnmount.js +0 -17
  38. package/dist/types/enums/index.d.ts +0 -20
  39. package/dist/types/hooks/react/index.d.ts +0 -5
  40. package/dist/types/hooks/react/useCreation.d.ts +0 -2
  41. package/dist/types/hooks/react/useLatest.d.ts +0 -1
  42. package/dist/types/hooks/react/useMount.d.ts +0 -11
  43. package/dist/types/hooks/react/useResponsive.d.ts +0 -16
  44. package/dist/types/hooks/react/useUnmount.d.ts +0 -6
  45. package/dist/types/index.d.ts +0 -2
  46. package/dist/types/utils/array.d.ts +0 -76
  47. package/dist/types/utils/index.d.ts +0 -6
  48. package/dist/types/utils/object.d.ts +0 -53
  49. package/dist/types/utils/string.d.ts +0 -13
  50. package/dist/types/utils/to.d.ts +0 -5
  51. package/dist/types/utils/tree/index.d.ts +0 -6
  52. package/dist/types/utils/tree/rowsToTree.d.ts +0 -10
  53. package/dist/types/utils/tree/treeFilter.d.ts +0 -6
  54. package/dist/types/utils/tree/treeFind.d.ts +0 -8
  55. package/dist/types/utils/tree/treeForEach.d.ts +0 -5
  56. package/dist/types/utils/tree/treeMap.d.ts +0 -6
  57. package/dist/types/utils/tree/treeToRows.d.ts +0 -9
  58. package/dist/types/utils/tree/types.d.ts +0 -24
  59. package/dist/types/utils/typeof.d.ts +0 -29
  60. package/dist/utils/array.js +0 -196
  61. package/dist/utils/index.js +0 -6
  62. package/dist/utils/object.js +0 -138
  63. package/dist/utils/string.js +0 -70
  64. package/dist/utils/to.js +0 -16
  65. package/dist/utils/tree/index.js +0 -6
  66. package/dist/utils/tree/rowsToTree.js +0 -35
  67. package/dist/utils/tree/treeFilter.js +0 -92
  68. package/dist/utils/tree/treeFind.js +0 -82
  69. package/dist/utils/tree/treeForEach.js +0 -60
  70. package/dist/utils/tree/treeMap.js +0 -79
  71. package/dist/utils/tree/treeToRows.js +0 -13
  72. package/dist/utils/tree/types.js +0 -10
  73. package/dist/utils/typeof.js +0 -114
@@ -1,11 +0,0 @@
1
- import { type EffectCallback } from "react";
2
- type MountCallback = EffectCallback | AsyncFunc;
3
- /**
4
- * 在组件初始化时执行的 Hook
5
- * - 即使在严格模式下也只执行一次
6
- * @reference https://ahooks.js.org/hooks/use-mount
7
- *
8
- * @param {MountCallback} effect 副作用函数
9
- */
10
- export declare function useMount(effect: MountCallback): void;
11
- export {};
@@ -1,16 +0,0 @@
1
- import type { Breakpoint } from "@pawover/types";
2
- type ResponsiveValues = Record<Breakpoint, boolean>;
3
- interface ResponsiveHookOptions {
4
- /**
5
- * 紧凑布局断点
6
- * - 低于此断点时使用紧凑布局
7
- * @default "xl"
8
- */
9
- compactBreakPoint?: Breakpoint;
10
- }
11
- export declare function useResponsive(options?: ResponsiveHookOptions): {
12
- responsive: ResponsiveValues;
13
- current: Breakpoint;
14
- isCompact: boolean;
15
- };
16
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * 在组件卸载时执行的 Hook
3
- *
4
- * @param {Func} effect 副作用函数
5
- */
6
- export declare function useUnmount(effect: Func): void;
@@ -1,2 +0,0 @@
1
- export * from "./enums";
2
- export * from "./utils";
@@ -1,76 +0,0 @@
1
- export declare function toArray<T>(candidate?: T | T[] | false): T[];
2
- /**
3
- * 获取数组第一项
4
- *
5
- * @param initialList 初始数组
6
- * @param saveValue 安全值
7
- */
8
- export declare function arrayFirst<T>(initialList: readonly T[], saveValue?: T): T | undefined;
9
- /**
10
- * 获取数组最后一项
11
- *
12
- * @param initialList 初始数组
13
- * @param saveValue 安全值
14
- */
15
- export declare function arrayLast<T>(initialList: readonly T[], saveValue?: T): T | undefined;
16
- export declare function arrayDiff<T>(initialList: readonly T[], diffList: readonly T[], match: (row: T) => PropertyKey): T[];
17
- /**
18
- * 数组竞争
19
- * - 返回在匹配函数的比较条件中获胜的最终项目,适用于更复杂的最小值/最大值计算
20
- *
21
- * @param initialList 数组
22
- * @param match 匹配函数
23
- */
24
- export declare function arrayCompete<T>(initialList: readonly T[], match: (a: T, b: T) => T): T | null;
25
- /**
26
- * 统计数组的项目出现次数
27
- * - 通过给定的标识符匹配函数,返回一个对象,其中键是回调函数返回的 key 值,每个值是一个整数,表示该 key 出现的次数
28
- *
29
- * @param initialList 初始数组
30
- * @param match 标识符匹配函数
31
- */
32
- export declare function arrayCounting<T, K extends PropertyKey>(initialList: readonly T[], match: (row: T) => K): Record<string, number>;
33
- /**
34
- * 数组项替换
35
- * - 在给定的数组中,替换符合匹配函数结果的项目。只替换第一个匹配项。始终返回原始数组的副本。
36
- *
37
- * @param initialList 初始数组
38
- * @param newItem 替换项
39
- * @param match 匹配函数
40
- */
41
- export declare function arrayReplace<T>(initialList: readonly T[], newItem: T, match: (row: T, index: number) => boolean): T[];
42
- /**
43
- * 数组选择
44
- * - 一次性应用 `filter` 和 `map` 操作
45
- *
46
- * @param initialList 初始数组
47
- * @param filter filter 函数
48
- * @param mapper map 函数
49
- */
50
- export declare function arrayPick<T, K = T>(initialList: readonly T[], filter: (row: T, index: number) => boolean, mapper?: ((row: T, index: number) => K) | undefined): (readonly T[] & unknown[]) | K[];
51
- /**
52
- * 数组切分
53
- * - 将数组以指定的长度切分后,组合在高维数组中
54
- *
55
- * @param initialList 初始数组
56
- * @param size 分割尺寸,默认 `10`
57
- */
58
- export declare function arraySplit<T>(initialList: readonly T[], size?: number): T[][];
59
- /**
60
- * 数组迭代
61
- * - 运行一个函数 n 次以生成一个值,迭代函数将作为还原器运行无数次,然后返回累积值
62
- *
63
- * @param count 迭代次数
64
- * @param iterate 迭代函数
65
- * @param initialValue 初始值
66
- */
67
- export declare function arrayIterate<T>(count: number, iterate: (currentValue: T, iteration: number) => T, initialValue: T): T;
68
- /**
69
- * 数组合并
70
- * - 通过给定的标识符匹配函数,用第二个数组中的匹配项替换第一个数组中匹配项的所有内容
71
- *
72
- * @param initialList 初始数组
73
- * @param mergeList 待合并数组
74
- * @param match 标识符匹配函数
75
- */
76
- export declare function arrayMerge<T>(initialList: readonly T[], mergeList: readonly T[], match: (item: T) => unknown): readonly T[] & unknown[];
@@ -1,6 +0,0 @@
1
- export * from "./array";
2
- export * from "./object";
3
- export * from "./string";
4
- export * from "./to";
5
- export * from "./tree";
6
- export * from "./typeof";
@@ -1,53 +0,0 @@
1
- import type { NonEmptyObject, UnionToTuple, ValueOf } from "type-fest";
2
- /**
3
- * 返回对象的可枚举属性和方法的名称
4
- * - `Object.keys` 始终返回 `string[]` 类型,此函数可以返回具体类型
5
- *
6
- * @param obj 对象
7
- * @returns 对象所有可枚举的属性的键名
8
- */
9
- export declare function objectKeys<O extends AnyObject>(obj: O): (keyof O)[];
10
- /**
11
- * 返回对象的可枚举属性的值数组
12
- *
13
- * @param obj 对象
14
- */
15
- export declare function objectValues<O extends AnyObject>(obj: O): UnionToTuple<ValueOf<O>>;
16
- /**
17
- * 返回对象的可枚举属性的键/值数组
18
- *
19
- * @param obj 对象
20
- */
21
- export declare function objectEntries<O extends AnyObject>(obj: O): [string & keyof O, O[keyof O]][];
22
- /**
23
- * 对象反转
24
- * - 返回交换了对象的可枚举属性的值/键对象
25
- *
26
- * @param obj 对象
27
- */
28
- export declare function objectSwitch<O extends AnyObject>(obj: NonEmptyObject<O>): Record<O[keyof O], keyof O>;
29
- /**
30
- * 对象合并
31
- * - 将两个对象递归合并为一个新对象,从右到左依次使用数值
32
- * - 递归只适用于子对象属性
33
- */
34
- export declare function objectAssign<O extends AnyObject>(obj: O, overrideObj: O): O;
35
- export declare function objectPick<O extends AnyObject, K extends keyof O>(obj: O, keys: readonly K[]): Pick<O, K>;
36
- /**
37
- * 获取枚举所有属性的键
38
- *
39
- * @param enumeration 枚举
40
- */
41
- export declare function enumKeys<E extends AnyObject>(enumeration: NonEmptyObject<E>): [keyof E, ...(keyof E)[]];
42
- /**
43
- * 获取枚举所有属性的值
44
- *
45
- * @param enumeration 枚举
46
- */
47
- export declare function enumValues<E extends AnyObject>(enumeration: NonEmptyObject<E>): UnionToTuple<ValueOf<E>>;
48
- /**
49
- * 返回枚举的属性的键/值数组
50
- *
51
- * @param enumeration 枚举
52
- */
53
- export declare function enumEntries<E extends AnyObject>(enumeration: NonEmptyObject<E>): [keyof E, E[keyof E]][];
@@ -1,13 +0,0 @@
1
- /**
2
- * 转义特殊字符
3
- *
4
- * @link https://github.com/sindresorhus/escape-string-regexp
5
- * @param value 字符串
6
- */
7
- export declare function escapeStringRegexp(value: string): string;
8
- /**
9
- * 首字母大小写
10
- */
11
- export declare function stringInitialCase(value: string, type: "lower" | "upper"): string;
12
- export declare function stringToJson<R extends AnyObject = AnyObject, D extends R = R>(data: string | null | undefined, safeValue: D): R;
13
- export declare function stringToValues<T extends number | string = number>(data: string | null | undefined, valueType?: "number" | "string"): T[];
@@ -1,5 +0,0 @@
1
- /**
2
- * @param promise
3
- * @param errorExt - 可以传递给err对象的其他信息
4
- */
5
- export declare function to<T, U = Error>(promise: Readonly<Promise<T>>, errorExt?: UnknownObject): Promise<[U, undefined] | [null, T]>;
@@ -1,6 +0,0 @@
1
- export { treeFilter } from "./treeFilter";
2
- export { treeFind } from "./treeFind";
3
- export { treeForEach } from "./treeForEach";
4
- export { treeMap } from "./treeMap";
5
- export { rowsToTree } from "./rowsToTree";
6
- export { treeToRows } from "./treeToRows";
@@ -1,10 +0,0 @@
1
- import type { ChildrenKey, ParentIdKey, RowKey } from "./types";
2
- export interface RowsToTreeOptions<RK extends string = RowKey, PK extends string = ParentIdKey, CK extends string = ChildrenKey> {
3
- rowKey?: RK;
4
- parentIdKey?: PK;
5
- childrenKey?: CK;
6
- }
7
- /**
8
- * 行结构 转 树结构
9
- */
10
- export declare function rowsToTree<T extends AnyObject = AnyObject, CK extends string = ChildrenKey, R = TreeLike<T, CK>, RK extends string = RowKey, PK extends string = ParentIdKey>(rows: T[], options?: RowsToTreeOptions<RK, PK, CK> | undefined): R[];
@@ -1,6 +0,0 @@
1
- import { type BaseCallbackMeta, type BaseInnerOptions, type BaseOptions, type ChildrenKey } from "./types";
2
- export type TreeFilterOptions<T extends AnyObject, CK extends string = ChildrenKey> = BaseOptions<T, CK>;
3
- export type TreeFilterInnerOption<T extends AnyObject, CK extends string = ChildrenKey> = BaseInnerOptions<T, CK>;
4
- export type TreeFilterCallback<T extends AnyObject> = (row: T, meta: BaseCallbackMeta<T>) => boolean;
5
- export declare function treeFilter<T extends AnyObject, CK extends string = ChildrenKey>(tree: T[], callback: TreeFilterCallback<T>, options?: TreeFilterOptions<T, CK>): T[];
6
- export declare function treeFilter<T extends AnyObject, CK extends string = ChildrenKey>(tree: T, callback: TreeFilterCallback<T>, options?: TreeFilterOptions<T, CK>): T;
@@ -1,8 +0,0 @@
1
- import { type BaseCallbackMeta, type BaseInnerOptions, type BaseOptions, type ChildrenKey } from "./types";
2
- export type TreeFindOptions<T extends AnyObject, CK extends string = ChildrenKey> = BaseOptions<T, CK>;
3
- export type TreeFindInnerOption<T extends AnyObject, CK extends string = ChildrenKey> = BaseInnerOptions<T, CK>;
4
- export type TreeFindCallback<T extends AnyObject> = (row: T, meta: BaseCallbackMeta<T>) => boolean;
5
- /**
6
- * 查找树节点,找到第一个返回非空值的节点
7
- */
8
- export declare function treeFind<T extends AnyObject, CK extends string = ChildrenKey>(tree: T | T[], callback: TreeFindCallback<T>, options?: TreeFindOptions<T, CK>): T | undefined;
@@ -1,5 +0,0 @@
1
- import { type BaseCallbackMeta, type BaseInnerOptions, type BaseOptions, type ChildrenKey } from "./types";
2
- export type TreeForeachOptions<T extends AnyObject, CK extends string = ChildrenKey> = BaseOptions<T, CK>;
3
- export type TreeForeachInnerOption<T extends AnyObject, CK extends string = ChildrenKey> = BaseInnerOptions<T, CK>;
4
- export type TreeForeachCallback<T extends AnyObject> = (row: T, meta: BaseCallbackMeta<T>) => void;
5
- export declare function treeForEach<T extends AnyObject, CK extends string = ChildrenKey>(tree: T | T[], callback: TreeForeachCallback<T>, options?: TreeForeachOptions<T, CK>): void;
@@ -1,6 +0,0 @@
1
- import { type BaseCallbackMeta, type BaseInnerOptions, type BaseOptions, type ChildrenKey } from "./types";
2
- export type TreeMapOptions<T extends AnyObject, CK extends string> = BaseOptions<T, CK>;
3
- export type TreeMapInnerOption<T extends AnyObject, CK extends string> = BaseInnerOptions<T, CK>;
4
- export type TreeMapCallback<R extends AnyObject, T extends AnyObject> = (row: T, meta: BaseCallbackMeta<T>) => R;
5
- export declare function treeMap<R extends AnyObject, T extends AnyObject, CK extends string = ChildrenKey>(tree: T[], callback: TreeMapCallback<R, T>, options?: TreeMapOptions<T, CK>): TreeLike<R, CK>[];
6
- export declare function treeMap<R extends AnyObject, T extends AnyObject, CK extends string = ChildrenKey>(tree: T, callback: TreeMapCallback<R, T>, options?: TreeMapOptions<T, CK>): TreeLike<R, CK>;
@@ -1,9 +0,0 @@
1
- import type { SetOptional } from "type-fest";
2
- import { type TreeForeachOptions } from "./treeForEach";
3
- import type { ChildrenKey } from "./types";
4
- type TreeToRowsOptions<T extends AnyObject, CK extends string = ChildrenKey> = TreeForeachOptions<T, CK>;
5
- /**
6
- * 树结构 转 行结构
7
- */
8
- export declare function treeToRows<T extends AnyObject, CK extends string = ChildrenKey, R extends AnyObject = SetOptional<T, CK>>(tree: T | T[], options?: TreeToRowsOptions<T, CK>): R[];
9
- export {};
@@ -1,24 +0,0 @@
1
- export type RowKey = "id";
2
- export type ParentIdKey = "parentId";
3
- export type ChildrenKey = "children";
4
- export type Strategy = "pre" | "post" | "breadth";
5
- export interface BaseCallbackMeta<T> {
6
- depth: number;
7
- parents?: T[];
8
- }
9
- export interface BaseOptions<T, CK extends string> {
10
- childrenKey?: CK;
11
- strategy?: Strategy;
12
- getChildrenKey?: (row: T, meta: BaseCallbackMeta<T>) => CK | undefined;
13
- }
14
- export interface BaseInnerOptions<T, CK extends string> {
15
- childrenKey: CK;
16
- parents: T[];
17
- depth: number;
18
- getChildrenKey?: (row: T, meta: BaseCallbackMeta<T>) => CK | undefined;
19
- }
20
- export interface QueueItem<T, CK extends string> {
21
- queueRow: T;
22
- queueOptions: BaseInnerOptions<T, CK>;
23
- }
24
- export declare function getFinalChildrenKey<T, CK extends string>(tree: T, meta: BaseCallbackMeta<T>, options: BaseInnerOptions<T, CK>): CK;
@@ -1,29 +0,0 @@
1
- import type { Class } from "type-fest";
2
- export declare function isString<T extends string>(value: unknown): value is T;
3
- export declare function isNumber<T extends number>(value: unknown): value is T;
4
- export declare function isBoolean<T extends boolean>(value: unknown): value is T;
5
- export declare function isObject<T extends Record<PropertyKey, unknown>>(value: unknown): value is T;
6
- export declare function isArray<T extends unknown[]>(value: unknown): value is T;
7
- export declare function isBigInt<T extends bigint>(value: unknown): value is T;
8
- export declare function isSymbol<T extends symbol>(value: unknown): value is T;
9
- export declare function isFunction<T extends Func>(value: unknown): value is T;
10
- export declare function isGeneratorFunction<T extends Func>(value: unknown): value is T;
11
- export declare function isAsyncFunction<T extends AsyncFunc>(value: unknown): value is T;
12
- export declare function isPromise<T extends Promise<unknown>>(value: unknown): value is T;
13
- export declare function isPromiseLike<T extends PromiseLike<unknown>>(value: unknown): value is T;
14
- export declare function isNull<T extends null>(value: unknown): value is T;
15
- export declare function isUndefined<T extends undefined>(value: unknown): value is T;
16
- export declare function isDate<T extends Date>(value: unknown): value is T;
17
- export declare function isRegExp<T extends RegExp>(value: unknown): value is T;
18
- export declare function isError<T extends Error>(value: unknown): value is T;
19
- export declare function isFile<T extends File>(value: unknown): value is T;
20
- export declare function isMap<T extends Map<unknown, unknown>>(value: unknown): value is T;
21
- export declare function isWeakMap<T extends WeakMap<AnyObject, unknown>>(value: unknown): value is T;
22
- export declare function isSet<T extends Set<unknown>>(value: unknown): value is T;
23
- export declare function isWeakSet<T extends WeakSet<AnyObject>>(value: unknown): value is T;
24
- export declare function isWindow<T extends Window>(value: unknown): value is T;
25
- export declare function isWebSocket<T extends WebSocket>(value: unknown): value is T;
26
- export declare function isURLSearchParams<T extends Window>(value: unknown): value is T;
27
- export declare function isClass<T extends Class<AnyObject>>(value: unknown): value is T;
28
- export declare function isInteger<T extends number>(value: unknown): value is T;
29
- export declare function isIterable<T extends Iterable<unknown>>(value: unknown): value is T;
@@ -1,196 +0,0 @@
1
- import { isArray, isFunction } from "./typeof";
2
- export function toArray(candidate) {
3
- if (candidate === undefined || candidate === false) {
4
- return [];
5
- }
6
- return isArray(candidate) ? candidate : [candidate];
7
- }
8
- /**
9
- * 获取数组第一项
10
- *
11
- * @param initialList 初始数组
12
- * @param saveValue 安全值
13
- */
14
- export function arrayFirst(initialList, saveValue) {
15
- if (!isArray(initialList) || initialList.length === 0) {
16
- return saveValue;
17
- }
18
- return initialList[0];
19
- }
20
- /**
21
- * 获取数组最后一项
22
- *
23
- * @param initialList 初始数组
24
- * @param saveValue 安全值
25
- */
26
- export function arrayLast(initialList, saveValue) {
27
- if (!isArray(initialList) || initialList.length === 0) {
28
- return saveValue;
29
- }
30
- return initialList[initialList.length - 1];
31
- }
32
- export function arrayDiff(initialList, diffList, match) {
33
- if (!isArray(initialList) && !isArray(diffList)) {
34
- return [];
35
- }
36
- if (!isArray(initialList) || !initialList.length) {
37
- return [...diffList];
38
- }
39
- if (!isArray(diffList) || !diffList.length) {
40
- return [...initialList];
41
- }
42
- if (!isFunction(match)) {
43
- return [];
44
- }
45
- const keys = diffList.reduce((prev, curr) => {
46
- prev[match(curr)] = true;
47
- return prev;
48
- }, {});
49
- return initialList.filter((a) => !keys[match(a)]);
50
- }
51
- /**
52
- * 数组竞争
53
- * - 返回在匹配函数的比较条件中获胜的最终项目,适用于更复杂的最小值/最大值计算
54
- *
55
- * @param initialList 数组
56
- * @param match 匹配函数
57
- */
58
- export function arrayCompete(initialList, match) {
59
- if (!isArray(initialList) || initialList.length === 0 || !isFunction(match)) {
60
- return null;
61
- }
62
- return initialList.reduce(match);
63
- }
64
- /**
65
- * 统计数组的项目出现次数
66
- * - 通过给定的标识符匹配函数,返回一个对象,其中键是回调函数返回的 key 值,每个值是一个整数,表示该 key 出现的次数
67
- *
68
- * @param initialList 初始数组
69
- * @param match 标识符匹配函数
70
- */
71
- export function arrayCounting(initialList, match) {
72
- if (!isArray(initialList) || !isFunction(match)) {
73
- return {};
74
- }
75
- return initialList.reduce((prev, curr) => {
76
- const id = match(curr).toString();
77
- prev[id] = (prev[id] ?? 0) + 1;
78
- return prev;
79
- }, {});
80
- }
81
- /**
82
- * 数组项替换
83
- * - 在给定的数组中,替换符合匹配函数结果的项目。只替换第一个匹配项。始终返回原始数组的副本。
84
- *
85
- * @param initialList 初始数组
86
- * @param newItem 替换项
87
- * @param match 匹配函数
88
- */
89
- export function arrayReplace(initialList, newItem, match) {
90
- if (!initialList) {
91
- return [];
92
- }
93
- if (newItem === undefined || !isFunction(match)) {
94
- return [...initialList];
95
- }
96
- for (let i = 0; i < initialList.length; i++) {
97
- const item = initialList[i];
98
- if (item !== undefined && match(item, i)) {
99
- return [...initialList.slice(0, i), newItem, ...initialList.slice(i + 1, initialList.length)];
100
- }
101
- }
102
- return [...initialList];
103
- }
104
- /**
105
- * 数组选择
106
- * - 一次性应用 `filter` 和 `map` 操作
107
- *
108
- * @param initialList 初始数组
109
- * @param filter filter 函数
110
- * @param mapper map 函数
111
- */
112
- export function arrayPick(initialList, filter, mapper) {
113
- if (!isArray(initialList)) {
114
- return [];
115
- }
116
- if (!isFunction(filter)) {
117
- return initialList;
118
- }
119
- const hasMapper = isFunction(mapper);
120
- return initialList.reduce((prev, curr, index) => {
121
- if (!filter(curr, index)) {
122
- return prev;
123
- }
124
- if (hasMapper) {
125
- prev.push(mapper(curr, index));
126
- }
127
- else {
128
- prev.push(curr);
129
- }
130
- return prev;
131
- }, []);
132
- }
133
- /**
134
- * 数组切分
135
- * - 将数组以指定的长度切分后,组合在高维数组中
136
- *
137
- * @param initialList 初始数组
138
- * @param size 分割尺寸,默认 `10`
139
- */
140
- export function arraySplit(initialList, size = 10) {
141
- if (!isArray(initialList)) {
142
- return [];
143
- }
144
- const count = Math.ceil(initialList.length / size);
145
- return Array.from({ length: count })
146
- .fill(null)
147
- .map((_c, i) => {
148
- return initialList.slice(i * size, i * size + size);
149
- });
150
- }
151
- /**
152
- * 数组迭代
153
- * - 运行一个函数 n 次以生成一个值,迭代函数将作为还原器运行无数次,然后返回累积值
154
- *
155
- * @param count 迭代次数
156
- * @param iterate 迭代函数
157
- * @param initialValue 初始值
158
- */
159
- export function arrayIterate(count, iterate, initialValue) {
160
- let result = initialValue;
161
- if (isFunction(iterate)) {
162
- for (let i = 1; i <= count; i++) {
163
- result = iterate(result, i);
164
- }
165
- }
166
- return result;
167
- }
168
- /**
169
- * 数组合并
170
- * - 通过给定的标识符匹配函数,用第二个数组中的匹配项替换第一个数组中匹配项的所有内容
171
- *
172
- * @param initialList 初始数组
173
- * @param mergeList 待合并数组
174
- * @param match 标识符匹配函数
175
- */
176
- export function arrayMerge(initialList, mergeList, match) {
177
- if (!isArray(initialList)) {
178
- return [];
179
- }
180
- if (!isArray(mergeList)) {
181
- return initialList;
182
- }
183
- if (!isFunction(match)) {
184
- return initialList;
185
- }
186
- return initialList.reduce((prev, curr) => {
187
- const matched = mergeList.find((list) => match(curr) === match(list));
188
- if (matched) {
189
- prev.push(matched);
190
- }
191
- else {
192
- prev.push(curr);
193
- }
194
- return prev;
195
- }, []);
196
- }
@@ -1,6 +0,0 @@
1
- export * from "./array";
2
- export * from "./object";
3
- export * from "./string";
4
- export * from "./to";
5
- export * from "./tree";
6
- export * from "./typeof";
@@ -1,138 +0,0 @@
1
- import { isArray, isObject } from "./typeof";
2
- /**
3
- * 返回对象的可枚举属性和方法的名称
4
- * - `Object.keys` 始终返回 `string[]` 类型,此函数可以返回具体类型
5
- *
6
- * @param obj 对象
7
- * @returns 对象所有可枚举的属性的键名
8
- */
9
- export function objectKeys(obj) {
10
- return Object.keys(obj);
11
- }
12
- /**
13
- * 返回对象的可枚举属性的值数组
14
- *
15
- * @param obj 对象
16
- */
17
- export function objectValues(obj) {
18
- return Object.values(obj);
19
- }
20
- /**
21
- * 返回对象的可枚举属性的键/值数组
22
- *
23
- * @param obj 对象
24
- */
25
- export function objectEntries(obj) {
26
- return Object.entries(obj);
27
- }
28
- /**
29
- * 对象反转
30
- * - 返回交换了对象的可枚举属性的值/键对象
31
- *
32
- * @param obj 对象
33
- */
34
- export function objectSwitch(obj) {
35
- const result = {};
36
- if (!isObject(obj)) {
37
- return result;
38
- }
39
- for (const [k, v] of objectEntries(obj)) {
40
- result[v] = k;
41
- }
42
- return result;
43
- }
44
- /**
45
- * 对象合并
46
- * - 将两个对象递归合并为一个新对象,从右到左依次使用数值
47
- * - 递归只适用于子对象属性
48
- */
49
- export function objectAssign(obj, overrideObj) {
50
- const result = {};
51
- if (!isObject(obj) && !isObject(overrideObj)) {
52
- return result;
53
- }
54
- if (!isObject(obj) || !isObject(overrideObj)) {
55
- return obj ?? overrideObj ?? result;
56
- }
57
- return Object.entries({ ...obj, ...overrideObj }).reduce((prev, [key, value]) => {
58
- return {
59
- ...prev,
60
- [key]: (() => {
61
- if (isObject(obj[key])) {
62
- return objectAssign(obj[key], value);
63
- }
64
- return value;
65
- })(),
66
- };
67
- }, result);
68
- }
69
- export function objectPick(obj, keys) {
70
- const result = {};
71
- if (!isObject(obj)) {
72
- return result;
73
- }
74
- if (!isArray(keys)) {
75
- return obj;
76
- }
77
- return keys.reduce((prev, curr) => {
78
- if (curr in obj) {
79
- prev[curr] = obj[curr];
80
- }
81
- return prev;
82
- }, result);
83
- }
84
- function enumType(enumeration) {
85
- if (!isObject(enumeration)) {
86
- throw Error(`function enumKeys expected parameter is a enum, but got ${typeof enumeration}`);
87
- }
88
- if (!objectKeys(enumeration).length) {
89
- throw Error("Enum requires at least one member");
90
- }
91
- return enumeration;
92
- }
93
- /**
94
- * 获取枚举所有属性的键
95
- *
96
- * @param enumeration 枚举
97
- */
98
- export function enumKeys(enumeration) {
99
- const e = enumType(enumeration);
100
- const keys = objectKeys(e);
101
- const values = objectValues(e);
102
- const isTwoWayEnum = keys.every((k) => values.some((v) => `${v}` === k));
103
- if (isTwoWayEnum) {
104
- return keys.splice(keys.length / 2, keys.length / 2);
105
- }
106
- return keys;
107
- }
108
- /**
109
- * 获取枚举所有属性的值
110
- *
111
- * @param enumeration 枚举
112
- */
113
- export function enumValues(enumeration) {
114
- const e = enumType(enumeration);
115
- const keys = objectKeys(e);
116
- const values = objectValues(e);
117
- const isTwoWayEnum = keys.every((k) => values.some((v) => `${v}` === k));
118
- if (isTwoWayEnum) {
119
- return values.splice(keys.length / 2, keys.length / 2);
120
- }
121
- return values;
122
- }
123
- /**
124
- * 返回枚举的属性的键/值数组
125
- *
126
- * @param enumeration 枚举
127
- */
128
- export function enumEntries(enumeration) {
129
- const e = enumType(enumeration);
130
- const keys = objectKeys(e);
131
- const values = objectValues(e);
132
- const entries = objectEntries(e);
133
- const isTwoWayEnum = keys.every((k) => values.some((v) => `${v}` === k));
134
- if (isTwoWayEnum) {
135
- return entries.splice(keys.length / 2, keys.length / 2);
136
- }
137
- return entries;
138
- }