@kopexa/shared-utils 1.1.4 → 1.1.6

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.
@@ -1,3 +1,6 @@
1
+ /** biome-ignore-all lint/complexity/noBannedTypes: utility library */
2
+ /** biome-ignore-all lint/suspicious/noExplicitAny: utility library */
3
+ type Args<T extends Function> = T extends (...args: infer R) => any ? R : never;
1
4
  /**
2
5
  * Generates a unique identifier using a specified prefix and a random number.
3
6
  *
@@ -8,5 +11,21 @@
8
11
  * getUniqueID('btn'); // returns 'btn-123456'
9
12
  */
10
13
  declare function getUniqueID(prefix: string): string;
14
+ /**
15
+ * Creates a function that invokes each provided function with the same argument, until
16
+ * one of the functions calls `event.preventDefault()`.
17
+ *
18
+ * @param fns - An array of functions that may or may not be defined.
19
+ * @returns A function that takes an event and invokes each handler with this event.
20
+ *
21
+ * @typeParam T - A function type that takes an event-like argument.
22
+ *
23
+ * @example
24
+ * const handler1 = event => console.log('Handled by first', event.type);
25
+ * const handler2 = event => event.preventDefault();
26
+ * const allHandlers = callAllHandlers(handler1, handler2);
27
+ * allHandlers({ type: 'click' });
28
+ */
29
+ declare function callAllHandlers<T extends (event: any) => void>(...fns: (T | undefined)[]): (event: Args<T>[0]) => void;
11
30
 
12
- export { getUniqueID };
31
+ export { callAllHandlers, getUniqueID };
@@ -1,3 +1,6 @@
1
+ /** biome-ignore-all lint/complexity/noBannedTypes: utility library */
2
+ /** biome-ignore-all lint/suspicious/noExplicitAny: utility library */
3
+ type Args<T extends Function> = T extends (...args: infer R) => any ? R : never;
1
4
  /**
2
5
  * Generates a unique identifier using a specified prefix and a random number.
3
6
  *
@@ -8,5 +11,21 @@
8
11
  * getUniqueID('btn'); // returns 'btn-123456'
9
12
  */
10
13
  declare function getUniqueID(prefix: string): string;
14
+ /**
15
+ * Creates a function that invokes each provided function with the same argument, until
16
+ * one of the functions calls `event.preventDefault()`.
17
+ *
18
+ * @param fns - An array of functions that may or may not be defined.
19
+ * @returns A function that takes an event and invokes each handler with this event.
20
+ *
21
+ * @typeParam T - A function type that takes an event-like argument.
22
+ *
23
+ * @example
24
+ * const handler1 = event => console.log('Handled by first', event.type);
25
+ * const handler2 = event => event.preventDefault();
26
+ * const allHandlers = callAllHandlers(handler1, handler2);
27
+ * allHandlers({ type: 'click' });
28
+ */
29
+ declare function callAllHandlers<T extends (event: any) => void>(...fns: (T | undefined)[]): (event: Args<T>[0]) => void;
11
30
 
12
- export { getUniqueID };
31
+ export { callAllHandlers, getUniqueID };
package/dist/functions.js CHANGED
@@ -20,13 +20,23 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/functions.ts
21
21
  var functions_exports = {};
22
22
  __export(functions_exports, {
23
+ callAllHandlers: () => callAllHandlers,
23
24
  getUniqueID: () => getUniqueID
24
25
  });
25
26
  module.exports = __toCommonJS(functions_exports);
26
27
  function getUniqueID(prefix) {
27
28
  return `${prefix}-${Math.floor(Math.random() * 1e6)}`;
28
29
  }
30
+ function callAllHandlers(...fns) {
31
+ return function func(event) {
32
+ fns.some((fn) => {
33
+ fn == null ? void 0 : fn(event);
34
+ return event == null ? void 0 : event.defaultPrevented;
35
+ });
36
+ };
37
+ }
29
38
  // Annotate the CommonJS export names for ESM import in node:
30
39
  0 && (module.exports = {
40
+ callAllHandlers,
31
41
  getUniqueID
32
42
  });
@@ -2,6 +2,15 @@
2
2
  function getUniqueID(prefix) {
3
3
  return `${prefix}-${Math.floor(Math.random() * 1e6)}`;
4
4
  }
