@ivujs/i-utils 1.1.1
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/LICENSE +21 -0
- package/README.md +149 -0
- package/dist/cjs/ID-card/index.cjs +122 -0
- package/dist/cjs/array/index.cjs +465 -0
- package/dist/cjs/clipboard/index.cjs +135 -0
- package/dist/cjs/color/index.cjs +210 -0
- package/dist/cjs/constants/date.cjs +186 -0
- package/dist/cjs/constants/lang.cjs +13 -0
- package/dist/cjs/constants/math.cjs +13 -0
- package/dist/cjs/constants/regexp.cjs +57 -0
- package/dist/cjs/constants/sort.cjs +15 -0
- package/dist/cjs/cookie/index.cjs +71 -0
- package/dist/cjs/crypto/aes/aes.cjs +533 -0
- package/dist/cjs/crypto/aes/index.cjs +28 -0
- package/dist/cjs/crypto/base32/base32.cjs +385 -0
- package/dist/cjs/crypto/base32/index.cjs +44 -0
- package/dist/cjs/crypto/base64/base64.cjs +433 -0
- package/dist/cjs/crypto/base64/index.cjs +64 -0
- package/dist/cjs/crypto/des/des.cjs +1054 -0
- package/dist/cjs/crypto/des/index.cjs +30 -0
- package/dist/cjs/crypto/md5/index.cjs +25 -0
- package/dist/cjs/crypto/md5/md5.cjs +944 -0
- package/dist/cjs/crypto/sha/sha1/index.cjs +25 -0
- package/dist/cjs/crypto/sha/sha1/sha1.cjs +602 -0
- package/dist/cjs/crypto/sha/sha256/index.cjs +46 -0
- package/dist/cjs/crypto/sha/sha256/sha256.cjs +654 -0
- package/dist/cjs/crypto/sha/sha3/index.cjs +44 -0
- package/dist/cjs/crypto/sha/sha3/sha3.cjs +747 -0
- package/dist/cjs/crypto/sha/sha512/index.cjs +88 -0
- package/dist/cjs/crypto/sha/sha512/sha512.cjs +1186 -0
- package/dist/cjs/crypto/sm/lib/asn1.cjs +167 -0
- package/dist/cjs/crypto/sm/lib/ec.cjs +352 -0
- package/dist/cjs/crypto/sm/lib/jsbn.cjs +1633 -0
- package/dist/cjs/crypto/sm/lib/sm3.cjs +173 -0
- package/dist/cjs/crypto/sm/lib/utils.cjs +198 -0
- package/dist/cjs/crypto/sm/sm2/index.cjs +122 -0
- package/dist/cjs/crypto/sm/sm2/sm2.cjs +269 -0
- package/dist/cjs/crypto/sm/sm3/index.cjs +15 -0
- package/dist/cjs/crypto/sm/sm3/sm3.cjs +103 -0
- package/dist/cjs/crypto/sm/sm4/index.cjs +28 -0
- package/dist/cjs/crypto/sm/sm4/sm4.cjs +362 -0
- package/dist/cjs/crypto/tea/index.cjs +26 -0
- package/dist/cjs/crypto/tea/tea.cjs +215 -0
- package/dist/cjs/date/index.cjs +1349 -0
- package/dist/cjs/desensitized/index.cjs +74 -0
- package/dist/cjs/device/index.cjs +159 -0
- package/dist/cjs/dom/index.cjs +176 -0
- package/dist/cjs/file/index.cjs +339 -0
- package/dist/cjs/function/index.cjs +67 -0
- package/dist/cjs/id/index.cjs +46 -0
- package/dist/cjs/index.cjs +399 -0
- package/dist/cjs/keycode/index.cjs +142 -0
- package/dist/cjs/math/index.cjs +284 -0
- package/dist/cjs/number/index.cjs +14 -0
- package/dist/cjs/object/index.cjs +216 -0
- package/dist/cjs/pagination/index.cjs +140 -0
- package/dist/cjs/random/index.cjs +25 -0
- package/dist/cjs/regexp/index.cjs +109 -0
- package/dist/cjs/storage/index.cjs +11 -0
- package/dist/cjs/storage/localStorage.cjs +40 -0
- package/dist/cjs/storage/sessionStorage.cjs +40 -0
- package/dist/cjs/string/index.cjs +338 -0
- package/dist/cjs/url/index.cjs +279 -0
- package/dist/cjs/validate/index.cjs +487 -0
- package/dist/cjs/weapp/index.cjs +151 -0
- package/dist/es/ID-card/index.mjs +116 -0
- package/dist/es/array/index.mjs +437 -0
- package/dist/es/clipboard/index.mjs +129 -0
- package/dist/es/color/index.mjs +200 -0
- package/dist/es/constants/date.mjs +184 -0
- package/dist/es/constants/lang.mjs +11 -0
- package/dist/es/constants/math.mjs +11 -0
- package/dist/es/constants/regexp.mjs +55 -0
- package/dist/es/constants/sort.mjs +13 -0
- package/dist/es/cookie/index.mjs +65 -0
- package/dist/es/crypto/aes/aes.mjs +529 -0
- package/dist/es/crypto/aes/index.mjs +25 -0
- package/dist/es/crypto/base32/base32.mjs +381 -0
- package/dist/es/crypto/base32/index.mjs +39 -0
- package/dist/es/crypto/base64/base64.mjs +427 -0
- package/dist/es/crypto/base64/index.mjs +57 -0
- package/dist/es/crypto/des/des.mjs +1050 -0
- package/dist/es/crypto/des/index.mjs +27 -0
- package/dist/es/crypto/md5/index.mjs +22 -0
- package/dist/es/crypto/md5/md5.mjs +941 -0
- package/dist/es/crypto/sha/sha1/index.mjs +22 -0
- package/dist/es/crypto/sha/sha1/sha1.mjs +599 -0
- package/dist/es/crypto/sha/sha256/index.mjs +41 -0
- package/dist/es/crypto/sha/sha256/sha256.mjs +649 -0
- package/dist/es/crypto/sha/sha3/index.mjs +39 -0
- package/dist/es/crypto/sha/sha3/sha3.mjs +742 -0
- package/dist/es/crypto/sha/sha512/index.mjs +79 -0
- package/dist/es/crypto/sha/sha512/sha512.mjs +1177 -0
- package/dist/es/crypto/sm/lib/asn1.mjs +164 -0
- package/dist/es/crypto/sm/lib/ec.mjs +349 -0
- package/dist/es/crypto/sm/lib/jsbn.mjs +1630 -0
- package/dist/es/crypto/sm/lib/sm3.mjs +170 -0
- package/dist/es/crypto/sm/lib/utils.mjs +186 -0
- package/dist/es/crypto/sm/sm2/index.mjs +111 -0
- package/dist/es/crypto/sm/sm2/sm2.mjs +258 -0
- package/dist/es/crypto/sm/sm3/index.mjs +13 -0
- package/dist/es/crypto/sm/sm3/sm3.mjs +99 -0
- package/dist/es/crypto/sm/sm4/index.mjs +25 -0
- package/dist/es/crypto/sm/sm4/sm4.mjs +359 -0
- package/dist/es/crypto/tea/index.mjs +23 -0
- package/dist/es/crypto/tea/tea.mjs +211 -0
- package/dist/es/date/index.mjs +1262 -0
- package/dist/es/desensitized/index.mjs +69 -0
- package/dist/es/device/index.mjs +145 -0
- package/dist/es/dom/index.mjs +164 -0
- package/dist/es/file/index.mjs +323 -0
- package/dist/es/function/index.mjs +63 -0
- package/dist/es/id/index.mjs +43 -0
- package/dist/es/index.mjs +59 -0
- package/dist/es/keycode/index.mjs +139 -0
- package/dist/es/math/index.mjs +274 -0
- package/dist/es/number/index.mjs +12 -0
- package/dist/es/object/index.mjs +203 -0
- package/dist/es/pagination/index.mjs +134 -0
- package/dist/es/random/index.mjs +22 -0
- package/dist/es/regexp/index.mjs +98 -0
- package/dist/es/storage/index.mjs +9 -0
- package/dist/es/storage/localStorage.mjs +35 -0
- package/dist/es/storage/sessionStorage.mjs +35 -0
- package/dist/es/string/index.mjs +321 -0
- package/dist/es/url/index.mjs +261 -0
- package/dist/es/validate/index.mjs +451 -0
- package/dist/es/weapp/index.mjs +140 -0
- package/dist/lib/index.full.esm.js +15825 -0
- package/dist/lib/index.full.esm.min.js +58 -0
- package/dist/lib/index.full.umd.js +16179 -0
- package/package.json +54 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 防抖函数
|
|
3
|
+
* @description 事件执行后,在延迟时间内如果再次执行,会清空定时器重新延迟执行,举例:用户在输入框进行输入搜索,最终是会获取到最后一次输入,节约请求资源
|
|
4
|
+
* @param {Function} fn 目标函数
|
|
5
|
+
* @param {Number} delay 延迟时间,单位毫秒,默认 1*1000 毫秒
|
|
6
|
+
* @param {Boolean} immediate 是否立即执行,默认true
|
|
7
|
+
* @returns {Function} 返回function()
|
|
8
|
+
*/
|
|
9
|
+
function debounce(fn, delay = 1 * 1000, immediate = true) {
|
|
10
|
+
let timer;
|
|
11
|
+
return function () {
|
|
12
|
+
const _args = arguments;
|
|
13
|
+
// 先关闭定时器
|
|
14
|
+
if (timer) clearTimeout(timer);
|
|
15
|
+
// 立即执行判断
|
|
16
|
+
if (immediate) {
|
|
17
|
+
// 如果需要立即执行
|
|
18
|
+
// 开启新定时器防止短时间内再次触发
|
|
19
|
+
const canExecute = !timer;
|
|
20
|
+
timer = setTimeout(function () {
|
|
21
|
+
timer = null;
|
|
22
|
+
}, delay);
|
|
23
|
+
if (canExecute) fn.apply(this, _args);
|
|
24
|
+
} else {
|
|
25
|
+
// 如果不需要立即执行
|
|
26
|
+
// 每次触发开启新定时器即可
|
|
27
|
+
timer = setTimeout(function () {
|
|
28
|
+
fn.apply(this, _args);
|
|
29
|
+
}, delay);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 节流函数
|
|
36
|
+
* @description 高频触发时,在指定时间间隔内只执行一次,举例:监听页面滚动,不会频繁触发,只会在固定时间内获取一次
|
|
37
|
+
* @param {Function} fn 目标函数
|
|
38
|
+
* @param {Number} interval 时间间隔,单位毫秒,默认1*1000毫秒
|
|
39
|
+
* @returns {Function} 返回function()
|
|
40
|
+
*/
|
|
41
|
+
function throttle(fn, interval = 1 * 1000) {
|
|
42
|
+
let timer;
|
|
43
|
+
return function () {
|
|
44
|
+
const _args = arguments;
|
|
45
|
+
// 有定时器则返回
|
|
46
|
+
if (timer) return;
|
|
47
|
+
timer = setTimeout(() => {
|
|
48
|
+
timer = null;
|
|
49
|
+
fn.apply(this, _args);
|
|
50
|
+
}, interval);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 睡眠延迟执行
|
|
56
|
+
* @description 需要配合 async/await 来达到延迟效果
|
|
57
|
+
* @param {Number} delay 延迟时间,单位毫秒,默认1*1000毫秒
|
|
58
|
+
*/
|
|
59
|
+
function sleep(delay = 1 * 1000) {
|
|
60
|
+
return new Promise((resolve) => setTimeout(resolve, delay));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { debounce, sleep, throttle };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 生成UUID
|
|
3
|
+
* @param {Number} len 生成的长度,默认32位
|
|
4
|
+
* @param {Number} radix 进制数,默认16进制
|
|
5
|
+
* @example
|
|
6
|
+
* getUUID() // 输出:0a559343dbbf0e7e6c1de90163e7aa0a
|
|
7
|
+
* @returns {String} 返回字符串
|
|
8
|
+
*/
|
|
9
|
+
function getUUID(len = 32, radix = 16) {
|
|
10
|
+
const CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
|
|
11
|
+
let uuid = [],
|
|
12
|
+
i;
|
|
13
|
+
radix = radix || CHARS.length;
|
|
14
|
+
if (len) {
|
|
15
|
+
for (i = 0; i < len; i++) uuid[i] = CHARS[0 | (Math.random() * radix)];
|
|
16
|
+
} else {
|
|
17
|
+
let r;
|
|
18
|
+
uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
|
|
19
|
+
uuid[14] = "4";
|
|
20
|
+
for (i = 0; i < 36; i++) {
|
|
21
|
+
if (!uuid[i]) {
|
|
22
|
+
r = 0 | (Math.random() * 16);
|
|
23
|
+
uuid[i] = CHARS[i === 19 ? (r & 0x3) | 0x8 : r];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return uuid.join("");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 生成GUID
|
|
32
|
+
* @example
|
|
33
|
+
* getGUID() // 输出:275ec770-0853-6767-4875-7b270220ce9c
|
|
34
|
+
* @returns {String} 返回字符串
|
|
35
|
+
*/
|
|
36
|
+
function getGUID() {
|
|
37
|
+
let s4 = function () {
|
|
38
|
+
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
|
|
39
|
+
};
|
|
40
|
+
return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { getGUID, getUUID };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export { SORT } from './constants/sort.mjs';
|
|
2
|
+
export { MATH } from './constants/math.mjs';
|
|
3
|
+
export { LANG } from './constants/lang.mjs';
|
|
4
|
+
export { REGEXP } from './constants/regexp.mjs';
|
|
5
|
+
export { DATE } from './constants/date.mjs';
|
|
6
|
+
export { formatRmbChinese, formatThousand, inString, replaceAll, toCamelCase, toKebabCase, toLower, toPascalCase, toSnakeCase, toUpper, trim, trimAll, trimEnd, trimStart, zeroEnd, zeroStart } from './string/index.mjs';
|
|
7
|
+
export { parseInt } from './number/index.mjs';
|
|
8
|
+
export { arrayAvg, arrayBottom, arrayComplement, arrayCreate, arrayDifference, arrayDown, arrayEquals, arrayInsert, arrayInsertAfter, arrayInsertBefore, arrayIntersect, arrayMax, arrayMin, arrayRemove, arrayRemoveAfter, arrayRemoveBefore, arrayShuffle, arraySort, arraySum, arraySwap, arrayToTree, arrayTop, arrayUnion, arrayUnique, arrayUp, inArray, treeToArray } from './array/index.mjs';
|
|
9
|
+
export { clone, cloneDeep, getTargetValueByPath, jsonToMap, mapToJson, mapToObject, merge, objectEquals, objectToMap, parseJson, setTargetValueByPath, stringifyJson } from './object/index.mjs';
|
|
10
|
+
export { debounce, sleep, throttle } from './function/index.mjs';
|
|
11
|
+
export { addDate, addHours, addMillisecond, addMinutes, addMonth, addQuarter, addSeconds, addWeek, addYear, formatDate, getAge, getBetweenDates, getBetweenMonths, getBetweenYears, getChineseZodiac, getDate, getDateArray, getDateObject, getDateTime, getDayOfMonth, getDayOfWeek, getDayOfYear, getDaysOfMonth, getDaysOfWeek, getDaysOfYear, getDiffDay, getDiffMonth, getDiffWeek, getDiffYear, getFirstDateOfMonth, getFirstDateOfWeek, getFirstDateOfYear, getFullDateOfMonth, getFullDateOfWeek, getFullDateOfYear, getLastDateOfMonth, getLastDateOfWeek, getLastDateOfYear, getNow, getOverTime, getPastTime, getQuarter, getTimestamp, getUnixTimestamp, getWeek, getWeekOfMonth, getWeekOfYear, getWeeksOfMonth, getWeeksOfYear, getZodiac, isAM, isAfter, isAfterTomorrow, isBefore, isBeforeYesterday, isBetween, isCommonYear, isFirstDayOfMonth, isFirstDayOfWeek, isFirstDayOfYear, isLastDayOfMonth, isLastDayOfWeek, isLastDayOfYear, isLeapYear, isPM, isSame, isSameMonth, isSameOrAfter, isSameOrBefore, isSameWeek, isSameYear, isToday, isTomorrow, isWeekend, isWorkday, isYesterday, lastMonth, lastWeek, lastYear, nextMonth, nextWeek, nextYear, parseDate, today, tomorrow, yesterday } from './date/index.mjs';
|
|
12
|
+
export { add, divide, gcd, modulo, multiply, scm, subtract, toDecimal, toFixed } from './math/index.mjs';
|
|
13
|
+
export { isChinese, isEmail, isEnglish, isExternal, isIdCard, isLowerCase, isMobile, isUpperCase, isUrl, regexpTest } from './regexp/index.mjs';
|
|
14
|
+
export { getRandom, getRandomDigit } from './random/index.mjs';
|
|
15
|
+
export { base64ToBlob, base64ToFile, blobToBase64, blobToFile, blobToText, downloadBlobFile, downloadFileUrl, fileToBase64, fileToBlob, fileToUrl, formatFileSize, getFileName, getFileSuffix, urlToBase64, urlToFile } from './file/index.mjs';
|
|
16
|
+
export { getRandomHex, getRandomRgb, getRandomRgba, hexToHsl, hexToRgb, hexToRgba, rgbToHex, rgbaToHex, rgbaToHsl } from './color/index.mjs';
|
|
17
|
+
export { deepCompare, equals, equalsIgnoreCase, isArray, isAsyncFunction, isBigInt, isBlank, isBoolean, isDate, isDecimal, isEmpty, isError, isFalse, isFunction, isInteger, isJson, isMap, isNaN, isNotBlank, isNotEmpty, isNotNaN, isNotNull, isNotUndefined, isNull, isNumber, isObject, isPromise, isRegExp, isSet, isString, isSymbol, isTrue, isUndefined, isWeakMap, isWeakSet } from './validate/index.mjs';
|
|
18
|
+
export { getKeyCode, getKeyName } from './keycode/index.mjs';
|
|
19
|
+
export { getGUID, getUUID } from './id/index.mjs';
|
|
20
|
+
import * as index from './crypto/base64/index.mjs';
|
|
21
|
+
export { index as base64 };
|
|
22
|
+
import * as index$1 from './crypto/base32/index.mjs';
|
|
23
|
+
export { index$1 as base32 };
|
|
24
|
+
export { md5, md5_hmac } from './crypto/md5/index.mjs';
|
|
25
|
+
export { sha1, sha1_hmac } from './crypto/sha/sha1/index.mjs';
|
|
26
|
+
export { sha224, sha224_hmac, sha256, sha256_hmac } from './crypto/sha/sha256/index.mjs';
|
|
27
|
+
export { sha384, sha384_hmac, sha512, sha512_224, sha512_224_hmac, sha512_256, sha512_256_hmac, sha512_hmac } from './crypto/sha/sha512/index.mjs';
|
|
28
|
+
export { sha3_224, sha3_256, sha3_384, sha3_512 } from './crypto/sha/sha3/index.mjs';
|
|
29
|
+
import * as index$5 from './crypto/sm/sm2/index.mjs';
|
|
30
|
+
export { index$5 as sm2 };
|
|
31
|
+
import * as index$6 from './crypto/sm/sm3/index.mjs';
|
|
32
|
+
export { index$6 as sm3 };
|
|
33
|
+
import * as index$7 from './crypto/sm/sm4/index.mjs';
|
|
34
|
+
export { index$7 as sm4 };
|
|
35
|
+
import * as index$2 from './crypto/aes/index.mjs';
|
|
36
|
+
export { index$2 as aes };
|
|
37
|
+
import * as index$3 from './crypto/des/index.mjs';
|
|
38
|
+
export { index$3 as des };
|
|
39
|
+
import * as index$4 from './crypto/tea/index.mjs';
|
|
40
|
+
export { index$4 as tea };
|
|
41
|
+
export { formatStartOfBankCard, formatStartOfIDCard, formatStartOfMobile, formatStartOfName } from './desensitized/index.mjs';
|
|
42
|
+
export { getAgeByIDCard, getBirthdayByIDCard, getInfoByIDCard, getProvinceByIDCard, getSexByIDCard } from './ID-card/index.mjs';
|
|
43
|
+
export { getLimit, getNextPage, getPrevPage, getRainbowPager, getTotalPage } from './pagination/index.mjs';
|
|
44
|
+
export { appendSearchParam, appendToSearchParam, formatSearchString, getHost, getHostName, getPort, getProtocol, getSearchParam, getSearchString, getUrlHash, getUrlPath, hasSearchParam, parseSearchParam, prependSearchParam, prependToSearchParam, removeSearchParam, setSearchParam } from './url/index.mjs';
|
|
45
|
+
export { clearCookie, getCookie, isSupportCookie, removeCookie, setCookie } from './cookie/index.mjs';
|
|
46
|
+
export { isSupportStorage } from './storage/index.mjs';
|
|
47
|
+
export { addClass, addStyle, copyText, getCopyText, getStyle, hasClass, htmlDecode, htmlEncode, removeClass, removeStyle, replaceClass } from './dom/index.mjs';
|
|
48
|
+
export { getBrowserInfo, isAndroid, isIos, isIpad, isIphone, isLinux, isMac, isPc, isPhone, isQQ, isWeixin, isWindows, isWindowsPhone } from './device/index.mjs';
|
|
49
|
+
export { clearClipboard, getClipboard, getClipboardText, setClipboard, setClipboardText } from './clipboard/index.mjs';
|
|
50
|
+
export { clearStorage, clearStorageSync, getStorage, getStorageInfo, getStorageInfoSync, getStorageSync, removeStorage, removeStorageSync, setStorage, setStorageSync } from './weapp/index.mjs';
|
|
51
|
+
export { clearLocalStorage, getLocalStorage, removeLocalStorage, setLocalStorage } from './storage/localStorage.mjs';
|
|
52
|
+
export { clearSessionStorage, getSessionStorage, removeSessionStorage, setSessionStorage } from './storage/sessionStorage.mjs';
|
|
53
|
+
|
|
54
|
+
// 测试加载成功方法
|
|
55
|
+
const loadedTest = function () {
|
|
56
|
+
console.log("Nice, iUtils loaded successfully!");
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export { loadedTest };
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
const keyCodeMap = {
|
|
2
|
+
// 字符键
|
|
3
|
+
8: "Backspace",
|
|
4
|
+
9: "Tab",
|
|
5
|
+
13: "Enter",
|
|
6
|
+
16: "Shift",
|
|
7
|
+
17: "Ctrl",
|
|
8
|
+
18: "Alt",
|
|
9
|
+
19: "Pause",
|
|
10
|
+
20: "Caps Lock",
|
|
11
|
+
27: "Escape",
|
|
12
|
+
32: "Space",
|
|
13
|
+
33: "Page Up",
|
|
14
|
+
34: "Page Down",
|
|
15
|
+
35: "End",
|
|
16
|
+
36: "Home",
|
|
17
|
+
37: "Left",
|
|
18
|
+
38: "Up",
|
|
19
|
+
39: "Right",
|
|
20
|
+
40: "Down",
|
|
21
|
+
42: "Print Screen",
|
|
22
|
+
45: "Insert",
|
|
23
|
+
46: "Delete",
|
|
24
|
+
// 数字键
|
|
25
|
+
48: "0",
|
|
26
|
+
49: "1",
|
|
27
|
+
50: "2",
|
|
28
|
+
51: "3",
|
|
29
|
+
52: "4",
|
|
30
|
+
53: "5",
|
|
31
|
+
54: "6",
|
|
32
|
+
55: "7",
|
|
33
|
+
56: "8",
|
|
34
|
+
57: "9",
|
|
35
|
+
// 字母键
|
|
36
|
+
65: "A",
|
|
37
|
+
66: "B",
|
|
38
|
+
67: "C",
|
|
39
|
+
68: "D",
|
|
40
|
+
69: "E",
|
|
41
|
+
70: "F",
|
|
42
|
+
71: "G",
|
|
43
|
+
72: "H",
|
|
44
|
+
73: "I",
|
|
45
|
+
74: "J",
|
|
46
|
+
75: "K",
|
|
47
|
+
76: "L",
|
|
48
|
+
77: "M",
|
|
49
|
+
78: "N",
|
|
50
|
+
79: "O",
|
|
51
|
+
80: "P",
|
|
52
|
+
81: "Q",
|
|
53
|
+
82: "R",
|
|
54
|
+
83: "S",
|
|
55
|
+
84: "T",
|
|
56
|
+
85: "U",
|
|
57
|
+
86: "V",
|
|
58
|
+
87: "W",
|
|
59
|
+
88: "X",
|
|
60
|
+
89: "Y",
|
|
61
|
+
90: "Z",
|
|
62
|
+
// window
|
|
63
|
+
91: "Windows",
|
|
64
|
+
93: "Right Click",
|
|
65
|
+
// Numpad
|
|
66
|
+
96: "Numpad 0",
|
|
67
|
+
97: "Numpad 1",
|
|
68
|
+
98: "Numpad 2",
|
|
69
|
+
99: "Numpad 3",
|
|
70
|
+
100: "Numpad 4",
|
|
71
|
+
101: "Numpad 5",
|
|
72
|
+
102: "Numpad 6",
|
|
73
|
+
103: "Numpad 7",
|
|
74
|
+
104: "Numpad 8",
|
|
75
|
+
105: "Numpad 9",
|
|
76
|
+
106: "Numpad *",
|
|
77
|
+
107: "Numpad +",
|
|
78
|
+
109: "Numpad -",
|
|
79
|
+
110: "Numpad .",
|
|
80
|
+
111: "Numpad /",
|
|
81
|
+
// F功能键
|
|
82
|
+
112: "F1",
|
|
83
|
+
113: "F2",
|
|
84
|
+
114: "F3",
|
|
85
|
+
115: "F4",
|
|
86
|
+
116: "F5",
|
|
87
|
+
117: "F6",
|
|
88
|
+
118: "F7",
|
|
89
|
+
119: "F8",
|
|
90
|
+
120: "F9",
|
|
91
|
+
121: "F10",
|
|
92
|
+
122: "F11",
|
|
93
|
+
123: "F12",
|
|
94
|
+
// 特殊符号
|
|
95
|
+
144: "Num Lock",
|
|
96
|
+
145: "Scroll Lock",
|
|
97
|
+
182: "My Computer",
|
|
98
|
+
183: "My Calculator",
|
|
99
|
+
186: ";",
|
|
100
|
+
187: "=",
|
|
101
|
+
188: ",",
|
|
102
|
+
189: "-",
|
|
103
|
+
190: ".",
|
|
104
|
+
191: "/",
|
|
105
|
+
192: "`",
|
|
106
|
+
219: "[",
|
|
107
|
+
220: "\\",
|
|
108
|
+
221: "]",
|
|
109
|
+
222: "'",
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 根据keycode获得键名
|
|
114
|
+
* @param {Number} keycode 键值
|
|
115
|
+
* @returns {String} 返回键名
|
|
116
|
+
*/
|
|
117
|
+
function getKeyName(keycode) {
|
|
118
|
+
if (keyCodeMap[keycode]) {
|
|
119
|
+
return keyCodeMap[keycode];
|
|
120
|
+
} else {
|
|
121
|
+
console.log("Unknow Key Code: " + keycode);
|
|
122
|
+
return "";
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 根据keyname获得键值
|
|
128
|
+
* @param {Number} keyname
|
|
129
|
+
* @returns {Number} 返回键值
|
|
130
|
+
*/
|
|
131
|
+
function getKeyCode(keyname) {
|
|
132
|
+
for (let key in keyCodeMap) {
|
|
133
|
+
if (keyCodeMap[key] === keyname) {
|
|
134
|
+
return key;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { getKeyCode, getKeyName };
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { isNaN } from '../validate/index.mjs';
|
|
2
|
+
import { MATH } from '../constants/math.mjs';
|
|
3
|
+
|
|
4
|
+
/* 数字计算 */
|
|
5
|
+
/**
|
|
6
|
+
* 两个数字相加
|
|
7
|
+
* @param {String|Number} arg1 第一个数字
|
|
8
|
+
* @param {String|Number} arg2 第二个数字
|
|
9
|
+
* @returns {Number} 返回计算后的数字
|
|
10
|
+
*/
|
|
11
|
+
function add(arg1, arg2) {
|
|
12
|
+
let r1, r2, m;
|
|
13
|
+
try {
|
|
14
|
+
r1 = arg1.toString().split(".")[1].length;
|
|
15
|
+
} catch (e) {
|
|
16
|
+
r1 = 0;
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
r2 = arg2.toString().split(".")[1].length;
|
|
20
|
+
} catch (e) {
|
|
21
|
+
r2 = 0;
|
|
22
|
+
}
|
|
23
|
+
m = Math.pow(10, Math.max(r1, r2));
|
|
24
|
+
return (arg1 * m + arg2 * m) / m;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 两个数字相减
|
|
29
|
+
* @param {String|Number} arg1 第一个数字
|
|
30
|
+
* @param {String|Number} arg2 第二个数字
|
|
31
|
+
* @returns {Number} 返回计算后的数字
|
|
32
|
+
*/
|
|
33
|
+
function subtract(arg1, arg2) {
|
|
34
|
+
let r1, r2, m, n;
|
|
35
|
+
try {
|
|
36
|
+
r1 = arg1.toString().split(".")[1].length;
|
|
37
|
+
} catch (e) {
|
|
38
|
+
r1 = 0;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
r2 = arg2.toString().split(".")[1].length;
|
|
42
|
+
} catch (e) {
|
|
43
|
+
r2 = 0;
|
|
44
|
+
}
|
|
45
|
+
m = Math.pow(10, Math.max(r1, r2));
|
|
46
|
+
n = r1 >= r2 ? r1 : r2;
|
|
47
|
+
return ((arg1 * m - arg2 * m) / m).toFixed(n);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 两个数字相乘
|
|
52
|
+
* @param {String|Number} arg1 第一个数字
|
|
53
|
+
* @param {String|Number} arg2 第二个数字
|
|
54
|
+
* @returns 返回计算后的数字
|
|
55
|
+
*/
|
|
56
|
+
function multiply(arg1, arg2) {
|
|
57
|
+
let m = 0,
|
|
58
|
+
s1 = arg1.toString(),
|
|
59
|
+
s2 = arg2.toString();
|
|
60
|
+
try {
|
|
61
|
+
m += s1.split(".")[1].length;
|
|
62
|
+
} catch (e) {}
|
|
63
|
+
try {
|
|
64
|
+
m += s2.split(".")[1].length;
|
|
65
|
+
} catch (e) {}
|
|
66
|
+
return (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 两个数字相除
|
|
71
|
+
* @param {String|Number} arg1 第一个数字
|
|
72
|
+
* @param {String|Number} arg2 第二个数字
|
|
73
|
+
* @returns {Number} 返回计算后的数字
|
|
74
|
+
*/
|
|
75
|
+
function divide(arg1, arg2) {
|
|
76
|
+
let t1 = 0,
|
|
77
|
+
t2 = 0,
|
|
78
|
+
r1,
|
|
79
|
+
r2;
|
|
80
|
+
try {
|
|
81
|
+
t1 = arg1.toString().split(".")[1].length;
|
|
82
|
+
} catch (e) {}
|
|
83
|
+
try {
|
|
84
|
+
t2 = arg2.toString().split(".")[1].length;
|
|
85
|
+
} catch (e) {}
|
|
86
|
+
r1 = Number(arg1.toString().replace(".", ""));
|
|
87
|
+
r2 = Number(arg2.toString().replace(".", ""));
|
|
88
|
+
return (r1 / r2) * Math.pow(10, t2 - t1);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 两个数字取模
|
|
93
|
+
* @param {String|Number} arg1 第一个数字
|
|
94
|
+
* @param {String|Number} arg2 第二个数字
|
|
95
|
+
* @returns {Number} 返回计算后的数字
|
|
96
|
+
*/
|
|
97
|
+
function modulo(arg1, arg2) {
|
|
98
|
+
let t1 = 0,
|
|
99
|
+
t2 = 0,
|
|
100
|
+
d = 0;
|
|
101
|
+
try {
|
|
102
|
+
t1 = arg1.toString().split(".")[1].length;
|
|
103
|
+
} catch (e) {}
|
|
104
|
+
try {
|
|
105
|
+
t2 = arg2.toString().split(".")[1].length;
|
|
106
|
+
} catch (e) {}
|
|
107
|
+
d = Math.pow(10, Math.max(t1, t2));
|
|
108
|
+
return (Math.round(Number(arg1) * d) % Math.round(Number(arg2) * d)) / d;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 最大公约数
|
|
113
|
+
* @param {String|Number} arg1 第一个数字
|
|
114
|
+
* @param {String|Number} arg2 第二个数字
|
|
115
|
+
* @returns {Number} 返回计算后的数字
|
|
116
|
+
*/
|
|
117
|
+
function gcd(arg1, arg2) {
|
|
118
|
+
return !arg2 ? arg1 : gcd(arg2, arg1 % arg2);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 最小公倍数
|
|
123
|
+
* @param {String|Number} arg1 第一个数字
|
|
124
|
+
* @param {String|Number} arg2 第二个数字
|
|
125
|
+
* @returns {Number} 返回计算后的数字
|
|
126
|
+
*/
|
|
127
|
+
function scm(arg1, arg2) {
|
|
128
|
+
return (arg1 * arg2) / gcd(arg1, arg2);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/* 数字精度 */
|
|
132
|
+
/**
|
|
133
|
+
* 强制保留小数位数
|
|
134
|
+
* @description 默认保留两位小数,解决原生的toFixed()会五舍六入的问题
|
|
135
|
+
* @param {String|Number} num 数字
|
|
136
|
+
* @param {Number} decimals 保留小数的位数,默认2位
|
|
137
|
+
* @param {Number} mode 保留小数模式
|
|
138
|
+
* @returns {String} 返回保留后的数字字符串
|
|
139
|
+
*/
|
|
140
|
+
function toFixed(num, decimals = 2, mode = MATH.ROUND) {
|
|
141
|
+
// 四舍五入
|
|
142
|
+
if (mode === MATH.ROUND) {
|
|
143
|
+
return _toFixedRound(num, decimals);
|
|
144
|
+
}
|
|
145
|
+
// 向下舍出
|
|
146
|
+
if (mode === MATH.ROUND_FLOOR) {
|
|
147
|
+
return _toFixedFloor(num, decimals);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 尽可能保留小数位数
|
|
153
|
+
* @param {String|Number} num 数字
|
|
154
|
+
* @param {Number} decimals 保留小数的位数,默认2位
|
|
155
|
+
* @param {Number} mode 保留小数模式
|
|
156
|
+
* @returns {Number} 返回保留后的数字
|
|
157
|
+
*/
|
|
158
|
+
function toDecimal(num, decimals = 2, mode = MATH.ROUND) {
|
|
159
|
+
// 四舍五入
|
|
160
|
+
if (mode === MATH.ROUND) {
|
|
161
|
+
return _toDecimalRound(num, decimals);
|
|
162
|
+
}
|
|
163
|
+
// 向下舍出
|
|
164
|
+
if (mode === MATH.ROUND_FLOOR) {
|
|
165
|
+
return _toDecimalFloor(num, decimals);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/* 内部函数 */
|
|
170
|
+
/**
|
|
171
|
+
* 四舍五入,强制保留小数位数
|
|
172
|
+
* @description 默认保留两位小数,此方法解决原生的toFixed()会五舍六入的问题
|
|
173
|
+
* @param {String|Number} num 数字
|
|
174
|
+
* @param {Number} decimals 保留小数的位数,默认2位
|
|
175
|
+
* @returns {String} 返回字符串的数字
|
|
176
|
+
*/
|
|
177
|
+
function _toFixedRound(num, decimals = 2) {
|
|
178
|
+
if (isNaN(num)) {
|
|
179
|
+
return "--";
|
|
180
|
+
}
|
|
181
|
+
let s = String(num);
|
|
182
|
+
if (!decimals) decimals = 0;
|
|
183
|
+
if (s.indexOf(".") === -1) s += ".";
|
|
184
|
+
s += new Array(decimals + 1).join("0");
|
|
185
|
+
if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (decimals + 1) + "})?)\\d*$").test(s)) {
|
|
186
|
+
let s = "0" + RegExp.$2,
|
|
187
|
+
pm = RegExp.$1,
|
|
188
|
+
a = RegExp.$3.length,
|
|
189
|
+
b = true;
|
|
190
|
+
if (a === decimals + 2) {
|
|
191
|
+
a = s.match(/\d/g);
|
|
192
|
+
if (parseInt(a[a.length - 1]) > 4) {
|
|
193
|
+
for (let i = a.length - 2; i >= 0; i--) {
|
|
194
|
+
a[i] = parseInt(a[i]) + 1;
|
|
195
|
+
if (a[i] === 10) {
|
|
196
|
+
a[i] = 0;
|
|
197
|
+
b = i !== 1;
|
|
198
|
+
} else break;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
s = a.join("").replace(new RegExp("(\\d+)(\\d{" + decimals + "})\\d$"), "$1.$2");
|
|
202
|
+
}
|
|
203
|
+
if (b) s = s.substr(1);
|
|
204
|
+
return (pm + s).replace(/\.$/, "");
|
|
205
|
+
}
|
|
206
|
+
return String(num);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* 向下舍出,强制保留小数位数
|
|
211
|
+
* @description 默认保留两位小数,此方法相当于强制截取小数位数
|
|
212
|
+
* @param {String|Number} num 数字
|
|
213
|
+
* @param {Number} decimals 保留小数的位数,默认2位
|
|
214
|
+
* @returns {Number} 返回字符串的数字
|
|
215
|
+
*/
|
|
216
|
+
function _toFixedFloor(num, decimals = 2) {
|
|
217
|
+
if (isNaN(num)) {
|
|
218
|
+
return "--";
|
|
219
|
+
}
|
|
220
|
+
// 默认为保留的小数点后两位
|
|
221
|
+
let dec = decimals;
|
|
222
|
+
let tempNum = Number(num);
|
|
223
|
+
let pointIndex = String(tempNum).indexOf(".") + 1; // 获取小数点的位置 + 1
|
|
224
|
+
let pointCount = pointIndex ? String(tempNum).length - pointIndex : 0; // 获取小数点后的个数(需要保证有小数位)
|
|
225
|
+
// 源数据为整数或者小数点后面小于decimals位的作补零处理
|
|
226
|
+
if (pointIndex === 0 || pointCount <= dec) {
|
|
227
|
+
let tempNumA = tempNum;
|
|
228
|
+
if (pointIndex === 0) {
|
|
229
|
+
tempNumA = `${tempNumA}.`;
|
|
230
|
+
for (let index = 0; index < dec - pointCount; index++) {
|
|
231
|
+
tempNumA = `${tempNumA}0`;
|
|
232
|
+
}
|
|
233
|
+
} else {
|
|
234
|
+
for (let index = 0; index < dec - pointCount; index++) {
|
|
235
|
+
tempNumA = `${tempNumA}0`;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return tempNumA;
|
|
239
|
+
}
|
|
240
|
+
let realVal = "";
|
|
241
|
+
// 截取当前数据到小数点后decimals位
|
|
242
|
+
realVal = `${String(tempNum).split(".")[0]}.${String(tempNum).split(".")[1].substring(0, dec)}`;
|
|
243
|
+
return String(realVal);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 四舍五入,尽可能保留小数
|
|
248
|
+
* @param {String|Number} num 数字
|
|
249
|
+
* @param {Number} decimals 保留小数的位数,默认2位
|
|
250
|
+
* @returns {Number} 返回保留后的数字
|
|
251
|
+
*/
|
|
252
|
+
function _toDecimalRound(num, decimals = 2) {
|
|
253
|
+
if (isNaN(num)) {
|
|
254
|
+
return "--";
|
|
255
|
+
}
|
|
256
|
+
let n = Math.pow(10, decimals);
|
|
257
|
+
return Math.round(num * n) / n;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* 向下舍入,尽可能保留小数
|
|
262
|
+
* @param {String|Number} num 数字
|
|
263
|
+
* @param {Number} decimals 保留小数的位数,默认2位
|
|
264
|
+
* @returns {Number} 返回保留后的数字
|
|
265
|
+
*/
|
|
266
|
+
function _toDecimalFloor(num, decimals = 2) {
|
|
267
|
+
if (isNaN(num)) {
|
|
268
|
+
return "--";
|
|
269
|
+
}
|
|
270
|
+
let n = Math.pow(10, decimals);
|
|
271
|
+
return Math.floor(num * n) / n;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export { add, divide, gcd, modulo, multiply, scm, subtract, toDecimal, toFixed };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 转为数字类型
|
|
3
|
+
* @description 解决部分浏览器在转换 '08','09'等是0开头时被默认转8进制问题
|
|
4
|
+
* @param {String,number} value 转换的值
|
|
5
|
+
* @param {Number} radix 进制数,默认10进制
|
|
6
|
+
* @returns {Number} 返回转换后的数字
|
|
7
|
+
*/
|
|
8
|
+
function parseInt(value, radix = 10) {
|
|
9
|
+
return Number.parseInt(value, radix);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { parseInt };
|