@arcgis/toolkit 4.34.0-next.61

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.
Files changed (43) hide show
  1. package/LICENSE.md +13 -0
  2. package/README.md +21 -0
  3. package/dist/array/index.cjs +12 -0
  4. package/dist/array/index.d.cts +12 -0
  5. package/dist/array/index.d.ts +12 -0
  6. package/dist/array/index.js +12 -0
  7. package/dist/dom/index.cjs +161 -0
  8. package/dist/dom/index.d.cts +115 -0
  9. package/dist/dom/index.d.ts +115 -0
  10. package/dist/dom/index.js +161 -0
  11. package/dist/error/index.cjs +36 -0
  12. package/dist/error/index.d.cts +32 -0
  13. package/dist/error/index.d.ts +32 -0
  14. package/dist/error/index.js +36 -0
  15. package/dist/function/index.cjs +16 -0
  16. package/dist/function/index.d.cts +19 -0
  17. package/dist/function/index.d.ts +19 -0
  18. package/dist/function/index.js +16 -0
  19. package/dist/intl/index.cjs +103 -0
  20. package/dist/intl/index.d.cts +121 -0
  21. package/dist/intl/index.d.ts +121 -0
  22. package/dist/intl/index.js +103 -0
  23. package/dist/predicate/index.cjs +8 -0
  24. package/dist/predicate/index.d.cts +24 -0
  25. package/dist/predicate/index.d.ts +24 -0
  26. package/dist/predicate/index.js +8 -0
  27. package/dist/promise/index.cjs +30 -0
  28. package/dist/promise/index.d.cts +46 -0
  29. package/dist/promise/index.d.ts +46 -0
  30. package/dist/promise/index.js +30 -0
  31. package/dist/string/index.cjs +91 -0
  32. package/dist/string/index.d.cts +102 -0
  33. package/dist/string/index.d.ts +102 -0
  34. package/dist/string/index.js +91 -0
  35. package/dist/tests/utils.d.cts +1 -0
  36. package/dist/tests/utils.d.ts +1 -0
  37. package/dist/type/index.d.cts +15 -0
  38. package/dist/type/index.d.ts +15 -0
  39. package/dist/url/index.cjs +26 -0
  40. package/dist/url/index.d.cts +18 -0
  41. package/dist/url/index.d.ts +18 -0
  42. package/dist/url/index.js +26 -0
  43. package/package.json +66 -0
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Check whether the code is executing in an Esri internal environment (for
3
+ * example, Lumina dev server). When true, your code can enable extra validation
4
+ * to detect incorrect usages or do runtime bug detection.
5
+ *
6
+ * The call to isEsriInternalEnv() MUST always appear behind one of the
7
+ * following guards to ensure it is correctly eliminated in production bundles:
8
+ *
9
+ * - `process.env.NODE_ENV !== "production"`
10
+ * - `process.env.NODE_ENV === "development"`
11
+ * - `process.env.NODE_ENV === "test"`
12
+ *
13
+ * [Documentation](https://qawebgis.esri.com/components/lumina/publishing#internal-esri-environment)
14
+ *
15
+ * @remarks
16
+ * This function is primary for usage in support packages. In Lumina component
17
+ * packages, simpler alternatives are provided:
18
+ * https://qawebgis.esri.com/components/lumina/publishing#bundling-code-conditionally
19
+ */
20
+ export declare const isEsriInternalEnv: () => boolean;
21
+ /**
22
+ * Calls a sync method and catch any errors. Returns undefined if error occurred.
23
+ *
24
+ * Can also provide a thisContext and rest arguments
25
+ */
26
+ export declare const safeCall: <Callback extends (...args: never[]) => unknown>(callback?: Callback, thisContext?: ThisParameterType<Callback>, ...rest: Parameters<Callback>) => ReturnType<Callback> | void;
27
+ /**
28
+ * Calls an async method and catch any errors. Returns undefined if error occurred.
29
+ *
30
+ * Can also provide a thisContext and rest arguments
31
+ */
32
+ export declare const safeAsyncCall: <Callback extends (...args: never[]) => unknown>(callback?: Callback, thisContext?: ThisParameterType<Callback>, ...rest: Parameters<Callback>) => Promise<Awaited<ReturnType<Callback>> | void>;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Check whether the code is executing in an Esri internal environment (for
3
+ * example, Lumina dev server). When true, your code can enable extra validation
4
+ * to detect incorrect usages or do runtime bug detection.
5
+ *
6
+ * The call to isEsriInternalEnv() MUST always appear behind one of the
7
+ * following guards to ensure it is correctly eliminated in production bundles:
8
+ *
9
+ * - `process.env.NODE_ENV !== "production"`
10
+ * - `process.env.NODE_ENV === "development"`
11
+ * - `process.env.NODE_ENV === "test"`
12
+ *
13
+ * [Documentation](https://qawebgis.esri.com/components/lumina/publishing#internal-esri-environment)
14
+ *
15
+ * @remarks
16
+ * This function is primary for usage in support packages. In Lumina component
17
+ * packages, simpler alternatives are provided:
18
+ * https://qawebgis.esri.com/components/lumina/publishing#bundling-code-conditionally
19
+ */
20
+ export declare const isEsriInternalEnv: () => boolean;
21
+ /**
22
+ * Calls a sync method and catch any errors. Returns undefined if error occurred.
23
+ *
24
+ * Can also provide a thisContext and rest arguments
25
+ */
26
+ export declare const safeCall: <Callback extends (...args: never[]) => unknown>(callback?: Callback, thisContext?: ThisParameterType<Callback>, ...rest: Parameters<Callback>) => ReturnType<Callback> | void;
27
+ /**
28
+ * Calls an async method and catch any errors. Returns undefined if error occurred.
29
+ *
30
+ * Can also provide a thisContext and rest arguments
31
+ */
32
+ export declare const safeAsyncCall: <Callback extends (...args: never[]) => unknown>(callback?: Callback, thisContext?: ThisParameterType<Callback>, ...rest: Parameters<Callback>) => Promise<Awaited<ReturnType<Callback>> | void>;
@@ -0,0 +1,36 @@
1
+ const isEsriInternalEnv = () => (
2
+ //#endregion isEsriInternalEnv
3
+ /*
4
+ * `globalThis.` is important here. Some bundlers remove the `typeof process`
5
+ * checks, but don't remove the usages of undefined variables - this can cause
6
+ * runtime error. By adding `globalThis.`, we avoid having `typeof process`
7
+ * check removed by the bundler.
8
+ * This does meant tree-shaking won't happen for the isEsriInternalEnv()
9
+ * check, but this is ok since this check is meant to always be behind the
10
+ * development/test guard.
11
+ * See https://devtopia.esri.com/WebGIS/arcgis-web-components/pull/2087#issuecomment-5152454
12
+ */
13
+ typeof globalThis.process === "object" && !!process.env.ESRI_INTERNAL
14
+ );
15
+ const safeCall = (callback, thisContext, ...rest) => {
16
+ try {
17
+ return callback?.call(thisContext, ...rest);
18
+ } catch (error) {
19
+ console.error(error, callback);
20
+ }
21
+ return void 0;
22
+ };
23
+ const safeAsyncCall = async (callback, thisContext, ...rest) => {
24
+ try {
25
+ const result = callback?.call(thisContext, ...rest);
26
+ return await result;
27
+ } catch (error) {
28
+ console.error(error, callback);
29
+ }
30
+ return void 0;
31
+ };
32
+ export {
33
+ isEsriInternalEnv,
34
+ safeAsyncCall,
35
+ safeCall
36
+ };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const identity = (value) => value;
4
+ const debounce = (func, waitFor = 100) => {
5
+ let timeout;
6
+ return (...args) => {
7
+ const later = () => {
8
+ clearTimeout(timeout);
9
+ func(...args);
10
+ };
11
+ clearTimeout(timeout);
12
+ timeout = setTimeout(later, waitFor);
13
+ };
14
+ };
15
+ exports.debounce = debounce;
16
+ exports.identity = identity;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Identity function - returns back the first parameter
3
+ *
4
+ * Useful when providing a "mapping function" is required, but you have no need
5
+ * to change the value
6
+ *
7
+ * @privateRemarks
8
+ * This is a trivial function, but it helps workaround a TypeScript bug:
9
+ * https://devtopia.esri.com/WebGIS/arcgis-web-components/issues/2047
10
+ */
11
+ export declare const identity: <T>(value: T) => T;
12
+ /**
13
+ * Allows to debounce a function.
14
+ *
15
+ * @param func Function to be debounced
16
+ * @param waitFor Debounce time in milliseconds
17
+ * @returns Returns a function that can be called to debounce the original function
18
+ */
19
+ export declare const debounce: <F extends (...args: Parameters<F>) => ReturnType<F>>(func: F, waitFor?: number) => ((...args: Parameters<F>) => void);
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Identity function - returns back the first parameter
3
+ *
4
+ * Useful when providing a "mapping function" is required, but you have no need
5
+ * to change the value
6
+ *
7
+ * @privateRemarks
8
+ * This is a trivial function, but it helps workaround a TypeScript bug:
9
+ * https://devtopia.esri.com/WebGIS/arcgis-web-components/issues/2047
10
+ */
11
+ export declare const identity: <T>(value: T) => T;
12
+ /**
13
+ * Allows to debounce a function.
14
+ *
15
+ * @param func Function to be debounced
16
+ * @param waitFor Debounce time in milliseconds
17
+ * @returns Returns a function that can be called to debounce the original function
18
+ */
19
+ export declare const debounce: <F extends (...args: Parameters<F>) => ReturnType<F>>(func: F, waitFor?: number) => ((...args: Parameters<F>) => void);
@@ -0,0 +1,16 @@
1
+ const identity = (value) => value;
2
+ const debounce = (func, waitFor = 100) => {
3
+ let timeout;
4
+ return (...args) => {
5
+ const later = () => {
6
+ clearTimeout(timeout);
7
+ func(...args);
8
+ };
9
+ clearTimeout(timeout);
10
+ timeout = setTimeout(later, waitFor);
11
+ };
12
+ };
13
+ export {
14
+ debounce,
15
+ identity
16
+ };
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const dom_index = require("../dom/index.cjs");
4
+ const supportedLocalesArray = "ar,bg,bs,ca,cs,da,de,el,en,es,et,fi,fr,he,hr,hu,id,it,ja,ko,lt,lv,nl,nb,no,pl,pt-BR,pt-PT,ro,ru,sk,sl,sr,sv,th,tr,uk,vi,zh-CN,zh-HK,zh-TW".split(
5
+ ","
6
+ );
7
+ const supportedLocales = (
8
+ //#endregion supportedLocales
9
+ /* @__PURE__ */ new Set(supportedLocalesArray)
10
+ );
11
+ const defaultLocale = "en";
12
+ const localeEquivalencies = {
13
+ //#region localeEquivalencies
14
+ // We use `pt-PT` as it will have the same translations as `pt`, which has no corresponding bundle
15
+ pt: "pt-PT",
16
+ // We support both 'nb' and 'no' (BCP 47) for Norwegian but only `no` has corresponding bundle
17
+ nb: "no",
18
+ // We support both 'nn' and 'no' (BCP 47) for Norwegian but only `no` has corresponding bundle
19
+ // See https://devtopia.esri.com/WebGIS/arcgis-web-components/issues/4667
20
+ nn: "no",
21
+ // We use `zh-CN` as base translation for chinese locales which has no corresponding bundle.
22
+ zh: "zh-CN"
23
+ //#endregion localeEquivalencies
24
+ };
25
+ const fetchT9nStringsBundle = async (locale, assetsPath, prefix = "") => {
26
+ const path = `${assetsPath}/${prefix}`;
27
+ const filePath = `${path}${locale}.json`;
28
+ t9nStringsCache[filePath] ?? (t9nStringsCache[filePath] = fetchBundle(locale, path));
29
+ return await t9nStringsCache[filePath];
30
+ };
31
+ const t9nStringsCache = {};
32
+ const fetchBundle = async (locale, path) => {
33
+ const filePath = `${path}${locale}.json`;
34
+ try {
35
+ const response = await fetch(filePath);
36
+ if (response.ok) {
37
+ return await response.json();
38
+ }
39
+ } catch (error) {
40
+ if (process.env.NODE_ENV !== "production") {
41
+ const is404ViteFallback = String(error).includes(`Unexpected token '<', "<!doctype "... is not valid JSON`);
42
+ if (is404ViteFallback) {
43
+ console.error(`[404] Localization strings not found at ${filePath}`);
44
+ } else {
45
+ console.error(`Error fetching localization strings at ${filePath}`, error);
46
+ }
47
+ } else {
48
+ console.error(error);
49
+ }
50
+ return {};
51
+ }
52
+ if (locale === defaultLocale) {
53
+ return {};
54
+ }
55
+ return await fetchBundle(defaultLocale, path);
56
+ };
57
+ const getElementLocale = (element) => {
58
+ const lang = dom_index.getElementAttribute(element, "lang", globalThis.navigator?.language || defaultLocale);
59
+ return { lang, t9nLocale: normalizeLocale(lang) };
60
+ };
61
+ const normalizeLocale = (lang) => {
62
+ const [rawLanguageCode, regionCode] = lang.split("-");
63
+ const languageCode = rawLanguageCode.toLowerCase();
64
+ let normalizedLocale = languageCode;
65
+ if (regionCode) {
66
+ normalizedLocale = `${languageCode}-${regionCode.toUpperCase()}`;
67
+ }
68
+ normalizedLocale = localeEquivalencies[normalizedLocale] ?? normalizedLocale;
69
+ if (supportedLocales.has(normalizedLocale)) {
70
+ return normalizedLocale;
71
+ }
72
+ if (regionCode) {
73
+ return normalizeLocale(languageCode);
74
+ }
75
+ return defaultLocale;
76
+ };
77
+ const startLocaleObserver = (element, getAssetsPath, onUpdated, assetName) => {
78
+ let result = void 0;
79
+ const callback = () => updateComponentLocaleState(element, getAssetsPath(), assetName).then((newResult) => {
80
+ if (result?.lang !== newResult.lang || result.t9nLocale !== newResult.t9nLocale || result.t9nStrings !== newResult.t9nStrings) {
81
+ onUpdated(newResult);
82
+ }
83
+ result = newResult;
84
+ }).catch(console.error);
85
+ queueMicrotask(callback);
86
+ return dom_index.observeAncestorsMutation(element, ["lang"], callback);
87
+ };
88
+ const updateComponentLocaleState = async (element, assetsPath, assetName = element.localName.split("-").slice(1).join("-")) => {
89
+ const { lang, t9nLocale } = getElementLocale(element);
90
+ const t9nAssetsPath = `${assetsPath}/${assetName}/t9n`;
91
+ const prefix = `messages.`;
92
+ const t9nStrings = (
93
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
94
+ assetName === null ? {} : await fetchT9nStringsBundle(t9nLocale, t9nAssetsPath, prefix)
95
+ );
96
+ return { lang, t9nLocale, t9nStrings };
97
+ };
98
+ exports.defaultLocale = defaultLocale;
99
+ exports.fetchT9nStringsBundle = fetchT9nStringsBundle;
100
+ exports.getElementLocale = getElementLocale;
101
+ exports.normalizeLocale = normalizeLocale;
102
+ exports.startLocaleObserver = startLocaleObserver;
103
+ exports.supportedLocales = supportedLocales;
@@ -0,0 +1,121 @@
1
+ declare const supportedLocalesArray: ["ar", "bg", "bs", "ca", "cs", "da", "de", "el", "en", "es", "et", "fi", "fr", "he", "hr", "hu", "id", "it", "ja", "ko", "lt", "lv", "nl", "nb", "no", "pl", "pt-BR", "pt-PT", "ro", "ru", "sk", "sl", "sr", "sv", "th", "tr", "uk", "vi", "zh-CN", "zh-HK", "zh-TW"];
2
+ /**
3
+ * The list of supported locales for ArcGIS Maps SDK for JavaScript components.
4
+ */
5
+ export declare const supportedLocales: Set<"hr" | "th" | "tr" | "ar" | "bg" | "bs" | "ca" | "cs" | "da" | "de" | "el" | "en" | "es" | "et" | "fi" | "fr" | "he" | "hu" | "id" | "it" | "ja" | "ko" | "lt" | "lv" | "nl" | "nb" | "no" | "pl" | "pt-BR" | "pt-PT" | "ro" | "ru" | "sk" | "sl" | "sr" | "sv" | "uk" | "vi" | "zh-CN" | "zh-HK" | "zh-TW">;
6
+ /**
7
+ * TypeScript union type for each locale supported by WebGIS SDK
8
+ */
9
+ export type SupportedLocale = (typeof supportedLocalesArray)[number];
10
+ /**
11
+ * The default locale for WebGIS SDK.
12
+ */
13
+ export declare const defaultLocale = "en";
14
+ /**
15
+ * Fetch the T9N strings bundle for the given locale, assets path and prefix.
16
+ * The locale must be one of the supported locales.
17
+ * If the locale is not supported, it will default to 'en'.
18
+ * If the T9N strings bundle cannot be found, it will default to 'en'.
19
+ *
20
+ * @param locale The locale for which to fetch the T9N strings
21
+ * @param assetsPath The path to the assets folder where the T9N strings are located
22
+ * @param prefix The prefix to use for the T9N strings file name.
23
+ *
24
+ * @remarks
25
+ * Rather than using this function directly, prefer the
26
+ * [useT9n()](https://qawebgis.esri.com/components/lumina/controllers/useT9n)
27
+ * controller.
28
+ *
29
+ * @remarks
30
+ * The function implements cache. Frequent calls are safe.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * // Will make a request for ./assets/coding-editor/t9n/messages.en.json
35
+ * const t9nStrings = await fetchT9nStringsBundle(
36
+ * "en",
37
+ * getAssetPath("./assets/coding-editor/t9n"),
38
+ * "messages."
39
+ * );
40
+ * ```
41
+ *
42
+ * @see [Lumina asset handling](https://qawebgis.esri.com/components/lumina/assets)
43
+ */
44
+ export declare const fetchT9nStringsBundle: <Strings extends GenericT9nStrings>(locale: string, assetsPath: string, prefix?: string) => Promise<Strings>;
45
+ /**
46
+ * The interface for translated strings.
47
+ */
48
+ export interface GenericT9nStrings {
49
+ [key: string]: GenericT9nStrings | string;
50
+ }
51
+ /**
52
+ * Get the locale of the given element.
53
+ * It will look for the lang attribute on the element and its ancestors.
54
+ * If not lang is found, it will default to 'en'.
55
+ *
56
+ * @returns an object with the `lang` and `t9nLocale` properties.
57
+ *
58
+ * @see [lang vs t9nLocale](https://qawebgis.esri.com/components/lumina/controllers/useT9n#difference-between-lang-and-t9nlocale).
59
+ */
60
+ export declare const getElementLocale: (element: HTMLElement) => {
61
+ readonly lang: string;
62
+ readonly t9nLocale: SupportedLocale;
63
+ };
64
+ /**
65
+ * Try to parse any language string into one of the locales supported by WebGIS SDK.
66
+ *
67
+ * @remarks
68
+ * If language includes a region code, but we don't have a bundle for it, the
69
+ * language is normalized to the language code only.
70
+ */
71
+ export declare const normalizeLocale: (lang: string) => SupportedLocale;
72
+ export interface LocaleObserverResult<Strings extends GenericT9nStrings = GenericT9nStrings> {
73
+ /** The T9N strings of the component */
74
+ t9nStrings: Strings;
75
+ /**
76
+ * The locale of the component set by the `lang` attribute on the component host element or one of its ancestors.
77
+ */
78
+ lang: string;
79
+ /**
80
+ * The locale used by the component to load the T9N strings.
81
+ * It may be different than the locale of the component host element that was set by the `lang` attribute.
82
+ */
83
+ t9nLocale: SupportedLocale;
84
+ }
85
+ /**
86
+ * Start the locale observer for the given component.
87
+ * The callback will be called when the locale changes for the component.
88
+ * It will observe the lang attribute on the component and its ancestors.
89
+ * The callback is called once at the beginning.
90
+ *
91
+ * @param element The HTML element that is doing the fetching
92
+ * This is used to determine the locale to fetch, observe the `lang` attribute
93
+ * changes, and is used as the default value for the `assetName` parameter.
94
+ * @param getAssetsPath The callback to get path to the assets folder where the
95
+ * T9N strings are located.
96
+ * @param onUpdated The callback to call when the locale changes.
97
+ * @param assetName Optionally override the asset file name.
98
+ * Default file name is the element tag name without the part before the
99
+ * first dash (e.g. `arcgis-map` becomes `map`).
100
+ *
101
+ * Set to null if the component has no localization strings, but you still
102
+ * wish to use `startLocaleObserver` to get the locale information.
103
+ *
104
+ * @remarks
105
+ * Rather than using this function directly, prefer the
106
+ * [useT9n()](https://qawebgis.esri.com/components/lumina/controllers/useT9n)
107
+ * controller.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * const observer = startLocaleObserver(
112
+ * element,
113
+ * () => getAssetPath("./assets"),
114
+ * ({ strings, lang, t9nLocale }) => console.log({ strings, lang, t9nLocale }),
115
+ * );
116
+ * // Later: cleanup
117
+ * observer();
118
+ * ```
119
+ */
120
+ export declare const startLocaleObserver: <Strings extends GenericT9nStrings = GenericT9nStrings>(element: HTMLElement, getAssetsPath: () => string, onUpdated: (payload: LocaleObserverResult<Strings>) => void, assetName?: string | null) => (() => void);
121
+ export {};
@@ -0,0 +1,121 @@
1
+ declare const supportedLocalesArray: ["ar", "bg", "bs", "ca", "cs", "da", "de", "el", "en", "es", "et", "fi", "fr", "he", "hr", "hu", "id", "it", "ja", "ko", "lt", "lv", "nl", "nb", "no", "pl", "pt-BR", "pt-PT", "ro", "ru", "sk", "sl", "sr", "sv", "th", "tr", "uk", "vi", "zh-CN", "zh-HK", "zh-TW"];
2
+ /**
3
+ * The list of supported locales for ArcGIS Maps SDK for JavaScript components.
4
+ */
5
+ export declare const supportedLocales: Set<"hr" | "th" | "tr" | "ar" | "bg" | "bs" | "ca" | "cs" | "da" | "de" | "el" | "en" | "es" | "et" | "fi" | "fr" | "he" | "hu" | "id" | "it" | "ja" | "ko" | "lt" | "lv" | "nl" | "nb" | "no" | "pl" | "pt-BR" | "pt-PT" | "ro" | "ru" | "sk" | "sl" | "sr" | "sv" | "uk" | "vi" | "zh-CN" | "zh-HK" | "zh-TW">;
6
+ /**
7
+ * TypeScript union type for each locale supported by WebGIS SDK
8
+ */
9
+ export type SupportedLocale = (typeof supportedLocalesArray)[number];
10
+ /**
11
+ * The default locale for WebGIS SDK.
12
+ */
13
+ export declare const defaultLocale = "en";
14
+ /**
15
+ * Fetch the T9N strings bundle for the given locale, assets path and prefix.
16
+ * The locale must be one of the supported locales.
17
+ * If the locale is not supported, it will default to 'en'.
18
+ * If the T9N strings bundle cannot be found, it will default to 'en'.
19
+ *
20
+ * @param locale The locale for which to fetch the T9N strings
21
+ * @param assetsPath The path to the assets folder where the T9N strings are located
22
+ * @param prefix The prefix to use for the T9N strings file name.
23
+ *
24
+ * @remarks
25
+ * Rather than using this function directly, prefer the
26
+ * [useT9n()](https://qawebgis.esri.com/components/lumina/controllers/useT9n)
27
+ * controller.
28
+ *
29
+ * @remarks
30
+ * The function implements cache. Frequent calls are safe.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * // Will make a request for ./assets/coding-editor/t9n/messages.en.json
35
+ * const t9nStrings = await fetchT9nStringsBundle(
36
+ * "en",
37
+ * getAssetPath("./assets/coding-editor/t9n"),
38
+ * "messages."
39
+ * );
40
+ * ```
41
+ *
42
+ * @see [Lumina asset handling](https://qawebgis.esri.com/components/lumina/assets)
43
+ */
44
+ export declare const fetchT9nStringsBundle: <Strings extends GenericT9nStrings>(locale: string, assetsPath: string, prefix?: string) => Promise<Strings>;
45
+ /**
46
+ * The interface for translated strings.
47
+ */
48
+ export interface GenericT9nStrings {
49
+ [key: string]: GenericT9nStrings | string;
50
+ }
51
+ /**
52
+ * Get the locale of the given element.
53
+ * It will look for the lang attribute on the element and its ancestors.
54
+ * If not lang is found, it will default to 'en'.
55
+ *
56
+ * @returns an object with the `lang` and `t9nLocale` properties.
57
+ *
58
+ * @see [lang vs t9nLocale](https://qawebgis.esri.com/components/lumina/controllers/useT9n#difference-between-lang-and-t9nlocale).
59
+ */
60
+ export declare const getElementLocale: (element: HTMLElement) => {
61
+ readonly lang: string;
62
+ readonly t9nLocale: SupportedLocale;
63
+ };
64
+ /**
65
+ * Try to parse any language string into one of the locales supported by WebGIS SDK.
66
+ *
67
+ * @remarks
68
+ * If language includes a region code, but we don't have a bundle for it, the
69
+ * language is normalized to the language code only.
70
+ */
71
+ export declare const normalizeLocale: (lang: string) => SupportedLocale;
72
+ export interface LocaleObserverResult<Strings extends GenericT9nStrings = GenericT9nStrings> {
73
+ /** The T9N strings of the component */
74
+ t9nStrings: Strings;
75
+ /**
76
+ * The locale of the component set by the `lang` attribute on the component host element or one of its ancestors.
77
+ */
78
+ lang: string;
79
+ /**
80
+ * The locale used by the component to load the T9N strings.
81
+ * It may be different than the locale of the component host element that was set by the `lang` attribute.
82
+ */
83
+ t9nLocale: SupportedLocale;
84
+ }
85
+ /**
86
+ * Start the locale observer for the given component.
87
+ * The callback will be called when the locale changes for the component.
88
+ * It will observe the lang attribute on the component and its ancestors.
89
+ * The callback is called once at the beginning.
90
+ *
91
+ * @param element The HTML element that is doing the fetching
92
+ * This is used to determine the locale to fetch, observe the `lang` attribute
93
+ * changes, and is used as the default value for the `assetName` parameter.
94
+ * @param getAssetsPath The callback to get path to the assets folder where the
95
+ * T9N strings are located.
96
+ * @param onUpdated The callback to call when the locale changes.
97
+ * @param assetName Optionally override the asset file name.
98
+ * Default file name is the element tag name without the part before the
99
+ * first dash (e.g. `arcgis-map` becomes `map`).
100
+ *
101
+ * Set to null if the component has no localization strings, but you still
102
+ * wish to use `startLocaleObserver` to get the locale information.
103
+ *
104
+ * @remarks
105
+ * Rather than using this function directly, prefer the
106
+ * [useT9n()](https://qawebgis.esri.com/components/lumina/controllers/useT9n)
107
+ * controller.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * const observer = startLocaleObserver(
112
+ * element,
113
+ * () => getAssetPath("./assets"),
114
+ * ({ strings, lang, t9nLocale }) => console.log({ strings, lang, t9nLocale }),
115
+ * );
116
+ * // Later: cleanup
117
+ * observer();
118
+ * ```
119
+ */
120
+ export declare const startLocaleObserver: <Strings extends GenericT9nStrings = GenericT9nStrings>(element: HTMLElement, getAssetsPath: () => string, onUpdated: (payload: LocaleObserverResult<Strings>) => void, assetName?: string | null) => (() => void);
121
+ export {};
@@ -0,0 +1,103 @@
1
+ import { getElementAttribute, observeAncestorsMutation } from "../dom/index.js";
2
+ const supportedLocalesArray = "ar,bg,bs,ca,cs,da,de,el,en,es,et,fi,fr,he,hr,hu,id,it,ja,ko,lt,lv,nl,nb,no,pl,pt-BR,pt-PT,ro,ru,sk,sl,sr,sv,th,tr,uk,vi,zh-CN,zh-HK,zh-TW".split(
3
+ ","
4
+ );
5
+ const supportedLocales = (
6
+ //#endregion supportedLocales
7
+ /* @__PURE__ */ new Set(supportedLocalesArray)
8
+ );
9
+ const defaultLocale = "en";
10
+ const localeEquivalencies = {
11
+ //#region localeEquivalencies
12
+ // We use `pt-PT` as it will have the same translations as `pt`, which has no corresponding bundle
13
+ pt: "pt-PT",
14
+ // We support both 'nb' and 'no' (BCP 47) for Norwegian but only `no` has corresponding bundle
15
+ nb: "no",
16
+ // We support both 'nn' and 'no' (BCP 47) for Norwegian but only `no` has corresponding bundle
17
+ // See https://devtopia.esri.com/WebGIS/arcgis-web-components/issues/4667
18
+ nn: "no",
19
+ // We use `zh-CN` as base translation for chinese locales which has no corresponding bundle.
20
+ zh: "zh-CN"
21
+ //#endregion localeEquivalencies
22
+ };
23
+ const fetchT9nStringsBundle = async (locale, assetsPath, prefix = "") => {
24
+ const path = `${assetsPath}/${prefix}`;
25
+ const filePath = `${path}${locale}.json`;
26
+ t9nStringsCache[filePath] ?? (t9nStringsCache[filePath] = fetchBundle(locale, path));
27
+ return await t9nStringsCache[filePath];
28
+ };
29
+ const t9nStringsCache = {};
30
+ const fetchBundle = async (locale, path) => {
31
+ const filePath = `${path}${locale}.json`;
32
+ try {
33
+ const response = await fetch(filePath);
34
+ if (response.ok) {
35
+ return await response.json();
36
+ }
37
+ } catch (error) {
38
+ if (process.env.NODE_ENV !== "production") {
39
+ const is404ViteFallback = String(error).includes(`Unexpected token '<', "<!doctype "... is not valid JSON`);
40
+ if (is404ViteFallback) {
41
+ console.error(`[404] Localization strings not found at ${filePath}`);
42
+ } else {
43
+ console.error(`Error fetching localization strings at ${filePath}`, error);
44
+ }
45
+ } else {
46
+ console.error(error);
47
+ }
48
+ return {};
49
+ }
50
+ if (locale === defaultLocale) {
51
+ return {};
52
+ }
53
+ return await fetchBundle(defaultLocale, path);
54
+ };
55
+ const getElementLocale = (element) => {
56
+ const lang = getElementAttribute(element, "lang", globalThis.navigator?.language || defaultLocale);
57
+ return { lang, t9nLocale: normalizeLocale(lang) };
58
+ };
59
+ const normalizeLocale = (lang) => {
60
+ const [rawLanguageCode, regionCode] = lang.split("-");
61
+ const languageCode = rawLanguageCode.toLowerCase();
62
+ let normalizedLocale = languageCode;
63
+ if (regionCode) {
64
+ normalizedLocale = `${languageCode}-${regionCode.toUpperCase()}`;
65
+ }
66
+ normalizedLocale = localeEquivalencies[normalizedLocale] ?? normalizedLocale;
67
+ if (supportedLocales.has(normalizedLocale)) {
68
+ return normalizedLocale;
69
+ }
70
+ if (regionCode) {
71
+ return normalizeLocale(languageCode);
72
+ }
73
+ return defaultLocale;
74
+ };
75
+ const startLocaleObserver = (element, getAssetsPath, onUpdated, assetName) => {
76
+ let result = void 0;
77
+ const callback = () => updateComponentLocaleState(element, getAssetsPath(), assetName).then((newResult) => {
78
+ if (result?.lang !== newResult.lang || result.t9nLocale !== newResult.t9nLocale || result.t9nStrings !== newResult.t9nStrings) {
79
+ onUpdated(newResult);
80
+ }
81
+ result = newResult;
82
+ }).catch(console.error);
83
+ queueMicrotask(callback);
84
+ return observeAncestorsMutation(element, ["lang"], callback);
85
+ };
86
+ const updateComponentLocaleState = async (element, assetsPath, assetName = element.localName.split("-").slice(1).join("-")) => {
87
+ const { lang, t9nLocale } = getElementLocale(element);
88
+ const t9nAssetsPath = `${assetsPath}/${assetName}/t9n`;
89
+ const prefix = `messages.`;
90
+ const t9nStrings = (
91
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
92
+ assetName === null ? {} : await fetchT9nStringsBundle(t9nLocale, t9nAssetsPath, prefix)
93
+ );
94
+ return { lang, t9nLocale, t9nStrings };
95
+ };
96
+ export {
97
+ defaultLocale,
98
+ fetchT9nStringsBundle,
99
+ getElementLocale,
100
+ normalizeLocale,
101
+ startLocaleObserver,
102
+ supportedLocales
103
+ };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const isNotUndefined = (item) => item !== void 0;
4
+ const isNotNull = (item) => item !== null;
5
+ const isNotNil = (x) => x != null;
6
+ exports.isNotNil = isNotNil;
7
+ exports.isNotNull = isNotNull;
8
+ exports.isNotUndefined = isNotUndefined;
@@ -0,0 +1,24 @@
1
+ import { nil } from '../type';
2
+ /**
3
+ * Safeguard to ensure that an item is not undefined.
4
+ * @param item The item to check.
5
+ * @returns Returns true if the item is not undefined.
6
+ * @example
7
+ * ```ts
8
+ * const arr = [1, undefined, 3];
9
+ * const result = arr.filter(isNotUndefined);
10
+ * ```
11
+ */
12
+ export declare const isNotUndefined: <T>(item: T | undefined) => item is T;
13
+ /**
14
+ * Safeguard to ensure that an item is not null.
15
+ * @param item The item to check.
16
+ * @returns Returns true if the item is not null.
17
+ */
18
+ export declare const isNotNull: <T>(item: T | null) => item is T;
19
+ /**
20
+ * Safeguard to ensure that an item is not null.
21
+ * @param item The item to check.
22
+ * @returns Returns true if the item is not and not undefined and not null.
23
+ */
24
+ export declare const isNotNil: <T>(x: nil | T) => x is T;