@sv443-network/userutils 10.0.5 → 10.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/CHANGELOG.md +24 -0
- package/README.md +38 -32
- package/dist/UserUtils.cjs +555 -397
- package/dist/UserUtils.mjs +557 -397
- package/dist/UserUtils.umd.js +443 -304
- package/dist/lib/consts.d.ts +7 -0
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/translation.d.ts +80 -2
- package/package.json +4 -5
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Contains the semver version strings of UserUtils and the bundled library CoreUtils. */
|
|
2
|
+
export declare const versions: {
|
|
3
|
+
/** Semver version string of the bundled library CoreUtils. */
|
|
4
|
+
CoreUtils: string;
|
|
5
|
+
/** Semver version string of UserUtils. */
|
|
6
|
+
UserUtils: string;
|
|
7
|
+
};
|
package/dist/lib/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* UserUtils is a lightweight library with various utilities for userscripts, allowing you to register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and much more
|
|
4
4
|
*/
|
|
5
5
|
export * from "@sv443-network/coreutils";
|
|
6
|
+
export * from "./consts.js";
|
|
6
7
|
export * from "./Dialog.js";
|
|
7
8
|
export * from "./dom.js";
|
|
8
9
|
export * from "./Errors.js";
|
|
@@ -54,6 +54,10 @@ export type TransformTuple<TTrKey extends string = string> = [RegExp, TransformF
|
|
|
54
54
|
export type TrKeys<TTrObj, P extends string = ""> = {
|
|
55
55
|
[K in keyof TTrObj]: K extends string | number | boolean | null | undefined ? TTrObj[K] extends object ? TrKeys<TTrObj[K], `${P}${K}.`> : `${P}${K}` : never;
|
|
56
56
|
}[keyof TTrObj];
|
|
57
|
+
/** All translations loaded into memory */
|
|
58
|
+
declare const trans: {
|
|
59
|
+
[language: string]: TrObject;
|
|
60
|
+
};
|
|
57
61
|
/**
|
|
58
62
|
* Returns the translated text for the specified key in the specified language.
|
|
59
63
|
* If the key is not found in the specified previously registered translation, the key itself is returned.
|
|
@@ -84,10 +88,11 @@ declare function useTr<TTrKey extends string = string>(language: string): (key:
|
|
|
84
88
|
*/
|
|
85
89
|
declare function hasKey<TTrKey extends string = string>(language: string | undefined, key: TTrKey): boolean;
|
|
86
90
|
/**
|
|
87
|
-
* Registers a new language and its translations - if the language already exists, it will be
|
|
91
|
+
* Registers a new language and its translations - if the language already exists, it will be wholly replaced by the new one. If merging is necessary, it will have to be done before calling this function.
|
|
92
|
+
*
|
|
88
93
|
* The translations are a key-value pair where the key is the translation key and the value is the translated text.
|
|
89
94
|
* The translations can also be infinitely nested objects, resulting in a dot-separated key.
|
|
90
|
-
* @param language
|
|
95
|
+
* @param language Arbitrary language code or name to register for these translations. These should ideally stick to a standard like [IETF BCP 47](https://en.wikipedia.org/wiki/IETF_language_tag) (the standard used by JavaScript), but could really be anything.
|
|
91
96
|
* @param translations Translations for the specified language
|
|
92
97
|
* @example ```ts
|
|
93
98
|
* tr.addTranslations("en", {
|
|
@@ -106,6 +111,11 @@ declare function addTranslations(language: string, translations: TrObject): void
|
|
|
106
111
|
* @returns Translations for the specified language
|
|
107
112
|
*/
|
|
108
113
|
declare function getTranslations(language?: string): TrObject | undefined;
|
|
114
|
+
/**
|
|
115
|
+
* Returns all translations currently loaded into memory, indexed by language.
|
|
116
|
+
* @param asCopy Set to `false` to get a reference to the actual translations object instead of a copy (default). Might be useful for modifying translations in-memory without using {@linkcode tr.addTranslations()} to replace the entire object.
|
|
117
|
+
*/
|
|
118
|
+
declare function getAllTranslations(asCopy?: boolean): typeof trans;
|
|
109
119
|
/**
|
|
110
120
|
* The fallback language to use when a translation key is not found in the currently active language.
|
|
111
121
|
* Leave undefined to disable fallbacks and just return the translation key if translations are not found.
|
|
@@ -172,13 +182,81 @@ declare const tr: {
|
|
|
172
182
|
hasKey: <TTrKey extends string = string>(language: string | undefined, key: TTrKey) => ReturnType<typeof hasKey<TTrKey>>;
|
|
173
183
|
addTranslations: typeof addTranslations;
|
|
174
184
|
getTranslations: typeof getTranslations;
|
|
185
|
+
getAllTranslations: typeof getAllTranslations;
|
|
175
186
|
deleteTranslations: (language: string) => boolean;
|
|
176
187
|
setFallbackLanguage: typeof setFallbackLanguage;
|
|
177
188
|
getFallbackLanguage: typeof getFallbackLanguage;
|
|
178
189
|
addTransform: typeof addTransform;
|
|
179
190
|
deleteTransform: typeof deleteTransform;
|
|
191
|
+
/** Collection of predefined transform functions that can be added via {@linkcode tr.addTransform()} */
|
|
180
192
|
transforms: {
|
|
193
|
+
/**
|
|
194
|
+
* This transform will replace placeholders matching `${key}` with the value of the passed argument(s).
|
|
195
|
+
* The arguments can be passed in keyed object form or positionally via the spread operator:
|
|
196
|
+
* - Keyed: If the first argument is an object and `key` is found in it, the value will be used for the replacement.
|
|
197
|
+
* - Positional: If the first argument is not an object or has a `toString()` method that returns something that doesn't start with `[object`, the values will be positionally inserted in the order they were passed.
|
|
198
|
+
*
|
|
199
|
+
* @example ```ts
|
|
200
|
+
* tr.addTranslations("en", {
|
|
201
|
+
* "greeting": "Hello, ${user}!\nYou have ${notifs} notifications.",
|
|
202
|
+
* });
|
|
203
|
+
* tr.addTransform(tr.transforms.templateLiteral);
|
|
204
|
+
*
|
|
205
|
+
* const t = tr.use("en");
|
|
206
|
+
*
|
|
207
|
+
* // both calls return the same result:
|
|
208
|
+
* t("greeting", { user: "John", notifs: 5 }); // "Hello, John!\nYou have 5 notifications."
|
|
209
|
+
* t("greeting", "John", 5); // "Hello, John!\nYou have 5 notifications."
|
|
210
|
+
*
|
|
211
|
+
* // when a key isn't found in the object, it will be left as-is:
|
|
212
|
+
* t("greeting", { user: "John" }); // "Hello, John!\nYou have ${notifs} notifications."
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
181
215
|
templateLiteral: TransformTuple<string>;
|
|
216
|
+
/**
|
|
217
|
+
* This transform will replace placeholders matching `{{key}}` with the value of the passed argument(s).
|
|
218
|
+
* This format is commonly used in i18n libraries. Note that advanced syntax is not supported, only simple key replacement.
|
|
219
|
+
* The arguments can be passed in keyed object form or positionally via the spread operator:
|
|
220
|
+
* - Keyed: If the first argument is an object and `key` is found in it, the value will be used for the replacement.
|
|
221
|
+
* - Positional: If the first argument is not an object or has a `toString()` method that returns something that doesn't start with `[object`, the values will be positionally inserted in the order they were passed.
|
|
222
|
+
*
|
|
223
|
+
* @example ```ts
|
|
224
|
+
* tr.addTranslations("en", {
|
|
225
|
+
* "greeting": "Hello, {{user}}!\nYou have {{notifs}} notifications.",
|
|
226
|
+
* });
|
|
227
|
+
* tr.addTransform(tr.transforms.i18n);
|
|
228
|
+
*
|
|
229
|
+
* const t = tr.use("en");
|
|
230
|
+
*
|
|
231
|
+
* // both calls return the same result:
|
|
232
|
+
* t("greeting", { user: "Alice", notifs: 5 }); // "Hello, Alice!\nYou have 5 notifications."
|
|
233
|
+
* t("greeting", "Alice", 5); // "Hello, Alice!\nYou have 5 notifications."
|
|
234
|
+
*
|
|
235
|
+
* // when a key isn't found in the object, it will be left as-is:
|
|
236
|
+
* t("greeting", { user: "Alice" }); // "Hello, Alice!\nYou have {{notifs}} notifications."
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
i18n: TransformTuple<string>;
|
|
240
|
+
/**
|
|
241
|
+
* This transform will replace `%n` placeholders with the value of the passed arguments.
|
|
242
|
+
* The `%n` placeholders are 1-indexed, meaning `%1` will be replaced by the first argument (index 0), `%2` by the second (index 1), and so on.
|
|
243
|
+
* Objects will be stringified via `String()` before being inserted.
|
|
244
|
+
*
|
|
245
|
+
* @example ```ts
|
|
246
|
+
* tr.addTranslations("en", {
|
|
247
|
+
* "greeting": "Hello, %1!\nYou have %2 notifications.",
|
|
248
|
+
* });
|
|
249
|
+
* tr.addTransform(tr.transforms.percent);
|
|
250
|
+
*
|
|
251
|
+
* const t = tr.use("en");
|
|
252
|
+
*
|
|
253
|
+
* // arguments are inserted in the order they're passed:
|
|
254
|
+
* t("greeting", "Bob", 5); // "Hello, Bob!\nYou have 5 notifications."
|
|
255
|
+
*
|
|
256
|
+
* // when a value isn't found, the placeholder will be left as-is:
|
|
257
|
+
* t("greeting", "Bob"); // "Hello, Bob!\nYou have %2 notifications."
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
182
260
|
percent: TransformTuple<string>;
|
|
183
261
|
};
|
|
184
262
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sv443-network/userutils",
|
|
3
3
|
"libName": "UserUtils",
|
|
4
|
-
"version": "10.0
|
|
4
|
+
"version": "10.1.0",
|
|
5
5
|
"description": "General purpose DOM/GreaseMonkey library that allows you to register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and much more",
|
|
6
6
|
"main": "dist/UserUtils.mjs",
|
|
7
7
|
"module": "dist/UserUtils.mjs",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"homepage": "https://github.com/Sv443-Network/UserUtils",
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@sv443-network/coreutils": "3.0
|
|
36
|
+
"@sv443-network/coreutils": "3.3.0",
|
|
37
37
|
"nanoevents": "9.1.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
@@ -55,7 +55,6 @@
|
|
|
55
55
|
"express": "4.22.1",
|
|
56
56
|
"globals": "15.15.0",
|
|
57
57
|
"jsdom": "26.1.0",
|
|
58
|
-
"kleur": "4.1.5",
|
|
59
58
|
"tslib": "2.8.1",
|
|
60
59
|
"tsup": "8.5.1",
|
|
61
60
|
"tsx": "4.21.0",
|
|
@@ -87,7 +86,7 @@
|
|
|
87
86
|
"test-gm-serve": "node --import tsx ./test/TestPage/server.mts",
|
|
88
87
|
"test-gm-dev": "cd test/TestScript && pnpm dev",
|
|
89
88
|
"test-gm": "concurrently \"pnpm test-gm-serve\" \"pnpm test-gm-dev\"",
|
|
90
|
-
"test": "vitest",
|
|
91
|
-
"test-coverage": "vitest --coverage"
|
|
89
|
+
"test": "pnpm build && vitest",
|
|
90
|
+
"test-coverage": "pnpm build && vitest --coverage"
|
|
92
91
|
}
|
|
93
92
|
}
|