@u27n/js 0.1.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/LICENSE +7 -0
- package/dist/core.d.ts +18 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +59 -0
- package/dist/core.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +16 -0
- package/src/core.ts +84 -0
- package/src/index.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright © 2026 Max J. Polster
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface LocaleImports {
|
|
2
|
+
[locale: string]: () => Promise<{
|
|
3
|
+
default: unknown[];
|
|
4
|
+
}>;
|
|
5
|
+
}
|
|
6
|
+
export interface I18nOptions {
|
|
7
|
+
source: string;
|
|
8
|
+
locales: LocaleImports;
|
|
9
|
+
}
|
|
10
|
+
export declare class I18n {
|
|
11
|
+
#private;
|
|
12
|
+
constructor(options: I18nOptions | I18n);
|
|
13
|
+
load(locale: string): Promise<void>;
|
|
14
|
+
get locale(): string;
|
|
15
|
+
set locale(value: string);
|
|
16
|
+
t: <T>(value: T, id?: number) => T;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,aAAa;IAC7B,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,EAAE,CAAA;KAAE,CAAC,CAAC;CACxD;AAaD,MAAM,WAAW,WAAW;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,aAAa,CAAC;CACvB;AAED,qBAAa,IAAI;;gBAKJ,OAAO,EAAE,WAAW,GAAG,IAAI;IASjC,IAAI,CAAC,MAAM,EAAE,MAAM;IAgBzB,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED,IAAI,MAAM,CAAC,KAAK,EAAE,MAAM,EAGvB;IAED,CAAC,GAAI,CAAC,EAAE,OAAO,CAAC,EAAE,KAAK,MAAM,KAAG,CAAC,CAK/B;CACF"}
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export class I18n {
|
|
2
|
+
#kernel;
|
|
3
|
+
#locale;
|
|
4
|
+
#data;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.#kernel = options instanceof I18n ? options.#kernel : {
|
|
7
|
+
s: options.source,
|
|
8
|
+
i: options.locales,
|
|
9
|
+
p: new Map(),
|
|
10
|
+
d: new Map(),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
async load(locale) {
|
|
14
|
+
const kernel = this.#kernel;
|
|
15
|
+
if (locale === kernel.s) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (!Object.hasOwn(kernel.i, locale)) {
|
|
19
|
+
throw new Error();
|
|
20
|
+
}
|
|
21
|
+
let promise = kernel.p.get(locale);
|
|
22
|
+
if (!promise) {
|
|
23
|
+
promise = load(kernel, locale);
|
|
24
|
+
kernel.p.set(locale, promise);
|
|
25
|
+
}
|
|
26
|
+
return promise;
|
|
27
|
+
}
|
|
28
|
+
get locale() {
|
|
29
|
+
return this.#locale ?? this.#kernel.s;
|
|
30
|
+
}
|
|
31
|
+
set locale(value) {
|
|
32
|
+
this.#locale = value;
|
|
33
|
+
this.#data = this.#kernel.d.get(value);
|
|
34
|
+
}
|
|
35
|
+
t = (value, id) => {
|
|
36
|
+
const data = this.#data;
|
|
37
|
+
return data === undefined || id === undefined
|
|
38
|
+
? value
|
|
39
|
+
: data[id];
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
async function load(kernel, locale) {
|
|
43
|
+
try {
|
|
44
|
+
const json = (await kernel.i[locale]()).default;
|
|
45
|
+
const data = [];
|
|
46
|
+
let state = 0;
|
|
47
|
+
for (let i = 0; i < json.length; i += 2) {
|
|
48
|
+
const id = state + json[i];
|
|
49
|
+
state = id + 1;
|
|
50
|
+
data[id] = json[i + 1];
|
|
51
|
+
}
|
|
52
|
+
kernel.d.set(locale, data);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
kernel.p.delete(locale);
|
|
56
|
+
throw err;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=core.js.map
|
package/dist/core.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAqBA,MAAM,OAAO,IAAI;IAChB,OAAO,CAAS;IAChB,OAAO,CAAqB;IAC5B,KAAK,CAAwB;IAE7B,YAAY,OAA2B;QACtC,IAAI,CAAC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,OAAO,IAAI,KAAK,SAAS,IAAI,EAAE,KAAK,SAAS;YAC5C,CAAC,CAAC,KAAK;YACP,CAAC,CAAC,IAAI,CAAC,EAAE,CAAM,CAAC;IAClB,CAAC,CAAC;CACF;AAED,KAAK,UAAU,IAAI,CAAC,MAAc,EAAE,MAAc;IACjD,IAAI,CAAC;QACJ,MAAM,IAAI,GAAG,CAAC,MAAM,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC;QAChD,MAAM,IAAI,GAAc,EAAE,CAAC;QAC3B,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;YACzC,MAAM,EAAE,GAAG,KAAK,GAAI,IAAI,CAAC,CAAC,CAAY,CAAC;YACvC,KAAK,GAAG,EAAE,GAAG,CAAC,CAAC;YACf,IAAI,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACxB,CAAC;QACD,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC5B,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACd,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACxB,MAAM,GAAG,CAAC;IACX,CAAC;AACF,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@u27n/js",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"default": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"start": "tsc -w --preserveWatchOutput"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"typescript": "^5.9.3"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/core.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
|
|
2
|
+
export interface LocaleImports {
|
|
3
|
+
[locale: string]: () => Promise<{ default: unknown[] }>;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface Kernel {
|
|
7
|
+
/** source locale */
|
|
8
|
+
s: string;
|
|
9
|
+
/** locale imports */
|
|
10
|
+
i: LocaleImports;
|
|
11
|
+
/** loading promises */
|
|
12
|
+
p: Map<string, Promise<void>>;
|
|
13
|
+
/** loaded locale data */
|
|
14
|
+
d: Map<string, unknown[]>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface I18nOptions {
|
|
18
|
+
source: string;
|
|
19
|
+
locales: LocaleImports;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class I18n {
|
|
23
|
+
#kernel: Kernel;
|
|
24
|
+
#locale: string | undefined;
|
|
25
|
+
#data: unknown[] | undefined;
|
|
26
|
+
|
|
27
|
+
constructor(options: I18nOptions | I18n) {
|
|
28
|
+
this.#kernel = options instanceof I18n ? options.#kernel : {
|
|
29
|
+
s: options.source,
|
|
30
|
+
i: options.locales,
|
|
31
|
+
p: new Map(),
|
|
32
|
+
d: new Map(),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async load(locale: string) {
|
|
37
|
+
const kernel = this.#kernel;
|
|
38
|
+
if (locale === kernel.s) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (!Object.hasOwn(kernel.i, locale)) {
|
|
42
|
+
throw new Error();
|
|
43
|
+
}
|
|
44
|
+
let promise = kernel.p.get(locale);
|
|
45
|
+
if (!promise) {
|
|
46
|
+
promise = load(kernel, locale);
|
|
47
|
+
kernel.p.set(locale, promise);
|
|
48
|
+
}
|
|
49
|
+
return promise;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get locale(): string {
|
|
53
|
+
return this.#locale ?? this.#kernel.s;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
set locale(value: string) {
|
|
57
|
+
this.#locale = value;
|
|
58
|
+
this.#data = this.#kernel.d.get(value);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
t = <T>(value: T, id?: number): T => {
|
|
62
|
+
const data = this.#data;
|
|
63
|
+
return data === undefined || id === undefined
|
|
64
|
+
? value
|
|
65
|
+
: data[id] as T;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function load(kernel: Kernel, locale: string) {
|
|
70
|
+
try {
|
|
71
|
+
const json = (await kernel.i[locale]()).default;
|
|
72
|
+
const data: unknown[] = [];
|
|
73
|
+
let state = 0;
|
|
74
|
+
for (let i = 0; i < json.length; i += 2) {
|
|
75
|
+
const id = state + (json[i] as number);
|
|
76
|
+
state = id + 1;
|
|
77
|
+
data[id] = json[i + 1];
|
|
78
|
+
}
|
|
79
|
+
kernel.d.set(locale, data);
|
|
80
|
+
} catch (err) {
|
|
81
|
+
kernel.p.delete(locale);
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./core.js";
|