@aidc-toolkit/core 1.0.24-beta → 1.0.26-beta
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/dist/index.d.ts +21 -170
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -128
- package/dist/index.js.map +1 -1
- package/dist/locale/i18n.d.ts +56 -0
- package/dist/locale/i18n.d.ts.map +1 -0
- package/dist/locale/i18n.js +97 -0
- package/dist/locale/i18n.js.map +1 -0
- package/dist/logger.d.ts +32 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +41 -0
- package/dist/logger.js.map +1 -0
- package/dist/type-helper.d.ts +71 -0
- package/dist/type-helper.d.ts.map +1 -0
- package/dist/type-helper.js +113 -0
- package/dist/type-helper.js.map +1 -0
- package/dist/type.d.ts +69 -0
- package/dist/type.d.ts.map +1 -0
- package/dist/type.js +2 -0
- package/dist/type.js.map +1 -0
- package/package.json +10 -6
- package/src/index.ts +4 -4
- package/src/locale/i18n.ts +6 -1
- package/src/logger.ts +8 -3
- package/src/type-helper.ts +33 -3
- package/src/type.ts +41 -10
- package/tsconfig-config.json +4 -0
- package/tsconfig-src.json +8 -0
- package/tsconfig.json +9 -1
- package/tsup.config.ts +3 -2
- package/dist/index.cjs +0 -173
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -170
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/type-helper.ts","../src/logger.ts","../src/locale/i18n.ts"],"sourcesContent":["/*!\n * Copyright © 2024-2025 Dolphin Data Development Ltd. and AIDC Toolkit\n * contributors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nexport type * from \"./type\";\nexport * from \"./type-helper\";\nexport * from \"./logger\";\nexport * from \"./locale/i18n\";\n","/**\n * Create an object with omitted or picked entries.\n *\n * @param omitting\n * True if omitting.\n *\n * @param o\n * Object.\n *\n * @param keys\n * Keys to omit or pick.\n *\n * @returns\n * Edited object.\n */\nfunction omitOrPick<Omitting extends boolean, T extends object, K extends keyof T>(omitting: Omitting, o: T, ...keys: K[]): Omitting extends true ? Omit<T, K> : Pick<T, K> {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Key and value types are known.\n return Object.fromEntries(Object.entries(o).filter(([key]) => keys.includes(key as K) !== omitting)) as ReturnType<typeof omitOrPick<Omitting, T, K>>;\n}\n\n/**\n * Create an object with omitted entries.\n *\n * @param o\n * Object.\n *\n * @param keys\n * Keys to omit.\n *\n * @returns\n * Edited object.\n */\nexport function omit<T extends object, K extends keyof T>(o: T, ...keys: K[]): Omit<T, K> {\n return omitOrPick(true, o, ...keys);\n}\n\n/**\n * Create an object with picked entries.\n *\n * @param o\n * Object.\n *\n * @param keys\n * Keys to pick.\n *\n * @returns\n * Edited object.\n */\nexport function pick<T extends object, K extends keyof T>(o: T, ...keys: K[]): Pick<T, K> {\n return omitOrPick(false, o, ...keys);\n}\n\n/**\n * Cast a property as a more narrow type.\n *\n * @param o\n * Object.\n *\n * @param key\n * Key of property to cast.\n *\n * @returns\n * Single-key object with property cast as desired type.\n */\nexport function propertyAs<TAsType extends T[K], T extends object, K extends keyof T>(o: T, key: K): Readonly<Omit<T, K> extends T ? Partial<Record<K, TAsType>> : Record<K, TAsType>> {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Type is determined by condition.\n return (key in o ?\n {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Force cast.\n [key]: o[key] as TAsType\n } :\n {}\n ) as ReturnType<typeof propertyAs<TAsType, T, K>>;\n}\n\n/**\n * Determine if argument is nullish. Application extension may pass `null` or `undefined` to missing parameters.\n *\n * @param argument\n * Argument.\n *\n * @returns\n * True if argument is undefined or null.\n */\nexport function isNullish<T>(argument: T | null | undefined): argument is null | undefined {\n return argument === null || argument === undefined;\n}\n","import { Logger } from \"tslog\";\n\n/**\n * Log levels.\n */\nexport const LogLevels = {\n Silly: 0,\n Trace: 1,\n Debug: 2,\n Info: 3,\n Warn: 4,\n Error: 5,\n Fatal: 6\n} as const;\n\n/**\n * Log level.\n */\nexport type LogLevel = typeof LogLevels[keyof typeof LogLevels];\n\n/**\n * Get a simple logger with an optional log level.\n *\n * @param logLevel\n * Log level as enumeration value or string if any.\n *\n * @returns\n * Logger.\n */\nexport function getLogger(logLevel?: string | number): Logger<unknown> {\n let minLevel: number;\n\n if (typeof logLevel === \"string\") {\n if (logLevel in LogLevels) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- String exists as a key.\n minLevel = LogLevels[logLevel as keyof typeof LogLevels];\n } else {\n throw new Error(`Unknown log level ${logLevel}`);\n }\n } else {\n minLevel = logLevel ?? LogLevels.Info;\n }\n\n return new Logger({\n minLevel\n });\n}\n","import type { i18n, LanguageDetectorModule, Resource } from \"i18next\";\nimport I18nextBrowserLanguageDetector from \"i18next-browser-languagedetector\";\nimport I18nextCLILanguageDetector from \"i18next-cli-language-detector\";\n\n/**\n * Locale strings type for generic manipulation.\n */\nexport interface LocaleResources {\n [key: string]: LocaleResources | string;\n}\n\n/**\n * Internationalization operating environments.\n */\nexport const I18nEnvironments = {\n /**\n * Command-line interface (e.g., unit tests).\n */\n CLI: 0,\n\n /**\n * Web server.\n */\n Server: 1,\n\n /**\n * Web browser.\n */\n Browser: 2\n} as const;\n\n/**\n * Internationalization operating environment.\n */\nexport type I18nEnvironment = typeof I18nEnvironments[keyof typeof I18nEnvironments];\n\n/**\n * Convert a string to lower case, skipping words that are all upper case.\n *\n * @param s\n * String.\n *\n * @returns\n * Lower case string.\n */\nfunction toLowerCase(s: string): string {\n // Words with no lower case letters are preserved as they are likely mnemonics.\n return s.split(\" \").map(word => /[a-z]/.test(word) ? word.toLowerCase() : word).join(\" \");\n}\n\n/**\n * Initialize internationalization.\n *\n * @param i18next\n * Internationalization object. As multiple objects exists, this parameter represents the one for the module for which\n * internationalization is being initialized.\n *\n * @param environment\n * Environment in which the application is running.\n *\n * @param debug\n * Debug setting.\n *\n * @param defaultNS\n * Default namespace.\n *\n * @param resources\n * Resources.\n *\n * @returns\n * Void promise.\n */\nexport async function i18nCoreInit(i18next: i18n, environment: I18nEnvironment, debug: boolean, defaultNS: string, ...resources: Resource[]): Promise<void> {\n // Initialization may be called more than once.\n if (!i18next.isInitialized) {\n const mergedResource: Resource = {};\n\n // Merge resources.\n for (const resource of resources) {\n // Merge languages.\n for (const [language, resourceLanguage] of Object.entries(resource)) {\n if (!(language in mergedResource)) {\n mergedResource[language] = {};\n }\n\n const mergedResourceLanguage = mergedResource[language];\n\n // Merge namespaces.\n for (const [namespace, resourceKey] of Object.entries(resourceLanguage)) {\n mergedResourceLanguage[namespace] = resourceKey;\n }\n }\n }\n\n let module: Parameters<typeof i18next.use>[0];\n\n switch (environment) {\n case I18nEnvironments.CLI:\n // TODO Refactor when https://github.com/neet/i18next-cli-language-detector/issues/281 resolved.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Per above.\n module = I18nextCLILanguageDetector as unknown as LanguageDetectorModule;\n break;\n\n case I18nEnvironments.Browser:\n module = I18nextBrowserLanguageDetector;\n break;\n\n default:\n throw new Error(\"Not supported\");\n }\n\n await i18next.use(module).init({\n debug,\n resources: mergedResource,\n fallbackLng: \"en\",\n defaultNS\n }).then(() => {\n // Add toLowerCase function.\n i18next.services.formatter?.add(\"toLowerCase\", value => typeof value === \"string\" ? toLowerCase(value) : String(value));\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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EAAE,MAAM,GAAG,EAAE,IAAI,UAAQ,QAAQ,KAAK,IAAI,IAAI,KAAK,YAAY,IAAI,IAAI,EAAE,KAAK,GAAG;AAC5F;AAwBA,eAAsB,aAAa,SAAe,aAA8B,OAAgB,cAAsB,WAAsC;AAExJ,MAAI,CAAC,QAAQ,eAAe;AACxB,UAAM,iBAA2B,CAAC;AAGlC,eAAW,YAAY,WAAW;AAE9B,iBAAW,CAAC,UAAU,gBAAgB,KAAK,OAAO,QAAQ,QAAQ,GAAG;AACjE,YAAI,EAAE,YAAY,iBAAiB;AAC/B,yBAAe,QAAQ,IAAI,CAAC;AAAA,QAChC;AAEA,cAAM,yBAAyB,eAAe,QAAQ;AAGtD,mBAAW,CAAC,WAAW,WAAW,KAAK,OAAO,QAAQ,gBAAgB,GAAG;AACrE,iCAAuB,SAAS,IAAI;AAAA,QACxC;AAAA,MACJ;AAAA,IACJ;AAEA,QAAIA;AAEJ,YAAQ,aAAa;AAAA,MACjB,KAAK,iBAAiB;AAGlB,QAAAA,UAAS,qCAAAC;AACT;AAAA,MAEJ,KAAK,iBAAiB;AAClB,QAAAD,UAAS,wCAAAE;AACT;AAAA,MAEJ;AACI,cAAM,IAAI,MAAM,eAAe;AAAA,IACvC;AAEA,UAAM,QAAQ,IAAIF,OAAM,EAAE,KAAK;AAAA,MAC3B;AAAA,MACA,WAAW;AAAA,MACX,aAAa;AAAA,MACb;AAAA,IACJ,CAAC,EAAE,KAAK,MAAM;AAEV,cAAQ,SAAS,WAAW,IAAI,eAAe,WAAS,OAAO,UAAU,WAAW,YAAY,KAAK,IAAI,OAAO,KAAK,CAAC;AAAA,IAC1H,CAAC;AAAA,EACL;AACJ;","names":["module","I18nextCLILanguageDetector","I18nextBrowserLanguageDetector"]}
|
package/dist/index.d.cts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { Logger } from 'tslog';
|
|
2
|
-
import { i18n, Resource } from 'i18next';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Typed function, applicable to any function, stricter than {@link Function}.
|
|
6
|
-
*/
|
|
7
|
-
type TypedFunction<TMethod extends (...args: Parameters<TMethod>) => ReturnType<TMethod>> = (...args: Parameters<TMethod>) => ReturnType<TMethod>;
|
|
8
|
-
/**
|
|
9
|
-
* Typed synchronous function, applicable to any function that doesn't return a Promise.
|
|
10
|
-
*/
|
|
11
|
-
type TypedSyncFunction<TMethod extends TypedFunction<TMethod>> = [ReturnType<TMethod>] extends [PromiseLike<unknown>] ? never : TypedFunction<TMethod>;
|
|
12
|
-
/**
|
|
13
|
-
* Determine the fundamental promised type. This is stricter than `Awaited\<Type\>` in that it requires a Promise.
|
|
14
|
-
*/
|
|
15
|
-
type PromisedType<T> = [T] extends [PromiseLike<infer TPromised>] ? TPromised : never;
|
|
16
|
-
/**
|
|
17
|
-
* Typed asynchronous function, applicable to any function that returns a Promise.
|
|
18
|
-
*/
|
|
19
|
-
type TypedAsyncFunction<TMethod extends (...args: Parameters<TMethod>) => PromiseLike<PromisedType<ReturnType<TMethod>>>> = (...args: Parameters<TMethod>) => Promise<PromisedType<ReturnType<TMethod>>>;
|
|
20
|
-
/**
|
|
21
|
-
* Nullishable type. Extends a type by allowing `null` and `undefined`.
|
|
22
|
-
*/
|
|
23
|
-
type Nullishable<T> = T | null | undefined;
|
|
24
|
-
/**
|
|
25
|
-
* Non-nullishable type. If T is an object type, it is spread and attributes within it are made non-nullishable.
|
|
26
|
-
* Equivalent to a deep `Required\<T\>` for an object and `NonNullable\<T\>` for any other type.
|
|
27
|
-
*/
|
|
28
|
-
type NonNullishable<T> = T extends object ? {
|
|
29
|
-
[P in keyof T]-?: NonNullishable<T[P]>;
|
|
30
|
-
} : NonNullable<T>;
|
|
31
|
-
/**
|
|
32
|
-
* Make some keys within a type optional.
|
|
33
|
-
*/
|
|
34
|
-
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
35
|
-
/**
|
|
36
|
-
* Type to restrict property keys to those that are strings and that support a specified type.
|
|
37
|
-
*/
|
|
38
|
-
type PropertyKeys<T, TProperty> = {
|
|
39
|
-
[K in keyof T]: K extends string ? T[K] extends TProperty ? K : never : never;
|
|
40
|
-
}[keyof T];
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Create an object with omitted entries.
|
|
44
|
-
*
|
|
45
|
-
* @param o
|
|
46
|
-
* Object.
|
|
47
|
-
*
|
|
48
|
-
* @param keys
|
|
49
|
-
* Keys to omit.
|
|
50
|
-
*
|
|
51
|
-
* @returns
|
|
52
|
-
* Edited object.
|
|
53
|
-
*/
|
|
54
|
-
declare function omit<T extends object, K extends keyof T>(o: T, ...keys: K[]): Omit<T, K>;
|
|
55
|
-
/**
|
|
56
|
-
* Create an object with picked entries.
|
|
57
|
-
*
|
|
58
|
-
* @param o
|
|
59
|
-
* Object.
|
|
60
|
-
*
|
|
61
|
-
* @param keys
|
|
62
|
-
* Keys to pick.
|
|
63
|
-
*
|
|
64
|
-
* @returns
|
|
65
|
-
* Edited object.
|
|
66
|
-
*/
|
|
67
|
-
declare function pick<T extends object, K extends keyof T>(o: T, ...keys: K[]): Pick<T, K>;
|
|
68
|
-
/**
|
|
69
|
-
* Cast a property as a more narrow type.
|
|
70
|
-
*
|
|
71
|
-
* @param o
|
|
72
|
-
* Object.
|
|
73
|
-
*
|
|
74
|
-
* @param key
|
|
75
|
-
* Key of property to cast.
|
|
76
|
-
*
|
|
77
|
-
* @returns
|
|
78
|
-
* Single-key object with property cast as desired type.
|
|
79
|
-
*/
|
|
80
|
-
declare function propertyAs<TAsType extends T[K], T extends object, K extends keyof T>(o: T, key: K): Readonly<Omit<T, K> extends T ? Partial<Record<K, TAsType>> : Record<K, TAsType>>;
|
|
81
|
-
/**
|
|
82
|
-
* Determine if argument is nullish. Application extension may pass `null` or `undefined` to missing parameters.
|
|
83
|
-
*
|
|
84
|
-
* @param argument
|
|
85
|
-
* Argument.
|
|
86
|
-
*
|
|
87
|
-
* @returns
|
|
88
|
-
* True if argument is undefined or null.
|
|
89
|
-
*/
|
|
90
|
-
declare function isNullish<T>(argument: T | null | undefined): argument is null | undefined;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Log levels.
|
|
94
|
-
*/
|
|
95
|
-
declare const LogLevels: {
|
|
96
|
-
readonly Silly: 0;
|
|
97
|
-
readonly Trace: 1;
|
|
98
|
-
readonly Debug: 2;
|
|
99
|
-
readonly Info: 3;
|
|
100
|
-
readonly Warn: 4;
|
|
101
|
-
readonly Error: 5;
|
|
102
|
-
readonly Fatal: 6;
|
|
103
|
-
};
|
|
104
|
-
/**
|
|
105
|
-
* Log level.
|
|
106
|
-
*/
|
|
107
|
-
type LogLevel = typeof LogLevels[keyof typeof LogLevels];
|
|
108
|
-
/**
|
|
109
|
-
* Get a simple logger with an optional log level.
|
|
110
|
-
*
|
|
111
|
-
* @param logLevel
|
|
112
|
-
* Log level as enumeration value or string if any.
|
|
113
|
-
*
|
|
114
|
-
* @returns
|
|
115
|
-
* Logger.
|
|
116
|
-
*/
|
|
117
|
-
declare function getLogger(logLevel?: string | number): Logger<unknown>;
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Locale strings type for generic manipulation.
|
|
121
|
-
*/
|
|
122
|
-
interface LocaleResources {
|
|
123
|
-
[key: string]: LocaleResources | string;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Internationalization operating environments.
|
|
127
|
-
*/
|
|
128
|
-
declare const I18nEnvironments: {
|
|
129
|
-
/**
|
|
130
|
-
* Command-line interface (e.g., unit tests).
|
|
131
|
-
*/
|
|
132
|
-
readonly CLI: 0;
|
|
133
|
-
/**
|
|
134
|
-
* Web server.
|
|
135
|
-
*/
|
|
136
|
-
readonly Server: 1;
|
|
137
|
-
/**
|
|
138
|
-
* Web browser.
|
|
139
|
-
*/
|
|
140
|
-
readonly Browser: 2;
|
|
141
|
-
};
|
|
142
|
-
/**
|
|
143
|
-
* Internationalization operating environment.
|
|
144
|
-
*/
|
|
145
|
-
type I18nEnvironment = typeof I18nEnvironments[keyof typeof I18nEnvironments];
|
|
146
|
-
/**
|
|
147
|
-
* Initialize internationalization.
|
|
148
|
-
*
|
|
149
|
-
* @param i18next
|
|
150
|
-
* Internationalization object. As multiple objects exists, this parameter represents the one for the module for which
|
|
151
|
-
* internationalization is being initialized.
|
|
152
|
-
*
|
|
153
|
-
* @param environment
|
|
154
|
-
* Environment in which the application is running.
|
|
155
|
-
*
|
|
156
|
-
* @param debug
|
|
157
|
-
* Debug setting.
|
|
158
|
-
*
|
|
159
|
-
* @param defaultNS
|
|
160
|
-
* Default namespace.
|
|
161
|
-
*
|
|
162
|
-
* @param resources
|
|
163
|
-
* Resources.
|
|
164
|
-
*
|
|
165
|
-
* @returns
|
|
166
|
-
* Void promise.
|
|
167
|
-
*/
|
|
168
|
-
declare function i18nCoreInit(i18next: i18n, environment: I18nEnvironment, debug: boolean, defaultNS: string, ...resources: Resource[]): Promise<void>;
|
|
169
|
-
|
|
170
|
-
export { type I18nEnvironment, I18nEnvironments, type LocaleResources, type LogLevel, LogLevels, type NonNullishable, type Nullishable, type Optional, type PropertyKeys, type TypedAsyncFunction, type TypedFunction, type TypedSyncFunction, getLogger, i18nCoreInit, isNullish, omit, pick, propertyAs };
|