5
+ function callAllHandlers(...fns) {
6
+ return function func(event) {
7
+ fns.some((fn) => {
8
+ fn == null ? void 0 : fn(event);
9
+ return event == null ? void 0 : event.defaultPrevented;
10
+ });
11
+ };
12
+ }
5
13
  export {
14
+ callAllHandlers,
6
15
  getUniqueID
7
16
  };
package/dist/index.d.mts CHANGED
@@ -1,8 +1,8 @@
1
1
  export { ariaAttr, dataAttr, isArray, isEmpty, isEmptyArray, isEmptyObject, isObject } from './assertion.mjs';
2
2
  export { cn } from './clsx.mjs';
3
- export { getUniqueID } from './functions.mjs';
3
+ export { callAllHandlers, getUniqueID } from './functions.mjs';
4
4
  export { clamp } from './numbers.mjs';
5
- export { compact } from './object.mjs';
5
+ export { compact, mapPropsVariants, objectToDeps } from './object.mjs';
6
6
  export { chain } from './ra.mjs';
7
7
  export { getInitials, safeText } from './text.mjs';
8
8
  import 'clsx';
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export { ariaAttr, dataAttr, isArray, isEmpty, isEmptyArray, isEmptyObject, isObject } from './assertion.js';
2
2
  export { cn } from './clsx.js';
3
- export { getUniqueID } from './functions.js';
3
+ export { callAllHandlers, getUniqueID } from './functions.js';
4
4
  export { clamp } from './numbers.js';
5
- export { compact } from './object.js';
5
+ export { compact, mapPropsVariants, objectToDeps } from './object.js';
6
6
  export { chain } from './ra.js';
7
7
  export { getInitials, safeText } from './text.js';
8
8
  import 'clsx';
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  ariaAttr: () => ariaAttr,
24
+ callAllHandlers: () => callAllHandlers,
24
25
  chain: () => chain,
25
26
  clamp: () => clamp,
26
27
  cn: () => cn,
@@ -33,6 +34,8 @@ __export(index_exports, {
33
34
  isEmptyArray: () => isEmptyArray,
34
35
  isEmptyObject: () => isEmptyObject,
35
36
  isObject: () => isObject,
37
+ mapPropsVariants: () => mapPropsVariants,
38
+ objectToDeps: () => objectToDeps,
36
39
  safeText: () => safeText
37
40
  });
38
41
  module.exports = __toCommonJS(index_exports);
