solid-tiny-utils 0.7.0 → 0.8.0
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.
- package/README.md +9 -9
- package/dist/color/{hex-rgb.d.ts → hex-rgb.d.mts} +5 -3
- package/dist/color/hex-rgb.mjs +62 -0
- package/dist/color/{oklch-rgb.d.ts → oklch-rgb.d.mts} +9 -8
- package/dist/color/oklch-rgb.mjs +101 -0
- package/dist/color/{validation.d.ts → validation.d.mts} +5 -3
- package/dist/color/validation.mjs +35 -0
- package/dist/dom/{css.d.ts → css.d.mts} +6 -4
- package/dist/dom/css.mjs +51 -0
- package/dist/event/create-click-outside.d.mts +9 -0
- package/dist/event/create-click-outside.mjs +33 -0
- package/dist/event/{create-event-listener.d.ts → create-event-listener.d.mts} +7 -6
- package/dist/event/create-event-listener.mjs +34 -0
- package/dist/event/{create-intersection-observer.d.ts → create-intersection-observer.d.mts} +5 -4
- package/dist/event/create-intersection-observer.mjs +29 -0
- package/dist/event/{create-visibility-observer.d.ts → create-visibility-observer.d.mts} +7 -5
- package/dist/event/create-visibility-observer.mjs +46 -0
- package/dist/event/index.d.mts +12 -0
- package/dist/event/index.mjs +15 -0
- package/dist/event/{make-event-listener.d.ts → make-event-listener.d.mts} +8 -7
- package/dist/event/make-event-listener.mjs +35 -0
- package/dist/event/on-motion-end.d.mts +63 -0
- package/dist/event/on-motion-end.mjs +96 -0
- package/dist/fn/create-debounce.d.mts +7 -0
- package/dist/fn/create-debounce.mjs +20 -0
- package/dist/fn/{create-loop-exec.d.ts → create-loop-exec.d.mts} +7 -6
- package/dist/fn/create-loop-exec.mjs +59 -0
- package/dist/fn/create-throttle.d.mts +7 -0
- package/dist/fn/create-throttle.mjs +21 -0
- package/dist/fn/index.d.mts +8 -0
- package/dist/fn/index.mjs +9 -0
- package/dist/index.d.mts +30 -0
- package/dist/index.mjs +27 -0
- package/dist/jsx/{attrs.d.ts → attrs.d.mts} +3 -2
- package/dist/jsx/attrs.mjs +26 -0
- package/dist/reactive/access.d.mts +6 -0
- package/dist/reactive/access.mjs +9 -0
- package/dist/reactive/{create-debounce-watch.d.ts → create-debounce-watch.d.mts} +6 -4
- package/dist/reactive/create-debounce-watch.mjs +15 -0
- package/dist/reactive/{create-watch.d.ts → create-watch.d.mts} +4 -3
- package/dist/reactive/create-watch.mjs +9 -0
- package/dist/reactive/index.d.mts +3 -0
- package/dist/types/{fn.d.ts → fn.d.mts} +3 -2
- package/dist/types/index.d.mts +2 -0
- package/dist/types/{maybe.d.ts → maybe.d.mts} +4 -3
- package/dist/utils/{array.d.ts → array.d.mts} +3 -2
- package/dist/utils/array.mjs +66 -0
- package/dist/utils/async.d.mts +8 -0
- package/dist/utils/async.mjs +17 -0
- package/dist/utils/{is.d.ts → is.d.mts} +3 -2
- package/dist/utils/is.mjs +72 -0
- package/dist/utils/{number.d.ts → number.d.mts} +3 -2
- package/dist/utils/number.mjs +127 -0
- package/dist/utils/{random.d.ts → random.d.mts} +3 -2
- package/dist/utils/random.mjs +33 -0
- package/dist/utils/{str.d.ts → str.d.mts} +4 -3
- package/dist/utils/str.mjs +120 -0
- package/package.json +22 -19
- package/dist/chunk-4L6FK7MF.js +0 -9
- package/dist/chunk-6G7GFZV2.js +0 -93
- package/dist/chunk-6OVLJ45M.js +0 -51
- package/dist/chunk-ACZGS7DG.js +0 -72
- package/dist/chunk-AZAXMGEB.js +0 -0
- package/dist/chunk-BLX3XSA6.js +0 -0
- package/dist/chunk-BT47ISVC.js +0 -36
- package/dist/chunk-EZML2DEC.js +0 -0
- package/dist/chunk-FFBJP5FE.js +0 -0
- package/dist/chunk-II6INKPZ.js +0 -8
- package/dist/chunk-KFLH3AZ6.js +0 -40
- package/dist/chunk-KKFGUHFR.js +0 -8
- package/dist/chunk-KM4Q6THD.js +0 -50
- package/dist/chunk-LEWF7QAQ.js +0 -47
- package/dist/chunk-LUFOWTRW.js +0 -0
- package/dist/chunk-PD6VHMH6.js +0 -26
- package/dist/chunk-QPEF6LHH.js +0 -111
- package/dist/chunk-QQVSG76Z.js +0 -36
- package/dist/chunk-S7U3LZNS.js +0 -23
- package/dist/chunk-SK6Y2YH6.js +0 -0
- package/dist/chunk-TDJLPDJF.js +0 -66
- package/dist/chunk-TGWWPUWD.js +0 -55
- package/dist/chunk-U5LQ2AS5.js +0 -0
- package/dist/chunk-WJHRONQU.js +0 -54
- package/dist/chunk-Y4GYGFIT.js +0 -85
- package/dist/chunk-YK5QQQ43.js +0 -12
- package/dist/chunk-YXRZ2KMJ.js +0 -0
- package/dist/color/hex-rgb.js +0 -15
- package/dist/color/index.d.ts +0 -3
- package/dist/color/index.js +0 -30
- package/dist/color/oklch-rgb.js +0 -15
- package/dist/color/validation.js +0 -18
- package/dist/dom/css.js +0 -19
- package/dist/dom/index.d.ts +0 -2
- package/dist/dom/index.js +0 -20
- package/dist/event/create-click-outside.d.ts +0 -8
- package/dist/event/create-click-outside.js +0 -17
- package/dist/event/create-event-listener.js +0 -17
- package/dist/event/create-intersection-observer.js +0 -16
- package/dist/event/create-visibility-observer.js +0 -16
- package/dist/event/index.d.ts +0 -8
- package/dist/event/index.js +0 -33
- package/dist/event/make-event-listener.js +0 -16
- package/dist/fn/create-debounce.d.ts +0 -6
- package/dist/fn/create-debounce.js +0 -15
- package/dist/fn/create-loop-exec.js +0 -15
- package/dist/fn/create-throttle.d.ts +0 -6
- package/dist/fn/create-throttle.js +0 -15
- package/dist/fn/index.d.ts +0 -9
- package/dist/fn/index.js +0 -21
- package/dist/index.d.ts +0 -27
- package/dist/index.js +0 -166
- package/dist/jsx/attrs.js +0 -6
- package/dist/jsx/index.d.ts +0 -1
- package/dist/jsx/index.js +0 -7
- package/dist/reactive/access.d.ts +0 -6
- package/dist/reactive/access.js +0 -13
- package/dist/reactive/create-debounce-watch.js +0 -15
- package/dist/reactive/create-watch.js +0 -6
- package/dist/reactive/index.d.ts +0 -5
- package/dist/reactive/index.js +0 -21
- package/dist/types/fn.js +0 -1
- package/dist/types/index.d.ts +0 -3
- package/dist/types/index.js +0 -3
- package/dist/types/maybe.js +0 -1
- package/dist/utils/array.js +0 -13
- package/dist/utils/async.d.ts +0 -6
- package/dist/utils/async.js +0 -6
- package/dist/utils/index.d.ts +0 -6
- package/dist/utils/index.js +0 -85
- package/dist/utils/is.js +0 -30
- package/dist/utils/number.js +0 -14
- package/dist/utils/object.d.ts +0 -10
- package/dist/utils/object.js +0 -7
- package/dist/utils/random.js +0 -14
- package/dist/utils/str.js +0 -20
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { isServer } from "solid-js/web";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/is.ts
|
|
4
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: need any */
|
|
5
|
+
const isSymbol = (value) => {
|
|
6
|
+
return !!value && value.constructor === Symbol;
|
|
7
|
+
};
|
|
8
|
+
const isArray = Array.isArray;
|
|
9
|
+
const isObject = (value) => {
|
|
10
|
+
return !!value && value.constructor === Object;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Checks if the given value is primitive.
|
|
14
|
+
*
|
|
15
|
+
* Primitive Types: number , string , boolean , symbol, bigint, undefined, null
|
|
16
|
+
*
|
|
17
|
+
* @param {*} value value to check
|
|
18
|
+
* @returns {boolean} result
|
|
19
|
+
*/
|
|
20
|
+
const isPrimitive = (value) => {
|
|
21
|
+
return value === void 0 || value === null || typeof value !== "object" && typeof value !== "function";
|
|
22
|
+
};
|
|
23
|
+
const isFn = (value) => {
|
|
24
|
+
return !!(value && value.constructor && value.call && value.apply);
|
|
25
|
+
};
|
|
26
|
+
const isString = (value) => {
|
|
27
|
+
return typeof value === "string" || value instanceof String;
|
|
28
|
+
};
|
|
29
|
+
const isInt = (value) => {
|
|
30
|
+
return isNumber(value) && value % 1 === 0;
|
|
31
|
+
};
|
|
32
|
+
const isFloat = (value) => {
|
|
33
|
+
return isNumber(value) && value % 1 !== 0;
|
|
34
|
+
};
|
|
35
|
+
const isNumber = (value) => {
|
|
36
|
+
try {
|
|
37
|
+
return Number(value) === value;
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const isDate = (value) => {
|
|
43
|
+
return Object.prototype.toString.call(value) === "[object Date]";
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* This is really a _best guess_ promise checking. You
|
|
47
|
+
* should probably use Promise.resolve(value) to be 100%
|
|
48
|
+
* sure you're handling it correctly.
|
|
49
|
+
*/
|
|
50
|
+
const isPromise = (value) => {
|
|
51
|
+
if (!value) return false;
|
|
52
|
+
if (!value.then) return false;
|
|
53
|
+
if (!isFn(value.then)) return false;
|
|
54
|
+
return true;
|
|
55
|
+
};
|
|
56
|
+
const isEmpty = (value) => {
|
|
57
|
+
if (value === true || value === false) return true;
|
|
58
|
+
if (value === null || value === void 0) return true;
|
|
59
|
+
if (isNumber(value)) return value === 0;
|
|
60
|
+
if (isDate(value)) return isNaN(value.getTime());
|
|
61
|
+
if (isFn(value)) return false;
|
|
62
|
+
if (isSymbol(value)) return false;
|
|
63
|
+
const length = value.length;
|
|
64
|
+
if (isNumber(length)) return length === 0;
|
|
65
|
+
const size = value.size;
|
|
66
|
+
if (isNumber(size)) return size === 0;
|
|
67
|
+
return Object.keys(value).length === 0;
|
|
68
|
+
};
|
|
69
|
+
const isClient = !isServer;
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
export { isArray, isClient, isDate, isEmpty, isFloat, isFn, isInt, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/utils/number.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Returns the smallest of the given numbers.
|
|
3
4
|
*
|
|
@@ -109,5 +110,5 @@ declare function inRange(x: number, minimum?: number, maximum?: number, inclusiv
|
|
|
109
110
|
* ```
|
|
110
111
|
*/
|
|
111
112
|
declare function toHex(x: number, pad?: number): string;
|
|
112
|
-
|
|
113
|
-
export { clamp, inRange, max, min, toHex };
|
|
113
|
+
//#endregion
|
|
114
|
+
export { clamp, inRange, max, min, toHex };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
//#region src/utils/number.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns the smallest of the given numbers.
|
|
4
|
+
*
|
|
5
|
+
* This is a convenience wrapper around `Math.min` that provides better type safety
|
|
6
|
+
* and consistency with other utility functions.
|
|
7
|
+
*
|
|
8
|
+
* @param numbers - The numbers to compare
|
|
9
|
+
* @returns The smallest number from the input
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* min(1, 2, 3) // => 1
|
|
14
|
+
* min(-5, 0, 10) // => -5
|
|
15
|
+
* min(3.14, 2.71, 1.41) // => 1.41
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function min(...numbers) {
|
|
19
|
+
return Math.min(...numbers);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Returns the largest of the given numbers.
|
|
23
|
+
*
|
|
24
|
+
* This is a convenience wrapper around `Math.max` that provides better type safety
|
|
25
|
+
* and consistency with other utility functions.
|
|
26
|
+
*
|
|
27
|
+
* @param numbers - The numbers to compare
|
|
28
|
+
* @returns The largest number from the input
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* max(1, 2, 3) // => 3
|
|
33
|
+
* max(-5, 0, 10) // => 10
|
|
34
|
+
* max(3.14, 2.71, 1.41) // => 3.14
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
function max(...numbers) {
|
|
38
|
+
return Math.max(...numbers);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Clamps a number to within the specified bounds.
|
|
42
|
+
*
|
|
43
|
+
* If the number is less than the minimum, returns the minimum.
|
|
44
|
+
* If the number is greater than the maximum, returns the maximum.
|
|
45
|
+
* Otherwise, returns the number unchanged.
|
|
46
|
+
*
|
|
47
|
+
* @param x - The number to clamp
|
|
48
|
+
* @param minimum - The lower bound (inclusive)
|
|
49
|
+
* @param maximum - The upper bound (inclusive)
|
|
50
|
+
* @returns The clamped number
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* clamp(0.5) // => 0.5 (within default 0-1 range)
|
|
55
|
+
* clamp(-0.1) // => 0 (clamped to minimum)
|
|
56
|
+
* clamp(1.5) // => 1 (clamped to maximum)
|
|
57
|
+
* clamp(15, 10, 20) // => 15 (within range)
|
|
58
|
+
* clamp(5, 10, 20) // => 10 (clamped to minimum)
|
|
59
|
+
* clamp(25, 10, 20) // => 20 (clamped to maximum)
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
function clamp(x, minimum = 0, maximum = 1) {
|
|
63
|
+
return min(maximum, max(minimum, x));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks if a number is within a specified range.
|
|
67
|
+
*
|
|
68
|
+
* Supports different inclusivity modes:
|
|
69
|
+
* - `'[]'` - Both bounds inclusive (default): `minimum ≤ x ≤ maximum`
|
|
70
|
+
* - `'()'` - Both bounds exclusive: `minimum < x < maximum`
|
|
71
|
+
* - `'[)'` - Lower inclusive, upper exclusive: `minimum ≤ x < maximum`
|
|
72
|
+
* - `'(]'` - Lower exclusive, upper inclusive: `minimum < x ≤ maximum`
|
|
73
|
+
*
|
|
74
|
+
* @param x - The number to check
|
|
75
|
+
* @param minimum - The lower bound of the range
|
|
76
|
+
* @param maximum - The upper bound of the range
|
|
77
|
+
* @param inclusivity - Specifies whether the bounds are inclusive or exclusive
|
|
78
|
+
* @returns `true` if the number is within the range, `false` otherwise
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* // Default inclusive range [0, 1]
|
|
83
|
+
* inRange(0.5) // => true
|
|
84
|
+
* inRange(0) // => true (inclusive)
|
|
85
|
+
* inRange(1) // => true (inclusive)
|
|
86
|
+
* inRange(-0.1) // => false
|
|
87
|
+
* inRange(1.1) // => false
|
|
88
|
+
*
|
|
89
|
+
* // Custom range [10, 20]
|
|
90
|
+
* inRange(15, 10, 20) // => true
|
|
91
|
+
* inRange(10, 10, 20) // => true (inclusive)
|
|
92
|
+
* inRange(20, 10, 20) // => true (inclusive)
|
|
93
|
+
*
|
|
94
|
+
* // Exclusive bounds (10, 20)
|
|
95
|
+
* inRange(15, 10, 20, '()') // => true
|
|
96
|
+
* inRange(10, 10, 20, '()') // => false (exclusive)
|
|
97
|
+
* inRange(20, 10, 20, '()') // => false (exclusive)
|
|
98
|
+
*
|
|
99
|
+
* // Mixed inclusivity [10, 20)
|
|
100
|
+
* inRange(10, 10, 20, '[)') // => true (lower inclusive)
|
|
101
|
+
* inRange(20, 10, 20, '[)') // => false (upper exclusive)
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
function inRange(x, minimum = 0, maximum = 1, inclusivity = "[]") {
|
|
105
|
+
const minCheck = inclusivity[0] === "[" ? x >= minimum : x > minimum;
|
|
106
|
+
const maxCheck = inclusivity[1] === "]" ? x <= maximum : x < maximum;
|
|
107
|
+
return minCheck && maxCheck;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Converts a number to a hexadecimal string.
|
|
111
|
+
*
|
|
112
|
+
* @param x - The number to convert
|
|
113
|
+
* @param pad - The number of digits to pad the output with (default: 2)
|
|
114
|
+
* @returns The hexadecimal representation of the number
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* toHEX(255) // => "ff"
|
|
119
|
+
* toHEX(255, 4) // => "00ff"
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
function toHex(x, pad = 2) {
|
|
123
|
+
return x.toString(16).padStart(pad, "0");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
//#endregion
|
|
127
|
+
export { clamp, inRange, max, min, toHex };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/utils/random.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Generates a random number between min and max
|
|
3
4
|
*/
|
|
@@ -9,5 +10,5 @@ declare const random: (min: number, max: number) => number;
|
|
|
9
10
|
declare const draw: <T>(array: readonly T[]) => T | null;
|
|
10
11
|
declare const shuffle: <T>(array: readonly T[]) => T[];
|
|
11
12
|
declare const uid: (length: number, specials?: string) => string;
|
|
12
|
-
|
|
13
|
-
export { draw, random, shuffle, uid };
|
|
13
|
+
//#endregion
|
|
14
|
+
export { draw, random, shuffle, uid };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { iterate } from "./array.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/random.ts
|
|
4
|
+
/**
|
|
5
|
+
* Generates a random number between min and max
|
|
6
|
+
*/
|
|
7
|
+
const random = (min, max) => {
|
|
8
|
+
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Draw a random item from a list. Returns
|
|
12
|
+
* null if the list is empty
|
|
13
|
+
*/
|
|
14
|
+
const draw = (array) => {
|
|
15
|
+
const max = array.length;
|
|
16
|
+
if (max === 0) return null;
|
|
17
|
+
return array[random(0, max - 1)];
|
|
18
|
+
};
|
|
19
|
+
const shuffle = (array) => {
|
|
20
|
+
return array.map((a) => ({
|
|
21
|
+
rand: Math.random(),
|
|
22
|
+
value: a
|
|
23
|
+
})).sort((a, b) => a.rand - b.rand).map((a) => a.value);
|
|
24
|
+
};
|
|
25
|
+
const uid = (length, specials = "") => {
|
|
26
|
+
const characters = `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789${specials}`;
|
|
27
|
+
return iterate(length, (acc) => {
|
|
28
|
+
return acc + characters.charAt(random(0, characters.length - 1));
|
|
29
|
+
}, "");
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
export { draw, random, shuffle, uid };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/utils/str.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Capitalize the first word of the string
|
|
3
4
|
*
|
|
@@ -23,7 +24,7 @@ declare const camel: (str: string) => string;
|
|
|
23
24
|
* snake('helloWord') -> 'hello_world'
|
|
24
25
|
*/
|
|
25
26
|
declare const snake: (str: string, options?: {
|
|
26
|
-
|
|
27
|
+
splitOnNumber?: boolean;
|
|
27
28
|
}) => string;
|
|
28
29
|
/**
|
|
29
30
|
* Formats the given string in dash case fashion
|
|
@@ -71,5 +72,5 @@ declare const template: (str: string, data: Record<string, any>, regex?: RegExp)
|
|
|
71
72
|
* ```
|
|
72
73
|
*/
|
|
73
74
|
declare const trim: (str: string | null | undefined, charsToTrim?: string) => string;
|
|
74
|
-
|
|
75
|
-
export { camel, capitalize, dash, pascal, snake, template, title, trim };
|
|
75
|
+
//#endregion
|
|
76
|
+
export { camel, capitalize, dash, pascal, snake, template, title, trim };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
//#region src/utils/str.ts
|
|
2
|
+
/**
|
|
3
|
+
* Capitalize the first word of the string
|
|
4
|
+
*
|
|
5
|
+
* capitalize('hello') -> 'Hello'
|
|
6
|
+
* capitalize('va va voom') -> 'Va va voom'
|
|
7
|
+
*/
|
|
8
|
+
const capitalize = (str) => {
|
|
9
|
+
if (!str || str.length === 0) return "";
|
|
10
|
+
const lower = str.toLowerCase();
|
|
11
|
+
return lower.substring(0, 1).toUpperCase() + lower.substring(1, lower.length);
|
|
12
|
+
};
|
|
13
|
+
const splitRegexp = /(?=[A-Z])|[.\-\s_]/;
|
|
14
|
+
/**
|
|
15
|
+
* Formats the given string in camel case fashion
|
|
16
|
+
*
|
|
17
|
+
* camel('hello world') -> 'helloWorld'
|
|
18
|
+
* camel('va va-VOOM') -> 'vaVaVoom'
|
|
19
|
+
* camel('helloWorld') -> 'helloWorld'
|
|
20
|
+
*/
|
|
21
|
+
const camel = (str) => {
|
|
22
|
+
const parts = str?.replace(/([A-Z])+/g, capitalize)?.split(splitRegexp).map((x) => x.toLowerCase()) ?? [];
|
|
23
|
+
if (parts.length === 0) return "";
|
|
24
|
+
if (parts.length === 1) return parts[0];
|
|
25
|
+
return parts.reduce((acc, part) => {
|
|
26
|
+
return `${acc}${part.charAt(0).toUpperCase()}${part.slice(1)}`;
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
const splitOnNumberRegexp = /([A-Za-z]{1}[0-9]{1})/;
|
|
30
|
+
/**
|
|
31
|
+
* Formats the given string in snake case fashion
|
|
32
|
+
*
|
|
33
|
+
* splitOnNumber? Treat number as capital, default is true
|
|
34
|
+
*
|
|
35
|
+
* snake('hello world') -> 'hello_world'
|
|
36
|
+
* snake('va va-VOOM') -> 'va_va_voom'
|
|
37
|
+
* snake('helloWord') -> 'hello_world'
|
|
38
|
+
*/
|
|
39
|
+
const snake = (str, options) => {
|
|
40
|
+
const parts = str?.replace(/([A-Z])+/g, capitalize).split(splitRegexp).map((x) => x.toLowerCase()) ?? [];
|
|
41
|
+
if (parts.length === 0) return "";
|
|
42
|
+
if (parts.length === 1) return parts[0];
|
|
43
|
+
const result = parts.reduce((acc, part) => {
|
|
44
|
+
return `${acc}_${part.toLowerCase()}`;
|
|
45
|
+
});
|
|
46
|
+
return options?.splitOnNumber === false ? result : result.replace(splitOnNumberRegexp, (val) => `${val[0]}_${val[1]}`);
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Formats the given string in dash case fashion
|
|
50
|
+
*
|
|
51
|
+
* dash('hello world') -> 'hello-world'
|
|
52
|
+
* dash('va va_VOOM') -> 'va-va-voom'
|
|
53
|
+
* dash('helloWord') -> 'hello-word'
|
|
54
|
+
*/
|
|
55
|
+
const dash = (str) => {
|
|
56
|
+
const parts = str?.replace(/([A-Z])+/g, capitalize)?.split(splitRegexp).map((x) => x.toLowerCase()) ?? [];
|
|
57
|
+
if (parts.length === 0) return "";
|
|
58
|
+
if (parts.length === 1) return parts[0];
|
|
59
|
+
return parts.reduce((acc, part) => {
|
|
60
|
+
return `${acc}-${part.toLowerCase()}`;
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
const pascalSplitRegexp = /[.\-\s_]/;
|
|
64
|
+
/**
|
|
65
|
+
* Formats the given string in pascal case fashion
|
|
66
|
+
*
|
|
67
|
+
* pascal('hello world') -> 'HelloWorld'
|
|
68
|
+
* pascal('va va boom') -> 'VaVaBoom'
|
|
69
|
+
*/
|
|
70
|
+
const pascal = (str) => {
|
|
71
|
+
const parts = str?.split(pascalSplitRegexp).map((x) => x.toLowerCase()) ?? [];
|
|
72
|
+
if (parts.length === 0) return "";
|
|
73
|
+
return parts.map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join("");
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Formats the given string in title case fashion
|
|
77
|
+
*
|
|
78
|
+
* title('hello world') -> 'Hello World'
|
|
79
|
+
* title('va_va_boom') -> 'Va Va Boom'
|
|
80
|
+
* title('root-hook') -> 'Root Hook'
|
|
81
|
+
* title('queryItems') -> 'Query Items'
|
|
82
|
+
*/
|
|
83
|
+
const title = (str) => {
|
|
84
|
+
if (!str) return "";
|
|
85
|
+
return str.split(splitRegexp).map((s) => s.trim()).filter((s) => !!s).map((s) => capitalize(s.toLowerCase())).join(" ");
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* template is used to replace data by name in template strings.
|
|
89
|
+
* The default expression looks for {{name}} to identify names.
|
|
90
|
+
*
|
|
91
|
+
* Ex. template('Hello, {{name}}', { name: 'ray' })
|
|
92
|
+
* Ex. template('Hello, <name>', { name: 'ray' }, /<(.+?)>/g)
|
|
93
|
+
*/
|
|
94
|
+
const template = (str, data, regex = /\{\{(.+?)\}\}/g) => {
|
|
95
|
+
return Array.from(str.matchAll(regex)).reduce((acc, match) => {
|
|
96
|
+
return acc.replace(match[0], data[match[1]]);
|
|
97
|
+
}, str);
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Trims all prefix and suffix characters from the given
|
|
101
|
+
* string. Like the builtin trim function but accepts
|
|
102
|
+
* other characters you would like to trim and trims
|
|
103
|
+
* multiple characters.
|
|
104
|
+
*
|
|
105
|
+
* ```typescript
|
|
106
|
+
* trim(' hello ') // => 'hello'
|
|
107
|
+
* trim('__hello__', '_') // => 'hello'
|
|
108
|
+
* trim('/repos/:owner/:repo/', '/') // => 'repos/:owner/:repo'
|
|
109
|
+
* trim('222222__hello__1111111', '12_') // => 'hello'
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
const trim = (str, charsToTrim = " ") => {
|
|
113
|
+
if (!str) return "";
|
|
114
|
+
const toTrim = charsToTrim.replace(/[\W]{1}/g, "\\$&");
|
|
115
|
+
const regex = new RegExp(`^[${toTrim}]+|[${toTrim}]+$`, "g");
|
|
116
|
+
return str.replace(regex, "");
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
//#endregion
|
|
120
|
+
export { camel, capitalize, dash, pascal, snake, template, title, trim };
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solid-tiny-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "A collection of tiny utilities for SolidJS applications",
|
|
5
|
-
"main": "./dist/index.
|
|
5
|
+
"main": "./dist/index.mjs",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist"
|
|
@@ -12,48 +12,51 @@
|
|
|
12
12
|
"solid-js": "^1.9.7"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
|
-
"@biomejs/biome": "2.
|
|
16
|
-
"@solidjs/router": "^0.15.
|
|
15
|
+
"@biomejs/biome": "2.3.10",
|
|
16
|
+
"@solidjs/router": "^0.15.4",
|
|
17
17
|
"@solidjs/testing-library": "^0.8.10",
|
|
18
|
-
"@testing-library/jest-dom": "^6.
|
|
18
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
19
19
|
"@testing-library/user-event": "^14.6.1",
|
|
20
20
|
"@types/culori": "^4.0.0",
|
|
21
|
-
"@types/node": "^
|
|
22
|
-
"@vitest/ui": "^
|
|
21
|
+
"@types/node": "^25.0.3",
|
|
22
|
+
"@vitest/ui": "^4.0.16",
|
|
23
23
|
"bumpp": "^10.2.3",
|
|
24
24
|
"culori": "^4.0.2",
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"tsup": "^8.5.0",
|
|
25
|
+
"jsdom": "^27.3.0",
|
|
26
|
+
"tsdown": "^0.18.3",
|
|
28
27
|
"typescript": "^5.8.3",
|
|
29
|
-
"ultracite": "5.
|
|
28
|
+
"ultracite": "6.5.0",
|
|
30
29
|
"unocss": "^66.3.3",
|
|
31
|
-
"vite": "^
|
|
30
|
+
"vite": "^7.3.0",
|
|
32
31
|
"vite-plugin-solid": "^2.11.0",
|
|
33
32
|
"vite-plugin-solid-pages": "^0.3.4",
|
|
34
|
-
"vitest": "^
|
|
33
|
+
"vitest": "^4.0.16",
|
|
34
|
+
"solid-tiny-context": "0.2.3"
|
|
35
35
|
},
|
|
36
36
|
"keywords": [
|
|
37
37
|
"solidjs",
|
|
38
38
|
"solid",
|
|
39
|
-
"utilities",
|
|
40
39
|
"reactive",
|
|
41
40
|
"typescript",
|
|
42
41
|
"utils"
|
|
43
42
|
],
|
|
44
43
|
"author": "",
|
|
45
44
|
"license": "MIT",
|
|
46
|
-
"
|
|
47
|
-
|
|
45
|
+
"module": "./dist/index.mjs",
|
|
46
|
+
"types": "./dist/index.d.mts",
|
|
47
|
+
"exports": {
|
|
48
|
+
".": "./dist/index.mjs",
|
|
49
|
+
"./package.json": "./package.json"
|
|
48
50
|
},
|
|
49
51
|
"scripts": {
|
|
50
|
-
"build": "
|
|
52
|
+
"build": "tsdown",
|
|
51
53
|
"dev": "vite -c ./playground/vite.config.ts",
|
|
52
54
|
"test": "vitest --run",
|
|
53
55
|
"clean": "rimraf dist node_modules pnpm-lock.yaml",
|
|
54
56
|
"test:ui": "vitest --ui",
|
|
55
|
-
"lint": "pnpm dlx ultracite check",
|
|
56
|
-
"lint:
|
|
57
|
+
"lint": "pnpm dlx ultracite@latest check",
|
|
58
|
+
"lint:error": "pnpm dlx ultracite@latest check --diagnostic-level error",
|
|
59
|
+
"lint:fix": "pnpm dlx ultracite@latest fix",
|
|
57
60
|
"type-check": "tsc --noEmit --skipLibCheck",
|
|
58
61
|
"bump": "pnpm type-check && pnpm lint:fix && pnpm test && pnpm bumpp --no-push",
|
|
59
62
|
"prepublish": "pnpm build"
|
package/dist/chunk-4L6FK7MF.js
DELETED
package/dist/chunk-6G7GFZV2.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
// src/utils/is.ts
|
|
2
|
-
import { isServer } from "solid-js/web";
|
|
3
|
-
var isSymbol = (value) => {
|
|
4
|
-
return !!value && value.constructor === Symbol;
|
|
5
|
-
};
|
|
6
|
-
var isArray = Array.isArray;
|
|
7
|
-
var isObject = (value) => {
|
|
8
|
-
return !!value && value.constructor === Object;
|
|
9
|
-
};
|
|
10
|
-
var isPrimitive = (value) => {
|
|
11
|
-
return value === void 0 || value === null || typeof value !== "object" && typeof value !== "function";
|
|
12
|
-
};
|
|
13
|
-
var isFn = (value) => {
|
|
14
|
-
return !!(value && value.constructor && value.call && value.apply);
|
|
15
|
-
};
|
|
16
|
-
var isString = (value) => {
|
|
17
|
-
return typeof value === "string" || value instanceof String;
|
|
18
|
-
};
|
|
19
|
-
var isInt = (value) => {
|
|
20
|
-
return isNumber(value) && value % 1 === 0;
|
|
21
|
-
};
|
|
22
|
-
var isFloat = (value) => {
|
|
23
|
-
return isNumber(value) && value % 1 !== 0;
|
|
24
|
-
};
|
|
25
|
-
var isNumber = (value) => {
|
|
26
|
-
try {
|
|
27
|
-
return Number(value) === value;
|
|
28
|
-
} catch {
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
var isDate = (value) => {
|
|
33
|
-
return Object.prototype.toString.call(value) === "[object Date]";
|
|
34
|
-
};
|
|
35
|
-
var isPromise = (value) => {
|
|
36
|
-
if (!value) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
if (!value.then) {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
if (!isFn(value.then)) {
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
return true;
|
|
46
|
-
};
|
|
47
|
-
var isEmpty = (value) => {
|
|
48
|
-
if (value === true || value === false) {
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
if (value === null || value === void 0) {
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
if (isNumber(value)) {
|
|
55
|
-
return value === 0;
|
|
56
|
-
}
|
|
57
|
-
if (isDate(value)) {
|
|
58
|
-
return isNaN(value.getTime());
|
|
59
|
-
}
|
|
60
|
-
if (isFn(value)) {
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
if (isSymbol(value)) {
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
const length = value.length;
|
|
67
|
-
if (isNumber(length)) {
|
|
68
|
-
return length === 0;
|
|
69
|
-
}
|
|
70
|
-
const size = value.size;
|
|
71
|
-
if (isNumber(size)) {
|
|
72
|
-
return size === 0;
|
|
73
|
-
}
|
|
74
|
-
const keys = Object.keys(value).length;
|
|
75
|
-
return keys === 0;
|
|
76
|
-
};
|
|
77
|
-
var isClient = !isServer;
|
|
78
|
-
|
|
79
|
-
export {
|
|
80
|
-
isSymbol,
|
|
81
|
-
isArray,
|
|
82
|
-
isObject,
|
|
83
|
-
isPrimitive,
|
|
84
|
-
isFn,
|
|
85
|
-
isString,
|
|
86
|
-
isInt,
|
|
87
|
-
isFloat,
|
|
88
|
-
isNumber,
|
|
89
|
-
isDate,
|
|
90
|
-
isPromise,
|
|
91
|
-
isEmpty,
|
|
92
|
-
isClient
|
|
93
|
-
};
|
package/dist/chunk-6OVLJ45M.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
makeEventListener
|
|
3
|
-
} from "./chunk-TGWWPUWD.js";
|
|
4
|
-
import {
|
|
5
|
-
noop
|
|
6
|
-
} from "./chunk-QPEF6LHH.js";
|
|
7
|
-
import {
|
|
8
|
-
access
|
|
9
|
-
} from "./chunk-YK5QQQ43.js";
|
|
10
|
-
import {
|
|
11
|
-
createWatch
|
|
12
|
-
} from "./chunk-4L6FK7MF.js";
|
|
13
|
-
import {
|
|
14
|
-
clearArray
|
|
15
|
-
} from "./chunk-BT47ISVC.js";
|
|
16
|
-
import {
|
|
17
|
-
isArray
|
|
18
|
-
} from "./chunk-6G7GFZV2.js";
|
|
19
|
-
|
|
20
|
-
// src/event/create-event-listener.ts
|
|
21
|
-
function createEventListener(...args) {
|
|
22
|
-
const target = args[0];
|
|
23
|
-
const events = args[1];
|
|
24
|
-
let listeners = args[2];
|
|
25
|
-
const options = args[3] ?? noop;
|
|
26
|
-
if (!isArray(listeners)) {
|
|
27
|
-
listeners = [listeners];
|
|
28
|
-
}
|
|
29
|
-
const cleanups = [];
|
|
30
|
-
const cleanup = () => {
|
|
31
|
-
for (const c of cleanups) {
|
|
32
|
-
c();
|
|
33
|
-
}
|
|
34
|
-
clearArray(cleanups);
|
|
35
|
-
};
|
|
36
|
-
createWatch(
|
|
37
|
-
() => [access(target), access(events), access(options)],
|
|
38
|
-
([tars, evs, opts]) => {
|
|
39
|
-
cleanup();
|
|
40
|
-
if (!(tars && evs)) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
cleanups.push(makeEventListener(tars, evs, listeners, opts));
|
|
44
|
-
}
|
|
45
|
-
);
|
|
46
|
-
return cleanup;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export {
|
|
50
|
-
createEventListener
|
|
51
|
-
};
|