@codady/utils 0.0.23 → 0.0.25
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/CHANGELOG.md +4 -4
- package/dist/utils.cjs.js +128 -2
- package/dist/utils.cjs.min.js +3 -3
- package/dist/utils.esm.js +128 -2
- package/dist/utils.esm.min.js +3 -3
- package/dist/utils.umd.js +128 -2
- package/dist/utils.umd.min.js +3 -3
- package/dist.zip +0 -0
- package/modules.js +21 -1
- package/modules.ts +22 -1
- package/package.json +1 -1
- package/src/addClasses.js +41 -0
- package/src/addClasses.ts +42 -0
- package/src/alias.js +2 -0
- package/src/alias.ts +2 -0
- package/src/classes.js +114 -0
- package/src/classes.ts +140 -0
- package/src/comma.js +2 -0
- package/src/comma.ts +2 -0
- package/src/createTools.js +79 -0
- package/src/createTools.ts +129 -0
- package/src/getClasses.js +16 -0
- package/src/getClasses.ts +20 -0
- package/src/hasClasses.js +36 -0
- package/src/hasClasses.ts +39 -0
- package/src/namespace.js +2 -0
- package/src/namespace.ts +2 -0
- package/src/parseClasses.js +34 -0
- package/src/parseClasses.ts +39 -0
- package/src/removeClasses.js +44 -0
- package/src/removeClasses.ts +47 -0
- package/src/space.js +2 -0
- package/src/space.ts +2 -0
- package/src/trim.js +41 -0
- package/src/trim.ts +44 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 13:37:57
|
|
3
|
+
* @function createTools
|
|
4
|
+
* @description Creates a group of icon button tools and appends them to a parent element.
|
|
5
|
+
* The `data` array can contain tool definitions, where each tool can be defined either as an object or a string shorthand.
|
|
6
|
+
* @param {toolsItem[]} data - The data representing the tools. Each item in the array can be an object with properties for the tool or a string representing a shorthand tool.
|
|
7
|
+
* @param {Element | string} parent - The DOM element or selector where the tools will be appended.
|
|
8
|
+
* @returns {HTMLElement} The container element that holds the tools.
|
|
9
|
+
* @example
|
|
10
|
+
* ax.createTools([{},{},'toggle','close']);
|
|
11
|
+
* <!-- Returns a node containing the tool buttons -->
|
|
12
|
+
*/
|
|
13
|
+
'use strict';
|
|
14
|
+
|
|
15
|
+
import classes from './classes';
|
|
16
|
+
import createEl from './createEl';
|
|
17
|
+
import isEmpty from './isEmpty';
|
|
18
|
+
import ALIAS from './alias';
|
|
19
|
+
import NAMESPACE from './namespace';
|
|
20
|
+
import addClasses from './addClasses';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {Object} toolsItem
|
|
24
|
+
* @description Represents a single tool item with properties that define its behavior and appearance.
|
|
25
|
+
* @property {string} name - The name of the tool (required).
|
|
26
|
+
* @property {string} [nodeName='span'] - The HTML tag to use for the tool element (optional, default is 'span').
|
|
27
|
+
* @property {string} [icon] - The icon to display for the tool (optional).
|
|
28
|
+
* @property {string} [disk] - The URL of the disk image for the tool (optional).
|
|
29
|
+
* @property {string} [cube] - The URL of the cube image for the tool (optional).
|
|
30
|
+
* @property {string} [image] - The URL of the image to display for the tool (optional).
|
|
31
|
+
* @property {string | string[]} [classes] - CSS class names to apply to the tool (optional).
|
|
32
|
+
* @property {string} [styles] - Inline styles to apply to the tool (optional).
|
|
33
|
+
* @property {string} [label] - A label to display next to the tool (optional).
|
|
34
|
+
* @property {boolean} [expandable=false] - Whether the tool is expandable (optional, default is false).
|
|
35
|
+
* @property {string} [title] - A title to show as a tooltip when the user hovers over the tool (optional).
|
|
36
|
+
* @property {boolean} [focusable=false] - Whether the tool is focusable via keyboard (optional, default is false).
|
|
37
|
+
* @property {Function | null} [action=null] - A callback function to be executed when the tool is activated (optional).
|
|
38
|
+
* @property {Record<string, string>} [attrs] - Additional attributes to add to the tool element (optional).
|
|
39
|
+
* @property {any} [key: string] - Other unspecified properties that can be added dynamically.
|
|
40
|
+
*/
|
|
41
|
+
export type toolsItem = {
|
|
42
|
+
name: string;
|
|
43
|
+
nodeName?: string;
|
|
44
|
+
icon?: string;
|
|
45
|
+
disk?: string;
|
|
46
|
+
cube?: string;
|
|
47
|
+
image?: string;
|
|
48
|
+
classes?: string | string[];
|
|
49
|
+
styles?: string;
|
|
50
|
+
label?: string;
|
|
51
|
+
expandable?: boolean;
|
|
52
|
+
title?: string;
|
|
53
|
+
focusable?: boolean;
|
|
54
|
+
action?: null | Function;
|
|
55
|
+
attrs?: Record<string, string>;
|
|
56
|
+
[key: string]: any;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @description Creates a set of icon button tools and appends them to the specified parent element.
|
|
61
|
+
* @param {toolsItem[]} data - The tool data, each tool can be either an object or a shorthand string.
|
|
62
|
+
* @returns {HTMLElement} The container DOM element that holds the tools.
|
|
63
|
+
*/
|
|
64
|
+
const createTools = (data: toolsItem[]):HTMLElement => {
|
|
65
|
+
/*! data = [
|
|
66
|
+
{
|
|
67
|
+
name: 'close',
|
|
68
|
+
nodeName: 'span',
|
|
69
|
+
icon:'',
|
|
70
|
+
disk:'',
|
|
71
|
+
cube:'',
|
|
72
|
+
image:'',
|
|
73
|
+
attrs: {},
|
|
74
|
+
classes:'',
|
|
75
|
+
styles:'',
|
|
76
|
+
label:'',
|
|
77
|
+
expandable:false,
|
|
78
|
+
focusable:true,
|
|
79
|
+
action: null,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: 'max',
|
|
83
|
+
nodeName: 'span',
|
|
84
|
+
icon:'',
|
|
85
|
+
attrs: {},
|
|
86
|
+
classes:'',
|
|
87
|
+
styles:'',
|
|
88
|
+
label:'',
|
|
89
|
+
expandable:false,
|
|
90
|
+
action: null,
|
|
91
|
+
},
|
|
92
|
+
] */
|
|
93
|
+
const toolsEl = createEl('span', { class: `${NAMESPACE}-box-tools` }),
|
|
94
|
+
renderFn = (props: any) => {
|
|
95
|
+
const dftAttrs: any = {},
|
|
96
|
+
arrow = props.extendable ? `<i ${ALIAS}="arrow"></i>` : '',
|
|
97
|
+
iconStr = props.icon ? `<i ${ALIAS}="icon">${props.icon}</i>` : '',
|
|
98
|
+
diskStr = props.disk ? `<i ${ALIAS}="disk"><img src="${props.disk}"/></i>` : '',
|
|
99
|
+
cubeStr = props.cube ? `<i ${ALIAS}="cube"><img src="${props.cube}"/></i>` : '',
|
|
100
|
+
imageStr = props.image ? `<i ${ALIAS}="image"><img src="${props.image}"/></i>` : '',
|
|
101
|
+
label = props.label ? `<i ${ALIAS}="label">${props.label}</i>` : '',
|
|
102
|
+
html = iconStr + diskStr + cubeStr + imageStr + label + arrow;
|
|
103
|
+
//使用title提示
|
|
104
|
+
props.title && (dftAttrs.title = props.title);
|
|
105
|
+
//可聚焦,增加tabindex=1
|
|
106
|
+
props.focusable && (dftAttrs.tabindex = 1);
|
|
107
|
+
//attrs是其他属性,可能会覆盖title、tabindex
|
|
108
|
+
props.wrapEl = createEl(props.nodeName || 'span', Object.assign(dftAttrs, props.attrs), html);
|
|
109
|
+
props.iconEl = props.wrapEl.querySelector(`[${ALIAS}="icon"]`);
|
|
110
|
+
props.cubeEl = props.wrapEl.querySelector(`[${ALIAS}="cube"]`);
|
|
111
|
+
props.diskEl = props.wrapEl.querySelector(`[${ALIAS}="disk"]`);
|
|
112
|
+
props.imageEl = props.wrapEl.querySelector(`[${ALIAS}="image"]`);
|
|
113
|
+
props.labelEl = props.wrapEl.querySelector(`[${ALIAS}="label"]`);
|
|
114
|
+
//增加classes和styles
|
|
115
|
+
!isEmpty(props.classes) && addClasses(props.wrapEl,props.classes);
|
|
116
|
+
!isEmpty(props.styles) && (props.wrapEl.style.cssText += props.styles);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
//此处不用map方法,是避免改变原data的内存地址指向
|
|
120
|
+
for (let item of data) {
|
|
121
|
+
//data=[{},{},'toggle','close']
|
|
122
|
+
renderFn(item);
|
|
123
|
+
toolsEl.appendChild(item.wrapEl);
|
|
124
|
+
item?.action?.(item);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return toolsEl;
|
|
128
|
+
}
|
|
129
|
+
export default createTools;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 12:05:04
|
|
3
|
+
* @namespace getClasses
|
|
4
|
+
* @description Utility function to retrieve the list of CSS classes applied to a DOM element.
|
|
5
|
+
* This function will fetch the `class` attribute from a DOM element and parse it into an array of class names.
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
import getEl from "./getEl";
|
|
9
|
+
import parseClasses from "./parseClasses";
|
|
10
|
+
const getClasses = (target) => {
|
|
11
|
+
// Get the DOM element from the selector or Node
|
|
12
|
+
let el = getEl(target);
|
|
13
|
+
// If the element is found, parse and return its classes; otherwise, return an empty array
|
|
14
|
+
return el ? parseClasses(el.getAttribute('class') || '') : [];
|
|
15
|
+
};
|
|
16
|
+
export default getClasses;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 12:05:04
|
|
3
|
+
* @namespace getClasses
|
|
4
|
+
* @description Utility function to retrieve the list of CSS classes applied to a DOM element.
|
|
5
|
+
* This function will fetch the `class` attribute from a DOM element and parse it into an array of class names.
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import getEl from "./getEl";
|
|
10
|
+
import parseClasses from "./parseClasses";
|
|
11
|
+
|
|
12
|
+
const getClasses = (target: string | Node | null): string[] => {
|
|
13
|
+
// Get the DOM element from the selector or Node
|
|
14
|
+
let el = getEl(target);
|
|
15
|
+
|
|
16
|
+
// If the element is found, parse and return its classes; otherwise, return an empty array
|
|
17
|
+
return el ? parseClasses((el as Element).getAttribute('class') || '') : [];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default getClasses;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 12:10:08
|
|
3
|
+
* @namespace hasClasses
|
|
4
|
+
* @description Checks if the given DOM element has all of the specified CSS classes.
|
|
5
|
+
*
|
|
6
|
+
* This function takes one or more class names and checks if the target DOM element contains **all** the specified classes.
|
|
7
|
+
* It returns `true` if the element has all the specified classes, otherwise `false`.
|
|
8
|
+
*
|
|
9
|
+
* @param {string | Node | null} target - The target DOM element, Node, or CSS selector to check for the classes.
|
|
10
|
+
* - If a string, it is interpreted as a CSS selector (e.g., `#elementId`).
|
|
11
|
+
* - If a DOM node or element is passed, it is used directly.
|
|
12
|
+
* - If `null` is passed, the function will return `false` because there is no element to check.
|
|
13
|
+
* @param {string | string[]} classes - A string or an array of class names to check.
|
|
14
|
+
* - A space/comma-separated string of class names (e.g., `'class1 class2'`).
|
|
15
|
+
* - An array of class names (e.g., `['class1', 'class2']`).
|
|
16
|
+
* @returns {boolean} Returns `true` if the element has all of the specified classes, otherwise `false`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ax.hasClasses('#demo', 'demo01 demo02'); // Returns true if element with id 'demo' has both 'demo01' and 'demo02'
|
|
20
|
+
* ax.hasClasses('#demo', ['demo01', 'demo02']); // Returns true if element with id 'demo' has both 'demo01' and 'demo02'
|
|
21
|
+
*/
|
|
22
|
+
'use strict';
|
|
23
|
+
import getEl from "./getEl";
|
|
24
|
+
import parseClasses from "./parseClasses";
|
|
25
|
+
const hasClasses = (target, classes) => {
|
|
26
|
+
const el = getEl(target), arr = parseClasses(classes);
|
|
27
|
+
if (!el || arr.length === 0)
|
|
28
|
+
return false;
|
|
29
|
+
for (let k of arr) {
|
|
30
|
+
if (!el.classList.contains(k)) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
};
|
|
36
|
+
export default hasClasses;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 12:10:08
|
|
3
|
+
* @namespace hasClasses
|
|
4
|
+
* @description Checks if the given DOM element has all of the specified CSS classes.
|
|
5
|
+
*
|
|
6
|
+
* This function takes one or more class names and checks if the target DOM element contains **all** the specified classes.
|
|
7
|
+
* It returns `true` if the element has all the specified classes, otherwise `false`.
|
|
8
|
+
*
|
|
9
|
+
* @param {string | Node | null} target - The target DOM element, Node, or CSS selector to check for the classes.
|
|
10
|
+
* - If a string, it is interpreted as a CSS selector (e.g., `#elementId`).
|
|
11
|
+
* - If a DOM node or element is passed, it is used directly.
|
|
12
|
+
* - If `null` is passed, the function will return `false` because there is no element to check.
|
|
13
|
+
* @param {string | string[]} classes - A string or an array of class names to check.
|
|
14
|
+
* - A space/comma-separated string of class names (e.g., `'class1 class2'`).
|
|
15
|
+
* - An array of class names (e.g., `['class1', 'class2']`).
|
|
16
|
+
* @returns {boolean} Returns `true` if the element has all of the specified classes, otherwise `false`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ax.hasClasses('#demo', 'demo01 demo02'); // Returns true if element with id 'demo' has both 'demo01' and 'demo02'
|
|
20
|
+
* ax.hasClasses('#demo', ['demo01', 'demo02']); // Returns true if element with id 'demo' has both 'demo01' and 'demo02'
|
|
21
|
+
*/
|
|
22
|
+
'use strict';
|
|
23
|
+
|
|
24
|
+
import getEl from "./getEl";
|
|
25
|
+
import parseClasses from "./parseClasses";
|
|
26
|
+
|
|
27
|
+
const hasClasses = (target: string | Node | null, classes: string | string[]): boolean => {
|
|
28
|
+
const el = getEl(target),
|
|
29
|
+
arr = parseClasses(classes);
|
|
30
|
+
if (!el || arr.length === 0) return false;
|
|
31
|
+
|
|
32
|
+
for (let k of arr) {
|
|
33
|
+
if (!(el as Element).classList.contains(k)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
export default hasClasses;
|
package/src/namespace.js
ADDED
package/src/namespace.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
import COMMA from "./comma";
|
|
16
|
+
import SPACE from "./space";
|
|
17
|
+
import trim from "./trim";
|
|
18
|
+
const parseClasses = (data) => {
|
|
19
|
+
let separator, result = [];
|
|
20
|
+
if (Array.isArray(data)) {
|
|
21
|
+
// If data is already an array, filter out invalid values
|
|
22
|
+
result = data.filter((k) => k && typeof k === 'string');
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
// Trim the input string and handle multiple spaces
|
|
26
|
+
data = trim(data);
|
|
27
|
+
// Use comma as the separator if present, otherwise use space
|
|
28
|
+
separator = data.includes(COMMA) ? COMMA : SPACE;
|
|
29
|
+
result = data.split(separator);
|
|
30
|
+
}
|
|
31
|
+
// Trim each item globally and filter out any empty strings
|
|
32
|
+
return result.map((k) => trim(k, 'global')).filter(Boolean);
|
|
33
|
+
};
|
|
34
|
+
export default parseClasses;
|
|
@@ -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
package/src/space.ts
ADDED
package/src/trim.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 14:15:24
|
|
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] - 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 ''.
|
|
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 = '') => {
|
|
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 14:15:24
|
|
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] - 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 ''.
|
|
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' | ''=''): 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;
|