mazey 5.3.1 → 5.4.1
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/README.md +65 -20
- package/lib/index.cjs.js +2 -2
- package/lib/index.d.ts +152 -106
- package/lib/index.esm.js +2 -2
- package/lib/mazey.min.js +2 -2
- package/lib/typing.d.ts +13 -1
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -127,6 +127,18 @@ interface BrowserInfo {
|
|
|
127
127
|
colorScheme: string;
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
+
/**
|
|
131
|
+
* A resolved preference value and its human-readable label.
|
|
132
|
+
*
|
|
133
|
+
* @category Browser Information
|
|
134
|
+
*/
|
|
135
|
+
interface PreferenceResult<T extends string = string> {
|
|
136
|
+
/** Machine-readable value used by application logic. */
|
|
137
|
+
value: T;
|
|
138
|
+
/** Human-readable label suitable for display. */
|
|
139
|
+
label: string;
|
|
140
|
+
}
|
|
141
|
+
|
|
130
142
|
interface DefineListeners {
|
|
131
143
|
[key: string]: any;
|
|
132
144
|
}
|
|
@@ -3396,151 +3408,185 @@ type ThemePreference = "system" | "light" | "dark";
|
|
|
3396
3408
|
*/
|
|
3397
3409
|
type ResolvedTheme = "light" | "dark";
|
|
3398
3410
|
/**
|
|
3399
|
-
*
|
|
3411
|
+
* Resolve the current website theme.
|
|
3400
3412
|
*
|
|
3401
|
-
*
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
*
|
|
3413
|
+
* Resolution checks the fixed `theme` URL query, the supplied local-storage
|
|
3414
|
+
* key, the current `prefers-color-scheme` media query, and finally the fixed
|
|
3415
|
+
* `light` fallback. Query values accept only `light` and `dark`. Storage also
|
|
3416
|
+
* accepts `system`, which resolves to a concrete value while retaining the
|
|
3417
|
+
* `System` label. A valid query preference is written under the supplied
|
|
3418
|
+
* storage key when browser storage is available; resolution still succeeds
|
|
3419
|
+
* when the write fails.
|
|
3406
3420
|
*
|
|
3407
|
-
*
|
|
3408
|
-
*/
|
|
3409
|
-
type ThemePreferenceSource = "query" | "storage" | "system" | "fallback";
|
|
3410
|
-
/**
|
|
3411
|
-
* Options for {@link resolveThemePreference}.
|
|
3421
|
+
* Resolution matrix:
|
|
3412
3422
|
*
|
|
3413
|
-
*
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3425
|
-
|
|
3426
|
-
/** Theme used when the system preference is unavailable. Defaults to `light`. */
|
|
3427
|
-
fallback?: ResolvedTheme;
|
|
3428
|
-
}
|
|
3429
|
-
/**
|
|
3430
|
-
* Options for {@link setThemePreference}.
|
|
3423
|
+
* | Query | Storage | System dark | Result |
|
|
3424
|
+
* | ------- | -------- | ----------: | ------------------------------------- |
|
|
3425
|
+
* | `dark` | `light` | false | `{ value: "dark", label: "Dark" }` |
|
|
3426
|
+
* | `light` | `dark` | true | `{ value: "light", label: "Light" }` |
|
|
3427
|
+
* | invalid | `dark` | false | `{ value: "dark", label: "Dark" }` |
|
|
3428
|
+
* | missing | `light` | true | `{ value: "light", label: "Light" }` |
|
|
3429
|
+
* | missing | `system` | true | `{ value: "dark", label: "System" }` |
|
|
3430
|
+
* | missing | `system` | false | `{ value: "light", label: "System" }` |
|
|
3431
|
+
* | missing | invalid | true | `{ value: "dark", label: "System" }` |
|
|
3432
|
+
* | missing | missing | false | `{ value: "light", label: "System" }` |
|
|
3433
|
+
* | missing | missing | unavailable | `{ value: "light", label: "Light" }` |
|
|
3434
|
+
*
|
|
3435
|
+
* Usage:
|
|
3431
3436
|
*
|
|
3437
|
+
* ```ts
|
|
3438
|
+
* import {
|
|
3439
|
+
* resolveThemePreference,
|
|
3440
|
+
* setThemePreference,
|
|
3441
|
+
* } from "mazey";
|
|
3442
|
+
*
|
|
3443
|
+
* const theme = resolveThemePreference(
|
|
3444
|
+
* "MY_WEBSITE_THEME"
|
|
3445
|
+
* );
|
|
3446
|
+
*
|
|
3447
|
+
* console.log(theme);
|
|
3448
|
+
*
|
|
3449
|
+
* setThemePreference(
|
|
3450
|
+
* "MY_WEBSITE_THEME",
|
|
3451
|
+
* "system"
|
|
3452
|
+
* );
|
|
3453
|
+
* ```
|
|
3454
|
+
*
|
|
3455
|
+
* Possible output:
|
|
3456
|
+
*
|
|
3457
|
+
* ```text
|
|
3458
|
+
* {
|
|
3459
|
+
* value: "dark",
|
|
3460
|
+
* label: "System"
|
|
3461
|
+
* }
|
|
3462
|
+
* ```
|
|
3463
|
+
*
|
|
3464
|
+
* @param storageKey Project-specific local-storage key.
|
|
3465
|
+
* @returns The concrete `light` or `dark` value and the label of the preference that selected it.
|
|
3466
|
+
* @throws {TypeError} If `storageKey` is not a non-empty string.
|
|
3467
|
+
* @remarks Safe during SSR and resilient to unavailable or throwing browser APIs. A valid URL preference is written to storage when possible; other resolution paths remain read-only. The function never mutates the DOM or adds listeners.
|
|
3432
3468
|
* @category Browser Information
|
|
3433
3469
|
*/
|
|
3434
|
-
|
|
3435
|
-
/** Project-specific local-storage key, such as `MAZEY_THEME`. */
|
|
3436
|
-
storageKey: string;
|
|
3437
|
-
/** Supported preference to persist. */
|
|
3438
|
-
preference: ThemePreference;
|
|
3439
|
-
/** Storage used to persist the preference. Defaults to `window.localStorage` when available. */
|
|
3440
|
-
storage?: Pick<Storage, "setItem"> | null;
|
|
3441
|
-
}
|
|
3470
|
+
declare function resolveThemePreference(storageKey: string): PreferenceResult<ResolvedTheme>;
|
|
3442
3471
|
/**
|
|
3443
|
-
*
|
|
3472
|
+
* Persist a website theme preference.
|
|
3473
|
+
*
|
|
3474
|
+
* The exact lowercase value `system`, `light`, or `dark` is written under the
|
|
3475
|
+
* project-specific key. The function does not resolve or apply the theme.
|
|
3476
|
+
*
|
|
3477
|
+
* Usage:
|
|
3478
|
+
*
|
|
3479
|
+
* ```ts
|
|
3480
|
+
* import { setThemePreference } from "mazey";
|
|
3481
|
+
*
|
|
3482
|
+
* const stored = setThemePreference(
|
|
3483
|
+
* "MY_WEBSITE_THEME",
|
|
3484
|
+
* "dark"
|
|
3485
|
+
* );
|
|
3486
|
+
*
|
|
3487
|
+
* console.log(stored);
|
|
3488
|
+
* ```
|
|
3489
|
+
*
|
|
3490
|
+
* Output when browser storage is available:
|
|
3491
|
+
*
|
|
3492
|
+
* ```text
|
|
3493
|
+
* true
|
|
3494
|
+
* ```
|
|
3444
3495
|
*
|
|
3496
|
+
* @param storageKey Project-specific local-storage key.
|
|
3497
|
+
* @param value Theme preference to persist.
|
|
3498
|
+
* @returns `true` when storage succeeds, or `false` when storage is unavailable or rejects the write.
|
|
3499
|
+
* @throws {TypeError} If `storageKey` is empty or `value` is not `system`, `light`, or `dark`.
|
|
3500
|
+
* @remarks Safe during SSR. This function writes only the preference and never mutates the DOM, applies a theme, or adds listeners.
|
|
3445
3501
|
* @category Browser Information
|
|
3446
3502
|
*/
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
preference: ThemePreference;
|
|
3450
|
-
/** Effective light or dark theme. */
|
|
3451
|
-
resolvedTheme: ResolvedTheme;
|
|
3452
|
-
/** Human-readable name of the selected preference. */
|
|
3453
|
-
displayName: ThemePreferenceDisplayName;
|
|
3454
|
-
/** Highest-priority source that supplied the selected preference. */
|
|
3455
|
-
source: ThemePreferenceSource;
|
|
3456
|
-
}
|
|
3503
|
+
declare function setThemePreference(storageKey: string, value: ThemePreference): boolean;
|
|
3504
|
+
|
|
3457
3505
|
/**
|
|
3458
|
-
* Resolve
|
|
3506
|
+
* Resolve the current website language.
|
|
3459
3507
|
*
|
|
3460
|
-
* Resolution
|
|
3461
|
-
*
|
|
3462
|
-
*
|
|
3463
|
-
*
|
|
3464
|
-
* the consuming project. Query `system` and all unsupported values are ignored.
|
|
3508
|
+
* Resolution checks the fixed `lang` URL query, the supplied local-storage
|
|
3509
|
+
* key, `navigator.language`, and finally the fixed `en` fallback. Each value
|
|
3510
|
+
* is trimmed, treats `_` as `-`, and is canonicalized as a language tag.
|
|
3511
|
+
* `navigator.languages` is intentionally ignored.
|
|
3465
3512
|
*
|
|
3466
|
-
* The
|
|
3467
|
-
*
|
|
3468
|
-
*
|
|
3469
|
-
* resolved from the current media query or fallback.
|
|
3513
|
+
* The label is generated at runtime with `Intl.DisplayNames` in the resolved
|
|
3514
|
+
* language when available. Runtime wording may vary; the canonical language
|
|
3515
|
+
* value is used as the label when display-name generation is unavailable.
|
|
3470
3516
|
*
|
|
3471
3517
|
* Usage:
|
|
3472
3518
|
*
|
|
3473
|
-
* ```
|
|
3474
|
-
* import {
|
|
3519
|
+
* ```ts
|
|
3520
|
+
* import {
|
|
3521
|
+
* resolveLanguagePreference,
|
|
3522
|
+
* setLanguagePreference,
|
|
3523
|
+
* } from "mazey";
|
|
3475
3524
|
*
|
|
3476
|
-
* const
|
|
3477
|
-
*
|
|
3478
|
-
*
|
|
3479
|
-
*
|
|
3480
|
-
* console.log(theme.resolvedTheme);
|
|
3481
|
-
* console.log(theme.displayName);
|
|
3482
|
-
* console.log(theme.source);
|
|
3483
|
-
* ```
|
|
3525
|
+
* const language =
|
|
3526
|
+
* resolveLanguagePreference(
|
|
3527
|
+
* "MY_WEBSITE_LANGUAGE"
|
|
3528
|
+
* );
|
|
3484
3529
|
*
|
|
3485
|
-
*
|
|
3530
|
+
* console.log(language);
|
|
3486
3531
|
*
|
|
3487
|
-
*
|
|
3488
|
-
*
|
|
3489
|
-
*
|
|
3490
|
-
*
|
|
3491
|
-
* system
|
|
3532
|
+
* setLanguagePreference(
|
|
3533
|
+
* "MY_WEBSITE_LANGUAGE",
|
|
3534
|
+
* "ja-JP"
|
|
3535
|
+
* );
|
|
3492
3536
|
* ```
|
|
3493
3537
|
*
|
|
3494
|
-
*
|
|
3538
|
+
* Possible output:
|
|
3495
3539
|
*
|
|
3496
|
-
* ```
|
|
3497
|
-
*
|
|
3498
|
-
*
|
|
3499
|
-
*
|
|
3500
|
-
* }
|
|
3540
|
+
* ```text
|
|
3541
|
+
* {
|
|
3542
|
+
* value: "ja-JP",
|
|
3543
|
+
* label: "日本語(日本)"
|
|
3544
|
+
* }
|
|
3501
3545
|
* ```
|
|
3502
3546
|
*
|
|
3503
|
-
* @param
|
|
3504
|
-
* @returns The
|
|
3505
|
-
* @throws {TypeError} If
|
|
3506
|
-
* @remarks Safe during SSR and resilient to unavailable or throwing browser APIs. This function
|
|
3547
|
+
* @param storageKey Project-specific local-storage key.
|
|
3548
|
+
* @returns The canonical current language and a runtime-generated display label.
|
|
3549
|
+
* @throws {TypeError} If `storageKey` is not a non-empty string.
|
|
3550
|
+
* @remarks Safe during SSR and resilient to unavailable or throwing browser and `Intl` APIs. This function reads preferences only and never writes storage, mutates the DOM, loads translations, redirects, or adds listeners.
|
|
3507
3551
|
* @category Browser Information
|
|
3508
3552
|
*/
|
|
3509
|
-
declare function
|
|
3553
|
+
declare function resolveLanguagePreference(storageKey: string): PreferenceResult<string>;
|
|
3510
3554
|
/**
|
|
3511
|
-
*
|
|
3512
|
-
*
|
|
3513
|
-
* The preference is written as the exact lowercase value `system`, `light`,
|
|
3514
|
-
* or `dark` under a project-specific storage key. The function returns
|
|
3515
|
-
* `false` instead of throwing when storage is unavailable, including during
|
|
3516
|
-
* SSR and in privacy-restricted browser environments. It does not resolve or
|
|
3517
|
-
* apply the theme and does not mutate the DOM.
|
|
3555
|
+
* Canonicalize and persist a website language.
|
|
3518
3556
|
*
|
|
3519
3557
|
* Usage:
|
|
3520
3558
|
*
|
|
3521
|
-
* ```
|
|
3522
|
-
* import {
|
|
3559
|
+
* ```ts
|
|
3560
|
+
* import { setLanguagePreference } from "mazey";
|
|
3561
|
+
*
|
|
3562
|
+
* const stored = setLanguagePreference(
|
|
3563
|
+
* "MY_WEBSITE_LANGUAGE",
|
|
3564
|
+
* "ZH_cn"
|
|
3565
|
+
* );
|
|
3523
3566
|
*
|
|
3524
|
-
* const stored = setThemePreference({
|
|
3525
|
-
* storageKey: "MY_WEBSITE_THEME",
|
|
3526
|
-
* preference: "dark",
|
|
3527
|
-
* });
|
|
3528
3567
|
* console.log(stored);
|
|
3529
3568
|
* ```
|
|
3530
3569
|
*
|
|
3531
|
-
* Output:
|
|
3570
|
+
* Output when browser storage is available:
|
|
3532
3571
|
*
|
|
3533
3572
|
* ```text
|
|
3534
3573
|
* true
|
|
3535
3574
|
* ```
|
|
3536
3575
|
*
|
|
3537
|
-
*
|
|
3538
|
-
*
|
|
3539
|
-
*
|
|
3540
|
-
*
|
|
3576
|
+
* The stored value is canonicalized to:
|
|
3577
|
+
*
|
|
3578
|
+
* ```text
|
|
3579
|
+
* zh-CN
|
|
3580
|
+
* ```
|
|
3581
|
+
*
|
|
3582
|
+
* @param storageKey Project-specific local-storage key.
|
|
3583
|
+
* @param language Language tag to canonicalize and persist.
|
|
3584
|
+
* @returns `true` when storage succeeds, or `false` when storage is unavailable or rejects the write.
|
|
3585
|
+
* @throws {TypeError} If `storageKey` is empty or `language` is empty or malformed.
|
|
3586
|
+
* @remarks Safe during SSR. This function writes only the canonical language and never mutates the DOM, applies translations, redirects, or adds listeners.
|
|
3541
3587
|
* @category Browser Information
|
|
3542
3588
|
*/
|
|
3543
|
-
declare function
|
|
3589
|
+
declare function setLanguagePreference(storageKey: string, language: string): boolean;
|
|
3544
3590
|
|
|
3545
3591
|
/**
|
|
3546
3592
|
* Create a configurable wrapper around the global `console`.
|
|
@@ -3612,5 +3658,5 @@ declare const mazeyCon: Console;
|
|
|
3612
3658
|
*/
|
|
3613
3659
|
declare const timeCon: Console;
|
|
3614
3660
|
|
|
3615
|
-
export { addClass, addEvent, addStyle, calLongestCommonSubsequence, calLongestCommonSubstring, camelCase2Underscore, camelCaseToKebabCase, cancelBubble, clearHTML, clearHtml, convert10To26, convertCamelToKebab, convertCamelToUnder, convertHttpToHttps, convertKebabToCamel, convertObjectToQuery, convertToHtmlBreaks, convertUnderToCamel, cutCHSString, cutZHString, debounce, deepCopy, deepCopyObject, deepFreeze, delCookie, doFn, extractElementText, fireEvent, floatFixed, floatToPercent, formatDate, formatDistanceToNow, formatDurationFromMs, genBrowserAttrs, genCustomConsole, genHashCode, genRndNumString, genStyleString, genUniqueNumString, generateCalendarVersion, generateRndNum, generateUniqueNum, getAllQueryParams, getBrowserInfo, getCLS, getCookie, getCurrentVersion, getDateDifference, getDefineListeners, getDomain, getFCP, getFID, getFP, getFileSize, getFriendlyInterval, getHashQueryParam, getLCP, getLocalStorage, getPageMeta, getPerformance, getQueryParam, getScriptQueryParam, getSessionStorage, getTTFB, getUrlFileType, getUrlHost, getUrlParam, getUrlPath, hasClass, inRate, invokeEvent, invokeFn, isArray, isBool, isBoolean, isBrowser, isFunction, isHit, isJSONString, isJsonString, isNonEmptyArray, isNonEmptyObject, isNumber, isPureObject, isSafePWAEnv, isStandalonePWA, isString, isSupportWebp, isSupportedEntryType, isThisHour, isThisMonth, isThisWeek, isThisYear, isToday, isUdfOrNul, isValidCssSelector, isValidData, isValidDate, isValidEmail, isValidHttpUrl, isValidPhoneNumber, isValidUrl, loadCSS, loadImage, loadScript, loadScriptIfUndefined, longestComSubsequence, longestComSubstring, mNow, mTrim, mazeyCon, newLine, onURLChange, parseGitHubRepository, parseJsonSafe, removeClass, removeCookie, removeEvent, removeHTML, removeHtml, repeatUntilConditionMet, replaceHttp, resolveThemePreference, sanitizeInput, setClass, setCookie, setImgSizeBySrc, setImgWidHeiBySrc, setLocalStorage, setSessionStorage, setThemePreference, sha256Hex, sleep, throttle, timeCon, toJavaScriptGlobalName, truncateZHString, unsanitize, unsanitizeInput, updateQueryParam, waitTime, windowLoaded, zAxiosIsValidRes };
|
|
3616
|
-
export type { GitHubRepositoryDetails, IsSafePWAEnvOptions,
|
|
3661
|
+
export { addClass, addEvent, addStyle, calLongestCommonSubsequence, calLongestCommonSubstring, camelCase2Underscore, camelCaseToKebabCase, cancelBubble, clearHTML, clearHtml, convert10To26, convertCamelToKebab, convertCamelToUnder, convertHttpToHttps, convertKebabToCamel, convertObjectToQuery, convertToHtmlBreaks, convertUnderToCamel, cutCHSString, cutZHString, debounce, deepCopy, deepCopyObject, deepFreeze, delCookie, doFn, extractElementText, fireEvent, floatFixed, floatToPercent, formatDate, formatDistanceToNow, formatDurationFromMs, genBrowserAttrs, genCustomConsole, genHashCode, genRndNumString, genStyleString, genUniqueNumString, generateCalendarVersion, generateRndNum, generateUniqueNum, getAllQueryParams, getBrowserInfo, getCLS, getCookie, getCurrentVersion, getDateDifference, getDefineListeners, getDomain, getFCP, getFID, getFP, getFileSize, getFriendlyInterval, getHashQueryParam, getLCP, getLocalStorage, getPageMeta, getPerformance, getQueryParam, getScriptQueryParam, getSessionStorage, getTTFB, getUrlFileType, getUrlHost, getUrlParam, getUrlPath, hasClass, inRate, invokeEvent, invokeFn, isArray, isBool, isBoolean, isBrowser, isFunction, isHit, isJSONString, isJsonString, isNonEmptyArray, isNonEmptyObject, isNumber, isPureObject, isSafePWAEnv, isStandalonePWA, isString, isSupportWebp, isSupportedEntryType, isThisHour, isThisMonth, isThisWeek, isThisYear, isToday, isUdfOrNul, isValidCssSelector, isValidData, isValidDate, isValidEmail, isValidHttpUrl, isValidPhoneNumber, isValidUrl, loadCSS, loadImage, loadScript, loadScriptIfUndefined, longestComSubsequence, longestComSubstring, mNow, mTrim, mazeyCon, newLine, onURLChange, parseGitHubRepository, parseJsonSafe, removeClass, removeCookie, removeEvent, removeHTML, removeHtml, repeatUntilConditionMet, replaceHttp, resolveLanguagePreference, resolveThemePreference, sanitizeInput, setClass, setCookie, setImgSizeBySrc, setImgWidHeiBySrc, setLanguagePreference, setLocalStorage, setSessionStorage, setThemePreference, sha256Hex, sleep, throttle, timeCon, toJavaScriptGlobalName, truncateZHString, unsanitize, unsanitizeInput, updateQueryParam, waitTime, windowLoaded, zAxiosIsValidRes };
|
|
3662
|
+
export type { GitHubRepositoryDetails, IsSafePWAEnvOptions, PreferenceResult, ResolvedTheme, ThemePreference };
|