@tanwan/utils 1.0.0 → 1.2.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/CHANGELOG.md +19 -0
- package/dist/addClass.cjs +19 -0
- package/dist/addClass.d.cts +16 -0
- package/dist/addClass.d.ts +17 -0
- package/dist/addClass.js +19 -0
- package/dist/{string/camelCase.cjs → camelCase.cjs} +0 -1
- package/dist/{string/camelCase.d.cts → camelCase.d.cts} +1 -2
- package/dist/{string/camelCase.d.ts → camelCase.d.ts} +1 -2
- package/dist/{string/camelCase.js → camelCase.js} +0 -3
- package/dist/{string/capitalize.cjs → capitalize.cjs} +1 -1
- package/dist/{string/capitalize.d.cts → capitalize.d.cts} +2 -3
- package/dist/{string/capitalize.d.ts → capitalize.d.ts} +2 -3
- package/dist/{string/capitalize.js → capitalize.js} +1 -3
- package/dist/{array/chunk.cjs → chunk.cjs} +1 -1
- package/dist/{array/chunk.d.cts → chunk.d.cts} +2 -3
- package/dist/{array/chunk.d.ts → chunk.d.ts} +2 -3
- package/dist/{array/chunk.js → chunk.js} +1 -3
- package/dist/{number/clamp.cjs → clamp.cjs} +1 -1
- package/dist/{number/clamp.d.cts → clamp.d.cts} +2 -3
- package/dist/{number/clamp.d.ts → clamp.d.ts} +2 -3
- package/dist/{number/clamp.js → clamp.js} +1 -3
- package/dist/convertCurrencyToChinese.cjs +56 -0
- package/dist/convertCurrencyToChinese.d.cts +18 -0
- package/dist/convertCurrencyToChinese.d.ts +19 -0
- package/dist/convertCurrencyToChinese.js +56 -0
- package/dist/copyToClipboard.cjs +38 -0
- package/dist/copyToClipboard.d.cts +17 -0
- package/dist/copyToClipboard.d.ts +18 -0
- package/dist/copyToClipboard.js +38 -0
- package/dist/createElement.cjs +27 -0
- package/dist/createElement.d.cts +21 -0
- package/dist/createElement.d.ts +22 -0
- package/dist/createElement.js +27 -0
- package/dist/{function/debounce.cjs → debounce.cjs} +1 -1
- package/dist/{function/debounce.d.cts → debounce.d.cts} +2 -3
- package/dist/{function/debounce.d.ts → debounce.d.ts} +2 -3
- package/dist/{function/debounce.js → debounce.js} +1 -3
- package/dist/debounceSubmit.cjs +34 -0
- package/dist/debounceSubmit.d.cts +22 -0
- package/dist/debounceSubmit.d.ts +23 -0
- package/dist/debounceSubmit.js +34 -0
- package/dist/decodeBase64.cjs +19 -0
- package/dist/decodeBase64.d.cts +16 -0
- package/dist/decodeBase64.d.ts +17 -0
- package/dist/decodeBase64.js +19 -0
- package/dist/{base/deepClone.cjs → deepClone.cjs} +3 -3
- package/dist/{base/deepClone.d.cts → deepClone.d.cts} +2 -3
- package/dist/{base/deepClone.d.ts → deepClone.d.ts} +2 -3
- package/dist/{base/deepClone.js → deepClone.js} +1 -3
- package/dist/deepMerge.cjs +39 -0
- package/dist/deepMerge.d.cts +25 -0
- package/dist/deepMerge.d.ts +26 -0
- package/dist/deepMerge.js +39 -0
- package/dist/delay.cjs +8 -0
- package/dist/delay.d.cts +18 -0
- package/dist/delay.d.ts +19 -0
- package/dist/delay.js +8 -0
- package/dist/difference.cjs +24 -0
- package/dist/difference.d.cts +20 -0
- package/dist/difference.d.ts +21 -0
- package/dist/difference.js +24 -0
- package/dist/downloadFile.cjs +31 -0
- package/dist/downloadFile.d.cts +20 -0
- package/dist/downloadFile.d.ts +21 -0
- package/dist/downloadFile.js +31 -0
- package/dist/encodeBase64.cjs +20 -0
- package/dist/encodeBase64.d.cts +17 -0
- package/dist/encodeBase64.d.ts +18 -0
- package/dist/encodeBase64.js +20 -0
- package/dist/{array/flatten.cjs → flatten.cjs} +1 -1
- package/dist/{array/flatten.d.cts → flatten.d.cts} +2 -3
- package/dist/{array/flatten.d.ts → flatten.d.ts} +2 -3
- package/dist/{array/flatten.js → flatten.js} +1 -3
- package/dist/formatBankCard.cjs +19 -0
- package/dist/formatBankCard.d.cts +16 -0
- package/dist/formatBankCard.d.ts +17 -0
- package/dist/formatBankCard.js +19 -0
- package/dist/formatCurrency.cjs +7 -0
- package/dist/formatCurrency.d.cts +27 -0
- package/dist/formatCurrency.d.ts +28 -0
- package/dist/formatCurrency.js +7 -0
- package/dist/{date/formatDate.cjs → formatDate.cjs} +1 -1
- package/dist/{date/formatDate.d.cts → formatDate.d.cts} +2 -3
- package/dist/{date/formatDate.d.ts → formatDate.d.ts} +2 -3
- package/dist/{date/formatDate.js → formatDate.js} +1 -3
- package/dist/formatFileSize.cjs +33 -0
- package/dist/formatFileSize.d.cts +19 -0
- package/dist/formatFileSize.d.ts +20 -0
- package/dist/formatFileSize.js +33 -0
- package/dist/formatIdCard.cjs +21 -0
- package/dist/formatIdCard.d.cts +17 -0
- package/dist/formatIdCard.d.ts +18 -0
- package/dist/formatIdCard.js +21 -0
- package/dist/formatPhone.cjs +21 -0
- package/dist/formatPhone.d.cts +17 -0
- package/dist/formatPhone.d.ts +18 -0
- package/dist/formatPhone.js +21 -0
- package/dist/{number/formatThousands.cjs → formatThousands.cjs} +1 -1
- package/dist/{number/formatThousands.d.cts → formatThousands.d.cts} +2 -3
- package/dist/{number/formatThousands.d.ts → formatThousands.d.ts} +2 -3
- package/dist/{number/formatThousands.js → formatThousands.js} +1 -3
- package/dist/generateCode.cjs +29 -0
- package/dist/generateCode.d.cts +19 -0
- package/dist/generateCode.d.ts +20 -0
- package/dist/generateCode.js +29 -0
- package/dist/getAge.cjs +30 -0
- package/dist/getAge.d.cts +18 -0
- package/dist/getAge.d.ts +19 -0
- package/dist/getAge.js +30 -0
- package/dist/getByPath.cjs +29 -0
- package/dist/getByPath.d.cts +20 -0
- package/dist/getByPath.d.ts +21 -0
- package/dist/getByPath.js +29 -0
- package/dist/getCssVar.cjs +22 -0
- package/dist/getCssVar.d.cts +18 -0
- package/dist/getCssVar.d.ts +19 -0
- package/dist/getCssVar.js +22 -0
- package/dist/{date/getRelativeTime.cjs → getRelativeTime.cjs} +1 -1
- package/dist/{date/getRelativeTime.d.cts → getRelativeTime.d.cts} +2 -3
- package/dist/{date/getRelativeTime.d.ts → getRelativeTime.d.ts} +2 -3
- package/dist/{date/getRelativeTime.js → getRelativeTime.js} +1 -3
- package/dist/getScrollTop.cjs +28 -0
- package/dist/getScrollTop.d.cts +18 -0
- package/dist/getScrollTop.d.ts +19 -0
- package/dist/getScrollTop.js +28 -0
- package/dist/getStorage.cjs +27 -0
- package/dist/getStorage.d.cts +18 -0
- package/dist/getStorage.d.ts +19 -0
- package/dist/getStorage.js +27 -0
- package/dist/getStyle.cjs +20 -0
- package/dist/getStyle.d.cts +17 -0
- package/dist/getStyle.d.ts +18 -0
- package/dist/getStyle.js +20 -0
- package/dist/getTextWidth.cjs +26 -0
- package/dist/getTextWidth.d.cts +16 -0
- package/dist/getTextWidth.d.ts +17 -0
- package/dist/getTextWidth.js +26 -0
- package/dist/groupBy.cjs +36 -0
- package/dist/groupBy.d.cts +26 -0
- package/dist/groupBy.d.ts +27 -0
- package/dist/groupBy.js +36 -0
- package/dist/hasClass.cjs +19 -0
- package/dist/hasClass.d.cts +16 -0
- package/dist/hasClass.d.ts +17 -0
- package/dist/hasClass.js +19 -0
- package/dist/index.cjs +180 -36
- package/dist/index.d.cts +91 -19
- package/dist/index.d.ts +91 -19
- package/dist/index.iife.js +1 -1
- package/dist/index.js +91 -19
- package/dist/insertAfter.cjs +19 -0
- package/dist/insertAfter.d.cts +15 -0
- package/dist/insertAfter.d.ts +16 -0
- package/dist/insertAfter.js +19 -0
- package/dist/intersection.cjs +24 -0
- package/dist/intersection.d.cts +20 -0
- package/dist/intersection.d.ts +21 -0
- package/dist/intersection.js +24 -0
- package/dist/invert.cjs +21 -0
- package/dist/invert.d.cts +16 -0
- package/dist/invert.d.ts +17 -0
- package/dist/invert.js +21 -0
- package/dist/{base/isArray.cjs → isArray.cjs} +1 -1
- package/dist/{base/isArray.d.cts → isArray.d.cts} +2 -3
- package/dist/{base/isArray.d.ts → isArray.d.ts} +2 -3
- package/dist/{base/isArray.js → isArray.js} +1 -3
- package/dist/isBrowser.cjs +17 -0
- package/dist/isBrowser.d.cts +14 -0
- package/dist/isBrowser.d.ts +15 -0
- package/dist/isBrowser.js +17 -0
- package/dist/isDate.cjs +18 -0
- package/dist/isDate.d.cts +15 -0
- package/dist/isDate.d.ts +16 -0
- package/dist/isDate.js +18 -0
- package/dist/isEmpty.cjs +42 -0
- package/dist/isEmpty.d.cts +33 -0
- package/dist/isEmpty.d.ts +34 -0
- package/dist/isEmpty.js +42 -0
- package/dist/isFunction.cjs +19 -0
- package/dist/isFunction.d.cts +16 -0
- package/dist/isFunction.d.ts +17 -0
- package/dist/isFunction.js +19 -0
- package/dist/isInViewport.cjs +22 -0
- package/dist/isInViewport.d.cts +17 -0
- package/dist/isInViewport.d.ts +18 -0
- package/dist/isInViewport.js +22 -0
- package/dist/isNil.cjs +19 -0
- package/dist/isNil.d.cts +16 -0
- package/dist/isNil.d.ts +17 -0
- package/dist/isNil.js +19 -0
- package/dist/isNumber.cjs +19 -0
- package/dist/isNumber.d.cts +16 -0
- package/dist/isNumber.d.ts +17 -0
- package/dist/isNumber.js +19 -0
- package/dist/{base/isObject.cjs → isObject.cjs} +1 -1
- package/dist/{base/isObject.d.cts → isObject.d.cts} +2 -3
- package/dist/{base/isObject.d.ts → isObject.d.ts} +2 -3
- package/dist/{base/isObject.js → isObject.js} +1 -3
- package/dist/isPrimitive.cjs +21 -0
- package/dist/isPrimitive.d.cts +18 -0
- package/dist/isPrimitive.d.ts +19 -0
- package/dist/isPrimitive.js +21 -0
- package/dist/isPromise.cjs +18 -0
- package/dist/isPromise.d.cts +15 -0
- package/dist/isPromise.d.ts +16 -0
- package/dist/isPromise.js +18 -0
- package/dist/{base/isString.cjs → isString.cjs} +1 -1
- package/dist/{base/isString.d.cts → isString.d.cts} +2 -3
- package/dist/{base/isString.d.ts → isString.d.ts} +2 -3
- package/dist/{base/isString.js → isString.js} +1 -3
- package/dist/joinUrl.cjs +25 -0
- package/dist/joinUrl.d.cts +19 -0
- package/dist/joinUrl.d.ts +20 -0
- package/dist/joinUrl.js +25 -0
- package/dist/{string/kebabCase.d.cts → kebabCase.d.cts} +1 -2
- package/dist/{string/kebabCase.d.ts → kebabCase.d.ts} +1 -2
- package/dist/{string/kebabCase.js → kebabCase.js} +0 -2
- package/dist/lazyLoadImages.cjs +36 -0
- package/dist/lazyLoadImages.d.cts +19 -0
- package/dist/lazyLoadImages.d.ts +20 -0
- package/dist/lazyLoadImages.js +36 -0
- package/dist/listToTree.cjs +25 -0
- package/dist/listToTree.d.cts +32 -0
- package/dist/listToTree.d.ts +33 -0
- package/dist/listToTree.js +25 -0
- package/dist/mapValues.cjs +25 -0
- package/dist/mapValues.d.cts +20 -0
- package/dist/mapValues.d.ts +21 -0
- package/dist/mapValues.js +25 -0
- package/dist/maskString.cjs +28 -0
- package/dist/maskString.d.cts +24 -0
- package/dist/maskString.d.ts +25 -0
- package/dist/maskString.js +28 -0
- package/dist/memoize.cjs +32 -0
- package/dist/memoize.d.cts +22 -0
- package/dist/memoize.d.ts +23 -0
- package/dist/memoize.js +32 -0
- package/dist/off.cjs +22 -0
- package/dist/off.d.cts +19 -0
- package/dist/off.d.ts +20 -0
- package/dist/off.js +22 -0
- package/dist/omit.cjs +23 -0
- package/dist/omit.d.cts +17 -0
- package/dist/omit.d.ts +18 -0
- package/dist/omit.js +23 -0
- package/dist/on.cjs +21 -0
- package/dist/on.d.cts +18 -0
- package/dist/on.d.ts +19 -0
- package/dist/on.js +21 -0
- package/dist/once.cjs +31 -0
- package/dist/once.d.cts +20 -0
- package/dist/once.d.ts +21 -0
- package/dist/once.js +31 -0
- package/dist/parseQueryString.cjs +48 -0
- package/dist/parseQueryString.d.cts +21 -0
- package/dist/parseQueryString.d.ts +22 -0
- package/dist/parseQueryString.js +48 -0
- package/dist/pascalCase.cjs +18 -0
- package/dist/pascalCase.d.cts +15 -0
- package/dist/pascalCase.d.ts +16 -0
- package/dist/pascalCase.js +18 -0
- package/dist/pick.cjs +22 -0
- package/dist/pick.d.cts +17 -0
- package/dist/pick.d.ts +18 -0
- package/dist/pick.js +22 -0
- package/dist/{number/randomInt.cjs → randomInt.cjs} +1 -1
- package/dist/{number/randomInt.d.cts → randomInt.d.cts} +2 -3
- package/dist/{number/randomInt.d.ts → randomInt.d.ts} +2 -3
- package/dist/{number/randomInt.js → randomInt.js} +1 -3
- package/dist/range.cjs +27 -0
- package/dist/range.d.cts +20 -0
- package/dist/range.d.ts +21 -0
- package/dist/range.js +27 -0
- package/dist/removeClass.cjs +19 -0
- package/dist/removeClass.d.cts +16 -0
- package/dist/removeClass.d.ts +17 -0
- package/dist/removeClass.js +19 -0
- package/dist/removeElement.cjs +17 -0
- package/dist/removeElement.d.cts +14 -0
- package/dist/removeElement.d.ts +15 -0
- package/dist/removeElement.js +17 -0
- package/dist/removeStorage.cjs +19 -0
- package/dist/removeStorage.d.cts +14 -0
- package/dist/removeStorage.d.ts +15 -0
- package/dist/removeStorage.js +19 -0
- package/dist/retry.cjs +27 -0
- package/dist/retry.d.cts +17 -0
- package/dist/retry.d.ts +18 -0
- package/dist/retry.js +27 -0
- package/dist/sample.cjs +20 -0
- package/dist/sample.d.cts +16 -0
- package/dist/sample.d.ts +17 -0
- package/dist/sample.js +20 -0
- package/dist/scrollToElement.cjs +23 -0
- package/dist/scrollToElement.d.cts +16 -0
- package/dist/scrollToElement.d.ts +17 -0
- package/dist/scrollToElement.js +23 -0
- package/dist/setCssVar.cjs +22 -0
- package/dist/setCssVar.d.cts +18 -0
- package/dist/setCssVar.d.ts +19 -0
- package/dist/setCssVar.js +22 -0
- package/dist/setStorage.cjs +25 -0
- package/dist/setStorage.d.cts +17 -0
- package/dist/setStorage.d.ts +18 -0
- package/dist/setStorage.js +25 -0
- package/dist/setStyle.cjs +22 -0
- package/dist/setStyle.d.cts +19 -0
- package/dist/setStyle.d.ts +20 -0
- package/dist/setStyle.js +22 -0
- package/dist/{array/shuffle.cjs → shuffle.cjs} +1 -1
- package/dist/{array/shuffle.d.cts → shuffle.d.cts} +2 -3
- package/dist/{array/shuffle.d.ts → shuffle.d.ts} +2 -3
- package/dist/{array/shuffle.js → shuffle.js} +1 -3
- package/dist/snakeCase.cjs +18 -0
- package/dist/snakeCase.d.cts +15 -0
- package/dist/snakeCase.d.ts +16 -0
- package/dist/snakeCase.js +18 -0
- package/dist/sortBy.cjs +30 -0
- package/dist/sortBy.d.cts +20 -0
- package/dist/sortBy.d.ts +21 -0
- package/dist/sortBy.js +30 -0
- package/dist/stringifyQueryString.cjs +38 -0
- package/dist/stringifyQueryString.d.cts +21 -0
- package/dist/stringifyQueryString.d.ts +22 -0
- package/dist/stringifyQueryString.js +38 -0
- package/dist/{function/throttle.cjs → throttle.cjs} +1 -1
- package/dist/{function/throttle.d.cts → throttle.d.cts} +2 -3
- package/dist/{function/throttle.d.ts → throttle.d.ts} +2 -3
- package/dist/{function/throttle.js → throttle.js} +1 -3
- package/dist/toggleClass.cjs +22 -0
- package/dist/toggleClass.d.cts +19 -0
- package/dist/toggleClass.d.ts +20 -0
- package/dist/toggleClass.js +22 -0
- package/dist/treeFind.cjs +34 -0
- package/dist/treeFind.d.cts +25 -0
- package/dist/treeFind.d.ts +26 -0
- package/dist/treeFind.js +34 -0
- package/dist/treeToList.cjs +33 -0
- package/dist/treeToList.d.cts +24 -0
- package/dist/treeToList.d.ts +25 -0
- package/dist/treeToList.js +33 -0
- package/dist/truncate.cjs +23 -0
- package/dist/truncate.d.cts +19 -0
- package/dist/truncate.d.ts +20 -0
- package/dist/truncate.js +23 -0
- package/dist/uniqBy.cjs +30 -0
- package/dist/uniqBy.d.cts +20 -0
- package/dist/uniqBy.d.ts +21 -0
- package/dist/uniqBy.js +30 -0
- package/dist/{array/unique.cjs → unique.cjs} +1 -1
- package/dist/{array/unique.d.cts → unique.d.cts} +2 -3
- package/dist/{array/unique.d.ts → unique.d.ts} +2 -3
- package/dist/{array/unique.js → unique.js} +1 -3
- package/dist/uuid.cjs +23 -0
- package/dist/uuid.d.cts +16 -0
- package/dist/uuid.d.ts +17 -0
- package/dist/uuid.js +23 -0
- package/dist/validateEmail.cjs +19 -0
- package/dist/validateEmail.d.cts +16 -0
- package/dist/validateEmail.d.ts +17 -0
- package/dist/validateEmail.js +19 -0
- package/dist/validateIdCard.cjs +62 -0
- package/dist/validateIdCard.d.cts +15 -0
- package/dist/validateIdCard.d.ts +16 -0
- package/dist/validateIdCard.js +62 -0
- package/dist/validatePhone.cjs +19 -0
- package/dist/validatePhone.d.cts +16 -0
- package/dist/validatePhone.d.ts +17 -0
- package/dist/validatePhone.js +19 -0
- package/package.json +351 -58
- package/dist/array/chunk.d.cts.map +0 -1
- package/dist/array/chunk.d.ts.map +0 -1
- package/dist/array/chunk.js.map +0 -1
- package/dist/array/flatten.d.cts.map +0 -1
- package/dist/array/flatten.d.ts.map +0 -1
- package/dist/array/flatten.js.map +0 -1
- package/dist/array/shuffle.d.cts.map +0 -1
- package/dist/array/shuffle.d.ts.map +0 -1
- package/dist/array/shuffle.js.map +0 -1
- package/dist/array/unique.d.cts.map +0 -1
- package/dist/array/unique.d.ts.map +0 -1
- package/dist/array/unique.js.map +0 -1
- package/dist/base/deepClone.d.cts.map +0 -1
- package/dist/base/deepClone.d.ts.map +0 -1
- package/dist/base/deepClone.js.map +0 -1
- package/dist/base/isArray.d.cts.map +0 -1
- package/dist/base/isArray.d.ts.map +0 -1
- package/dist/base/isArray.js.map +0 -1
- package/dist/base/isObject.d.cts.map +0 -1
- package/dist/base/isObject.d.ts.map +0 -1
- package/dist/base/isObject.js.map +0 -1
- package/dist/base/isString.d.cts.map +0 -1
- package/dist/base/isString.d.ts.map +0 -1
- package/dist/base/isString.js.map +0 -1
- package/dist/date/formatDate.d.cts.map +0 -1
- package/dist/date/formatDate.d.ts.map +0 -1
- package/dist/date/formatDate.js.map +0 -1
- package/dist/date/getRelativeTime.d.cts.map +0 -1
- package/dist/date/getRelativeTime.d.ts.map +0 -1
- package/dist/date/getRelativeTime.js.map +0 -1
- package/dist/function/debounce.d.cts.map +0 -1
- package/dist/function/debounce.d.ts.map +0 -1
- package/dist/function/debounce.js.map +0 -1
- package/dist/function/throttle.d.cts.map +0 -1
- package/dist/function/throttle.d.ts.map +0 -1
- package/dist/function/throttle.js.map +0 -1
- package/dist/number/clamp.d.cts.map +0 -1
- package/dist/number/clamp.d.ts.map +0 -1
- package/dist/number/clamp.js.map +0 -1
- package/dist/number/formatThousands.d.cts.map +0 -1
- package/dist/number/formatThousands.d.ts.map +0 -1
- package/dist/number/formatThousands.js.map +0 -1
- package/dist/number/randomInt.d.cts.map +0 -1
- package/dist/number/randomInt.d.ts.map +0 -1
- package/dist/number/randomInt.js.map +0 -1
- package/dist/string/camelCase.d.cts.map +0 -1
- package/dist/string/camelCase.d.ts.map +0 -1
- package/dist/string/camelCase.js.map +0 -1
- package/dist/string/capitalize.d.cts.map +0 -1
- package/dist/string/capitalize.d.ts.map +0 -1
- package/dist/string/capitalize.js.map +0 -1
- package/dist/string/kebabCase.d.cts.map +0 -1
- package/dist/string/kebabCase.d.ts.map +0 -1
- package/dist/string/kebabCase.js.map +0 -1
- /package/dist/{string/kebabCase.cjs → kebabCase.cjs} +0 -0
package/dist/treeFind.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region src/tree/treeFind/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 在树结构中查找节点(深度优先)
|
|
4
|
+
*
|
|
5
|
+
* @param tree - 树结构数组
|
|
6
|
+
* @param predicate - 匹配函数
|
|
7
|
+
* @param options - 配置项
|
|
8
|
+
* @param options.children - 子节点属性名,默认 `'children'`
|
|
9
|
+
* @returns 第一个匹配的节点,未找到返回 `undefined`
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import treeFind from '@tanwan/utils/treeFind'
|
|
14
|
+
* const tree = [
|
|
15
|
+
* { id: 1, children: [{ id: 2 }] },
|
|
16
|
+
* { id: 3, children: [] },
|
|
17
|
+
* ]
|
|
18
|
+
* treeFind(tree, (node) => node.id === 2)
|
|
19
|
+
* // { id: 2 }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function treeFind(tree, predicate, options = {}) {
|
|
23
|
+
const { children = "children" } = options;
|
|
24
|
+
for (const node of tree) {
|
|
25
|
+
if (predicate(node)) return node;
|
|
26
|
+
const childNodes = node[children];
|
|
27
|
+
if (Array.isArray(childNodes)) {
|
|
28
|
+
const found = treeFind(childNodes, predicate, options);
|
|
29
|
+
if (found) return found;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { treeFind as default };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/tree/treeToList/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 将树结构拍平为数组(BFS 广度优先)
|
|
4
|
+
*
|
|
5
|
+
* @param tree - 树结构数组
|
|
6
|
+
* @param options - 配置项
|
|
7
|
+
* @param options.children - 子节点属性名,默认 `'children'`
|
|
8
|
+
* @returns 扁平数组(不含 children 属性)
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import treeToList from '@tanwan/utils/treeToList'
|
|
13
|
+
* const tree = [
|
|
14
|
+
* { id: 1, children: [{ id: 2, children: [] }] },
|
|
15
|
+
* { id: 3, children: [] },
|
|
16
|
+
* ]
|
|
17
|
+
* treeToList(tree)
|
|
18
|
+
* // [{ id: 1 }, { id: 3 }, { id: 2 }]
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function treeToList(tree, options = {}) {
|
|
22
|
+
const { children = "children" } = options;
|
|
23
|
+
const result = [];
|
|
24
|
+
const queue = [...tree];
|
|
25
|
+
while (queue.length) {
|
|
26
|
+
const { [children]: childNodes, ...rest } = queue.shift();
|
|
27
|
+
result.push(rest);
|
|
28
|
+
if (Array.isArray(childNodes)) queue.push(...childNodes);
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
module.exports = treeToList;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/tree/treeToList/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 将树结构拍平为数组(BFS 广度优先)
|
|
4
|
+
*
|
|
5
|
+
* @param tree - 树结构数组
|
|
6
|
+
* @param options - 配置项
|
|
7
|
+
* @param options.children - 子节点属性名,默认 `'children'`
|
|
8
|
+
* @returns 扁平数组(不含 children 属性)
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import treeToList from '@tanwan/utils/treeToList'
|
|
13
|
+
* const tree = [
|
|
14
|
+
* { id: 1, children: [{ id: 2, children: [] }] },
|
|
15
|
+
* { id: 3, children: [] },
|
|
16
|
+
* ]
|
|
17
|
+
* treeToList(tree)
|
|
18
|
+
* // [{ id: 1 }, { id: 3 }, { id: 2 }]
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
declare function treeToList<T extends Record<string, unknown>>(tree: T[], options?: {
|
|
22
|
+
children?: string;
|
|
23
|
+
}): Omit<T, "children">[];
|
|
24
|
+
export = treeToList;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/tree/treeToList/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 将树结构拍平为数组(BFS 广度优先)
|
|
4
|
+
*
|
|
5
|
+
* @param tree - 树结构数组
|
|
6
|
+
* @param options - 配置项
|
|
7
|
+
* @param options.children - 子节点属性名,默认 `'children'`
|
|
8
|
+
* @returns 扁平数组(不含 children 属性)
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import treeToList from '@tanwan/utils/treeToList'
|
|
13
|
+
* const tree = [
|
|
14
|
+
* { id: 1, children: [{ id: 2, children: [] }] },
|
|
15
|
+
* { id: 3, children: [] },
|
|
16
|
+
* ]
|
|
17
|
+
* treeToList(tree)
|
|
18
|
+
* // [{ id: 1 }, { id: 3 }, { id: 2 }]
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
declare function treeToList<T extends Record<string, unknown>>(tree: T[], options?: {
|
|
22
|
+
children?: string;
|
|
23
|
+
}): Omit<T, "children">[];
|
|
24
|
+
//#endregion
|
|
25
|
+
export { treeToList as default };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/tree/treeToList/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 将树结构拍平为数组(BFS 广度优先)
|
|
4
|
+
*
|
|
5
|
+
* @param tree - 树结构数组
|
|
6
|
+
* @param options - 配置项
|
|
7
|
+
* @param options.children - 子节点属性名,默认 `'children'`
|
|
8
|
+
* @returns 扁平数组(不含 children 属性)
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import treeToList from '@tanwan/utils/treeToList'
|
|
13
|
+
* const tree = [
|
|
14
|
+
* { id: 1, children: [{ id: 2, children: [] }] },
|
|
15
|
+
* { id: 3, children: [] },
|
|
16
|
+
* ]
|
|
17
|
+
* treeToList(tree)
|
|
18
|
+
* // [{ id: 1 }, { id: 3 }, { id: 2 }]
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function treeToList(tree, options = {}) {
|
|
22
|
+
const { children = "children" } = options;
|
|
23
|
+
const result = [];
|
|
24
|
+
const queue = [...tree];
|
|
25
|
+
while (queue.length) {
|
|
26
|
+
const { [children]: childNodes, ...rest } = queue.shift();
|
|
27
|
+
result.push(rest);
|
|
28
|
+
if (Array.isArray(childNodes)) queue.push(...childNodes);
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { treeToList as default };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/string/truncate/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 截断字符串并添加省略号
|
|
4
|
+
*
|
|
5
|
+
* @param str - 原字符串
|
|
6
|
+
* @param maxLength - 最大长度(含省略号)
|
|
7
|
+
* @param suffix - 省略号后缀,默认 `'…'`
|
|
8
|
+
* @returns 截断后的字符串
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import truncate from '@tanwan/utils/truncate'
|
|
13
|
+
* truncate('Hello World', 8) // 'Hello W…'
|
|
14
|
+
* truncate('Hello', 10) // 'Hello'(不截断)
|
|
15
|
+
* truncate('Hello World', 8, '...') // 'Hello...'
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function truncate(str, maxLength, suffix = "…") {
|
|
19
|
+
if (str.length <= maxLength) return str;
|
|
20
|
+
return str.slice(0, maxLength - suffix.length) + suffix;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
module.exports = truncate;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/string/truncate/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 截断字符串并添加省略号
|
|
4
|
+
*
|
|
5
|
+
* @param str - 原字符串
|
|
6
|
+
* @param maxLength - 最大长度(含省略号)
|
|
7
|
+
* @param suffix - 省略号后缀,默认 `'…'`
|
|
8
|
+
* @returns 截断后的字符串
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import truncate from '@tanwan/utils/truncate'
|
|
13
|
+
* truncate('Hello World', 8) // 'Hello W…'
|
|
14
|
+
* truncate('Hello', 10) // 'Hello'(不截断)
|
|
15
|
+
* truncate('Hello World', 8, '...') // 'Hello...'
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
declare function truncate(str: string, maxLength: number, suffix?: string): string;
|
|
19
|
+
export = truncate;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/string/truncate/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 截断字符串并添加省略号
|
|
4
|
+
*
|
|
5
|
+
* @param str - 原字符串
|
|
6
|
+
* @param maxLength - 最大长度(含省略号)
|
|
7
|
+
* @param suffix - 省略号后缀,默认 `'…'`
|
|
8
|
+
* @returns 截断后的字符串
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import truncate from '@tanwan/utils/truncate'
|
|
13
|
+
* truncate('Hello World', 8) // 'Hello W…'
|
|
14
|
+
* truncate('Hello', 10) // 'Hello'(不截断)
|
|
15
|
+
* truncate('Hello World', 8, '...') // 'Hello...'
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
declare function truncate(str: string, maxLength: number, suffix?: string): string;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { truncate as default };
|
package/dist/truncate.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/string/truncate/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 截断字符串并添加省略号
|
|
4
|
+
*
|
|
5
|
+
* @param str - 原字符串
|
|
6
|
+
* @param maxLength - 最大长度(含省略号)
|
|
7
|
+
* @param suffix - 省略号后缀,默认 `'…'`
|
|
8
|
+
* @returns 截断后的字符串
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import truncate from '@tanwan/utils/truncate'
|
|
13
|
+
* truncate('Hello World', 8) // 'Hello W…'
|
|
14
|
+
* truncate('Hello', 10) // 'Hello'(不截断)
|
|
15
|
+
* truncate('Hello World', 8, '...') // 'Hello...'
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function truncate(str, maxLength, suffix = "…") {
|
|
19
|
+
if (str.length <= maxLength) return str;
|
|
20
|
+
return str.slice(0, maxLength - suffix.length) + suffix;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { truncate as default };
|
package/dist/uniqBy.cjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region src/array/uniqBy/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 按指定 key 或迭代函数去重(保留第一个出现的元素)
|
|
4
|
+
*
|
|
5
|
+
* @param arr - 待去重数组
|
|
6
|
+
* @param iteratee - 去重依据:属性名或返回唯一标识的函数
|
|
7
|
+
* @returns 去重后的新数组
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import uniqBy from '@tanwan/utils/uniqBy'
|
|
12
|
+
* uniqBy([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 1, name: 'c' }], 'id')
|
|
13
|
+
* // [{ id: 1, name: 'a' }, { id: 2, name: 'b' }]
|
|
14
|
+
*
|
|
15
|
+
* uniqBy([1.1, 2.2, 1.3], Math.floor)
|
|
16
|
+
* // [1.1, 2.2]
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function uniqBy(arr, iteratee) {
|
|
20
|
+
const seen = /* @__PURE__ */ new Set();
|
|
21
|
+
const getValue = typeof iteratee === "function" ? iteratee : (item) => item[iteratee];
|
|
22
|
+
return arr.filter((item) => {
|
|
23
|
+
const value = getValue(item);
|
|
24
|
+
if (seen.has(value)) return false;
|
|
25
|
+
seen.add(value);
|
|
26
|
+
return true;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
module.exports = uniqBy;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/array/uniqBy/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 按指定 key 或迭代函数去重(保留第一个出现的元素)
|
|
4
|
+
*
|
|
5
|
+
* @param arr - 待去重数组
|
|
6
|
+
* @param iteratee - 去重依据:属性名或返回唯一标识的函数
|
|
7
|
+
* @returns 去重后的新数组
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import uniqBy from '@tanwan/utils/uniqBy'
|
|
12
|
+
* uniqBy([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 1, name: 'c' }], 'id')
|
|
13
|
+
* // [{ id: 1, name: 'a' }, { id: 2, name: 'b' }]
|
|
14
|
+
*
|
|
15
|
+
* uniqBy([1.1, 2.2, 1.3], Math.floor)
|
|
16
|
+
* // [1.1, 2.2]
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
declare function uniqBy<T>(arr: T[], iteratee: keyof T | ((item: T) => string | number)): T[];
|
|
20
|
+
export = uniqBy;
|
package/dist/uniqBy.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/array/uniqBy/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 按指定 key 或迭代函数去重(保留第一个出现的元素)
|
|
4
|
+
*
|
|
5
|
+
* @param arr - 待去重数组
|
|
6
|
+
* @param iteratee - 去重依据:属性名或返回唯一标识的函数
|
|
7
|
+
* @returns 去重后的新数组
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import uniqBy from '@tanwan/utils/uniqBy'
|
|
12
|
+
* uniqBy([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 1, name: 'c' }], 'id')
|
|
13
|
+
* // [{ id: 1, name: 'a' }, { id: 2, name: 'b' }]
|
|
14
|
+
*
|
|
15
|
+
* uniqBy([1.1, 2.2, 1.3], Math.floor)
|
|
16
|
+
* // [1.1, 2.2]
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
declare function uniqBy<T>(arr: T[], iteratee: keyof T | ((item: T) => string | number)): T[];
|
|
20
|
+
//#endregion
|
|
21
|
+
export { uniqBy as default };
|
package/dist/uniqBy.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region src/array/uniqBy/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 按指定 key 或迭代函数去重(保留第一个出现的元素)
|
|
4
|
+
*
|
|
5
|
+
* @param arr - 待去重数组
|
|
6
|
+
* @param iteratee - 去重依据:属性名或返回唯一标识的函数
|
|
7
|
+
* @returns 去重后的新数组
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import uniqBy from '@tanwan/utils/uniqBy'
|
|
12
|
+
* uniqBy([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 1, name: 'c' }], 'id')
|
|
13
|
+
* // [{ id: 1, name: 'a' }, { id: 2, name: 'b' }]
|
|
14
|
+
*
|
|
15
|
+
* uniqBy([1.1, 2.2, 1.3], Math.floor)
|
|
16
|
+
* // [1.1, 2.2]
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function uniqBy(arr, iteratee) {
|
|
20
|
+
const seen = /* @__PURE__ */ new Set();
|
|
21
|
+
const getValue = typeof iteratee === "function" ? iteratee : (item) => item[iteratee];
|
|
22
|
+
return arr.filter((item) => {
|
|
23
|
+
const value = getValue(item);
|
|
24
|
+
if (seen.has(value)) return false;
|
|
25
|
+
seen.add(value);
|
|
26
|
+
return true;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
export { uniqBy as default };
|
|
@@ -3,11 +3,10 @@
|
|
|
3
3
|
* 数组去重
|
|
4
4
|
* @example
|
|
5
5
|
* ```ts
|
|
6
|
-
* import unique from '@
|
|
6
|
+
* import unique from '@tanwan/utils/unique'
|
|
7
7
|
* unique([1, 2, 2, 3]) // [1, 2, 3]
|
|
8
8
|
* unique(['a', 'b', 'a']) // ['a', 'b']
|
|
9
9
|
* ```
|
|
10
10
|
*/
|
|
11
11
|
declare function unique<T>(arr: T[]): T[];
|
|
12
|
-
export = unique;
|
|
13
|
-
//# sourceMappingURL=unique.d.cts.map
|
|
12
|
+
export = unique;
|
|
@@ -3,12 +3,11 @@
|
|
|
3
3
|
* 数组去重
|
|
4
4
|
* @example
|
|
5
5
|
* ```ts
|
|
6
|
-
* import unique from '@
|
|
6
|
+
* import unique from '@tanwan/utils/unique'
|
|
7
7
|
* unique([1, 2, 2, 3]) // [1, 2, 3]
|
|
8
8
|
* unique(['a', 'b', 'a']) // ['a', 'b']
|
|
9
9
|
* ```
|
|
10
10
|
*/
|
|
11
11
|
declare function unique<T>(arr: T[]): T[];
|
|
12
12
|
//#endregion
|
|
13
|
-
export { unique as default };
|
|
14
|
-
//# sourceMappingURL=unique.d.ts.map
|
|
13
|
+
export { unique as default };
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* 数组去重
|
|
4
4
|
* @example
|
|
5
5
|
* ```ts
|
|
6
|
-
* import unique from '@
|
|
6
|
+
* import unique from '@tanwan/utils/unique'
|
|
7
7
|
* unique([1, 2, 2, 3]) // [1, 2, 3]
|
|
8
8
|
* unique(['a', 'b', 'a']) // ['a', 'b']
|
|
9
9
|
* ```
|
|
@@ -13,5 +13,3 @@ function unique(arr) {
|
|
|
13
13
|
}
|
|
14
14
|
//#endregion
|
|
15
15
|
export { unique as default };
|
|
16
|
-
|
|
17
|
-
//# sourceMappingURL=unique.js.map
|
package/dist/uuid.cjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/string/uuid/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 生成 UUID v4
|
|
4
|
+
*
|
|
5
|
+
* 优先使用 `crypto.randomUUID()`,不支持时自动降级为手动生成方案
|
|
6
|
+
*
|
|
7
|
+
* @returns UUID v4 格式字符串,例如 `'550e8400-e29b-41d4-a716-446655440000'`
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import uuid from '@tanwan/utils/uuid'
|
|
12
|
+
* uuid() // 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d'
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
function uuid() {
|
|
16
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
17
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
18
|
+
const r = Math.random() * 16 | 0;
|
|
19
|
+
return (c === "x" ? r : r & 3 | 8).toString(16);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
module.exports = uuid;
|
package/dist/uuid.d.cts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/string/uuid/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 生成 UUID v4
|
|
4
|
+
*
|
|
5
|
+
* 优先使用 `crypto.randomUUID()`,不支持时自动降级为手动生成方案
|
|
6
|
+
*
|
|
7
|
+
* @returns UUID v4 格式字符串,例如 `'550e8400-e29b-41d4-a716-446655440000'`
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import uuid from '@tanwan/utils/uuid'
|
|
12
|
+
* uuid() // 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d'
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
declare function uuid(): string;
|
|
16
|
+
export = uuid;
|
package/dist/uuid.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/string/uuid/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 生成 UUID v4
|
|
4
|
+
*
|
|
5
|
+
* 优先使用 `crypto.randomUUID()`,不支持时自动降级为手动生成方案
|
|
6
|
+
*
|
|
7
|
+
* @returns UUID v4 格式字符串,例如 `'550e8400-e29b-41d4-a716-446655440000'`
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import uuid from '@tanwan/utils/uuid'
|
|
12
|
+
* uuid() // 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d'
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
declare function uuid(): string;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { uuid as default };
|
package/dist/uuid.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/string/uuid/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 生成 UUID v4
|
|
4
|
+
*
|
|
5
|
+
* 优先使用 `crypto.randomUUID()`,不支持时自动降级为手动生成方案
|
|
6
|
+
*
|
|
7
|
+
* @returns UUID v4 格式字符串,例如 `'550e8400-e29b-41d4-a716-446655440000'`
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import uuid from '@tanwan/utils/uuid'
|
|
12
|
+
* uuid() // 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d'
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
function uuid() {
|
|
16
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
17
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
18
|
+
const r = Math.random() * 16 | 0;
|
|
19
|
+
return (c === "x" ? r : r & 3 | 8).toString(16);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { uuid as default };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/business/validateEmail/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 校验邮箱格式
|
|
4
|
+
*
|
|
5
|
+
* @param email - 待校验的邮箱
|
|
6
|
+
* @returns 是否为合法邮箱
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import validateEmail from '@tanwan/utils/validateEmail'
|
|
11
|
+
* validateEmail('user@example.com') // true
|
|
12
|
+
* validateEmail('invalid-email') // false
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
function validateEmail(email) {
|
|
16
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
module.exports = validateEmail;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/business/validateEmail/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 校验邮箱格式
|
|
4
|
+
*
|
|
5
|
+
* @param email - 待校验的邮箱
|
|
6
|
+
* @returns 是否为合法邮箱
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import validateEmail from '@tanwan/utils/validateEmail'
|
|
11
|
+
* validateEmail('user@example.com') // true
|
|
12
|
+
* validateEmail('invalid-email') // false
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
declare function validateEmail(email: string): boolean;
|
|
16
|
+
export = validateEmail;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/business/validateEmail/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 校验邮箱格式
|
|
4
|
+
*
|
|
5
|
+
* @param email - 待校验的邮箱
|
|
6
|
+
* @returns 是否为合法邮箱
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import validateEmail from '@tanwan/utils/validateEmail'
|
|
11
|
+
* validateEmail('user@example.com') // true
|
|
12
|
+
* validateEmail('invalid-email') // false
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
declare function validateEmail(email: string): boolean;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { validateEmail as default };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/business/validateEmail/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 校验邮箱格式
|
|
4
|
+
*
|
|
5
|
+
* @param email - 待校验的邮箱
|
|
6
|
+
* @returns 是否为合法邮箱
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import validateEmail from '@tanwan/utils/validateEmail'
|
|
11
|
+
* validateEmail('user@example.com') // true
|
|
12
|
+
* validateEmail('invalid-email') // false
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
function validateEmail(email) {
|
|
16
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { validateEmail as default };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//#region src/business/validateIdCard/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 校验身份证号码格式(含校验位算法)
|
|
4
|
+
*
|
|
5
|
+
* @param idCard - 18 位身份证号
|
|
6
|
+
* @returns 是否为合法身份证号
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import validateIdCard from '@tanwan/utils/validateIdCard'
|
|
11
|
+
* validateIdCard('110101199001011234') // true(示例号,实际需按算法校验)
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
function validateIdCard(idCard) {
|
|
15
|
+
if (!/^\d{17}[\dXx]$/.test(idCard)) return false;
|
|
16
|
+
const provinceCode = idCard.substring(0, 2);
|
|
17
|
+
if (Number.parseInt(provinceCode) < 11 || Number.parseInt(provinceCode) > 82) return false;
|
|
18
|
+
const birth = idCard.substring(6, 14);
|
|
19
|
+
const year = Number.parseInt(birth.substring(0, 4));
|
|
20
|
+
const month = Number.parseInt(birth.substring(4, 6));
|
|
21
|
+
const day = Number.parseInt(birth.substring(6, 8));
|
|
22
|
+
if (year < 1900 || year > 2100) return false;
|
|
23
|
+
if (month < 1 || month > 12) return false;
|
|
24
|
+
if (day < 1 || day > 31) return false;
|
|
25
|
+
const weights = [
|
|
26
|
+
7,
|
|
27
|
+
9,
|
|
28
|
+
10,
|
|
29
|
+
5,
|
|
30
|
+
8,
|
|
31
|
+
4,
|
|
32
|
+
2,
|
|
33
|
+
1,
|
|
34
|
+
6,
|
|
35
|
+
3,
|
|
36
|
+
7,
|
|
37
|
+
9,
|
|
38
|
+
10,
|
|
39
|
+
5,
|
|
40
|
+
8,
|
|
41
|
+
4,
|
|
42
|
+
2
|
|
43
|
+
];
|
|
44
|
+
const checkCodes = [
|
|
45
|
+
"1",
|
|
46
|
+
"0",
|
|
47
|
+
"X",
|
|
48
|
+
"9",
|
|
49
|
+
"8",
|
|
50
|
+
"7",
|
|
51
|
+
"6",
|
|
52
|
+
"5",
|
|
53
|
+
"4",
|
|
54
|
+
"3",
|
|
55
|
+
"2"
|
|
56
|
+
];
|
|
57
|
+
let sum = 0;
|
|
58
|
+
for (let i = 0; i < 17; i++) sum += Number.parseInt(idCard[i]) * weights[i];
|
|
59
|
+
return checkCodes[sum % 11] === idCard[17].toUpperCase();
|
|
60
|
+
}
|
|
61
|
+
//#endregion
|
|
62
|
+
module.exports = validateIdCard;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/business/validateIdCard/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 校验身份证号码格式(含校验位算法)
|
|
4
|
+
*
|
|
5
|
+
* @param idCard - 18 位身份证号
|
|
6
|
+
* @returns 是否为合法身份证号
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import validateIdCard from '@tanwan/utils/validateIdCard'
|
|
11
|
+
* validateIdCard('110101199001011234') // true(示例号,实际需按算法校验)
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
declare function validateIdCard(idCard: string): boolean;
|
|
15
|
+
export = validateIdCard;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/business/validateIdCard/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 校验身份证号码格式(含校验位算法)
|
|
4
|
+
*
|
|
5
|
+
* @param idCard - 18 位身份证号
|
|
6
|
+
* @returns 是否为合法身份证号
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import validateIdCard from '@tanwan/utils/validateIdCard'
|
|
11
|
+
* validateIdCard('110101199001011234') // true(示例号,实际需按算法校验)
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
declare function validateIdCard(idCard: string): boolean;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { validateIdCard as default };
|