@codady/utils 0.0.38 → 0.0.39
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 +18 -0
- package/dist/utils.cjs.js +472 -23
- package/dist/utils.cjs.min.js +2 -2
- package/dist/utils.esm.js +472 -23
- package/dist/utils.esm.min.js +2 -2
- package/dist/utils.umd.js +472 -23
- package/dist/utils.umd.min.js +2 -2
- package/dist.zip +0 -0
- package/examples/ajax-get.html +59 -0
- package/examples/ajax-hook.html +55 -0
- package/examples/ajax-method.html +36 -0
- package/examples/ajax-post.html +37 -0
- package/examples/buildUrl.html +99 -0
- package/examples/getUrlHash.html +71 -0
- package/modules.js +13 -1
- package/modules.ts +13 -1
- package/package.json +1 -1
- package/src/ajax.js +363 -0
- package/src/ajax.ts +450 -0
- package/src/buildUrl.js +64 -0
- package/src/buildUrl.ts +86 -0
- package/src/capitalize - /345/211/257/346/234/254.js" +19 -0
- package/src/capitalize.js +19 -0
- package/src/capitalize.ts +20 -0
- package/src/cleanQueryString.js +19 -0
- package/src/cleanQueryString.ts +20 -0
- package/src/getBodyHTML.js +53 -0
- package/src/getBodyHTML.ts +61 -0
- package/src/getEl.js +1 -1
- package/src/getEl.ts +6 -5
- package/src/getEls.js +1 -1
- package/src/getEls.ts +5 -5
- package/src/getUrlHash.js +37 -0
- package/src/getUrlHash.ts +39 -0
- package/src/isEmpty.js +24 -23
- package/src/isEmpty.ts +26 -23
- package/src/sliceStrEnd.js +63 -0
- package/src/sliceStrEnd.ts +60 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/20 13:55:13
|
|
3
|
+
* Cleans a query string by removing the leading '?' or '&' character if it exists.
|
|
4
|
+
* This ensures that the string is in a valid format for use in URLSearchParams.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} data - The query string to clean.
|
|
7
|
+
* @returns {string} The cleaned query string without leading '?' or '&'.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* cleanQueryString('?key=value&name=John'); // Returns 'key=value&name=John'
|
|
11
|
+
* cleanQueryString('&key=value&name=John'); // Returns 'key=value&name=John'
|
|
12
|
+
* cleanQueryString('key=value&name=John'); // Returns 'key=value&name=John'
|
|
13
|
+
*/
|
|
14
|
+
const cleanQueryString = (data: string): string => {
|
|
15
|
+
return typeof data === 'string' && (data.startsWith('?') || data.startsWith('&'))
|
|
16
|
+
? data.slice(1) // Remove the leading '?' or '&'
|
|
17
|
+
: data; // Return the string as-is if no leading character is present
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default cleanQueryString;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/19 15:58:30
|
|
3
|
+
* Extract the inner content of the <body> tag from an HTML string.
|
|
4
|
+
* This method uses the browser's native DOMParser for reliable parsing,
|
|
5
|
+
* which is safer and more robust than using Regular Expressions.
|
|
6
|
+
* * @param {string} htmlText - The raw HTML string received from an asynchronous request.
|
|
7
|
+
* @param {string} [selector] - Optional ID or selector (e.g., '.hello' or '#hello').
|
|
8
|
+
* @returns {string} - The trimmed innerHTML of the body, or the original text if parsing fails.
|
|
9
|
+
*/
|
|
10
|
+
const getBodyHTML = (htmlText, selector) => {
|
|
11
|
+
// Return early if the input is not a valid string or doesn't look like HTML
|
|
12
|
+
if (!htmlText || typeof htmlText !== 'string') {
|
|
13
|
+
return '';
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
/**
|
|
17
|
+
* 1. Initialize DOMParser
|
|
18
|
+
* DOMParser can parse XML or HTML source code from a string into a DOM Document.
|
|
19
|
+
*/
|
|
20
|
+
const parser = new DOMParser(),
|
|
21
|
+
/**
|
|
22
|
+
* 2. Parse the string into a Document object
|
|
23
|
+
* The 'text/html' mime type tells the parser to use the HTML lifting/error-correction algorithms.
|
|
24
|
+
*/
|
|
25
|
+
doc = parser.parseFromString(htmlText, 'text/html'),
|
|
26
|
+
/**
|
|
27
|
+
* 3. Extract body content
|
|
28
|
+
* Accessing doc.body automatically skips <!DOCTYPE>, <html>, and <head> sections.
|
|
29
|
+
* We use trim() to remove leading/trailing whitespace common in server responses.
|
|
30
|
+
*/
|
|
31
|
+
bodyContent = doc.body.innerHTML;
|
|
32
|
+
if (selector) {
|
|
33
|
+
// Normalize hash: ensure it's a valid ID selector
|
|
34
|
+
const targetEl = doc.querySelector(selector);
|
|
35
|
+
if (targetEl) {
|
|
36
|
+
return targetEl.innerHTML;
|
|
37
|
+
}
|
|
38
|
+
// If hash is provided but element not found, we fallback to body or warn
|
|
39
|
+
console.warn(`Element with selector "${selector}" not found in the HTML.`);
|
|
40
|
+
}
|
|
41
|
+
return bodyContent ? bodyContent.trim() : htmlText;
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
/**
|
|
45
|
+
* Fallback mechanism
|
|
46
|
+
* If the environment (like Node.js without JSDOM) doesn't support DOMParser,
|
|
47
|
+
* or the string is malformed, we log the error and return the raw text.
|
|
48
|
+
*/
|
|
49
|
+
console.error("Failed to parse HTML content using DOMParser:", error);
|
|
50
|
+
return htmlText;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
export default getBodyHTML;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/19 15:58:30
|
|
3
|
+
* Extract the inner content of the <body> tag from an HTML string.
|
|
4
|
+
* This method uses the browser's native DOMParser for reliable parsing,
|
|
5
|
+
* which is safer and more robust than using Regular Expressions.
|
|
6
|
+
* * @param {string} htmlText - The raw HTML string received from an asynchronous request.
|
|
7
|
+
* @param {string} [selector] - Optional ID or selector (e.g., '.hello' or '#hello').
|
|
8
|
+
* @returns {string} - The trimmed innerHTML of the body, or the original text if parsing fails.
|
|
9
|
+
*/
|
|
10
|
+
const getBodyHTML = (htmlText: string, selector?: string): string => {
|
|
11
|
+
// Return early if the input is not a valid string or doesn't look like HTML
|
|
12
|
+
if (!htmlText || typeof htmlText !== 'string') {
|
|
13
|
+
return '';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
/**
|
|
18
|
+
* 1. Initialize DOMParser
|
|
19
|
+
* DOMParser can parse XML or HTML source code from a string into a DOM Document.
|
|
20
|
+
*/
|
|
21
|
+
const parser: DOMParser = new DOMParser(),
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 2. Parse the string into a Document object
|
|
25
|
+
* The 'text/html' mime type tells the parser to use the HTML lifting/error-correction algorithms.
|
|
26
|
+
*/
|
|
27
|
+
doc: Document = parser.parseFromString(htmlText, 'text/html'),
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 3. Extract body content
|
|
31
|
+
* Accessing doc.body automatically skips <!DOCTYPE>, <html>, and <head> sections.
|
|
32
|
+
* We use trim() to remove leading/trailing whitespace common in server responses.
|
|
33
|
+
*/
|
|
34
|
+
bodyContent: string = doc.body.innerHTML;
|
|
35
|
+
|
|
36
|
+
if (selector) {
|
|
37
|
+
// Normalize hash: ensure it's a valid ID selector
|
|
38
|
+
const targetEl = doc.querySelector(selector);
|
|
39
|
+
|
|
40
|
+
if (targetEl) {
|
|
41
|
+
return targetEl.innerHTML;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// If hash is provided but element not found, we fallback to body or warn
|
|
45
|
+
console.warn(`Element with selector "${selector}" not found in the HTML.`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
return bodyContent ? bodyContent.trim() : htmlText;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
/**
|
|
52
|
+
* Fallback mechanism
|
|
53
|
+
* If the environment (like Node.js without JSDOM) doesn't support DOMParser,
|
|
54
|
+
* or the string is malformed, we log the error and return the raw text.
|
|
55
|
+
*/
|
|
56
|
+
console.error("Failed to parse HTML content using DOMParser:", error);
|
|
57
|
+
return htmlText;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export default getBodyHTML;
|
package/src/getEl.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since Last modified: 2026/01/
|
|
2
|
+
* @since Last modified: 2026/01/19 10:15:24
|
|
3
3
|
* @function getEl
|
|
4
4
|
* @description Get a DOM node (real or virtual). Supports obtaining nodes from various types (not just HTMLElement).
|
|
5
5
|
* @param {string|Node} obj - Can be a node selector (e.g., #id, .classname, NODENAME, [attribute]) or any node type (HTMLElement, Node, DocumentFragment, etc.).
|
package/src/getEl.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since Last modified: 2026/01/
|
|
2
|
+
* @since Last modified: 2026/01/19 10:15:24
|
|
3
3
|
* @function getEl
|
|
4
4
|
* @description Get a DOM node (real or virtual). Supports obtaining nodes from various types (not just HTMLElement).
|
|
5
5
|
* @param {string|Node} obj - Can be a node selector (e.g., #id, .classname, NODENAME, [attribute]) or any node type (HTMLElement, Node, DocumentFragment, etc.).
|
|
@@ -16,20 +16,21 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
import getDataType from './getDataType';
|
|
18
18
|
|
|
19
|
+
export type QueryableNode = Document | DocumentFragment | Element | SVGElement | HTMLElement | ShadowRoot;
|
|
19
20
|
|
|
20
|
-
const getEl = (obj: string |
|
|
21
|
+
const getEl = (obj: string | QueryableNode | null, wrap: string | QueryableNode | null = document.body): QueryableNode | null => {
|
|
21
22
|
let objType = getDataType(obj),
|
|
22
23
|
parType = getDataType(wrap),
|
|
23
24
|
parent = parType.includes('HTML') || parType === 'ShadowRoot' ? wrap : document.querySelector(wrap as string),
|
|
24
25
|
//如果parent是template节点,需要通过node.content.querySelector取得子节点
|
|
25
26
|
root = parent && parent instanceof HTMLTemplateElement ? parent.content : parent,
|
|
26
|
-
result:
|
|
27
|
+
result: QueryableNode | null = null;
|
|
27
28
|
if (obj) {
|
|
28
29
|
if (objType.includes('HTML')) {
|
|
29
|
-
result = (obj as
|
|
30
|
+
result = (obj as QueryableNode);
|
|
30
31
|
} else if (objType === 'String') {
|
|
31
32
|
try {
|
|
32
|
-
result = ((
|
|
33
|
+
result = ((root as QueryableNode) || document).querySelector((obj as string).trim());
|
|
33
34
|
//可能会报错,报错则返回null
|
|
34
35
|
} catch {
|
|
35
36
|
result = null;
|
package/src/getEls.js
CHANGED
package/src/getEls.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since Last modified: 2026/01/
|
|
2
|
+
* @since Last modified: 2026/01/19 10:10:38
|
|
3
3
|
* @function getEls
|
|
4
4
|
* @description Get the node array.
|
|
5
5
|
* @param {*} data - It can be a single node selector or a node array.
|
|
@@ -19,15 +19,15 @@
|
|
|
19
19
|
//返回:一个节点数组。
|
|
20
20
|
'use strict';
|
|
21
21
|
import getDataType from './getDataType';
|
|
22
|
-
import getEl from './getEl';
|
|
22
|
+
import getEl, { QueryableNode } from './getEl';
|
|
23
23
|
import isEmpty from './isEmpty';
|
|
24
24
|
|
|
25
|
-
const getEls = (data:
|
|
25
|
+
const getEls = (data: string | QueryableNode | null | (string | QueryableNode)[], parent: string | QueryableNode | null = document.body): QueryableNode[] => {
|
|
26
26
|
let type = getDataType(data),
|
|
27
27
|
parentEl = getEl(parent),
|
|
28
28
|
root = parentEl && parentEl instanceof HTMLTemplateElement ? parentEl.content : (parentEl || document),
|
|
29
29
|
result: any[] = [];
|
|
30
|
-
|
|
30
|
+
//data为空直接返回空数组
|
|
31
31
|
if (isEmpty(data)) { return result; }
|
|
32
32
|
if (type.includes('HTML')) {
|
|
33
33
|
//一个节点
|
|
@@ -39,7 +39,7 @@ const getEls = (data: Node | string | null | (Node | string)[], parent: Node |
|
|
|
39
39
|
return [...(root as any).querySelectorAll(k)];
|
|
40
40
|
}).flat();
|
|
41
41
|
} else if (type === 'Array') {
|
|
42
|
-
result = (data as
|
|
42
|
+
result = (data as QueryableNode[]).map((k: any) => {
|
|
43
43
|
return getEl(k, parentEl);
|
|
44
44
|
});
|
|
45
45
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/19 09:56:44
|
|
3
|
+
* Extracts the hash (fragment) portion of a given URL or file path.
|
|
4
|
+
* * This function utilizes the native `URL` constructor to parse the input.
|
|
5
|
+
* It is a pure logical operation and does not trigger any network requests.
|
|
6
|
+
* * @param {string} url - The URL string or file path to parse (e.g., 'index.html#section', 'https://example.com#top').
|
|
7
|
+
* @returns {string} - The hash part of the URL including the '#' character (e.g., '#section').
|
|
8
|
+
* Returns an empty string if no hash is present or if the input is invalid.
|
|
9
|
+
* * @example
|
|
10
|
+
* getUrlHash('https://example.com/page#header'); // Returns '#header'
|
|
11
|
+
* getUrlHash('src/assets/style.css#debug'); // Returns '#debug'
|
|
12
|
+
*/
|
|
13
|
+
const getUrlHash = (url) => {
|
|
14
|
+
// Return empty if input is null, undefined, or not a string
|
|
15
|
+
if (!url || typeof url !== 'string') {
|
|
16
|
+
return '';
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
/**
|
|
20
|
+
* Use window.location.origin as the base URL to support relative paths.
|
|
21
|
+
* The URL constructor follows the WHATWG spec:
|
|
22
|
+
* 1. If 'url' is absolute (e.g., starts with http://), the base is ignored.
|
|
23
|
+
* 2. If 'url' is relative (e.g., starts with ../), it is resolved against the base.
|
|
24
|
+
* In both cases, the .hash property correctly extracts the fragment.
|
|
25
|
+
*/
|
|
26
|
+
const baseUrl = window?.location?.origin || 'https://www.axui.cn', urlObj = new URL(url, baseUrl);
|
|
27
|
+
return urlObj.hash;
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
/**
|
|
31
|
+
* Catch parsing errors for malformed strings.
|
|
32
|
+
* Since this is a utility, we fail silently and return an empty string.
|
|
33
|
+
*/
|
|
34
|
+
return '';
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
export default getUrlHash;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/19 09:56:44
|
|
3
|
+
* Extracts the hash (fragment) portion of a given URL or file path.
|
|
4
|
+
* * This function utilizes the native `URL` constructor to parse the input.
|
|
5
|
+
* It is a pure logical operation and does not trigger any network requests.
|
|
6
|
+
* * @param {string} url - The URL string or file path to parse (e.g., 'index.html#section', 'https://example.com#top').
|
|
7
|
+
* @returns {string} - The hash part of the URL including the '#' character (e.g., '#section').
|
|
8
|
+
* Returns an empty string if no hash is present or if the input is invalid.
|
|
9
|
+
* * @example
|
|
10
|
+
* getUrlHash('https://example.com/page#header'); // Returns '#header'
|
|
11
|
+
* getUrlHash('src/assets/style.css#debug'); // Returns '#debug'
|
|
12
|
+
*/
|
|
13
|
+
const getUrlHash = (url: string): string => {
|
|
14
|
+
// Return empty if input is null, undefined, or not a string
|
|
15
|
+
if (!url || typeof url !== 'string') {
|
|
16
|
+
return '';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
/**
|
|
21
|
+
* Use window.location.origin as the base URL to support relative paths.
|
|
22
|
+
* The URL constructor follows the WHATWG spec:
|
|
23
|
+
* 1. If 'url' is absolute (e.g., starts with http://), the base is ignored.
|
|
24
|
+
* 2. If 'url' is relative (e.g., starts with ../), it is resolved against the base.
|
|
25
|
+
* In both cases, the .hash property correctly extracts the fragment.
|
|
26
|
+
*/
|
|
27
|
+
const baseUrl: string = window?.location?.origin || 'https://www.axui.cn',
|
|
28
|
+
urlObj: URL = new URL(url, baseUrl);
|
|
29
|
+
return urlObj.hash;
|
|
30
|
+
} catch (error) {
|
|
31
|
+
/**
|
|
32
|
+
* Catch parsing errors for malformed strings.
|
|
33
|
+
* Since this is a utility, we fail silently and return an empty string.
|
|
34
|
+
*/
|
|
35
|
+
return '';
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default getUrlHash;
|
package/src/isEmpty.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since Last modified:
|
|
2
|
+
* @since Last modified: 2026/01/20 12:02:43
|
|
3
3
|
* @function isEmpty
|
|
4
4
|
* @description Determine whether it is empty data.The data itself is empty data: 0| ''|false|undefined|null; <br>empty function: function () {}|() => {}; <br>empty array and empty objects: []|{}| [null]| [ undefined]| ['']| [""];<br> empty symbol object: symbol()|symbol.For(), will be judged as empty.
|
|
5
5
|
* @param {*} data - Can be any data
|
|
@@ -16,30 +16,31 @@ const isEmpty = (data) => {
|
|
|
16
16
|
let type = getDataType(data), flag;
|
|
17
17
|
if (!data) {
|
|
18
18
|
//0,'',false,undefined,null
|
|
19
|
-
|
|
19
|
+
return true;
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
//[null]|[undefined]|['']|[""]
|
|
24
|
-
//[]|{}
|
|
25
|
-
//Symbol()|Symbol.for()
|
|
26
|
-
//Set,Map
|
|
27
|
-
//Date/Regex
|
|
28
|
-
flag = (type === 'Object') ? (Object.keys(data).length === 0) :
|
|
29
|
-
(type === 'Array') ? data.join('') === '' :
|
|
30
|
-
(type === 'Function') ? (data.toString().replace(/\s+/g, '').match(/{.*}/g)[0] === '{}') :
|
|
31
|
-
(type === 'Symbol') ? (data.toString().replace(/\s+/g, '').match(/\(.*\)/g)[0] === '()') :
|
|
32
|
-
(type === 'Set' || type === 'Map') ? data.size === 0 :
|
|
33
|
-
type === 'Date' ? isNaN(data.getTime()) :
|
|
34
|
-
type === 'RegExp' ? data.source === '' :
|
|
35
|
-
type === 'ArrayBuffer' ? data.byteLength === 0 :
|
|
36
|
-
(type === 'NodeList' || type === 'HTMLCollection') ? data.length === 0 :
|
|
37
|
-
('length' in data && typeof data.length === 'number') ? data.length === 0 :
|
|
38
|
-
('size' in data && typeof data.size === 'number') ? data.size === 0 :
|
|
39
|
-
(type === 'Error' || data instanceof Error) ? data.message === '' :
|
|
40
|
-
(type.includes('Array') && (['Uint8Array', 'Int8Array', 'Uint16Array', 'Int16Array', 'Uint32Array', 'Int32Array', 'Float32Array', 'Float64Array'].includes(type))) ? data.length === 0 :
|
|
41
|
-
false;
|
|
21
|
+
if (['String', 'Number', 'Boolean'].includes(type)) {
|
|
22
|
+
return false;
|
|
42
23
|
}
|
|
24
|
+
//function(){}|()=>{}
|
|
25
|
+
//[null]|[undefined]|['']|[""]
|
|
26
|
+
//[]|{}
|
|
27
|
+
//Symbol()|Symbol.for()
|
|
28
|
+
//Set,Map
|
|
29
|
+
//Date/Regex
|
|
30
|
+
flag = (type === 'Object') ? (Object.keys(data).length === 0) :
|
|
31
|
+
(type === 'Array') ? data.join('') === '' :
|
|
32
|
+
(type === 'Function') ? (data.toString().replace(/\s+/g, '').match(/{.*}/g)[0] === '{}') :
|
|
33
|
+
(type === 'Symbol') ? (data.toString().replace(/\s+/g, '').match(/\(.*\)/g)[0] === '()') :
|
|
34
|
+
(type === 'Set' || type === 'Map') ? data.size === 0 :
|
|
35
|
+
type === 'Date' ? isNaN(data.getTime()) :
|
|
36
|
+
type === 'RegExp' ? data.source === '' :
|
|
37
|
+
type === 'ArrayBuffer' ? data.byteLength === 0 :
|
|
38
|
+
(type === 'NodeList' || type === 'HTMLCollection') ? data.length === 0 :
|
|
39
|
+
('length' in data && typeof data.length === 'number') ? data.length === 0 :
|
|
40
|
+
('size' in data && typeof data.size === 'number') ? data.size === 0 :
|
|
41
|
+
(type === 'Error' || data instanceof Error) ? data.message === '' :
|
|
42
|
+
(type.includes('Array') && (['Uint8Array', 'Int8Array', 'Uint16Array', 'Int16Array', 'Uint32Array', 'Int32Array', 'Float32Array', 'Float64Array'].includes(type))) ? data.length === 0 :
|
|
43
|
+
false;
|
|
43
44
|
return flag;
|
|
44
45
|
};
|
|
45
46
|
export default isEmpty;
|
package/src/isEmpty.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since Last modified:
|
|
2
|
+
* @since Last modified: 2026/01/20 12:02:43
|
|
3
3
|
* @function isEmpty
|
|
4
4
|
* @description Determine whether it is empty data.The data itself is empty data: 0| ''|false|undefined|null; <br>empty function: function () {}|() => {}; <br>empty array and empty objects: []|{}| [null]| [ undefined]| ['']| [""];<br> empty symbol object: symbol()|symbol.For(), will be judged as empty.
|
|
5
5
|
* @param {*} data - Can be any data
|
|
@@ -16,29 +16,32 @@ const isEmpty = (data: any): boolean => {
|
|
|
16
16
|
let type = getDataType(data), flag: boolean;
|
|
17
17
|
if (!data) {
|
|
18
18
|
//0,'',false,undefined,null
|
|
19
|
-
|
|
20
|
-
} else {
|
|
21
|
-
//function(){}|()=>{}
|
|
22
|
-
//[null]|[undefined]|['']|[""]
|
|
23
|
-
//[]|{}
|
|
24
|
-
//Symbol()|Symbol.for()
|
|
25
|
-
//Set,Map
|
|
26
|
-
//Date/Regex
|
|
27
|
-
flag = (type === 'Object') ? (Object.keys(data).length === 0) :
|
|
28
|
-
(type === 'Array') ? data.join('') === '' :
|
|
29
|
-
(type === 'Function') ? (data.toString().replace(/\s+/g, '').match(/{.*}/g)[0] === '{}') :
|
|
30
|
-
(type === 'Symbol') ? (data.toString().replace(/\s+/g, '').match(/\(.*\)/g)[0] === '()') :
|
|
31
|
-
(type === 'Set' || type === 'Map') ? data.size === 0 :
|
|
32
|
-
type === 'Date' ? isNaN(data.getTime()) :
|
|
33
|
-
type === 'RegExp' ? data.source === '' :
|
|
34
|
-
type === 'ArrayBuffer' ? data.byteLength === 0 :
|
|
35
|
-
(type === 'NodeList' || type === 'HTMLCollection') ? data.length === 0 :
|
|
36
|
-
('length' in data && typeof data.length === 'number') ? data.length === 0 :
|
|
37
|
-
('size' in data && typeof data.size === 'number') ? data.size === 0 :
|
|
38
|
-
(type === 'Error' || data instanceof Error) ? data.message === '' :
|
|
39
|
-
(type.includes('Array') && (['Uint8Array', 'Int8Array', 'Uint16Array', 'Int16Array', 'Uint32Array', 'Int32Array', 'Float32Array', 'Float64Array'].includes(type))) ? data.length === 0 :
|
|
40
|
-
false;
|
|
19
|
+
return true;
|
|
41
20
|
}
|
|
21
|
+
if (['String', 'Number', 'Boolean'].includes(type)) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
//function(){}|()=>{}
|
|
25
|
+
//[null]|[undefined]|['']|[""]
|
|
26
|
+
//[]|{}
|
|
27
|
+
//Symbol()|Symbol.for()
|
|
28
|
+
//Set,Map
|
|
29
|
+
//Date/Regex
|
|
30
|
+
flag = (type === 'Object') ? (Object.keys(data).length === 0) :
|
|
31
|
+
(type === 'Array') ? data.join('') === '' :
|
|
32
|
+
(type === 'Function') ? (data.toString().replace(/\s+/g, '').match(/{.*}/g)[0] === '{}') :
|
|
33
|
+
(type === 'Symbol') ? (data.toString().replace(/\s+/g, '').match(/\(.*\)/g)[0] === '()') :
|
|
34
|
+
(type === 'Set' || type === 'Map') ? data.size === 0 :
|
|
35
|
+
type === 'Date' ? isNaN(data.getTime()) :
|
|
36
|
+
type === 'RegExp' ? data.source === '' :
|
|
37
|
+
type === 'ArrayBuffer' ? data.byteLength === 0 :
|
|
38
|
+
(type === 'NodeList' || type === 'HTMLCollection') ? data.length === 0 :
|
|
39
|
+
('length' in data && typeof data.length === 'number') ? data.length === 0 :
|
|
40
|
+
('size' in data && typeof data.size === 'number') ? data.size === 0 :
|
|
41
|
+
(type === 'Error' || data instanceof Error) ? data.message === '' :
|
|
42
|
+
(type.includes('Array') && (['Uint8Array', 'Int8Array', 'Uint16Array', 'Int16Array', 'Uint32Array', 'Int32Array', 'Float32Array', 'Float64Array'].includes(type))) ? data.length === 0 :
|
|
43
|
+
false;
|
|
44
|
+
|
|
42
45
|
return flag;
|
|
43
46
|
}
|
|
44
47
|
export default isEmpty;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2023-12-08 11:37:11
|
|
3
|
+
* @function sliceStrEnd
|
|
4
|
+
* @description Use keyword to cut forward or backwards from the string.
|
|
5
|
+
* @param {object} options - The parameter object of the function.
|
|
6
|
+
* @property {string} options.str - The string to be intercepted.Allow keyword many times.
|
|
7
|
+
* @property {string} [options.key='#'] - Keywords participating in intercepting.
|
|
8
|
+
* @property {string} [options.type='beforebegin'|'afterbegin'|'beforeend'|'afterend'] - Method of intercepting.The optional values include 'beforebegin','afterbegin','beforeend' and 'afterend'
|
|
9
|
+
* @property {boolean} [options.contain=true] - Whether the output result contains keyword.
|
|
10
|
+
* @returns {string|''} - Return the interception string does not change the original string.
|
|
11
|
+
* @see {@link https://codepen.io/axui/pen/NWoOKEY|demo @ codepen}
|
|
12
|
+
* @example
|
|
13
|
+
* ax.sliceStrEnd({str:'123#321',key:'#'});
|
|
14
|
+
* <!--return interception string -->
|
|
15
|
+
*/
|
|
16
|
+
//简介:使用关键字从字符串中向前或向后截取字符串。
|
|
17
|
+
//参数options:截取参数,是一个对象。
|
|
18
|
+
//参数属性str:将要被截取的字符串。允许key出现多次。
|
|
19
|
+
//参数属性key:截取的关键字。
|
|
20
|
+
//参数属性type:截取类型,可选值包括:'beforebegin','afterbegin','beforeend','afterend'。
|
|
21
|
+
//参数属性contain:输出结果是否包含key,默认true。
|
|
22
|
+
//返回:截取后的字符串,不改变原字符串。
|
|
23
|
+
'use strict';
|
|
24
|
+
const sliceStrEnd = ({ str = '', key = '#', type = 'afterend', contain = true }) => {
|
|
25
|
+
//str和key先强制转字符串
|
|
26
|
+
str = str.toString();
|
|
27
|
+
key = key.toString();
|
|
28
|
+
let result = '', indexKey = 0, lenKey = key.length, lenEnd = str.length, indexStart = 0;
|
|
29
|
+
//str和key不能为空,否则直接输出空值。允许填入0
|
|
30
|
+
if (!str || !key) {
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
str = str.trim();
|
|
34
|
+
if (str.includes(key)) {
|
|
35
|
+
if (type === 'beforebegin') {
|
|
36
|
+
indexKey = str.indexOf(key);
|
|
37
|
+
contain ? indexKey += lenKey : null;
|
|
38
|
+
//确认开始和结束
|
|
39
|
+
lenEnd = indexKey;
|
|
40
|
+
}
|
|
41
|
+
else if (type === 'afterbegin') {
|
|
42
|
+
indexKey = str.indexOf(key);
|
|
43
|
+
!contain ? indexKey += lenKey : null;
|
|
44
|
+
//确认开始和结束
|
|
45
|
+
indexStart = indexKey;
|
|
46
|
+
}
|
|
47
|
+
else if (type === 'beforeend') {
|
|
48
|
+
indexKey = str.lastIndexOf(key);
|
|
49
|
+
contain ? indexKey += lenKey : null;
|
|
50
|
+
//确认开始和结束
|
|
51
|
+
lenEnd = indexKey;
|
|
52
|
+
}
|
|
53
|
+
else if (type === 'afterend') {
|
|
54
|
+
indexKey = str.lastIndexOf(key);
|
|
55
|
+
!contain ? indexKey += lenKey : null;
|
|
56
|
+
//确认开始和结束
|
|
57
|
+
indexStart = indexKey;
|
|
58
|
+
}
|
|
59
|
+
result = str.substring(indexStart, lenEnd);
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
};
|
|
63
|
+
export default sliceStrEnd;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2023-12-08 11:37:11
|
|
3
|
+
* @function sliceStrEnd
|
|
4
|
+
* @description Use keyword to cut forward or backwards from the string.
|
|
5
|
+
* @param {object} options - The parameter object of the function.
|
|
6
|
+
* @property {string} options.str - The string to be intercepted.Allow keyword many times.
|
|
7
|
+
* @property {string} [options.key='#'] - Keywords participating in intercepting.
|
|
8
|
+
* @property {string} [options.type='beforebegin'|'afterbegin'|'beforeend'|'afterend'] - Method of intercepting.The optional values include 'beforebegin','afterbegin','beforeend' and 'afterend'
|
|
9
|
+
* @property {boolean} [options.contain=true] - Whether the output result contains keyword.
|
|
10
|
+
* @returns {string|''} - Return the interception string does not change the original string.
|
|
11
|
+
* @see {@link https://codepen.io/axui/pen/NWoOKEY|demo @ codepen}
|
|
12
|
+
* @example
|
|
13
|
+
* ax.sliceStrEnd({str:'123#321',key:'#'});
|
|
14
|
+
* <!--return interception string -->
|
|
15
|
+
*/
|
|
16
|
+
//简介:使用关键字从字符串中向前或向后截取字符串。
|
|
17
|
+
//参数options:截取参数,是一个对象。
|
|
18
|
+
//参数属性str:将要被截取的字符串。允许key出现多次。
|
|
19
|
+
//参数属性key:截取的关键字。
|
|
20
|
+
//参数属性type:截取类型,可选值包括:'beforebegin','afterbegin','beforeend','afterend'。
|
|
21
|
+
//参数属性contain:输出结果是否包含key,默认true。
|
|
22
|
+
//返回:截取后的字符串,不改变原字符串。
|
|
23
|
+
'use strict';
|
|
24
|
+
const sliceStrEnd = ({ str = '', key = '#', type = 'afterend', contain = true }: { str: string, key?: string, type?: 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend', contain?: boolean }): string => {
|
|
25
|
+
//str和key先强制转字符串
|
|
26
|
+
str = str.toString();
|
|
27
|
+
key = key.toString();
|
|
28
|
+
let result = '', indexKey = 0, lenKey = key.length, lenEnd = str.length, indexStart = 0;
|
|
29
|
+
//str和key不能为空,否则直接输出空值。允许填入0
|
|
30
|
+
if (!str || !key) {
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
str = str.trim();
|
|
34
|
+
if (str.includes(key)) {
|
|
35
|
+
if (type === 'beforebegin') {
|
|
36
|
+
indexKey = str.indexOf(key);
|
|
37
|
+
contain ? indexKey += lenKey : null;
|
|
38
|
+
//确认开始和结束
|
|
39
|
+
lenEnd = indexKey;
|
|
40
|
+
} else if (type === 'afterbegin') {
|
|
41
|
+
indexKey = str.indexOf(key);
|
|
42
|
+
!contain ? indexKey += lenKey : null;
|
|
43
|
+
//确认开始和结束
|
|
44
|
+
indexStart = indexKey;
|
|
45
|
+
} else if (type === 'beforeend') {
|
|
46
|
+
indexKey = str.lastIndexOf(key);
|
|
47
|
+
contain ? indexKey += lenKey : null;
|
|
48
|
+
//确认开始和结束
|
|
49
|
+
lenEnd = indexKey;
|
|
50
|
+
} else if (type === 'afterend') {
|
|
51
|
+
indexKey = str.lastIndexOf(key);
|
|
52
|
+
!contain ? indexKey += lenKey : null;
|
|
53
|
+
//确认开始和结束
|
|
54
|
+
indexStart = indexKey;
|
|
55
|
+
}
|
|
56
|
+
result = str.substring(indexStart, lenEnd);
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
export default sliceStrEnd;
|