@pawover/kit 0.0.0-alpha.5 → 0.0.0-alpha.7

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.
@@ -0,0 +1,13 @@
1
+ export interface CloningStrategy {
2
+ cloneMap: <K, V>(parent: Map<K, V>, track: (newParent: Map<K, V>) => Map<K, V>, clone: <T>(value: T) => T) => Map<K, V> | null;
3
+ cloneSet: <T>(parent: Set<T>, track: (newParent: Set<T>) => Set<T>, clone: <T>(value: T) => T) => Set<T> | null;
4
+ cloneArray: <T>(parent: readonly T[], track: (newParent: T[]) => T[], clone: <T>(value: T) => T) => T[] | null;
5
+ cloneObject: <T extends AnyObject>(parent: T, track: (newParent: T) => T, clone: <T>(value: T) => T) => T | null;
6
+ cloneOther: <T>(parent: T, track: (newParent: T) => T, clone: <T>(value: T) => T) => T | null;
7
+ }
8
+ export declare const DefaultCloningStrategy: CloningStrategy;
9
+ /**
10
+ * cloneDeep
11
+ * @reference https://github.com/radashi-org/radashi/blob/main/src/object/cloneDeep.ts
12
+ */
13
+ export declare function cloneDeep<T extends AnyObject>(root: T, customStrategy?: Partial<CloningStrategy>): T;
@@ -1,4 +1,5 @@
1
1
  export * from "./array";
2
+ export * from "./clone";
2
3
  export * from "./object";
3
4
  export * from "./string";
4
5
  export * from "./to";
