@complex-suite/utils
Advanced tools
| // utils 包类型集中管理 | ||
| // 来源:src/{class, type, number, object, reactive, string, utils} 各模块 | ||
| // 来自 class/_Data.ts | ||
| export type formatConfigType = { | ||
| name?: string // 模块名称 | ||
| level: number // 响应式等级 | ||
| recommend: boolean // 是否推荐响应式 | ||
| module?: boolean // 是否为模块 | ||
| [prop: string]: undefined | boolean | string | number | ||
| } | ||
| // 来自 class/Limit.ts | ||
| export type LimitType = 'forbid' | 'allow' | ||
| export interface LimitInitOption { | ||
| type?: LimitType | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| list?: any[] | ||
| } | ||
| // 来自 class/Wait.ts | ||
| export interface WaitInitOption { | ||
| notice?: { | ||
| message: string | ||
| offset: number | ||
| } | ||
| timeout?: { | ||
| message: string | ||
| offset: number | ||
| } | ||
| } | ||
| export type waitFunction = () => void | ||
| // 来自 type/getType.ts | ||
| export type SimpleType = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "array" | "null" | ||
| // 来自 type/getComplexType.ts | ||
| export type ComplexType = SimpleType | "file" | "blob" | "regExp" | "date" | 'map' | 'set' | ||
| // 来自 number/getNum.ts | ||
| export type mathType = 'round' | 'floor' | 'ceil' | ||
| // 来自 object/updateData.ts | ||
| export type updateDataValueType = Record<PropertyKey, any> | unknown[] | ||
| // 来自 reactive/defineReactive.ts | ||
| export type defineReactiveOptionType = { | ||
| descriptor?: PropertyDescriptor | ||
| get?: (value: unknown) => unknown | ||
| set?: (val: unknown, oldVal: unknown) => unknown | ||
| } | ||
| // 来自 string/getRandomLetter.ts | ||
| export type letterType = { | ||
| small?: boolean, | ||
| big?: boolean, | ||
| number?: boolean | ||
| } | ||
| // 来自 utils/exportMsg.ts | ||
| export type consoleType = 'error' | 'warn' | 'log' |
+7
-0
@@ -0,1 +1,8 @@ | ||
| ### 3.0.7 | ||
| - feat(locale): 导出消息类型并改用 vue-tsc 进行类型检查。 | ||
| - refactor: 集中提取并统一导出类型定义。 | ||
| ### 3.0.6 | ||
| - refactor: 将TypeScript类型导入语法升级为type关键字形式。 | ||
| ### 2.10.5 | ||
@@ -2,0 +9,0 @@ - refactor: 将TypeScript类型导入语法升级为type关键字形式 |
+14
-24
| // class加载 | ||
| import _Data, { type formatConfigType } from './src/class/_Data' | ||
| import _Data from './src/class/_Data' | ||
| import Life, { type DataWithLife, type LifeInitOption } from './src/class/Life' | ||
| import { LifeData, LifeList, LifeMap, LifeValue, type LifeValueInitOptionWithExtra, type LifeValueInitOption } from './src/class/LifeData' | ||
| import Limit, { type LimitType, type LimitInitOption } from './src/class/Limit' | ||
| import Wait, { type WaitInitOption, type waitFunction } from './src/class/Wait' | ||
| import Limit from './src/class/Limit' | ||
| import Wait from './src/class/Wait' | ||
@@ -25,3 +25,3 @@ // storage加载 | ||
| import getInteger from './src/number/getInteger' | ||
| import getNum, { type mathType } from './src/number/getNum' | ||
| import getNum from './src/number/getNum' | ||
| import getPreciseNum from './src/number/getPreciseNum' | ||
@@ -53,3 +53,3 @@ import getRandomNum from './src/number/getRandomNum' | ||
| import showArrayProp from './src/object/showArrayProp' | ||
| import updateData, { type updateDataValueType } from './src/object/updateData' | ||
| import updateData from './src/object/updateData' | ||
@@ -63,3 +63,3 @@ // observe加载 | ||
| // reactive加载 | ||
| import defineReactive, { type defineReactiveOptionType } from './src/reactive/defineReactive' | ||
| import defineReactive from './src/reactive/defineReactive' | ||
@@ -72,3 +72,3 @@ // string加载 | ||
| import getRandomInList from './src/string/getRandomInList' | ||
| import getRandomLetter, { type letterType } from './src/string/getRandomLetter' | ||
| import getRandomLetter from './src/string/getRandomLetter' | ||
| import linetoCamel from './src/string/linetoCamel' | ||
@@ -79,4 +79,4 @@ import upperCaseFirstChar from './src/string/upperCaseFirstChar' | ||
| import getTag from './src/type/getTag' | ||
| import getType, { type SimpleType } from './src/type/getType' | ||
| import getComplexType, { type ComplexType } from './src/type/getComplexType' | ||
| import getType from './src/type/getType' | ||
| import getComplexType from './src/type/getComplexType' | ||
| import isArray from './src/type/isArray' | ||
@@ -112,3 +112,3 @@ import isBlob from './src/type/isBlob' | ||
| import $exportMsg from './src/utils/$exportMsg' | ||
| import exportMsg, { type consoleType } from './src/utils/exportMsg' | ||
| import exportMsg from './src/utils/exportMsg' | ||
| import parseUrl from './src/utils/parseUrl' | ||
@@ -136,6 +136,7 @@ import showJson from './src/utils/showJson' | ||
| export type * from './src/types' | ||
| export { | ||
| // class | ||
| _Data, | ||
| type formatConfigType, | ||
| Life, | ||
@@ -151,7 +152,3 @@ LifeData, | ||
| Limit, | ||
| type LimitType, | ||
| type LimitInitOption, | ||
| Wait, | ||
| type WaitInitOption, | ||
| type waitFunction, | ||
@@ -179,3 +176,2 @@ // storage | ||
| getNum, | ||
| type mathType, | ||
| getPreciseNum, | ||
@@ -210,4 +206,3 @@ getRandomNum, | ||
| updateData, | ||
| type updateDataValueType, | ||
| // observe | ||
@@ -224,4 +219,3 @@ observe, | ||
| defineReactive, | ||
| type defineReactiveOptionType, | ||
| // string | ||
@@ -234,3 +228,2 @@ camelToLine, | ||
| getRandomLetter, | ||
| type letterType, | ||
| linetoCamel, | ||
@@ -242,5 +235,3 @@ upperCaseFirstChar, | ||
| getType, | ||
| type SimpleType, | ||
| getComplexType, | ||
| type ComplexType, | ||
| isArray, | ||
@@ -279,3 +270,2 @@ isBlob, | ||
| exportMsg, | ||
| type consoleType, | ||
| parseUrl, | ||
@@ -282,0 +272,0 @@ showJson, |
+2
-3
| { | ||
| "name": "@complex-suite/utils", | ||
| "version": "3.0.6", | ||
| "version": "3.0.7", | ||
| "description": "a complex utils", | ||
@@ -8,4 +8,3 @@ "type": "module", | ||
| "exports": { | ||
| ".": "./index.ts", | ||
| "./locale/en": "./src/locale/en.ts" | ||
| ".": "./index.ts" | ||
| }, | ||
@@ -12,0 +11,0 @@ "repository": { |
| import exportMsg, { consoleType } from '../utils/exportMsg' | ||
| import type { formatConfigType } from '../types' | ||
| export type formatConfigType = { | ||
| name?: string // 模块名称 | ||
| level: number // 响应式等级 | ||
| recommend: boolean // 是否推荐响应式 | ||
| module?: boolean // 是否为模块 | ||
| [prop: string]: undefined | boolean | string | number | ||
| } | ||
| export type { formatConfigType } | ||
@@ -11,0 +6,0 @@ class _Data { |
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| export type LimitType = 'forbid' | 'allow' | ||
| import type { LimitType, LimitInitOption } from '../types' | ||
| export interface LimitInitOption { | ||
| type?: LimitType | ||
| list?: any[] | ||
| } | ||
| export type { LimitType, LimitInitOption } | ||
@@ -10,0 +7,0 @@ // 限制数据格式 |
+2
-11
| export interface WaitInitOption { | ||
| notice?: { | ||
| message: string | ||
| offset: number | ||
| } | ||
| timeout?: { | ||
| message: string | ||
| offset: number | ||
| } | ||
| } | ||
| import type { WaitInitOption, waitFunction } from '../types' | ||
| export type waitFunction = () => void | ||
| export type { WaitInitOption, waitFunction } | ||
@@ -15,0 +6,0 @@ class Wait { |
@@ -24,9 +24,11 @@ import Locale from './Locale' | ||
| * 【语言包按需加载/注册】构造时默认仅注册中文包。 | ||
| * 英文包 en 作为独立子路径导出(./en),主入口不加载它,避免无谓的模块求值开销。 | ||
| * 需要英文时显式导入并注册: | ||
| * import en from '@complex-suite/utils/locale/en' | ||
| * utilsLocale.registerLocale(en) | ||
| * 英文包通过 registerLoader 注册动态加载器,setLocaleAsync 时自动按需 import。 | ||
| * 如需手动注册: | ||
| * utilsLocale.registerLoader('en-US', () => import('./en').then(m => m.default)) | ||
| */ | ||
| const utilsLocale = new Locale<UtilsMessages>({ pack: zh, name: 'utils' }) | ||
| /** 注册英文包动态加载器,setLocaleAsync 时自动触发按需加载 */ | ||
| utilsLocale.registerLoader('en-US', () => import('./en').then(m => m.default)) | ||
| export { | ||
@@ -33,0 +35,0 @@ utilsLocale, |
+69
-0
@@ -54,2 +54,8 @@ import type { FlatMessages, MessageValue, LocalePack, LocaleOptions, ChangeFrom } from './types' | ||
| /** | ||
| * 语言包动态加载器映射:lang → loader。 | ||
| * loader 返回 Promise<LocalePack>,由 setLocaleAsync 在语言切换前自动触发。 | ||
| * 加载器为实例级,每个 Locale 实例管理自己领域的语言包(data 实例加载 data 领域词条等)。 | ||
| */ | ||
| private $languageLoaders: Map<string, () => Promise<LocalePack<any>>> = new Map() | ||
| /** | ||
| * 实例级开发环境判断函数,默认委托给静态 Locale.isDev()。 | ||
@@ -112,2 +118,65 @@ * 需自定义时直接覆写本字段:locale.isDev = () => yourChecker() | ||
| /** | ||
| * 注册语言包动态加载器 | ||
| * | ||
| * 加载器在 setLocaleAsync 切换语言前自动触发,用于按需加载非默认语言包。 | ||
| * 多个实例各注册各自领域的 loader(data/request/component 等), | ||
| * setLocaleAsync 会遍历总线中所有实例,触发其 loaders 完成全量加载后再切换。 | ||
| * | ||
| * @param lang 目标语言标识(如 'en-US') | ||
| * @param loader 异步加载函数,应返回动态 import 的语言包 | ||
| * | ||
| * @example | ||
| * // 在各子包 locale/index.ts 中: | ||
| * dataLocale.registerLoader('en-US', () => import('./en').then(m => m.default)) | ||
| */ | ||
| registerLoader(lang: string, loader: () => Promise<LocalePack<any>>): void { | ||
| this.$languageLoaders.set(lang, loader) | ||
| } | ||
| /** | ||
| * 异步切换语言:自动触发所有总线实例的动态加载,确保语言包就绪后完成切换 | ||
| * | ||
| * 与 setLocale 不同:setLocale 要求语言包已预注册,否则返回 false; | ||
| * setLocaleAsync 会先检查总线中所有实例是否已注册目标语言,对未注册的实例触发其 loader 动态导入, | ||
| * 全部就绪后调用 setLocale(同步总线联动)。 | ||
| * | ||
| * 加载失败不阻断其他实例继续加载,最终返回 false(但已加载成功的资源不会回滚)。 | ||
| * 全部成功且 setLocale 成功时返回 true。 | ||
| * | ||
| * @example | ||
| * // 用户点击切换到英文 | ||
| * await dataLocale.setLocaleAsync('en-US') | ||
| */ | ||
| async setLocaleAsync(lang: string, options?: { sync?: boolean }): Promise<boolean> { | ||
| if (Locale.$syncing) { | ||
| this.$log('warn', '同步过程中不允许调用 setLocaleAsync,已阻止。') | ||
| return false | ||
| } | ||
| // 相同语言不触发切换 | ||
| if (lang === this.$currentLang) { | ||
| return true | ||
| } | ||
| // 收集总线中所有未注册目标语言的实例,触发其 loader 动态加载 | ||
| const loadTasks: Promise<void>[] = [] | ||
| for (const instance of Locale.$bus) { | ||
| if (!instance.hasLocale(lang) && instance.$languageLoaders?.has(lang)) { | ||
| const loader = instance.$languageLoaders.get(lang)! | ||
| loadTasks.push( | ||
| loader().then(pack => { | ||
| instance.registerLocale(pack) | ||
| }).catch(err => { | ||
| instance.$log('error', `动态加载语言 "${lang}" 失败:${String(err)}`) | ||
| }) | ||
| ) | ||
| } | ||
| } | ||
| await Promise.all(loadTasks) | ||
| // 所有 loader 完成后调用同步 setLocale(包含总线联动) | ||
| return this.setLocale(lang, options) | ||
| } | ||
| /** | ||
| * @param options.pack 默认语言包,自动注册并设为当前语言 | ||
@@ -114,0 +183,0 @@ * @param options.name 实例名称,用于日志标识,便于多实例调试 |
@@ -60,8 +60,3 @@ # Locale 模块 | ||
| import { utilsLocale } from '@complex-suite/utils' | ||
| // 英文包作为独立子路径导出,按需导入(默认仅注册中文) | ||
| import en from '@complex-suite/utils/locale/en' | ||
| // 注册英文包 | ||
| utilsLocale.registerLocale(en) | ||
| // 翻译 | ||
@@ -71,3 +66,4 @@ console.log(utilsLocale.t('color.invalidFormat')) | ||
| // 切换语言(自动同步到总线中其他实例) | ||
| utilsLocale.setLocale('en-US') | ||
| // 默认仅注册中文,英文包通过注册的 dynamic loader 自动按需加载 | ||
| await utilsLocale.setLocaleAsync('en-US') | ||
| ``` | ||
@@ -74,0 +70,0 @@ |
| import formatNum from './formatNum' | ||
| import type { mathType } from '../types' | ||
| /** | ||
| * 格式化数字 | ||
| * @param {string | number} originNum 数据 | ||
| * @param {'origin' | 'round' | 'floor' | 'ceil'} type 格式化类型 | ||
| * @param {number} radix 保留小数点位数 | ||
| * @param {boolean} NANZERO NAN是否格式化为0 | ||
| * @returns {number} | ||
| */ | ||
| export type { mathType } | ||
| export type mathType = 'round' | 'floor' | 'ceil' | ||
@@ -14,0 +7,0 @@ function getNum(originNum: unknown, type: 'origin' | mathType = 'round', radix = 2, NANZERO = false) { |
| import getComplexType from "../type/getComplexType" | ||
| import { _isComplex } from "../type/isComplex" | ||
| import type { updateDataValueType } from '../types' | ||
| export type updateDataValueType = Record<PropertyKey, any> | unknown[] | ||
| export type { updateDataValueType } | ||
| function updateData<T extends updateDataValueType = updateDataValueType, O extends updateDataValueType = updateDataValueType>(targetData: T, originData?: O): T & O { | ||
@@ -7,0 +9,0 @@ if (originData !== undefined) { |
| import getComplexType from '../type/getComplexType' | ||
| import $exportMsg from '../utils/$exportMsg' | ||
| import { utilsLocale } from '../locale' | ||
| import type { defineReactiveOptionType } from '../types' | ||
| export type defineReactiveOptionType = { | ||
| descriptor?: PropertyDescriptor | ||
| get?: (value: unknown) => unknown | ||
| set?: (val: unknown, oldVal: unknown) => unknown | ||
| } | ||
| export type { defineReactiveOptionType } | ||
| /** | ||
@@ -12,0 +10,0 @@ * 创建响应式数据 |
| import getRandomInList from './getRandomInList' | ||
| import type { letterType } from '../types' | ||
| export type { letterType } | ||
| type letterDataType = { | ||
@@ -24,8 +27,2 @@ small: string[] | ||
| export type letterType = { | ||
| small?: boolean, | ||
| big?: boolean, | ||
| number?: boolean | ||
| } | ||
| /** | ||
@@ -32,0 +29,0 @@ * 获取随机字符 |
| import getType, { type SimpleType } from './getType' | ||
| import getTag from './getTag' | ||
| import type { ComplexType } from '../types' | ||
| export type ComplexType = SimpleType | "file" | "blob" | "regExp" | "date" | 'map' | 'set' | ||
| export type { ComplexType } | ||
@@ -19,3 +20,3 @@ const complexTypeDict = { | ||
| * @param {*} value 需要获取类型的值 | ||
| * @returns {"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "null" | "array" | "file" | "blob" | "regExp" | "date" | "map" | "set"} 返回value的数据类型 | ||
| * @returns {"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "null" | "array" | "file" | "blob" | "regExp" | "date" | "map" | "set"} 返回value的数据类型 | ||
| */ | ||
@@ -22,0 +23,0 @@ function getComplexType(value: unknown): ComplexType { |
| import isArray from './isArray' | ||
| import type { SimpleType } from '../types' | ||
| export type SimpleType = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "array" | "null" | ||
| export type { SimpleType } | ||
@@ -5,0 +6,0 @@ /** |
| import isError from '../type/isError' | ||
| import type { consoleType } from '../types' | ||
| export type consoleType = 'error' | 'warn' | 'log' | ||
| export type { consoleType } | ||
| /** | ||
@@ -6,0 +8,0 @@ * 错误信息输出函数 |
+2
-0
| import { defineConfig } from 'vitest/config' | ||
| import { fileURLToPath } from 'node:url' | ||
| export default defineConfig({ | ||
| root: fileURLToPath(new URL('.', import.meta.url)), | ||
| test: { | ||
@@ -5,0 +7,0 @@ // 在这里配置你的测试选项 |
215543
1.8%113
0.89%4932
1.67%