@codady/utils 0.0.22 → 0.0.24

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,39 @@
1
+ /**
2
+ * @since Last modified: 2026/01/07 11:45:59
3
+ * @function parseClasses
4
+ * @description Converts a string or an array of class names into an array of class names.
5
+ * If a string is provided, it can be a comma-separated or space-separated list of class names.
6
+ * The function cleans up any unnecessary spaces and ensures the result is an array of strings.
7
+ * @param {string | string[]} data - A string containing class names separated by commas or spaces, or an array of class names.
8
+ * @returns {string[]} - Returns an array of class names as strings.
9
+ * @example
10
+ * parseClasses('demo01,demo02'); // Returns ['demo01', 'demo02']
11
+ * parseClasses('demo01 demo02'); // Returns ['demo01', 'demo02']
12
+ * parseClasses(['demo01', 'demo02']); // Returns ['demo01', 'demo02']
13
+ */
14
+ 'use strict';
15
+
16
+ import COMMA from "./comma";
17
+ import SPACE from "./space";
18
+ import trim from "./trim";
19
+
20
+ const parseClasses = (data: string | string[]): string[] => {
21
+ let separator: string,
22
+ result: string[] = [];
23
+
24
+ if (Array.isArray(data)) {
25
+ // If data is already an array, filter out invalid values
26
+ result = (data as string[]).filter((k: any) => k && typeof k === 'string');
27
+ } else {
28
+ // Trim the input string and handle multiple spaces
29
+ data = trim(data as string);
30
+ // Use comma as the separator if present, otherwise use space
31
+ separator = data.includes(COMMA) ? COMMA : SPACE;
32
+ result = data.split(separator);
33
+ }
34
+
35
+ // Trim each item globally and filter out any empty strings
36
+ return result.map((k: string) => trim(k, 'global')).filter(Boolean);
37
+ };
38
+
39
+ export default parseClasses;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * @since Last modified: 2026/01/07 12:05:02
3
+ * @namespace removeClasses
4
+ * @description Removes one or more CSS classes from a DOM element. This function can remove a list of class names
5
+ * either passed as a space/comma-separated string or an array of strings.
6
+ * It also supports an optional `intercept` callback to modify or intercept the class removal process.
7
+ *
8
+ * @param {string | Node | null} target - The target DOM element, Node, or CSS selector from which the classes will be removed.
9
+ * It can be:
10
+ * - A string representing a CSS selector (e.g., `#elementId`)
11
+ * - A DOM Node or Element.
12
+ * - `null` (in which case, no action will be performed).
13
+ * @param {string | string[]} classes - A string or an array of CSS class names to remove. This can be:
14
+ * - A space/comma-separated string of class names (e.g., `'class1 class2'`)
15
+ * - An array of class names (e.g., `['class1', 'class2']`)
16
+ * @param {Function} [intercept] - An optional callback function that intercepts the class removal process.
17
+ * It takes the class name as a parameter and can return:
18
+ * - `true`: to remove the class name.
19
+ * - A new class name (string): to replace the class name with a new one.
20
+ * - `false` or `undefined`: to skip removing the class.
21
+ * @returns {void} This function does not return a value. It modifies the target DOM element.
22
+ *
23
+ */
24
+ 'use strict';
25
+ import getEl from "./getEl";
26
+ import parseClasses from "./parseClasses";
27
+ const removeClasses = (target, classes, intercept) => {
28
+ const el = getEl(target), arr = parseClasses(classes);
29
+ if (!el || arr.length === 0)
30
+ return;
31
+ arr.forEach((k) => {
32
+ let tmp;
33
+ if (intercept) {
34
+ tmp = intercept(k);
35
+ tmp === true ? el.classList.remove(k) :
36
+ (typeof tmp === 'string' && tmp) ? el.classList.remove(tmp) : null;
37
+ }
38
+ else {
39
+ el.classList.remove(k);
40
+ }
41
+ });
42
+ return;
43
+ };
44
+ export default removeClasses;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @since Last modified: 2026/01/07 12:05:02
3
+ * @namespace removeClasses
4
+ * @description Removes one or more CSS classes from a DOM element. This function can remove a list of class names
5
+ * either passed as a space/comma-separated string or an array of strings.
6
+ * It also supports an optional `intercept` callback to modify or intercept the class removal process.
7
+ *
8
+ * @param {string | Node | null} target - The target DOM element, Node, or CSS selector from which the classes will be removed.
9
+ * It can be:
10
+ * - A string representing a CSS selector (e.g., `#elementId`)
11
+ * - A DOM Node or Element.
12
+ * - `null` (in which case, no action will be performed).
13
+ * @param {string | string[]} classes - A string or an array of CSS class names to remove. This can be:
14
+ * - A space/comma-separated string of class names (e.g., `'class1 class2'`)
15
+ * - An array of class names (e.g., `['class1', 'class2']`)
16
+ * @param {Function} [intercept] - An optional callback function that intercepts the class removal process.
17
+ * It takes the class name as a parameter and can return:
18
+ * - `true`: to remove the class name.
19
+ * - A new class name (string): to replace the class name with a new one.
20
+ * - `false` or `undefined`: to skip removing the class.
21
+ * @returns {void} This function does not return a value. It modifies the target DOM element.
22
+ *
23
+ */
24
+ 'use strict';
25
+
26
+ import getEl from "./getEl";
27
+ import parseClasses from "./parseClasses";
28
+
29
+ const removeClasses = (target: string | Node | null, classes: string | string[], intercept?: (className: string) => string | boolean | void): void => {
30
+ const el = getEl(target),
31
+ arr = parseClasses(classes);
32
+ if (!el || arr.length === 0) return;
33
+
34
+ arr.forEach((k: string) => {
35
+ let tmp: any;
36
+ if (intercept) {
37
+ tmp = intercept(k);
38
+ tmp === true ? (el as Element).classList.remove(k) :
39
+ (typeof tmp === 'string' && tmp) ? (el as Element).classList.remove(tmp) : null;
40
+ } else {
41
+ (el as Element).classList.remove(k);
42
+ }
43
+ });
44
+ return;
45
+ }
46
+ export default removeClasses;
47
+
package/src/space.js ADDED
@@ -0,0 +1,2 @@
1
+ const SPACE = ' ';
2
+ export default SPACE;
package/src/space.ts ADDED
@@ -0,0 +1,2 @@
1
+ const SPACE = ' ';
2
+ export default SPACE;
package/src/trim.js ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @since Last modified: 2026/01/07 11:20:29
3
+ * @function trim
4
+ * @description Removes spaces, newlines, and carriage returns from the string.
5
+ * Supports removing spaces at specific positions (start, end, or both) or globally within the string.
6
+ * This is more powerful than the native `trim` method.
7
+ * @param {string} str - The string to be trimmed.
8
+ * @param {string} [placement='global'] - Defines where to remove spaces:
9
+ * - 'start' to remove spaces from the beginning.
10
+ * - 'end' to remove spaces from the end.
11
+ * - 'both' to remove spaces from both the start and the end.
12
+ * - 'global' to remove all spaces, newlines, and carriage returns globally.
13
+ * If omitted, the default is 'global'.
14
+ * @returns {string} - The string after trimming, without altering the original string.
15
+ * @example
16
+ * trim(' My name is Lily '); // Returns 'My name is Lily'
17
+ * trim(' My name is Lily ', 'start'); // Returns 'My name is Lily '
18
+ * trim(' My name is Lily ', 'end'); // Returns ' My name is Lily'
19
+ * trim(' My name is Lily ', 'both'); // Returns 'My name is Lily'
20
+ * trim(' My name is Lily ', 'global'); // Returns 'MynameisLily'
21
+ */
22
+ 'use strict';
23
+ const trim = (str, placement = 'global') => {
24
+ if (typeof str !== 'string') {
25
+ console.warn('Expected a string input');
26
+ return '';
27
+ }
28
+ switch (placement) {
29
+ case 'start':
30
+ return str.trimStart();
31
+ case 'end':
32
+ return str.trimEnd();
33
+ case 'both':
34
+ return str.trim();
35
+ case 'global':
36
+ return str.replace(/[\s\r\n]+/g, '');
37
+ default:
38
+ return str.trim().replace(/[\s\r\n]+/g, ' '); // Default behavior, trims both ends and replaces inner spaces
39
+ }
40
+ };
41
+ export default trim;
package/src/trim.ts ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * @since Last modified: 2026/01/07 11:20:29
3
+ * @function trim
4
+ * @description Removes spaces, newlines, and carriage returns from the string.
5
+ * Supports removing spaces at specific positions (start, end, or both) or globally within the string.
6
+ * This is more powerful than the native `trim` method.
7
+ * @param {string} str - The string to be trimmed.
8
+ * @param {string} [placement='global'] - Defines where to remove spaces:
9
+ * - 'start' to remove spaces from the beginning.
10
+ * - 'end' to remove spaces from the end.
11
+ * - 'both' to remove spaces from both the start and the end.
12
+ * - 'global' to remove all spaces, newlines, and carriage returns globally.
13
+ * If omitted, the default is 'global'.
14
+ * @returns {string} - The string after trimming, without altering the original string.
15
+ * @example
16
+ * trim(' My name is Lily '); // Returns 'My name is Lily'
17
+ * trim(' My name is Lily ', 'start'); // Returns 'My name is Lily '
18
+ * trim(' My name is Lily ', 'end'); // Returns ' My name is Lily'
19
+ * trim(' My name is Lily ', 'both'); // Returns 'My name is Lily'
20
+ * trim(' My name is Lily ', 'global'); // Returns 'MynameisLily'
21
+ */
22
+ 'use strict';
23
+
24
+ const trim = (str: string, placement: 'start' | 'end' | 'both' | 'global' = 'global'): string => {
25
+ if (typeof str !== 'string') {
26
+ console.warn('Expected a string input');
27
+ return '';
28
+ }
29
+
30
+ switch (placement) {
31
+ case 'start':
32
+ return str.trimStart();
33
+ case 'end':
34
+ return str.trimEnd();
35
+ case 'both':
36
+ return str.trim();
37
+ case 'global':
38
+ return str.replace(/[\s\r\n]+/g, '');
39
+ default:
40
+ return str.trim().replace(/[\s\r\n]+/g, ' '); // Default behavior, trims both ends and replaces inner spaces
41
+ }
42
+ };
43
+
44
+ export default trim;
@@ -1,22 +0,0 @@
1
- /**
2
- * @since Last modified: 2026/01/06 21:45:02
3
- * Converts an SVG string to a Base64 or URL-encoded format for use in `src` or background image.
4
- * This function ensures the SVG string is properly encoded to be safely used as a data URL.
5
- *
6
- * @param {string} svgString - The SVG string to be encoded.
7
- * @returns {string} - A data URL that can be used in `src` or as a background image, formatted as `data:image/svg+xml;charset=utf-8,`.
8
- *
9
- * @since Last modified: 2026/01/06 21:43:33
10
- *
11
- * Note:
12
- * - The function first URI-encodes the input string to make sure special characters are escaped.
13
- * - It replaces single quotes (') and double quotes (") with their respective URL-encoded forms (`%27` and `%22`) to prevent any potential issues in the URL.
14
- * - This approach provides better compatibility when embedding the SVG directly in HTML, CSS, or JavaScript.
15
- */
16
- export const svgToBase64 = (svgString) => {
17
- // 1. This method provides the best compatibility (escaping special characters)
18
- const encoded = encodeURIComponent(svgString)
19
- .replace(/'/g, "%27")
20
- .replace(/"/g, "%22");
21
- return `data:image/svg+xml;charset=utf-8,${encoded}`;
22
- };
@@ -1,22 +0,0 @@
1
- /**
2
- * @since Last modified: 2026/01/06 21:45:02
3
- * Converts an SVG string to a Base64 or URL-encoded format for use in `src` or background image.
4
- * This function ensures the SVG string is properly encoded to be safely used as a data URL.
5
- *
6
- * @param {string} svgString - The SVG string to be encoded.
7
- * @returns {string} - A data URL that can be used in `src` or as a background image, formatted as `data:image/svg+xml;charset=utf-8,`.
8
- *
9
- * @since Last modified: 2026/01/06 21:43:33
10
- *
11
- * Note:
12
- * - The function first URI-encodes the input string to make sure special characters are escaped.
13
- * - It replaces single quotes (') and double quotes (") with their respective URL-encoded forms (`%27` and `%22`) to prevent any potential issues in the URL.
14
- * - This approach provides better compatibility when embedding the SVG directly in HTML, CSS, or JavaScript.
15
- */
16
- export const svgToBase64 = (svgString: string):string => {
17
- // 1. This method provides the best compatibility (escaping special characters)
18
- const encoded = encodeURIComponent(svgString)
19
- .replace(/'/g, "%27")
20
- .replace(/"/g, "%22");
21
- return `data:image/svg+xml;charset=utf-8,${encoded}`;
22
- };