@@ -51,3 +51,4 @@ export declare function enumValues<E extends AnyObject>(enumeration: NonEmptyObj
51
51
  * @param enumeration 枚举
52
52
  */
53
53
  export declare function enumEntries<E extends AnyObject>(enumeration: NonEmptyObject<E>): [keyof E, E[keyof E]][];
54
+ export declare function mapEntries<TKey extends PropertyKey, TValue, TNewKey extends PropertyKey, TNewValue>(obj: UnknownObject<TKey, TValue>, toEntry: (key: TKey, value: TValue) => [TNewKey, TNewValue]): UnknownObject<TNewKey, TNewValue>;
@@ -1,3 +1,4 @@
1
+ import type { Replace } from "type-fest";
1
2
  /**
2
3
  * 转义特殊字符
3
4
  *
@@ -11,3 +12,4 @@ export declare function escapeStringRegexp(value: string): string;
11
12
  export declare function stringInitialCase(value: string, type: "lower" | "upper"): string;
12
13
  export declare function stringToJson<R extends AnyObject = AnyObject, D extends R = R>(data: string | null | undefined, safeValue: D): R;
13
14
  export declare function stringToValues<T extends number | string = number>(data: string | null | undefined, valueType?: "number" | "string"): T[];
15
+ export declare function stringReplace<I extends string, S extends string, R extends string>(input: I, search: S, replacement: R): Replace<I, S, R>;
@@ -0,0 +1,75 @@
1
+ import { isArray, isMap, isObject, isSet } from "./typeof";
2
+ export const DefaultCloningStrategy = {
3
+ cloneMap(input, track, clone) {
4
+ const output = track(new Map());
5
+ for (const [key, value] of input) {
6
+ output.set(key, clone(value));
7
+ }
8
+ return output;
9
+ },
10
+ cloneSet(input, track, clone) {
11
+ const output = track(new Set());
12
+ for (const value of input) {
13
+ output.add(clone(value));
14
+ }
15
+ return output;
16
+ },
17
+ cloneArray(input, track, clone) {
18
+ // Use .forEach for correct handling of sparse arrays
19
+ const output = track(new Array(input.length));
20
+ input.forEach((value, index) => {
21
+ output[index] = clone(value);
22
+ });
23
+ return output;
24
+ },
25
+ cloneObject(input, track, clone) {
26
+ const output = track(Object.create(Object.getPrototypeOf(input)));
27
+ for (const key of Reflect.ownKeys(input)) {
28
+ // By copying the property descriptors, we preserve computed
29
+ // properties and non-enumerable properties.
30
+ const descriptor = Object.getOwnPropertyDescriptor(input, key);
31
+ if ("value" in descriptor) {
32
+ descriptor.value = clone(descriptor.value);
33
+ }
34
+ Object.defineProperty(output, key, descriptor);
35
+ }
36
+ return output;
37
+ },
38
+ cloneOther(input, track) {
39
+ return track(input);
40
+ },
41
+ };
42
+ /**
43
+ * cloneDeep
44
+ * @reference https://github.com/radashi-org/radashi/blob/main/src/object/cloneDeep.ts
45
+ */
46
+ export function cloneDeep(root, customStrategy) {
47
+ const strategy = { ...DefaultCloningStrategy, ...customStrategy };
48
+ const tracked = new Map();
49
+ const track = (parent, newParent) => {
50
+ tracked.set(parent, newParent);
51
+ return newParent;
52
+ };
53
+ const clone = (value) => {
54
+ return value && typeof value === "object" ? (tracked.get(value) ?? cloneDeep(value, strategy)) : value;
55
+ };
56
+ const cloneDeep = (parent, strategy) => {
57
+ const cloneParent = (isObject(parent)
58
+ ? strategy.cloneObject
59
+ : isArray(parent)
60
+ ? strategy.cloneArray
61
+ : isMap(parent)
62
+ ? strategy.cloneMap
63
+ : isSet(parent)
64
+ ? strategy.cloneSet
65
+ : strategy.cloneOther);
66
+ const newParent = cloneParent(parent, track.bind(null, parent), clone);
67
+ if (!newParent) {
68
+ // Use the default strategy if null is returned.
69
+ return cloneDeep(parent, DefaultCloningStrategy);
70
+ }
71
+ tracked.set(parent, newParent);
72
+ return newParent;
73
+ };
74
+ return cloneDeep(root, strategy);
75
+ }
@@ -1,4 +1,5 @@
1
1
  export * from "./array";
2
+ export * from "./clone";
2
3
  export * from "./object";
3
4
  export * from "./string";
4
5
  export * from "./to";
@@ -54,9 +54,9 @@ export function objectAssign(obj, overrideObj) {
54
54
  if (!isObject(obj) || !isObject(overrideObj)) {
55
55
  return obj ?? overrideObj ?? result;
56
56
  }
57
- return Object.entries({ ...obj, ...overrideObj }).reduce((prev, [key, value]) => {
57
+ return objectEntries({ ...obj, ...overrideObj }).reduce((acc, [key, value]) => {
58
58
  return {
59
- ...prev,
59
+ ...acc,
60
60
  [key]: (() => {
61
61
  if (isObject(obj[key])) {
62
62
  return objectAssign(obj[key], value);
@@ -74,11 +74,11 @@ export function objectPick(obj, keys) {
74
74
  if (!isArray(keys)) {
75
75
  return obj;
76
76
  }
77
- return keys.reduce((prev, curr) => {
77
+ return keys.reduce((acc, curr) => {
78
78
  if (curr in obj) {
79
- prev[curr] = obj[curr];
79
+ acc[curr] = obj[curr];
80
80
  }
81
- return prev;
81
+ return acc;
82
82
  }, result);
83
83
  }
84
84
  function enumType(enumeration) {
@@ -136,3 +136,14 @@ export function enumEntries(enumeration) {
136
136
  }
137
137
  return entries;
138
138
  }
139
+ export function mapEntries(obj, toEntry) {
140
+ const defaultResult = {};
141
+ if (!obj) {
142
+ return defaultResult;
143
+ }
144
+ return objectEntries(obj).reduce((acc, [key, value]) => {
145
+ const [newKey, newValue] = toEntry(key, value);
146
+ acc[newKey] = newValue;
147
+ return acc;
148
+ }, defaultResult);
149
+ }
@@ -68,3 +68,6 @@ export function stringToValues(data, valueType = "number") {
68
68
  return [];
69
69
  }
70
70
  }
71
+ export function stringReplace(input, search, replacement) {
72
+ return input.replace(search, replacement);
73
+ }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "description": "pawover's kit",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
- "version": "0.0.0-alpha.5",
7
+ "version": "0.0.0-alpha.7",
8
8
  "packageManager": "pnpm@10.20.0",
9
9
  "engines": {
10
10
  "node": ">=22.20.0"
@@ -51,11 +51,11 @@
51
51
  "lib:up": "taze -I -r --exclude pnpm"
52
52
  },
53
53
  "devDependencies": {
54
- "@pawover/eslint-rules": "0.0.0-alpha.3",
54
+ "@pawover/eslint-rules": "0.0.0-alpha.7",
55
55
  "@pawover/types": "0.0.0-alpha.6",
56
56
  "@stylistic/eslint-plugin": "^5.5.0",
57
57
  "@types/node": "^24.10.1",
58
- "@types/react": "^19.2.4",
58
+ "@types/react": "^19.2.5",
59
59
  "ahooks": "^3.9.6",
60
60
  "eslint": "^9.39.1",
61
61
  "eslint-plugin-antfu": "^3.1.1",
@@ -1 +0,0 @@
1
- export * from "./react/index";
@@ -1 +0,0 @@
1
- export * from "./react/index";