@fast-china/utils 1.0.38 → 2.0.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 +21 -0
- package/CONTRIBUTING.md +87 -0
- package/README.md +111 -50
- package/README.zh.md +111 -50
- package/SECURITY.md +46 -0
- package/dist/array/index.d.mts +87 -0
- package/dist/array/index.d.mts.map +1 -0
- package/dist/array/index.mjs +161 -0
- package/dist/array/index.mjs.map +1 -0
- package/dist/async/index.d.mts +146 -0
- package/dist/async/index.d.mts.map +1 -0
- package/dist/async/index.mjs +336 -0
- package/dist/async/index.mjs.map +1 -0
- package/dist/base64/index.d.mts +105 -0
- package/dist/base64/index.d.mts.map +1 -0
- package/dist/base64/index.mjs +427 -0
- package/dist/base64/index.mjs.map +1 -0
- package/dist/color/index.d.mts +90 -0
- package/dist/color/index.d.mts.map +1 -0
- package/dist/color/index.mjs +178 -0
- package/dist/color/index.mjs.map +1 -0
- package/dist/crypto/index.d.mts +157 -0
- package/dist/crypto/index.d.mts.map +1 -0
- package/dist/crypto/index.mjs +541 -0
- package/dist/crypto/index.mjs.map +1 -0
- package/dist/date/index.d.mts +191 -0
- package/dist/date/index.d.mts.map +1 -0
- package/dist/date/index.mjs +383 -0
- package/dist/date/index.mjs.map +1 -0
- package/dist/dom/style.d.mts +30 -0
- package/dist/dom/style.d.mts.map +1 -0
- package/dist/dom/style.mjs +74 -0
- package/dist/dom/style.mjs.map +1 -0
- package/dist/env/index.d.mts +63 -0
- package/dist/env/index.d.mts.map +1 -0
- package/dist/env/index.mjs +97 -0
- package/dist/env/index.mjs.map +1 -0
- package/dist/identity/index.d.mts +78 -0
- package/dist/identity/index.d.mts.map +1 -0
- package/dist/identity/index.mjs +87 -0
- package/dist/identity/index.mjs.map +1 -0
- package/dist/index.d.mts +24 -0
- package/dist/index.mjs +23 -0
- package/dist/internal/text.mjs +36 -0
- package/dist/internal/text.mjs.map +1 -0
- package/dist/logger/index.d.mts +87 -0
- package/dist/logger/index.d.mts.map +1 -0
- package/dist/logger/index.mjs +124 -0
- package/dist/logger/index.mjs.map +1 -0
- package/dist/number/index.d.mts +89 -0
- package/dist/number/index.d.mts.map +1 -0
- package/dist/number/index.mjs +215 -0
- package/dist/number/index.mjs.map +1 -0
- package/dist/object/index.d.mts +76 -0
- package/dist/object/index.d.mts.map +1 -0
- package/dist/object/index.mjs +134 -0
- package/dist/object/index.mjs.map +1 -0
- package/dist/storage/index.d.mts +104 -0
- package/dist/storage/index.d.mts.map +1 -0
- package/dist/storage/index.mjs +324 -0
- package/dist/storage/index.mjs.map +1 -0
- package/dist/string/index.d.mts +130 -0
- package/dist/string/index.d.mts.map +1 -0
- package/dist/string/index.mjs +275 -0
- package/dist/string/index.mjs.map +1 -0
- package/dist/vue/emits.d.mts +24 -0
- package/dist/vue/emits.d.mts.map +1 -0
- package/dist/vue/emits.mjs +47 -0
- package/dist/vue/emits.mjs.map +1 -0
- package/dist/vue/expose.d.mts +12 -0
- package/dist/vue/expose.d.mts.map +1 -0
- package/dist/vue/expose.mjs +16 -0
- package/dist/vue/expose.mjs.map +1 -0
- package/dist/vue/func.d.mts +14 -0
- package/dist/vue/func.d.mts.map +1 -0
- package/dist/vue/func.mjs +16 -0
- package/dist/vue/func.mjs.map +1 -0
- package/dist/vue/index.d.mts +9 -0
- package/dist/vue/install.d.mts +69 -0
- package/dist/vue/install.d.mts.map +1 -0
- package/dist/vue/install.mjs +123 -0
- package/dist/vue/install.mjs.map +1 -0
- package/dist/vue/props.d.mts +23 -0
- package/dist/vue/props.d.mts.map +1 -0
- package/dist/vue/props.mjs +41 -0
- package/dist/vue/props.mjs.map +1 -0
- package/dist/vue/render.d.mts +13 -0
- package/dist/vue/render.d.mts.map +1 -0
- package/dist/vue/render.mjs +23 -0
- package/dist/vue/render.mjs.map +1 -0
- package/dist/vue/slots.d.mts +19 -0
- package/dist/vue/slots.d.mts.map +1 -0
- package/dist/vue/slots.mjs +13 -0
- package/dist/vue/slots.mjs.map +1 -0
- package/dist/vue/with.d.mts +12 -0
- package/dist/vue/with.d.mts.map +1 -0
- package/dist/vue/with.mjs +15 -0
- package/dist/vue/with.mjs.map +1 -0
- package/docs/API.md +96 -0
- package/docs/API.zh-CN.md +96 -0
- package/docs/DEVELOPMENT_RELEASE.zh-CN.md +65 -0
- package/docs/RUNTIME_CONTRACT.md +37 -0
- package/package.json +65 -73
- package/src/array/index.ts +173 -0
- package/src/async/index.ts +475 -0
- package/src/base64/index.ts +374 -0
- package/src/color/index.ts +208 -0
- package/src/crypto/index.ts +670 -0
- package/src/date/index.ts +451 -0
- package/src/dom/index.ts +6 -0
- package/src/dom/style.ts +92 -0
- package/src/env/index.ts +169 -0
- package/src/identity/index.ts +144 -0
- package/src/index.ts +20 -0
- package/src/internal/text.ts +46 -0
- package/src/logger/index.ts +219 -0
- package/src/number/index.ts +235 -0
- package/src/object/index.ts +160 -0
- package/src/storage/index.ts +524 -0
- package/src/string/index.ts +328 -0
- package/src/vue/emits.ts +71 -0
- package/src/vue/expose.ts +11 -0
- package/src/vue/func.ts +17 -0
- package/src/vue/index.ts +13 -0
- package/src/vue/install.ts +185 -0
- package/src/vue/props.ts +39 -0
- package/src/vue/render.ts +41 -0
- package/src/vue/slots.ts +23 -0
- package/src/vue/with.ts +10 -0
- package/Fast.png +0 -0
- package/dist/index.global.js +0 -14500
- package/dist/index.global.js.map +0 -1
- package/dist/index.global.min.js +0 -2
- package/dist/index.global.min.js.map +0 -1
- package/es/array/index.d.ts +0 -19
- package/es/array/index.mjs +0 -2
- package/es/array/index.mjs.map +0 -1
- package/es/base64/index.d.ts +0 -21
- package/es/base64/index.mjs +0 -2
- package/es/base64/index.mjs.map +0 -1
- package/es/click/index.d.ts +0 -33
- package/es/click/index.mjs +0 -2
- package/es/click/index.mjs.map +0 -1
- package/es/color/index.d.ts +0 -33
- package/es/color/index.mjs +0 -2
- package/es/color/index.mjs.map +0 -1
- package/es/console/index.d.ts +0 -31
- package/es/console/index.mjs +0 -2
- package/es/console/index.mjs.map +0 -1
- package/es/crypto/index.d.ts +0 -46
- package/es/crypto/index.mjs +0 -2
- package/es/crypto/index.mjs.map +0 -1
- package/es/date/index.d.ts +0 -44
- package/es/date/index.mjs +0 -2
- package/es/date/index.mjs.map +0 -1
- package/es/dom/index.d.ts +0 -1
- package/es/dom/index.mjs +0 -2
- package/es/dom/index.mjs.map +0 -1
- package/es/dom/style.d.ts +0 -12
- package/es/dom/style.mjs +0 -2
- package/es/dom/style.mjs.map +0 -1
- package/es/env/index.d.ts +0 -25
- package/es/env/index.mjs +0 -2
- package/es/env/index.mjs.map +0 -1
- package/es/error/index.d.ts +0 -3
- package/es/error/index.mjs +0 -2
- package/es/error/index.mjs.map +0 -1
- package/es/identity/index.d.ts +0 -14
- package/es/identity/index.mjs +0 -2
- package/es/identity/index.mjs.map +0 -1
- package/es/index.d.ts +0 -15
- package/es/index.es.d.ts +0 -2
- package/es/index.mjs +0 -2
- package/es/index.mjs.map +0 -1
- package/es/object/index.d.ts +0 -13
- package/es/object/index.mjs +0 -2
- package/es/object/index.mjs.map +0 -1
- package/es/storage/index.d.ts +0 -96
- package/es/storage/index.mjs +0 -2
- package/es/storage/index.mjs.map +0 -1
- package/es/string/index.d.ts +0 -114
- package/es/string/index.mjs +0 -2
- package/es/string/index.mjs.map +0 -1
- package/es/vue/emits.d.ts +0 -8
- package/es/vue/emits.mjs +0 -2
- package/es/vue/emits.mjs.map +0 -1
- package/es/vue/expose.d.ts +0 -4
- package/es/vue/expose.mjs +0 -2
- package/es/vue/expose.mjs.map +0 -1
- package/es/vue/func.d.ts +0 -6
- package/es/vue/func.mjs +0 -2
- package/es/vue/func.mjs.map +0 -1
- package/es/vue/index.d.ts +0 -8
- package/es/vue/index.mjs +0 -2
- package/es/vue/index.mjs.map +0 -1
- package/es/vue/install.d.ts +0 -5
- package/es/vue/install.mjs +0 -2
- package/es/vue/install.mjs.map +0 -1
- package/es/vue/props.d.ts +0 -9
- package/es/vue/props.mjs +0 -2
- package/es/vue/props.mjs.map +0 -1
- package/es/vue/slots.d.ts +0 -11
- package/es/vue/slots.mjs +0 -2
- package/es/vue/slots.mjs.map +0 -1
- package/es/vue/useRender.d.ts +0 -6
- package/es/vue/useRender.mjs +0 -2
- package/es/vue/useRender.mjs.map +0 -1
- package/es/vue/with.d.ts +0 -5
- package/es/vue/with.mjs +0 -2
- package/es/vue/with.mjs.map +0 -1
- package/lib/array/index.d.ts +0 -19
- package/lib/array/index.js +0 -2
- package/lib/array/index.js.map +0 -1
- package/lib/base64/index.d.ts +0 -21
- package/lib/base64/index.js +0 -2
- package/lib/base64/index.js.map +0 -1
- package/lib/click/index.d.ts +0 -33
- package/lib/click/index.js +0 -2
- package/lib/click/index.js.map +0 -1
- package/lib/color/index.d.ts +0 -33
- package/lib/color/index.js +0 -2
- package/lib/color/index.js.map +0 -1
- package/lib/console/index.d.ts +0 -31
- package/lib/console/index.js +0 -2
- package/lib/console/index.js.map +0 -1
- package/lib/crypto/index.d.ts +0 -46
- package/lib/crypto/index.js +0 -2
- package/lib/crypto/index.js.map +0 -1
- package/lib/date/index.d.ts +0 -44
- package/lib/date/index.js +0 -2
- package/lib/date/index.js.map +0 -1
- package/lib/dom/index.d.ts +0 -1
- package/lib/dom/index.js +0 -2
- package/lib/dom/index.js.map +0 -1
- package/lib/dom/style.d.ts +0 -12
- package/lib/dom/style.js +0 -2
- package/lib/dom/style.js.map +0 -1
- package/lib/env/index.d.ts +0 -25
- package/lib/env/index.js +0 -2
- package/lib/env/index.js.map +0 -1
- package/lib/error/index.d.ts +0 -3
- package/lib/error/index.js +0 -2
- package/lib/error/index.js.map +0 -1
- package/lib/identity/index.d.ts +0 -14
- package/lib/identity/index.js +0 -2
- package/lib/identity/index.js.map +0 -1
- package/lib/index.d.ts +0 -15
- package/lib/index.es.d.ts +0 -2
- package/lib/index.js +0 -2
- package/lib/index.js.map +0 -1
- package/lib/object/index.d.ts +0 -13
- package/lib/object/index.js +0 -2
- package/lib/object/index.js.map +0 -1
- package/lib/storage/index.d.ts +0 -96
- package/lib/storage/index.js +0 -2
- package/lib/storage/index.js.map +0 -1
- package/lib/string/index.d.ts +0 -114
- package/lib/string/index.js +0 -2
- package/lib/string/index.js.map +0 -1
- package/lib/vue/emits.d.ts +0 -8
- package/lib/vue/emits.js +0 -2
- package/lib/vue/emits.js.map +0 -1
- package/lib/vue/expose.d.ts +0 -4
- package/lib/vue/expose.js +0 -2
- package/lib/vue/expose.js.map +0 -1
- package/lib/vue/func.d.ts +0 -6
- package/lib/vue/func.js +0 -2
- package/lib/vue/func.js.map +0 -1
- package/lib/vue/index.d.ts +0 -8
- package/lib/vue/index.js +0 -2
- package/lib/vue/index.js.map +0 -1
- package/lib/vue/install.d.ts +0 -5
- package/lib/vue/install.js +0 -2
- package/lib/vue/install.js.map +0 -1
- package/lib/vue/props.d.ts +0 -9
- package/lib/vue/props.js +0 -2
- package/lib/vue/props.js.map +0 -1
- package/lib/vue/slots.d.ts +0 -11
- package/lib/vue/slots.js +0 -2
- package/lib/vue/slots.js.map +0 -1
- package/lib/vue/useRender.d.ts +0 -6
- package/lib/vue/useRender.js +0 -2
- package/lib/vue/useRender.js.map +0 -1
- package/lib/vue/with.d.ts +0 -5
- package/lib/vue/with.js +0 -2
- package/lib/vue/with.js.map +0 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
//#region src/logger/index.d.ts
|
|
2
|
+
/** 日志严重级别,按从低到高排列。 */
|
|
3
|
+
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
4
|
+
/** 日志输出目标需要实现的最小控制台接口。 */
|
|
5
|
+
interface LoggerSink {
|
|
6
|
+
/**
|
|
7
|
+
* 接收通过级别过滤后的调试参数。
|
|
8
|
+
* @param data - 已格式化的品牌、作用域、消息以及保持原始类型的附加值。
|
|
9
|
+
*/
|
|
10
|
+
debug: (...data: unknown[]) => void;
|
|
11
|
+
/**
|
|
12
|
+
* 接收通过级别过滤后的普通信息参数;对应 Logger 的 `info` 级别。
|
|
13
|
+
* @param data - 已格式化的品牌、作用域、消息以及保持原始类型的附加值。
|
|
14
|
+
*/
|
|
15
|
+
log: (...data: unknown[]) => void;
|
|
16
|
+
/**
|
|
17
|
+
* 接收通过级别过滤后的警告参数。
|
|
18
|
+
* @param data - 已格式化的品牌、作用域、消息以及保持原始类型的附加值。
|
|
19
|
+
*/
|
|
20
|
+
warn: (...data: unknown[]) => void;
|
|
21
|
+
/**
|
|
22
|
+
* 接收通过级别过滤后的错误参数。
|
|
23
|
+
* @param data - 已格式化的品牌、作用域、消息以及保持原始类型的附加值。
|
|
24
|
+
*/
|
|
25
|
+
error: (...data: unknown[]) => void;
|
|
26
|
+
}
|
|
27
|
+
/** {@link createLogger} 的不可变配置。 */
|
|
28
|
+
interface LoggerOptions {
|
|
29
|
+
/** 最低输出级别,默认 `info`;低于该优先级的消息不会传给 Sink。 */
|
|
30
|
+
level?: LogLevel;
|
|
31
|
+
/** 日志品牌前缀,默认 `Fast`;必须是无外围空白的非空字符串。 */
|
|
32
|
+
prefix?: string;
|
|
33
|
+
/** 可注入输出目标,默认当前运行时的 `console`;Logger 不会修改该对象。 */
|
|
34
|
+
sink?: LoggerSink;
|
|
35
|
+
/** uni-app App-Plus/HBuilderX 中把附加参数逐条转成单行文本输出,默认 `false`;其他平台忽略。 */
|
|
36
|
+
uniAppPlusSplit?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/** 配置隔离、无全局可变状态的轻量日志器。 */
|
|
39
|
+
interface Logger {
|
|
40
|
+
/**
|
|
41
|
+
* 输出指定作用域的调试信息。
|
|
42
|
+
* @param scope - 模块、组件或业务来源名称。
|
|
43
|
+
* @param message - 主消息文本。
|
|
44
|
+
* @param data - 保持原始类型的附加值。
|
|
45
|
+
* @throws `TypeError` 或 `RangeError` 当作用域不是无外围空白的非空字符串。
|
|
46
|
+
*/
|
|
47
|
+
debug: (scope: string, message: string, ...data: unknown[]) => void;
|
|
48
|
+
/**
|
|
49
|
+
* 输出指定作用域的普通信息。
|
|
50
|
+
* @param scope - 模块、组件或业务来源名称。
|
|
51
|
+
* @param message - 主消息文本。
|
|
52
|
+
* @param data - 保持原始类型的附加值。
|
|
53
|
+
* @throws `TypeError` 或 `RangeError` 当作用域不是无外围空白的非空字符串。
|
|
54
|
+
*/
|
|
55
|
+
info: (scope: string, message: string, ...data: unknown[]) => void;
|
|
56
|
+
/**
|
|
57
|
+
* 输出指定作用域的警告信息。
|
|
58
|
+
* @param scope - 模块、组件或业务来源名称。
|
|
59
|
+
* @param message - 主消息文本。
|
|
60
|
+
* @param data - 保持原始类型的附加值。
|
|
61
|
+
* @throws `TypeError` 或 `RangeError` 当作用域不是无外围空白的非空字符串。
|
|
62
|
+
*/
|
|
63
|
+
warn: (scope: string, message: string, ...data: unknown[]) => void;
|
|
64
|
+
/**
|
|
65
|
+
* 输出指定作用域的错误信息。
|
|
66
|
+
* @param scope - 模块、组件或业务来源名称。
|
|
67
|
+
* @param message - 主消息文本。
|
|
68
|
+
* @param data - 保持原始类型的附加值。
|
|
69
|
+
* @throws `TypeError` 或 `RangeError` 当作用域不是无外围空白的非空字符串。
|
|
70
|
+
*/
|
|
71
|
+
error: (scope: string, message: string, ...data: unknown[]) => void;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 创建独立日志器。
|
|
75
|
+
*
|
|
76
|
+
* @remarks 本库其他模块不会自动记录、吞掉或转换异常。日志内容可能进入持久化平台,
|
|
77
|
+
* 调用方不得传入密码、令牌、密钥或完整个人数据。
|
|
78
|
+
* @param options - 级别、前缀、输出目标和 uni-app App-Plus 拆分选项。
|
|
79
|
+
* @returns 不会修改全局控制台或其他日志器配置的新实例。
|
|
80
|
+
* @throws `RangeError` 当级别未知,或前缀、作用域不是有效的非空字符串。
|
|
81
|
+
*/
|
|
82
|
+
declare function createLogger(options?: LoggerOptions): Logger;
|
|
83
|
+
/** 默认使用 `Fast` 前缀和 `info` 级别的便捷日志器。 */
|
|
84
|
+
declare const logger: Logger;
|
|
85
|
+
//#endregion
|
|
86
|
+
export { LogLevel, Logger, LoggerOptions, LoggerSink, createLogger, logger };
|
|
87
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/logger/index.ts"],"mappings":";;KACY;;UAGK;;;;;EAKhB,WAAW;;;;;EAKX,SAAS;;;;;EAKT,UAAU;;;;;EAKV,WAAW;;;UAIK;;EAEhB,QAAQ;;EAER;;EAEA,OAAO;;EAEP;;;UAIgB;;;;;;;;EAQhB,QAAQ,eAAe,oBAAoB;;;;;;;;EAQ3C,OAAO,eAAe,oBAAoB;;;;;;;;EAQ1C,OAAO,eAAe,oBAAoB;;;;;;;;EAQ1C,QAAQ,eAAe,oBAAoB;;;;;;;;;;;iBA4F5B,aAAa,UAAS,gBAAqB;;cAsD9C,QAAQ"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
//#region src/logger/index.ts
|
|
2
|
+
const levelPriority = {
|
|
3
|
+
debug: 10,
|
|
4
|
+
info: 20,
|
|
5
|
+
warn: 30,
|
|
6
|
+
error: 40
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* 判断未知值是否为受支持日志级别。
|
|
10
|
+
*
|
|
11
|
+
* @param value - 待检查配置值。
|
|
12
|
+
* @returns 值是 `debug`、`info`、`warn` 或 `error` 时返回 `true`。
|
|
13
|
+
*/
|
|
14
|
+
const isLogLevel = (value) => typeof value === "string" && Object.hasOwn(levelPriority, value);
|
|
15
|
+
const runtimeLoggerGlobals = globalThis;
|
|
16
|
+
/**
|
|
17
|
+
* 检测 uni-app App-Plus 日志环境。
|
|
18
|
+
*
|
|
19
|
+
* @returns 全局 `uni` 与 `plus` 同时存在时返回 `true`。
|
|
20
|
+
*/
|
|
21
|
+
const isUniAppPlus = () => {
|
|
22
|
+
return runtimeLoggerGlobals.uni !== void 0 && runtimeLoggerGlobals.plus !== void 0;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* 把日志附加值转换为适合 HBuilderX 单行输出的文本。
|
|
26
|
+
*
|
|
27
|
+
* @remarks 循环引用会替换为 `[Circular]`,BigInt 保留 `n` 后缀,Error 优先输出堆栈。
|
|
28
|
+
* @param value - 任意日志附加值。
|
|
29
|
+
* @returns 不会因 JSON 序列化失败而中断日志调用的文本。
|
|
30
|
+
*/
|
|
31
|
+
const formatSplitValue = (value) => {
|
|
32
|
+
if (typeof value === "string") return value;
|
|
33
|
+
if (typeof value === "bigint") return `${value.toString()}n`;
|
|
34
|
+
if (value instanceof Error) return value.stack ?? `${value.name}: ${value.message}`;
|
|
35
|
+
const visited = /* @__PURE__ */ new WeakSet();
|
|
36
|
+
try {
|
|
37
|
+
const serialized = JSON.stringify(value, (_key, item) => {
|
|
38
|
+
if (typeof item === "bigint") return `${item.toString()}n`;
|
|
39
|
+
if (typeof item !== "object" || item === null) return item;
|
|
40
|
+
if (visited.has(item)) return "[Circular]";
|
|
41
|
+
visited.add(item);
|
|
42
|
+
return item;
|
|
43
|
+
}, 2);
|
|
44
|
+
return typeof serialized === "string" ? serialized : String(value);
|
|
45
|
+
} catch {
|
|
46
|
+
return String(value);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const defaultConsoleSink = {
|
|
50
|
+
debug: (...data) => {
|
|
51
|
+
if (typeof console.debug === "function") console.debug(...data);
|
|
52
|
+
else console.log(...data);
|
|
53
|
+
},
|
|
54
|
+
log: (...data) => {
|
|
55
|
+
console.log(...data);
|
|
56
|
+
},
|
|
57
|
+
warn: (...data) => {
|
|
58
|
+
console.warn(...data);
|
|
59
|
+
},
|
|
60
|
+
error: (...data) => {
|
|
61
|
+
console.error(...data);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* 创建独立日志器。
|
|
66
|
+
*
|
|
67
|
+
* @remarks 本库其他模块不会自动记录、吞掉或转换异常。日志内容可能进入持久化平台,
|
|
68
|
+
* 调用方不得传入密码、令牌、密钥或完整个人数据。
|
|
69
|
+
* @param options - 级别、前缀、输出目标和 uni-app App-Plus 拆分选项。
|
|
70
|
+
* @returns 不会修改全局控制台或其他日志器配置的新实例。
|
|
71
|
+
* @throws `RangeError` 当级别未知,或前缀、作用域不是有效的非空字符串。
|
|
72
|
+
*/
|
|
73
|
+
function createLogger(options = {}) {
|
|
74
|
+
const requestedLevel = options.level ?? "info";
|
|
75
|
+
const requestedPrefix = options.prefix ?? "Fast";
|
|
76
|
+
const sink = options.sink ?? defaultConsoleSink;
|
|
77
|
+
if (!isLogLevel(requestedLevel)) throw new RangeError(`Unknown logger level: ${String(requestedLevel)}.`);
|
|
78
|
+
if (typeof requestedPrefix !== "string" || requestedPrefix.length === 0) throw new RangeError("Logger prefix must be a non-empty string.");
|
|
79
|
+
const level = requestedLevel;
|
|
80
|
+
const prefix = requestedPrefix;
|
|
81
|
+
const uniAppPlusSplit = options.uniAppPlusSplit ?? false;
|
|
82
|
+
/**
|
|
83
|
+
* 应用级别过滤、标题格式和平台输出策略。
|
|
84
|
+
*
|
|
85
|
+
* @param messageLevel - 本条消息的严重级别。
|
|
86
|
+
* @param scope - 模块、组件或业务来源名称。
|
|
87
|
+
* @param message - 主消息文本。
|
|
88
|
+
* @param data - 保持原始类型的附加值。
|
|
89
|
+
* @throws `RangeError` 当作用域不是非空字符串或包含外围空白。
|
|
90
|
+
*/
|
|
91
|
+
const write = (messageLevel, scope, message, data) => {
|
|
92
|
+
if (typeof scope !== "string") throw new TypeError("Logger scope must be a string.");
|
|
93
|
+
if (scope.length === 0 || scope.trim() !== scope) throw new RangeError("Logger scope must be a non-empty string without surrounding whitespace.");
|
|
94
|
+
if (levelPriority[messageLevel] < levelPriority[level]) return;
|
|
95
|
+
const heading = `[${prefix}:${scope}]`;
|
|
96
|
+
const sinkMethod = messageLevel === "info" ? "log" : messageLevel;
|
|
97
|
+
if (uniAppPlusSplit && isUniAppPlus()) {
|
|
98
|
+
sink[sinkMethod](`${heading} ${message}`);
|
|
99
|
+
for (const item of data) sink[sinkMethod](formatSplitValue(item));
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
sink[sinkMethod](heading, message, ...data);
|
|
103
|
+
};
|
|
104
|
+
return {
|
|
105
|
+
debug: (scope, message, ...data) => {
|
|
106
|
+
write("debug", scope, message, data);
|
|
107
|
+
},
|
|
108
|
+
info: (scope, message, ...data) => {
|
|
109
|
+
write("info", scope, message, data);
|
|
110
|
+
},
|
|
111
|
+
warn: (scope, message, ...data) => {
|
|
112
|
+
write("warn", scope, message, data);
|
|
113
|
+
},
|
|
114
|
+
error: (scope, message, ...data) => {
|
|
115
|
+
write("error", scope, message, data);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/** 默认使用 `Fast` 前缀和 `info` 级别的便捷日志器。 */
|
|
120
|
+
const logger = createLogger();
|
|
121
|
+
//#endregion
|
|
122
|
+
export { createLogger, logger };
|
|
123
|
+
|
|
124
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/logger/index.ts"],"sourcesContent":["/** 日志严重级别,按从低到高排列。 */\nexport type LogLevel = \"debug\" | \"info\" | \"warn\" | \"error\";\n\n/** 日志输出目标需要实现的最小控制台接口。 */\nexport interface LoggerSink {\n\t/**\n\t * 接收通过级别过滤后的调试参数。\n\t * @param data - 已格式化的品牌、作用域、消息以及保持原始类型的附加值。\n\t */\n\tdebug: (...data: unknown[]) => void;\n\t/**\n\t * 接收通过级别过滤后的普通信息参数;对应 Logger 的 `info` 级别。\n\t * @param data - 已格式化的品牌、作用域、消息以及保持原始类型的附加值。\n\t */\n\tlog: (...data: unknown[]) => void;\n\t/**\n\t * 接收通过级别过滤后的警告参数。\n\t * @param data - 已格式化的品牌、作用域、消息以及保持原始类型的附加值。\n\t */\n\twarn: (...data: unknown[]) => void;\n\t/**\n\t * 接收通过级别过滤后的错误参数。\n\t * @param data - 已格式化的品牌、作用域、消息以及保持原始类型的附加值。\n\t */\n\terror: (...data: unknown[]) => void;\n}\n\n/** {@link createLogger} 的不可变配置。 */\nexport interface LoggerOptions {\n\t/** 最低输出级别,默认 `info`;低于该优先级的消息不会传给 Sink。 */\n\tlevel?: LogLevel;\n\t/** 日志品牌前缀,默认 `Fast`;必须是无外围空白的非空字符串。 */\n\tprefix?: string;\n\t/** 可注入输出目标,默认当前运行时的 `console`;Logger 不会修改该对象。 */\n\tsink?: LoggerSink;\n\t/** uni-app App-Plus/HBuilderX 中把附加参数逐条转成单行文本输出,默认 `false`;其他平台忽略。 */\n\tuniAppPlusSplit?: boolean;\n}\n\n/** 配置隔离、无全局可变状态的轻量日志器。 */\nexport interface Logger {\n\t/**\n\t * 输出指定作用域的调试信息。\n\t * @param scope - 模块、组件或业务来源名称。\n\t * @param message - 主消息文本。\n\t * @param data - 保持原始类型的附加值。\n\t * @throws `TypeError` 或 `RangeError` 当作用域不是无外围空白的非空字符串。\n\t */\n\tdebug: (scope: string, message: string, ...data: unknown[]) => void;\n\t/**\n\t * 输出指定作用域的普通信息。\n\t * @param scope - 模块、组件或业务来源名称。\n\t * @param message - 主消息文本。\n\t * @param data - 保持原始类型的附加值。\n\t * @throws `TypeError` 或 `RangeError` 当作用域不是无外围空白的非空字符串。\n\t */\n\tinfo: (scope: string, message: string, ...data: unknown[]) => void;\n\t/**\n\t * 输出指定作用域的警告信息。\n\t * @param scope - 模块、组件或业务来源名称。\n\t * @param message - 主消息文本。\n\t * @param data - 保持原始类型的附加值。\n\t * @throws `TypeError` 或 `RangeError` 当作用域不是无外围空白的非空字符串。\n\t */\n\twarn: (scope: string, message: string, ...data: unknown[]) => void;\n\t/**\n\t * 输出指定作用域的错误信息。\n\t * @param scope - 模块、组件或业务来源名称。\n\t * @param message - 主消息文本。\n\t * @param data - 保持原始类型的附加值。\n\t * @throws `TypeError` 或 `RangeError` 当作用域不是无外围空白的非空字符串。\n\t */\n\terror: (scope: string, message: string, ...data: unknown[]) => void;\n}\n\nconst levelPriority: Readonly<Record<LogLevel, number>> = {\n\tdebug: 10,\n\tinfo: 20,\n\twarn: 30,\n\terror: 40,\n};\n\n/**\n * 判断未知值是否为受支持日志级别。\n *\n * @param value - 待检查配置值。\n * @returns 值是 `debug`、`info`、`warn` 或 `error` 时返回 `true`。\n */\nconst isLogLevel = (value: unknown): value is LogLevel => typeof value === \"string\" && Object.hasOwn(levelPriority, value);\n\n/** uni-app App-Plus 检测所需的平台全局对象最小视图。 */\ninterface RuntimeLoggerGlobals {\n\t/** App-Plus 原生运行时标记。 */\n\tplus?: unknown;\n\t/** uni-app 运行时标记。 */\n\tuni?: unknown;\n}\n\nconst runtimeLoggerGlobals = globalThis as unknown as RuntimeLoggerGlobals;\n\n/**\n * 检测 uni-app App-Plus 日志环境。\n *\n * @returns 全局 `uni` 与 `plus` 同时存在时返回 `true`。\n */\nconst isUniAppPlus = (): boolean => {\n\treturn runtimeLoggerGlobals.uni !== undefined && runtimeLoggerGlobals.plus !== undefined;\n};\n\n/**\n * 把日志附加值转换为适合 HBuilderX 单行输出的文本。\n *\n * @remarks 循环引用会替换为 `[Circular]`,BigInt 保留 `n` 后缀,Error 优先输出堆栈。\n * @param value - 任意日志附加值。\n * @returns 不会因 JSON 序列化失败而中断日志调用的文本。\n */\nconst formatSplitValue = (value: unknown): string => {\n\tif (typeof value === \"string\") return value;\n\tif (typeof value === \"bigint\") return `${value.toString()}n`;\n\tif (value instanceof Error) return value.stack ?? `${value.name}: ${value.message}`;\n\tconst visited = new WeakSet();\n\ttry {\n\t\tconst serialized: unknown = JSON.stringify(\n\t\t\tvalue,\n\t\t\t(_key, item: unknown): unknown => {\n\t\t\t\tif (typeof item === \"bigint\") return `${item.toString()}n`;\n\t\t\t\tif (typeof item !== \"object\" || item === null) return item;\n\t\t\t\tif (visited.has(item)) return \"[Circular]\";\n\t\t\t\tvisited.add(item);\n\t\t\t\treturn item;\n\t\t\t},\n\t\t\t2\n\t\t);\n\t\treturn typeof serialized === \"string\" ? serialized : String(value);\n\t} catch {\n\t\treturn String(value);\n\t}\n};\n\nconst defaultConsoleSink: LoggerSink = {\n\tdebug: (...data): void => {\n\t\tif (typeof console.debug === \"function\") console.debug(...data);\n\t\telse console.log(...data);\n\t},\n\tlog: (...data): void => {\n\t\tconsole.log(...data);\n\t},\n\twarn: (...data): void => {\n\t\tconsole.warn(...data);\n\t},\n\terror: (...data): void => {\n\t\tconsole.error(...data);\n\t},\n};\n\n/**\n * 创建独立日志器。\n *\n * @remarks 本库其他模块不会自动记录、吞掉或转换异常。日志内容可能进入持久化平台,\n * 调用方不得传入密码、令牌、密钥或完整个人数据。\n * @param options - 级别、前缀、输出目标和 uni-app App-Plus 拆分选项。\n * @returns 不会修改全局控制台或其他日志器配置的新实例。\n * @throws `RangeError` 当级别未知,或前缀、作用域不是有效的非空字符串。\n */\nexport function createLogger(options: LoggerOptions = {}): Logger {\n\tconst requestedLevel: unknown = options.level ?? \"info\";\n\tconst requestedPrefix: unknown = options.prefix ?? \"Fast\";\n\tconst sink = options.sink ?? defaultConsoleSink;\n\tif (!isLogLevel(requestedLevel)) throw new RangeError(`Unknown logger level: ${String(requestedLevel)}.`);\n\tif (typeof requestedPrefix !== \"string\" || requestedPrefix.length === 0) {\n\t\tthrow new RangeError(\"Logger prefix must be a non-empty string.\");\n\t}\n\tconst level = requestedLevel;\n\tconst prefix = requestedPrefix;\n\tconst uniAppPlusSplit = options.uniAppPlusSplit ?? false;\n\n\t/**\n\t * 应用级别过滤、标题格式和平台输出策略。\n\t *\n\t * @param messageLevel - 本条消息的严重级别。\n\t * @param scope - 模块、组件或业务来源名称。\n\t * @param message - 主消息文本。\n\t * @param data - 保持原始类型的附加值。\n\t * @throws `RangeError` 当作用域不是非空字符串或包含外围空白。\n\t */\n\tconst write = (messageLevel: LogLevel, scope: string, message: string, data: readonly unknown[]): void => {\n\t\tif (typeof scope !== \"string\") throw new TypeError(\"Logger scope must be a string.\");\n\t\tif (scope.length === 0 || scope.trim() !== scope) {\n\t\t\tthrow new RangeError(\"Logger scope must be a non-empty string without surrounding whitespace.\");\n\t\t}\n\t\tif (levelPriority[messageLevel] < levelPriority[level]) return;\n\t\tconst heading = `[${prefix}:${scope}]`;\n\t\tconst sinkMethod: keyof LoggerSink = messageLevel === \"info\" ? \"log\" : messageLevel;\n\t\tif (uniAppPlusSplit && isUniAppPlus()) {\n\t\t\tsink[sinkMethod](`${heading} ${message}`);\n\t\t\tfor (const item of data) sink[sinkMethod](formatSplitValue(item));\n\t\t\treturn;\n\t\t}\n\t\tsink[sinkMethod](heading, message, ...data);\n\t};\n\n\treturn {\n\t\tdebug: (scope, message, ...data): void => {\n\t\t\twrite(\"debug\", scope, message, data);\n\t\t},\n\t\tinfo: (scope, message, ...data): void => {\n\t\t\twrite(\"info\", scope, message, data);\n\t\t},\n\t\twarn: (scope, message, ...data): void => {\n\t\t\twrite(\"warn\", scope, message, data);\n\t\t},\n\t\terror: (scope, message, ...data): void => {\n\t\t\twrite(\"error\", scope, message, data);\n\t\t},\n\t};\n}\n\n/** 默认使用 `Fast` 前缀和 `info` 级别的便捷日志器。 */\nexport const logger: Logger = createLogger();\n"],"mappings":"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"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
//#region src/number/index.d.ts
|
|
2
|
+
/** {@link formatBytes} 的格式化选项。 */
|
|
3
|
+
interface FormatBytesOptions {
|
|
4
|
+
/** 计量基数。`1000` 生成 SI 单位,`1024` 生成 IEC 单位;默认 `1024`。 */
|
|
5
|
+
base?: 1000 | 1024;
|
|
6
|
+
/** 小数位数,范围 0 至 20;默认 `2`。 */
|
|
7
|
+
decimals?: number;
|
|
8
|
+
/** `Intl.NumberFormat` 使用的语言;默认固定为 `en-US` 以保证输出稳定。 */
|
|
9
|
+
locale?: string | readonly string[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 把数字限制在闭区间内。
|
|
13
|
+
*
|
|
14
|
+
* @param value - 需要限制的数字。
|
|
15
|
+
* @param minimum - 闭区间下界。
|
|
16
|
+
* @param maximum - 闭区间上界。
|
|
17
|
+
* @returns `minimum <= result <= maximum` 的值。
|
|
18
|
+
* @throws `RangeError` 当参数为 `NaN` 或下界大于上界。
|
|
19
|
+
*/
|
|
20
|
+
declare function clamp(value: number, minimum: number, maximum: number): number;
|
|
21
|
+
/**
|
|
22
|
+
* 判断数字是否位于指定区间。
|
|
23
|
+
*
|
|
24
|
+
* @param value - 待检查数字。
|
|
25
|
+
* @param minimum - 包含的下界。
|
|
26
|
+
* @param maximum - 上界。
|
|
27
|
+
* @param includeMaximum - 是否包含上界;默认使用半开区间 `[minimum, maximum)`。
|
|
28
|
+
* @returns 数字满足区间边界时返回 `true`。
|
|
29
|
+
* @throws `RangeError` 当参数为 `NaN` 或下界大于上界。
|
|
30
|
+
*/
|
|
31
|
+
declare function inRange(value: number, minimum: number, maximum: number, includeMaximum?: boolean): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* 按十进制位数四舍五入。
|
|
34
|
+
*
|
|
35
|
+
* @remarks IEEE-754 浮点数仍可能存在不可表示误差;财务金额应使用十进制定点方案。
|
|
36
|
+
* @param value - 有限数字。
|
|
37
|
+
* @param digits - 小数位数;负数表示十位、百位等,范围 -15 至 15。
|
|
38
|
+
* @returns 按 `Math.round` 语义舍入后的数字。
|
|
39
|
+
* @throws `RangeError` 当值非有限或位数超出范围。
|
|
40
|
+
*/
|
|
41
|
+
declare function roundTo(value: number, digits?: number): number;
|
|
42
|
+
/**
|
|
43
|
+
* 对有限数字求和。
|
|
44
|
+
*
|
|
45
|
+
* @param values - 不会被修改的数字数组。
|
|
46
|
+
* @returns 算术和;空数组返回 `0`。
|
|
47
|
+
* @throws `RangeError` 当任一值非有限或累计结果溢出。
|
|
48
|
+
*/
|
|
49
|
+
declare function sum(values: readonly number[]): number;
|
|
50
|
+
/**
|
|
51
|
+
* 计算有限数字的算术平均值。
|
|
52
|
+
*
|
|
53
|
+
* @param values - 不会被修改的数字数组。
|
|
54
|
+
* @returns 空数组或只有稀疏空位的数组返回 `undefined`;空位不参与分母。
|
|
55
|
+
* @throws `RangeError` 当任一值非有限。
|
|
56
|
+
*/
|
|
57
|
+
declare function average(values: readonly number[]): number | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* 在两个数字间做线性插值。
|
|
60
|
+
*
|
|
61
|
+
* @remarks `amount` 不限制在 0 至 1;区间外的值会执行线性外推。
|
|
62
|
+
* @param start - `amount = 0` 时的起点。
|
|
63
|
+
* @param end - `amount = 1` 时的终点。
|
|
64
|
+
* @param amount - 插值或外推比例。
|
|
65
|
+
* @returns 线性计算结果。
|
|
66
|
+
* @throws `RangeError` 当任一参数非有限或结果超出有限数字范围。
|
|
67
|
+
*/
|
|
68
|
+
declare function lerp(start: number, end: number, amount: number): number;
|
|
69
|
+
/**
|
|
70
|
+
* 将非负字节数格式化为 SI 或 IEC 单位。
|
|
71
|
+
*
|
|
72
|
+
* @param bytes - 非负有限字节数。
|
|
73
|
+
* @param options - 基数、小数位和语言选项。
|
|
74
|
+
* @returns 例如 `1.5 KiB`。
|
|
75
|
+
* @throws `RangeError` 当字节数为负或非有限、基数不是 1000/1024、小数位非法,或 Locale 无效。
|
|
76
|
+
*/
|
|
77
|
+
declare function formatBytes(bytes: number, options?: FormatBytesOptions): string;
|
|
78
|
+
/**
|
|
79
|
+
* 使用 Web Crypto 在半开区间内生成无偏安全随机整数。
|
|
80
|
+
*
|
|
81
|
+
* @param minimum - 包含的安全整数下界。
|
|
82
|
+
* @param maximumExclusive - 不包含的安全整数上界;区间宽度最大为 2^32。
|
|
83
|
+
* @returns 均匀分布在 `[minimum, maximumExclusive)` 的安全整数。
|
|
84
|
+
* @throws 缺少 Web Crypto 时抛出 `Error`;参数非法时抛出 `RangeError`。
|
|
85
|
+
*/
|
|
86
|
+
declare function secureRandomInt(minimum: number, maximumExclusive: number): number;
|
|
87
|
+
//#endregion
|
|
88
|
+
export { FormatBytesOptions, average, clamp, formatBytes, inRange, lerp, roundTo, secureRandomInt, sum };
|
|
89
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/number/index.ts"],"mappings":";;UAYiB;;EAEhB;;EAEA;;EAEA;;;;;;;;;;;iBAgDe,MAAM,eAAe,iBAAiB;;;;;;;;;;;iBAkBtC,QAAQ,eAAe,iBAAiB,iBAAiB;;;;;;;;;;iBAiBzD,QAAQ,eAAe;;;;;;;;iBAkBvB,IAAI;;;;;;;;iBAsBJ,QAAQ;;;;;;;;;;;iBAsBR,KAAK,eAAe,aAAa;;;;;;;;;iBAiBjC,YAAY,eAAe,UAAS;;;;;;;;;iBA+BpC,gBAAgB,iBAAiB"}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
//#region src/number/index.ts
|
|
2
|
+
const byteUnits = [
|
|
3
|
+
"B",
|
|
4
|
+
"kB",
|
|
5
|
+
"MB",
|
|
6
|
+
"GB",
|
|
7
|
+
"TB",
|
|
8
|
+
"PB",
|
|
9
|
+
"EB",
|
|
10
|
+
"ZB",
|
|
11
|
+
"YB"
|
|
12
|
+
];
|
|
13
|
+
const binaryByteUnits = [
|
|
14
|
+
"B",
|
|
15
|
+
"KiB",
|
|
16
|
+
"MiB",
|
|
17
|
+
"GiB",
|
|
18
|
+
"TiB",
|
|
19
|
+
"PiB",
|
|
20
|
+
"EiB",
|
|
21
|
+
"ZiB",
|
|
22
|
+
"YiB"
|
|
23
|
+
];
|
|
24
|
+
const runtimeNumberGlobals = globalThis;
|
|
25
|
+
/**
|
|
26
|
+
* 拒绝 NaN,同时允许具体 API 自行决定是否接受 Infinity。
|
|
27
|
+
*
|
|
28
|
+
* @param value - 待校验数值。
|
|
29
|
+
* @param name - 用于错误消息的参数名称。
|
|
30
|
+
* @throws `RangeError` 当值为 NaN。
|
|
31
|
+
*/
|
|
32
|
+
const assertNotNaN = (value, name) => {
|
|
33
|
+
if (Number.isNaN(value)) throw new RangeError(`${name} cannot be NaN.`);
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* 校验有限数值。
|
|
37
|
+
*
|
|
38
|
+
* @param value - 待校验数值。
|
|
39
|
+
* @param name - 用于错误消息的参数名称。
|
|
40
|
+
* @throws `RangeError` 当值为 NaN 或无穷大。
|
|
41
|
+
*/
|
|
42
|
+
const assertFinite = (value, name) => {
|
|
43
|
+
if (!Number.isFinite(value)) throw new RangeError(`${name} must be finite.`);
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* 使用科学计数法移动十进制位。
|
|
47
|
+
*
|
|
48
|
+
* @remarks 该方式用于 `roundTo`,可减少直接乘除十次幂造成的额外二进制舍入误差。
|
|
49
|
+
* @param value - 原始数值。
|
|
50
|
+
* @param exponent - 十进制移动位数;正数向右移动。
|
|
51
|
+
* @returns 移动后的数值。
|
|
52
|
+
*/
|
|
53
|
+
const shiftDecimal = (value, exponent) => {
|
|
54
|
+
if (Object.is(value, -0)) return -0;
|
|
55
|
+
const [coefficient = "0", currentExponent = "0"] = value.toString().split("e");
|
|
56
|
+
return Number(`${coefficient}e${Number(currentExponent) + exponent}`);
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* 把数字限制在闭区间内。
|
|
60
|
+
*
|
|
61
|
+
* @param value - 需要限制的数字。
|
|
62
|
+
* @param minimum - 闭区间下界。
|
|
63
|
+
* @param maximum - 闭区间上界。
|
|
64
|
+
* @returns `minimum <= result <= maximum` 的值。
|
|
65
|
+
* @throws `RangeError` 当参数为 `NaN` 或下界大于上界。
|
|
66
|
+
*/
|
|
67
|
+
function clamp(value, minimum, maximum) {
|
|
68
|
+
assertNotNaN(value, "value");
|
|
69
|
+
assertNotNaN(minimum, "minimum");
|
|
70
|
+
assertNotNaN(maximum, "maximum");
|
|
71
|
+
if (minimum > maximum) throw new RangeError("minimum cannot be greater than maximum.");
|
|
72
|
+
return Math.min(Math.max(value, minimum), maximum);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 判断数字是否位于指定区间。
|
|
76
|
+
*
|
|
77
|
+
* @param value - 待检查数字。
|
|
78
|
+
* @param minimum - 包含的下界。
|
|
79
|
+
* @param maximum - 上界。
|
|
80
|
+
* @param includeMaximum - 是否包含上界;默认使用半开区间 `[minimum, maximum)`。
|
|
81
|
+
* @returns 数字满足区间边界时返回 `true`。
|
|
82
|
+
* @throws `RangeError` 当参数为 `NaN` 或下界大于上界。
|
|
83
|
+
*/
|
|
84
|
+
function inRange(value, minimum, maximum, includeMaximum = false) {
|
|
85
|
+
assertNotNaN(value, "value");
|
|
86
|
+
assertNotNaN(minimum, "minimum");
|
|
87
|
+
assertNotNaN(maximum, "maximum");
|
|
88
|
+
if (minimum > maximum) throw new RangeError("minimum cannot be greater than maximum.");
|
|
89
|
+
return value >= minimum && (includeMaximum ? value <= maximum : value < maximum);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 按十进制位数四舍五入。
|
|
93
|
+
*
|
|
94
|
+
* @remarks IEEE-754 浮点数仍可能存在不可表示误差;财务金额应使用十进制定点方案。
|
|
95
|
+
* @param value - 有限数字。
|
|
96
|
+
* @param digits - 小数位数;负数表示十位、百位等,范围 -15 至 15。
|
|
97
|
+
* @returns 按 `Math.round` 语义舍入后的数字。
|
|
98
|
+
* @throws `RangeError` 当值非有限或位数超出范围。
|
|
99
|
+
*/
|
|
100
|
+
function roundTo(value, digits = 0) {
|
|
101
|
+
assertFinite(value, "value");
|
|
102
|
+
if (!Number.isSafeInteger(digits) || digits < -15 || digits > 15) throw new RangeError("digits must be a safe integer between -15 and 15.");
|
|
103
|
+
const shifted = shiftDecimal(value, digits);
|
|
104
|
+
if (!Number.isFinite(shifted)) return value;
|
|
105
|
+
return shiftDecimal(Math.round(shifted), -digits);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 对有限数字求和。
|
|
109
|
+
*
|
|
110
|
+
* @param values - 不会被修改的数字数组。
|
|
111
|
+
* @returns 算术和;空数组返回 `0`。
|
|
112
|
+
* @throws `RangeError` 当任一值非有限或累计结果溢出。
|
|
113
|
+
*/
|
|
114
|
+
function sum(values) {
|
|
115
|
+
let total = 0;
|
|
116
|
+
let compensation = 0;
|
|
117
|
+
values.forEach((value) => {
|
|
118
|
+
assertFinite(value, "value");
|
|
119
|
+
const adjusted = value - compensation;
|
|
120
|
+
const next = total + adjusted;
|
|
121
|
+
if (!Number.isFinite(next)) throw new RangeError("The sum exceeds the finite number range.");
|
|
122
|
+
compensation = next - total - adjusted;
|
|
123
|
+
total = next;
|
|
124
|
+
});
|
|
125
|
+
return total;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 计算有限数字的算术平均值。
|
|
129
|
+
*
|
|
130
|
+
* @param values - 不会被修改的数字数组。
|
|
131
|
+
* @returns 空数组或只有稀疏空位的数组返回 `undefined`;空位不参与分母。
|
|
132
|
+
* @throws `RangeError` 当任一值非有限。
|
|
133
|
+
*/
|
|
134
|
+
function average(values) {
|
|
135
|
+
let count = 0;
|
|
136
|
+
let mean = 0;
|
|
137
|
+
values.forEach((value) => {
|
|
138
|
+
assertFinite(value, "value");
|
|
139
|
+
count += 1;
|
|
140
|
+
mean = mean * ((count - 1) / count) + value / count;
|
|
141
|
+
});
|
|
142
|
+
return count === 0 ? void 0 : mean;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* 在两个数字间做线性插值。
|
|
146
|
+
*
|
|
147
|
+
* @remarks `amount` 不限制在 0 至 1;区间外的值会执行线性外推。
|
|
148
|
+
* @param start - `amount = 0` 时的起点。
|
|
149
|
+
* @param end - `amount = 1` 时的终点。
|
|
150
|
+
* @param amount - 插值或外推比例。
|
|
151
|
+
* @returns 线性计算结果。
|
|
152
|
+
* @throws `RangeError` 当任一参数非有限或结果超出有限数字范围。
|
|
153
|
+
*/
|
|
154
|
+
function lerp(start, end, amount) {
|
|
155
|
+
assertFinite(start, "start");
|
|
156
|
+
assertFinite(end, "end");
|
|
157
|
+
assertFinite(amount, "amount");
|
|
158
|
+
const result = start * (1 - amount) + end * amount;
|
|
159
|
+
if (!Number.isFinite(result)) throw new RangeError("The interpolation result exceeds the finite number range.");
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* 将非负字节数格式化为 SI 或 IEC 单位。
|
|
164
|
+
*
|
|
165
|
+
* @param bytes - 非负有限字节数。
|
|
166
|
+
* @param options - 基数、小数位和语言选项。
|
|
167
|
+
* @returns 例如 `1.5 KiB`。
|
|
168
|
+
* @throws `RangeError` 当字节数为负或非有限、基数不是 1000/1024、小数位非法,或 Locale 无效。
|
|
169
|
+
*/
|
|
170
|
+
function formatBytes(bytes, options = {}) {
|
|
171
|
+
assertFinite(bytes, "bytes");
|
|
172
|
+
if (bytes < 0) throw new RangeError("bytes cannot be negative.");
|
|
173
|
+
const requestedBase = options.base ?? 1024;
|
|
174
|
+
if (requestedBase !== 1e3 && requestedBase !== 1024) throw new RangeError("base must be 1000 or 1024.");
|
|
175
|
+
const base = requestedBase;
|
|
176
|
+
const decimals = options.decimals ?? 2;
|
|
177
|
+
if (!Number.isSafeInteger(decimals) || decimals < 0 || decimals > 20) throw new RangeError("decimals must be a safe integer between 0 and 20.");
|
|
178
|
+
if (bytes === 0) return "0 B";
|
|
179
|
+
const units = base === 1024 ? binaryByteUnits : byteUnits;
|
|
180
|
+
const exponent = Math.min(Math.floor(Math.log(bytes) / Math.log(base)), units.length - 1);
|
|
181
|
+
const value = bytes / base ** exponent;
|
|
182
|
+
return `${new Intl.NumberFormat(options.locale ?? "en-US", {
|
|
183
|
+
maximumFractionDigits: decimals,
|
|
184
|
+
minimumFractionDigits: 0,
|
|
185
|
+
useGrouping: false
|
|
186
|
+
}).format(value)} ${units[exponent] ?? units.at(-1)}`;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* 使用 Web Crypto 在半开区间内生成无偏安全随机整数。
|
|
190
|
+
*
|
|
191
|
+
* @param minimum - 包含的安全整数下界。
|
|
192
|
+
* @param maximumExclusive - 不包含的安全整数上界;区间宽度最大为 2^32。
|
|
193
|
+
* @returns 均匀分布在 `[minimum, maximumExclusive)` 的安全整数。
|
|
194
|
+
* @throws 缺少 Web Crypto 时抛出 `Error`;参数非法时抛出 `RangeError`。
|
|
195
|
+
*/
|
|
196
|
+
function secureRandomInt(minimum, maximumExclusive) {
|
|
197
|
+
if (!Number.isSafeInteger(minimum) || !Number.isSafeInteger(maximumExclusive)) throw new RangeError("minimum and maximumExclusive must be safe integers.");
|
|
198
|
+
const range = maximumExclusive - minimum;
|
|
199
|
+
const uint32Range = 4294967296;
|
|
200
|
+
if (range <= 0 || range > uint32Range) throw new RangeError("The interval must be non-empty and no wider than 2^32.");
|
|
201
|
+
const crypto = runtimeNumberGlobals.crypto;
|
|
202
|
+
if (typeof crypto?.getRandomValues !== "function") throw new Error("Web Crypto random generation is unavailable in the current runtime.");
|
|
203
|
+
const limit = Math.floor(uint32Range / range) * range;
|
|
204
|
+
const values = /* @__PURE__ */ new Uint32Array(1);
|
|
205
|
+
let sample;
|
|
206
|
+
do {
|
|
207
|
+
crypto.getRandomValues(values);
|
|
208
|
+
sample = values[0] ?? uint32Range;
|
|
209
|
+
} while (sample >= limit);
|
|
210
|
+
return minimum + sample % range;
|
|
211
|
+
}
|
|
212
|
+
//#endregion
|
|
213
|
+
export { average, clamp, formatBytes, inRange, lerp, roundTo, secureRandomInt, sum };
|
|
214
|
+
|
|
215
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/number/index.ts"],"sourcesContent":["const byteUnits = [\"B\", \"kB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"] as const;\nconst binaryByteUnits = [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"] as const;\n\n/** 安全随机整数延迟读取的平台全局对象最小视图。 */\ninterface RuntimeNumberGlobals {\n\t/** 可选 Web Crypto 随机填充能力;缺失时安全随机整数明确失败。 */\n\tcrypto?: Partial<Pick<Crypto, \"getRandomValues\">>;\n}\n\nconst runtimeNumberGlobals = globalThis as unknown as RuntimeNumberGlobals;\n\n/** {@link formatBytes} 的格式化选项。 */\nexport interface FormatBytesOptions {\n\t/** 计量基数。`1000` 生成 SI 单位,`1024` 生成 IEC 单位;默认 `1024`。 */\n\tbase?: 1000 | 1024;\n\t/** 小数位数,范围 0 至 20;默认 `2`。 */\n\tdecimals?: number;\n\t/** `Intl.NumberFormat` 使用的语言;默认固定为 `en-US` 以保证输出稳定。 */\n\tlocale?: string | readonly string[];\n}\n\n/**\n * 拒绝 NaN,同时允许具体 API 自行决定是否接受 Infinity。\n *\n * @param value - 待校验数值。\n * @param name - 用于错误消息的参数名称。\n * @throws `RangeError` 当值为 NaN。\n */\nconst assertNotNaN = (value: number, name: string): void => {\n\tif (Number.isNaN(value)) throw new RangeError(`${name} cannot be NaN.`);\n};\n\n/**\n * 校验有限数值。\n *\n * @param value - 待校验数值。\n * @param name - 用于错误消息的参数名称。\n * @throws `RangeError` 当值为 NaN 或无穷大。\n */\nconst assertFinite = (value: number, name: string): void => {\n\tif (!Number.isFinite(value)) throw new RangeError(`${name} must be finite.`);\n};\n\n/**\n * 使用科学计数法移动十进制位。\n *\n * @remarks 该方式用于 `roundTo`,可减少直接乘除十次幂造成的额外二进制舍入误差。\n * @param value - 原始数值。\n * @param exponent - 十进制移动位数;正数向右移动。\n * @returns 移动后的数值。\n */\nconst shiftDecimal = (value: number, exponent: number): number => {\n\tif (Object.is(value, -0)) return -0;\n\tconst [coefficient = \"0\", currentExponent = \"0\"] = value.toString().split(\"e\");\n\treturn Number(`${coefficient}e${Number(currentExponent) + exponent}`);\n};\n\n/**\n * 把数字限制在闭区间内。\n *\n * @param value - 需要限制的数字。\n * @param minimum - 闭区间下界。\n * @param maximum - 闭区间上界。\n * @returns `minimum <= result <= maximum` 的值。\n * @throws `RangeError` 当参数为 `NaN` 或下界大于上界。\n */\nexport function clamp(value: number, minimum: number, maximum: number): number {\n\tassertNotNaN(value, \"value\");\n\tassertNotNaN(minimum, \"minimum\");\n\tassertNotNaN(maximum, \"maximum\");\n\tif (minimum > maximum) throw new RangeError(\"minimum cannot be greater than maximum.\");\n\treturn Math.min(Math.max(value, minimum), maximum);\n}\n\n/**\n * 判断数字是否位于指定区间。\n *\n * @param value - 待检查数字。\n * @param minimum - 包含的下界。\n * @param maximum - 上界。\n * @param includeMaximum - 是否包含上界;默认使用半开区间 `[minimum, maximum)`。\n * @returns 数字满足区间边界时返回 `true`。\n * @throws `RangeError` 当参数为 `NaN` 或下界大于上界。\n */\nexport function inRange(value: number, minimum: number, maximum: number, includeMaximum = false): boolean {\n\tassertNotNaN(value, \"value\");\n\tassertNotNaN(minimum, \"minimum\");\n\tassertNotNaN(maximum, \"maximum\");\n\tif (minimum > maximum) throw new RangeError(\"minimum cannot be greater than maximum.\");\n\treturn value >= minimum && (includeMaximum ? value <= maximum : value < maximum);\n}\n\n/**\n * 按十进制位数四舍五入。\n *\n * @remarks IEEE-754 浮点数仍可能存在不可表示误差;财务金额应使用十进制定点方案。\n * @param value - 有限数字。\n * @param digits - 小数位数;负数表示十位、百位等,范围 -15 至 15。\n * @returns 按 `Math.round` 语义舍入后的数字。\n * @throws `RangeError` 当值非有限或位数超出范围。\n */\nexport function roundTo(value: number, digits = 0): number {\n\tassertFinite(value, \"value\");\n\tif (!Number.isSafeInteger(digits) || digits < -15 || digits > 15) {\n\t\tthrow new RangeError(\"digits must be a safe integer between -15 and 15.\");\n\t}\n\tconst shifted = shiftDecimal(value, digits);\n\t// 对已经没有可表示小数的大数,乘以 10^digits 可能溢出;此时舍入不会改变值。\n\tif (!Number.isFinite(shifted)) return value;\n\treturn shiftDecimal(Math.round(shifted), -digits);\n}\n\n/**\n * 对有限数字求和。\n *\n * @param values - 不会被修改的数字数组。\n * @returns 算术和;空数组返回 `0`。\n * @throws `RangeError` 当任一值非有限或累计结果溢出。\n */\nexport function sum(values: readonly number[]): number {\n\tlet total = 0;\n\tlet compensation = 0;\n\tvalues.forEach((value) => {\n\t\tassertFinite(value, \"value\");\n\t\t// Kahan 补偿保存上一次浮点加法丢失的低位,减少大量小数累计误差。\n\t\tconst adjusted = value - compensation;\n\t\tconst next = total + adjusted;\n\t\tif (!Number.isFinite(next)) throw new RangeError(\"The sum exceeds the finite number range.\");\n\t\tcompensation = next - total - adjusted;\n\t\ttotal = next;\n\t});\n\treturn total;\n}\n\n/**\n * 计算有限数字的算术平均值。\n *\n * @param values - 不会被修改的数字数组。\n * @returns 空数组或只有稀疏空位的数组返回 `undefined`;空位不参与分母。\n * @throws `RangeError` 当任一值非有限。\n */\nexport function average(values: readonly number[]): number | undefined {\n\tlet count = 0;\n\tlet mean = 0;\n\tvalues.forEach((value) => {\n\t\tassertFinite(value, \"value\");\n\t\tcount += 1;\n\t\t// 加权增量形式避免先求和导致 MAX_VALUE + MAX_VALUE 溢出。\n\t\tmean = mean * ((count - 1) / count) + value / count;\n\t});\n\treturn count === 0 ? undefined : mean;\n}\n\n/**\n * 在两个数字间做线性插值。\n *\n * @remarks `amount` 不限制在 0 至 1;区间外的值会执行线性外推。\n * @param start - `amount = 0` 时的起点。\n * @param end - `amount = 1` 时的终点。\n * @param amount - 插值或外推比例。\n * @returns 线性计算结果。\n * @throws `RangeError` 当任一参数非有限或结果超出有限数字范围。\n */\nexport function lerp(start: number, end: number, amount: number): number {\n\tassertFinite(start, \"start\");\n\tassertFinite(end, \"end\");\n\tassertFinite(amount, \"amount\");\n\tconst result = start * (1 - amount) + end * amount;\n\tif (!Number.isFinite(result)) throw new RangeError(\"The interpolation result exceeds the finite number range.\");\n\treturn result;\n}\n\n/**\n * 将非负字节数格式化为 SI 或 IEC 单位。\n *\n * @param bytes - 非负有限字节数。\n * @param options - 基数、小数位和语言选项。\n * @returns 例如 `1.5 KiB`。\n * @throws `RangeError` 当字节数为负或非有限、基数不是 1000/1024、小数位非法,或 Locale 无效。\n */\nexport function formatBytes(bytes: number, options: FormatBytesOptions = {}): string {\n\tassertFinite(bytes, \"bytes\");\n\tif (bytes < 0) throw new RangeError(\"bytes cannot be negative.\");\n\tconst requestedBase: unknown = options.base ?? 1024;\n\tif (requestedBase !== 1000 && requestedBase !== 1024) throw new RangeError(\"base must be 1000 or 1024.\");\n\tconst base = requestedBase;\n\tconst decimals = options.decimals ?? 2;\n\tif (!Number.isSafeInteger(decimals) || decimals < 0 || decimals > 20) {\n\t\tthrow new RangeError(\"decimals must be a safe integer between 0 and 20.\");\n\t}\n\tif (bytes === 0) return \"0 B\";\n\n\tconst units = base === 1024 ? binaryByteUnits : byteUnits;\n\tconst exponent = Math.min(Math.floor(Math.log(bytes) / Math.log(base)), units.length - 1);\n\tconst value = bytes / base ** exponent;\n\tconst formatted = new Intl.NumberFormat(options.locale ?? \"en-US\", {\n\t\tmaximumFractionDigits: decimals,\n\t\tminimumFractionDigits: 0,\n\t\tuseGrouping: false,\n\t}).format(value);\n\treturn `${formatted} ${units[exponent] ?? units.at(-1)}`;\n}\n\n/**\n * 使用 Web Crypto 在半开区间内生成无偏安全随机整数。\n *\n * @param minimum - 包含的安全整数下界。\n * @param maximumExclusive - 不包含的安全整数上界;区间宽度最大为 2^32。\n * @returns 均匀分布在 `[minimum, maximumExclusive)` 的安全整数。\n * @throws 缺少 Web Crypto 时抛出 `Error`;参数非法时抛出 `RangeError`。\n */\nexport function secureRandomInt(minimum: number, maximumExclusive: number): number {\n\tif (!Number.isSafeInteger(minimum) || !Number.isSafeInteger(maximumExclusive)) {\n\t\tthrow new RangeError(\"minimum and maximumExclusive must be safe integers.\");\n\t}\n\tconst range = maximumExclusive - minimum;\n\tconst uint32Range = 0x1_0000_0000;\n\tif (range <= 0 || range > uint32Range) {\n\t\tthrow new RangeError(\"The interval must be non-empty and no wider than 2^32.\");\n\t}\n\tconst crypto = runtimeNumberGlobals.crypto;\n\tif (typeof crypto?.getRandomValues !== \"function\") {\n\t\tthrow new Error(\"Web Crypto random generation is unavailable in the current runtime.\");\n\t}\n\n\t// 只接受可以被区间宽度整除的最大 2^32 前缀,消除取模偏差。\n\tconst limit = Math.floor(uint32Range / range) * range;\n\tconst values = new Uint32Array(1);\n\tlet sample: number;\n\tdo {\n\t\tcrypto.getRandomValues(values);\n\t\tsample = values[0] ?? uint32Range;\n\t} while (sample >= limit);\n\treturn minimum + (sample % range);\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//#region src/object/index.d.ts
|
|
2
|
+
/** URL 查询参数支持的单值类型。 */
|
|
3
|
+
type QueryPrimitive = bigint | boolean | number | string | null | undefined;
|
|
4
|
+
/** URL 查询参数值;数组使用重复键表示。 */
|
|
5
|
+
type QueryValue = QueryPrimitive | readonly QueryPrimitive[];
|
|
6
|
+
/** {@link toQueryString} 的序列化选项。 */
|
|
7
|
+
interface QueryStringOptions {
|
|
8
|
+
/** 返回非空结果时是否添加 `?`;默认 `false`。 */
|
|
9
|
+
prefixQuestionMark?: boolean;
|
|
10
|
+
/** 是否按键的 UTF-16 码元顺序稳定排序;默认保留对象枚举顺序。 */
|
|
11
|
+
sort?: boolean;
|
|
12
|
+
/** 空格编码方式;默认遵循表单编码并输出 `+`。 */
|
|
13
|
+
space?: "percent" | "plus";
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 判断值是否是普通对象。
|
|
17
|
+
*
|
|
18
|
+
* @param value - 任意待检查值。
|
|
19
|
+
* @returns 原型为 `Object.prototype` 或 `null` 时返回 `true`。
|
|
20
|
+
*/
|
|
21
|
+
declare function isPlainObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
22
|
+
/**
|
|
23
|
+
* 安全判断对象是否拥有自己的属性。
|
|
24
|
+
*
|
|
25
|
+
* @remarks 不调用可能被对象覆盖的 `hasOwnProperty`。
|
|
26
|
+
* @param value - 待检查对象。
|
|
27
|
+
* @param key - 字符串、数字或 Symbol 属性键。
|
|
28
|
+
* @returns 属性为对象自有属性时返回 `true`,并收窄键类型。
|
|
29
|
+
*/
|
|
30
|
+
declare function hasOwn<ObjectType extends object, Key extends PropertyKey>(value: ObjectType, key: Key): key is Key & keyof ObjectType;
|
|
31
|
+
/**
|
|
32
|
+
* 从对象中选择指定自有可枚举属性。
|
|
33
|
+
*
|
|
34
|
+
* @param source - 不会被修改的源对象。
|
|
35
|
+
* @param keys - 需要保留的键;不存在的键被忽略。
|
|
36
|
+
* @returns 新对象,保持 `keys` 的遍历顺序。
|
|
37
|
+
*/
|
|
38
|
+
declare function pick<Source extends object, Keys extends readonly (keyof Source)[]>(source: Source, keys: Keys): Pick<Source, Keys[number]>;
|
|
39
|
+
/**
|
|
40
|
+
* 浅复制对象并删除指定属性。
|
|
41
|
+
*
|
|
42
|
+
* @param source - 不会被修改的源对象。
|
|
43
|
+
* @param keys - 需要排除的键。
|
|
44
|
+
* @returns 包含其余自有可枚举字符串与 Symbol 属性的新对象。
|
|
45
|
+
*/
|
|
46
|
+
declare function omit<Source extends object, Keys extends readonly (keyof Source)[]>(source: Source, keys: Keys): Omit<Source, Keys[number]>;
|
|
47
|
+
/**
|
|
48
|
+
* 映射对象的自有可枚举属性值。
|
|
49
|
+
*
|
|
50
|
+
* @param source - 不会被修改的源对象。
|
|
51
|
+
* @param mapper - 接收值、键和源对象的映射函数。
|
|
52
|
+
* @returns 保留原键的新对象。
|
|
53
|
+
*/
|
|
54
|
+
declare function mapValues<Source extends object, Result>(source: Source, mapper: (value: Source[keyof Source], key: keyof Source, source: Source) => Result): { [Key in keyof Source]: Result; };
|
|
55
|
+
/**
|
|
56
|
+
* 对自有可枚举属性执行 SameValue 浅比较。
|
|
57
|
+
*
|
|
58
|
+
* @remarks 嵌套对象只比较引用;`NaN` 相等,`0` 与 `-0` 不相等。
|
|
59
|
+
* @param left - 第一对象。
|
|
60
|
+
* @param right - 第二对象。
|
|
61
|
+
* @returns 自有可枚举键集合与对应值均满足 SameValue 时返回 `true`。
|
|
62
|
+
*/
|
|
63
|
+
declare function shallowEqual(left: object, right: object): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* 将对象序列化为标准 URL 查询字符串。
|
|
66
|
+
*
|
|
67
|
+
* @remarks `null` 与 `undefined` 被跳过;数组使用重复键;返回值不会修改输入。
|
|
68
|
+
* @param value - 查询参数对象。
|
|
69
|
+
* @param options - 排序、空格和问号前缀选项。
|
|
70
|
+
* @returns URL 编码后的查询字符串;没有参数时始终返回空字符串。
|
|
71
|
+
* @throws `RangeError` 当参数包含 `NaN` 或无穷数字。
|
|
72
|
+
*/
|
|
73
|
+
declare function toQueryString(value: Readonly<Record<string, QueryValue>>, options?: QueryStringOptions): string;
|
|
74
|
+
//#endregion
|
|
75
|
+
export { QueryPrimitive, QueryStringOptions, QueryValue, hasOwn, isPlainObject, mapValues, omit, pick, shallowEqual, toQueryString };
|
|
76
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/object/index.ts"],"mappings":";;KACY;;KAGA,aAAa,0BAA0B;;UAWlC;;EAEhB;;EAEA;;EAEA;;;;;;;;iBAsBe,cAAc,iBAAiB,SAAS,OAAO;;;;;;;;;iBAc/C,OAAO,2BAA2B,YAAY,aAAa,OAAO,YAAY,KAAK,MAAM,OAAO,YAAY;;;;;;;;iBAW5G,KAAK,uBAAuB,6BAA6B,WAAW,QAAQ,QAAQ,MAAM,OAAO,KAAK,QAAQ;;;;;;;;iBAe9G,KAAK,uBAAuB,6BAA6B,WAAW,QAAQ,QAAQ,MAAM,OAAO,KAAK,QAAQ;;;;;;;;iBAa9G,UAAU,uBAAuB,QAChD,QAAQ,QACR,SAAS,OAAO,aAAa,SAAS,WAAW,QAAQ,QAAQ,WAAW,YACvE,aAAa,SAAS;;;;;;;;;iBAgBZ,aAAa,cAAc;;;;;;;;;;iBA+B3B,cAAc,OAAO,SAAS,eAAe,cAAc,UAAS"}
|