@@ -71,6 +74,14 @@ function cn(...inputs) {
71
74
  function getUniqueID(prefix) {
72
75
  return `${prefix}-${Math.floor(Math.random() * 1e6)}`;
73
76
  }
77
+ function callAllHandlers(...fns) {
78
+ return function func(event) {
79
+ fns.some((fn) => {
80
+ fn == null ? void 0 : fn(event);
81
+ return event == null ? void 0 : event.defaultPrevented;
82
+ });
83
+ };
84
+ }
74
85
 
75
86
  // src/numbers.ts
76
87
  function clamp(value, min, max) {
@@ -85,6 +96,34 @@ function compact(object) {
85
96
  }
86
97
  return clone;
87
98
  }
99
+ var mapPropsVariants = (props, variantKeys, removeVariantProps = true) => {
100
+ if (!variantKeys) {
101
+ return [props, {}];
102
+ }
103
+ const picked = variantKeys.reduce((acc, key) => {
104
+ if (key in props) {
105
+ return { ...acc, [key]: props[key] };
106
+ } else {
107
+ return acc;
108
+ }
109
+ }, {});
110
+ if (removeVariantProps) {
111
+ const omitted = Object.keys(props).filter((key) => !variantKeys.includes(key)).reduce((acc, key) => ({ ...acc, [key]: props[key] }), {});
112
+ return [omitted, picked];
113
+ } else {
114
+ return [props, picked];
115
+ }
116
+ };
117
+ function objectToDeps(obj) {
118
+ if (!obj || typeof obj !== "object") {
119
+ return "";
120
+ }
121
+ try {
122
+ return JSON.stringify(obj);
123
+ } catch {
124
+ return "";
125
+ }
126
+ }
88
127
 
89
128
  // src/ra.ts
90
129
  function chain(...callbacks) {
@@ -114,6 +153,7 @@ var getInitials = (text) => {
114
153
  // Annotate the CommonJS export names for ESM import in node:
115
154
  0 && (module.exports = {
116
155
  ariaAttr,
156
+ callAllHandlers,
117
157
  chain,
118
158
  clamp,
119
159
  cn,
@@ -126,5 +166,7 @@ var getInitials = (text) => {
126
166
  isEmptyArray,
127
167
  isEmptyObject,
128
168
  isObject,
169
+ mapPropsVariants,
170
+ objectToDeps,
129
171
  safeText
130
172
  });
package/dist/index.mjs CHANGED
@@ -32,6 +32,14 @@ function cn(...inputs) {
32
32
  function getUniqueID(prefix) {
33
33
  return `${prefix}-${Math.floor(Math.random() * 1e6)}`;
34
34
  }
35
+ function callAllHandlers(...fns) {
36
+ return function func(event) {
37
+ fns.some((fn) => {
38
+ fn == null ? void 0 : fn(event);
39
+ return event == null ? void 0 : event.defaultPrevented;
40
+ });
41
+ };
42
+ }
35
43
 
36
44
  // src/numbers.ts
37
45
  function clamp(value, min, max) {
@@ -46,6 +54,34 @@ function compact(object) {
46
54
  }
47
55
  return clone;
48
56
  }
57
+ var mapPropsVariants = (props, variantKeys, removeVariantProps = true) => {
58
+ if (!variantKeys) {
59
+ return [props, {}];
60
+ }
61
+ const picked = variantKeys.reduce((acc, key) => {
62
+ if (key in props) {
63
+ return { ...acc, [key]: props[key] };
64
+ } else {
65
+ return acc;
66
+ }
67
+ }, {});
68
+ if (removeVariantProps) {
69
+ const omitted = Object.keys(props).filter((key) => !variantKeys.includes(key)).reduce((acc, key) => ({ ...acc, [key]: props[key] }), {});
70
+ return [omitted, picked];
71
+ } else {
72
+ return [props, picked];
73
+ }
74
+ };
75
+ function objectToDeps(obj) {
76
+ if (!obj || typeof obj !== "object") {
77
+ return "";
78
+ }
79
+ try {
80
+ return JSON.stringify(obj);
81
+ } catch {
82
+ return "";
83
+ }
84
+ }
49
85
 
50
86
  // src/ra.ts
51
87
  function chain(...callbacks) {
@@ -74,6 +110,7 @@ var getInitials = (text) => {
74
110
  };
75
111
  export {
76
112
  ariaAttr,
113
+ callAllHandlers,
77
114
  chain,
78
115
  clamp,
79
116
  cn,
@@ -86,5 +123,7 @@ export {
86
123
  isEmptyArray,
87
124
  isEmptyObject,
88
125
  isObject,
126
+ mapPropsVariants,
127
+ objectToDeps,
89
128
  safeText
90
129
  };
package/dist/object.d.mts CHANGED
@@ -1,4 +1,23 @@
1
1
  /** biome-ignore-all lint/suspicious/noExplicitAny: utility file */
2
+ /** biome-ignore-all lint/complexity/noBannedTypes: utility file */
3
+ /** biome-ignore-all lint/performance/noAccumulatingSpread: utility file */
4
+ type Extractable = {
5
+ [key: string]: any;
6
+ } | undefined;
2
7
  declare function compact<T extends Record<any, any>>(object: T): {} & T;
8
+ declare const mapPropsVariants: <T extends Record<string, any>, K extends keyof T>(props: T, variantKeys?: K[], removeVariantProps?: boolean) => readonly [Omit<T, K> | T, Pick<T, K> | {}];
9
+ /**
10
+ * Converts an object into a JSON string. Returns an empty string if the object
11
+ * is not extractable or if a circular reference is detected during stringification.
12
+ *
13
+ * @param obj - The object to convert into a dependency string.
14
+ *
15
+ * @returns A JSON string representation of the object or an empty string if conversion fails.
16
+ *
17
+ * @example
18
+ * objectToDeps({ key: 'value' }); // returns '{"key":"value"}'
19
+ * objectToDeps(undefined); // returns ""
20
+ */
21
+ declare function objectToDeps(obj: Extractable): string;
3
22
 
4
- export { compact };
23
+ export { compact, mapPropsVariants, objectToDeps };
package/dist/object.d.ts CHANGED
@@ -1,4 +1,23 @@
1
1
  /** biome-ignore-all lint/suspicious/noExplicitAny: utility file */
2
+ /** biome-ignore-all lint/complexity/noBannedTypes: utility file */
3
+ /** biome-ignore-all lint/performance/noAccumulatingSpread: utility file */
4
+ type Extractable = {
5
+ [key: string]: any;
6
+ } | undefined;
2
7
  declare function compact<T extends Record<any, any>>(object: T): {} & T;
8
+ declare const mapPropsVariants: <T extends Record<string, any>, K extends keyof T>(props: T, variantKeys?: K[], removeVariantProps?: boolean) => readonly [Omit<T, K> | T, Pick<T, K> | {}];
9
+ /**
10
+ * Converts an object into a JSON string. Returns an empty string if the object
11
+ * is not extractable or if a circular reference is detected during stringification.
12
+ *
13
+ * @param obj - The object to convert into a dependency string.
14
+ *
15
+ * @returns A JSON string representation of the object or an empty string if conversion fails.
16
+ *
17
+ * @example
18
+ * objectToDeps({ key: 'value' }); // returns '{"key":"value"}'
19
+ * objectToDeps(undefined); // returns ""
20
+ */
21
+ declare function objectToDeps(obj: Extractable): string;
3
22
 
4
- export { compact };
23
+ export { compact, mapPropsVariants, objectToDeps };
package/dist/object.js CHANGED
@@ -20,7 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/object.ts
21
21
  var object_exports = {};
22
22
  __export(object_exports, {
23
- compact: () => compact
23
+ compact: () => compact,
24
+ mapPropsVariants: () => mapPropsVariants,
25
+ objectToDeps: () => objectToDeps
24
26
  });
25
27
  module.exports = __toCommonJS(object_exports);
26
28
  function compact(object) {
@@ -30,7 +32,37 @@ function compact(object) {
30
32
  }
31
33
  return clone;
32
34
  }
35
+ var mapPropsVariants = (props, variantKeys, removeVariantProps = true) => {
36
+ if (!variantKeys) {
37
+ return [props, {}];
38
+ }
39
+ const picked = variantKeys.reduce((acc, key) => {
40
+ if (key in props) {
41
+ return { ...acc, [key]: props[key] };
42
+ } else {
43
+ return acc;
44
+ }
45
+ }, {});
46
+ if (removeVariantProps) {
47
+ const omitted = Object.keys(props).filter((key) => !variantKeys.includes(key)).reduce((acc, key) => ({ ...acc, [key]: props[key] }), {});
48
+ return [omitted, picked];
49
+ } else {
50
+ return [props, picked];
51
+ }
52
+ };
53
+ function objectToDeps(obj) {
54
+ if (!obj || typeof obj !== "object") {
55
+ return "";
56
+ }
57
+ try {
58
+ return JSON.stringify(obj);
59
+ } catch {
60
+ return "";
61
+ }
62
+ }
33
63
  // Annotate the CommonJS export names for ESM import in node:
34
64
  0 && (module.exports = {
35
- compact
65
+ compact,
66
+ mapPropsVariants,
67
+ objectToDeps
36
68
  });
package/dist/object.mjs CHANGED
@@ -6,6 +6,36 @@ function compact(object) {
6
6
  }
7
7
  return clone;
8
8
  }
9
+ var mapPropsVariants = (props, variantKeys, removeVariantProps = true) => {
10
+ if (!variantKeys) {
11
+ return [props, {}];
12
+ }
13
+ const picked = variantKeys.reduce((acc, key) => {
14
+ if (key in props) {
15
+ return { ...acc, [key]: props[key] };
16
+ } else {
17
+ return acc;
18
+ }
19
+ }, {});
20
+ if (removeVariantProps) {
21
+ const omitted = Object.keys(props).filter((key) => !variantKeys.includes(key)).reduce((acc, key) => ({ ...acc, [key]: props[key] }), {});
22
+ return [omitted, picked];
23
+ } else {
24
+ return [props, picked];
25
+ }
26
+ };
27
+ function objectToDeps(obj) {
28
+ if (!obj || typeof obj !== "object") {
29
+ return "";
30
+ }
31
+ try {
32
+ return JSON.stringify(obj);
33
+ } catch {
34
+ return "";
35
+ }
36
+ }
9
37
  export {
10
- compact
38
+ compact,
39
+ mapPropsVariants,
40
+ objectToDeps
11
41
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kopexa/shared-utils",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "A set of kopexa shared utilities",
5
5
  "keywords": [
6
6
  "system"