mazey 5.2.0 → 5.3.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/lib/index.d.ts CHANGED
@@ -1350,6 +1350,150 @@ declare function sha256Hex(input: string | BufferSource): Promise<string>;
1350
1350
  * @category Util
1351
1351
  */
1352
1352
  declare function isValidDate(value: unknown): boolean;
1353
+ /**
1354
+ * Check whether a date is today in the runtime's local timezone.
1355
+ *
1356
+ * Usage:
1357
+ *
1358
+ * ```javascript
1359
+ * import { isToday } from "mazey";
1360
+ *
1361
+ * const ret = isToday(new Date());
1362
+ * console.log(ret);
1363
+ * ```
1364
+ *
1365
+ * Output:
1366
+ *
1367
+ * ```text
1368
+ * true
1369
+ * ```
1370
+ *
1371
+ * @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
1372
+ * @returns Whether the value has the current local year, month, and day. Invalid input returns `false`.
1373
+ * @remarks Hours, minutes, seconds, and milliseconds are ignored. Results depend on the runtime's local timezone.
1374
+ * @category Util
1375
+ */
1376
+ declare function isToday(date: MazeyDate): boolean;
1377
+ /**
1378
+ * Check whether a date is in the current local calendar year.
1379
+ *
1380
+ * Usage:
1381
+ *
1382
+ * ```javascript
1383
+ * import { isThisYear } from "mazey";
1384
+ *
1385
+ * const ret = isThisYear(new Date());
1386
+ * console.log(ret);
1387
+ * ```
1388
+ *
1389
+ * Output:
1390
+ *
1391
+ * ```text
1392
+ * true
1393
+ * ```
1394
+ *
1395
+ * @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
1396
+ * @returns Whether the value has the current local year. Invalid input returns `false`.
1397
+ * @remarks Results depend on the runtime's local timezone.
1398
+ * @category Util
1399
+ */
1400
+ declare function isThisYear(date: MazeyDate): boolean;
1401
+ /**
1402
+ * Check whether a date is in the current local calendar month and year.
1403
+ *
1404
+ * Usage:
1405
+ *
1406
+ * ```javascript
1407
+ * import { isThisMonth } from "mazey";
1408
+ *
1409
+ * const ret = isThisMonth(new Date());
1410
+ * console.log(ret);
1411
+ * ```
1412
+ *
1413
+ * Output:
1414
+ *
1415
+ * ```text
1416
+ * true
1417
+ * ```
1418
+ *
1419
+ * @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
1420
+ * @returns Whether the value has the current local year and month. Invalid input returns `false`.
1421
+ * @remarks Results depend on the runtime's local timezone.
1422
+ * @category Util
1423
+ */
1424
+ declare function isThisMonth(date: MazeyDate): boolean;
1425
+ /**
1426
+ * Check whether a date is in the current Monday-first local calendar week.
1427
+ *
1428
+ * Usage:
1429
+ *
1430
+ * ```javascript
1431
+ * import { isThisWeek } from "mazey";
1432
+ *
1433
+ * const ret = isThisWeek(new Date());
1434
+ * console.log(ret);
1435
+ * ```
1436
+ *
1437
+ * Output:
1438
+ *
1439
+ * ```text
1440
+ * true
1441
+ * ```
1442
+ *
1443
+ * @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
1444
+ * @returns Whether the value is in the current local week. Invalid input returns `false`.
1445
+ * @remarks The week begins on Monday and ends before the following Monday. Boundaries use local time and a half-open range.
1446
+ * @category Util
1447
+ */
1448
+ declare function isThisWeek(date: MazeyDate): boolean;
1449
+ /**
1450
+ * Check whether a date is within the current local clock hour.
1451
+ *
1452
+ * Usage:
1453
+ *
1454
+ * ```javascript
1455
+ * import { isThisHour } from "mazey";
1456
+ *
1457
+ * const ret = isThisHour(new Date());
1458
+ * console.log(ret);
1459
+ * ```
1460
+ *
1461
+ * Output:
1462
+ *
1463
+ * ```text
1464
+ * true
1465
+ * ```
1466
+ *
1467
+ * @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
1468
+ * @returns Whether the value has the current local year, month, day, and hour. Invalid input returns `false`.
1469
+ * @remarks Minutes, seconds, and milliseconds are ignored. Results depend on the runtime's local timezone.
1470
+ * @category Util
1471
+ */
1472
+ declare function isThisHour(date: MazeyDate): boolean;
1473
+ /**
1474
+ * Format the absolute distance from a date to now in concise English words.
1475
+ *
1476
+ * Usage:
1477
+ *
1478
+ * ```javascript
1479
+ * import { formatDistanceToNow } from "mazey";
1480
+ *
1481
+ * const ret = formatDistanceToNow(new Date(Date.now() - 60 * 60 * 1000));
1482
+ * console.log(ret);
1483
+ * ```
1484
+ *
1485
+ * Output:
1486
+ *
1487
+ * ```text
1488
+ * about 1 hour
1489
+ * ```
1490
+ *
1491
+ * @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
1492
+ * @returns The absolute approximate distance phrase, or an empty string for invalid input.
1493
+ * @remarks Past and future dates use the same wording without `ago` or `in`. Months and years use fixed approximate durations of 30 and 365 days.
1494
+ * @category Util
1495
+ */
1496
+ declare function formatDistanceToNow(date: MazeyDate): string;
1353
1497
  /**
1354
1498
  * Return the formatted date string in the given format.
1355
1499
  *
@@ -3239,6 +3383,165 @@ declare function genBrowserAttrs(prefix?: string, separator?: string): string[];
3239
3383
  */
3240
3384
  declare function isSupportWebp(): Promise<boolean>;
3241
3385
 
3386
+ /**
3387
+ * A user-selectable color-theme preference.
3388
+ *
3389
+ * @category Browser Information
3390
+ */
3391
+ type ThemePreference = "system" | "light" | "dark";
3392
+ /**
3393
+ * A concrete color theme that can be applied to a page.
3394
+ *
3395
+ * @category Browser Information
3396
+ */
3397
+ type ResolvedTheme = "light" | "dark";
3398
+ /**
3399
+ * Human-readable names for supported theme preferences.
3400
+ *
3401
+ * @category Browser Information
3402
+ */
3403
+ type ThemePreferenceDisplayName = "System" | "Light" | "Dark";
3404
+ /**
3405
+ * The highest-priority source that selected a theme preference.
3406
+ *
3407
+ * @category Browser Information
3408
+ */
3409
+ type ThemePreferenceSource = "query" | "storage" | "system" | "fallback";
3410
+ /**
3411
+ * Options for {@link resolveThemePreference}.
3412
+ *
3413
+ * @category Browser Information
3414
+ */
3415
+ interface ResolveThemePreferenceOptions {
3416
+ /** Project-specific local-storage key, such as `MAZEY_THEME`. */
3417
+ storageKey: string;
3418
+ /** URL query parameter name. Defaults to `theme`. */
3419
+ queryParam?: string;
3420
+ /** URL inspected for a query override. Defaults to `window.location.href` when available. */
3421
+ url?: string | URL;
3422
+ /** Storage used to read a persisted preference. Defaults to `window.localStorage` when available. */
3423
+ storage?: Pick<Storage, "getItem"> | null;
3424
+ /** Media-query implementation. Defaults to `window.matchMedia` when available. */
3425
+ matchMedia?: (query: string) => Pick<MediaQueryList, "matches">;
3426
+ /** Theme used when the system preference is unavailable. Defaults to `light`. */
3427
+ fallback?: ResolvedTheme;
3428
+ }
3429
+ /**
3430
+ * Options for {@link setThemePreference}.
3431
+ *
3432
+ * @category Browser Information
3433
+ */
3434
+ interface SetThemePreferenceOptions {
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
+ }
3442
+ /**
3443
+ * Result returned by {@link resolveThemePreference}.
3444
+ *
3445
+ * @category Browser Information
3446
+ */
3447
+ interface ThemePreferenceResult {
3448
+ /** Selected user-facing preference. */
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
+ }
3457
+ /**
3458
+ * Resolve a website theme preference without mutating the DOM or storage.
3459
+ *
3460
+ * Resolution uses four priority levels: a `light` or `dark` URL query
3461
+ * override, a stored `system`/`light`/`dark` preference, the current system
3462
+ * color scheme, and finally a configured `light` or `dark` fallback. The
3463
+ * default query parameter is `theme`, and the storage key must be supplied by
3464
+ * the consuming project. Query `system` and all unsupported values are ignored.
3465
+ *
3466
+ * The result separates the selected preference from the effective theme and
3467
+ * includes its display name (`System`, `Light`, or `Dark`) and source. A stored
3468
+ * `system` preference retains `source: "storage"` while its effective theme is
3469
+ * resolved from the current media query or fallback.
3470
+ *
3471
+ * Usage:
3472
+ *
3473
+ * ```javascript
3474
+ * import { resolveThemePreference } from "mazey";
3475
+ *
3476
+ * const theme = resolveThemePreference({
3477
+ * storageKey: "MY_WEBSITE_THEME",
3478
+ * });
3479
+ * console.log(theme.preference);
3480
+ * console.log(theme.resolvedTheme);
3481
+ * console.log(theme.displayName);
3482
+ * console.log(theme.source);
3483
+ * ```
3484
+ *
3485
+ * Possible output:
3486
+ *
3487
+ * ```text
3488
+ * system
3489
+ * dark
3490
+ * System
3491
+ * system
3492
+ * ```
3493
+ *
3494
+ * URL override:
3495
+ *
3496
+ * ```javascript
3497
+ * const theme = resolveThemePreference({
3498
+ * storageKey: "MY_WEBSITE_THEME",
3499
+ * url: "https://example.com/?theme=light",
3500
+ * });
3501
+ * ```
3502
+ *
3503
+ * @param options Project storage key and optional URL, storage, media-query, and fallback inputs.
3504
+ * @returns The selected preference, effective theme, display name, and preference source.
3505
+ * @throws {TypeError} If required options or explicitly supplied option values are invalid.
3506
+ * @remarks Safe during SSR and resilient to unavailable or throwing browser APIs. This function does not write storage, mutate the DOM, or add listeners. System resolution reads the current media-query result rather than cached browser information.
3507
+ * @category Browser Information
3508
+ */
3509
+ declare function resolveThemePreference(options: ResolveThemePreferenceOptions): ThemePreferenceResult;
3510
+ /**
3511
+ * Persist a supported website theme preference.
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.
3518
+ *
3519
+ * Usage:
3520
+ *
3521
+ * ```javascript
3522
+ * import { setThemePreference } from "mazey";
3523
+ *
3524
+ * const stored = setThemePreference({
3525
+ * storageKey: "MY_WEBSITE_THEME",
3526
+ * preference: "dark",
3527
+ * });
3528
+ * console.log(stored);
3529
+ * ```
3530
+ *
3531
+ * Output:
3532
+ *
3533
+ * ```text
3534
+ * true
3535
+ * ```
3536
+ *
3537
+ * @param options Project storage key, supported preference, and optional storage implementation.
3538
+ * @returns `true` when the preference was written, or `false` when storage is unavailable or rejects the write.
3539
+ * @throws {TypeError} If the options, storage key, preference, or explicitly supplied storage implementation is invalid.
3540
+ * @remarks This function writes only the selected preference. Applying a resolved theme and updating controls remain the consuming project's responsibility.
3541
+ * @category Browser Information
3542
+ */
3543
+ declare function setThemePreference(options: SetThemePreferenceOptions): boolean;
3544
+
3242
3545
  /**
3243
3546
  * Create a configurable wrapper around the global `console`.
3244
3547
  *
@@ -3309,5 +3612,5 @@ declare const mazeyCon: Console;
3309
3612
  */
3310
3613
  declare const timeCon: Console;
3311
3614
 
3312
- 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, 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, 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, sanitizeInput, setClass, setCookie, setImgSizeBySrc, setImgWidHeiBySrc, setLocalStorage, setSessionStorage, sha256Hex, sleep, throttle, timeCon, toJavaScriptGlobalName, truncateZHString, unsanitize, unsanitizeInput, updateQueryParam, waitTime, windowLoaded, zAxiosIsValidRes };
3313
- export type { GitHubRepositoryDetails, IsSafePWAEnvOptions };
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, ResolveThemePreferenceOptions, ResolvedTheme, SetThemePreferenceOptions, ThemePreference, ThemePreferenceDisplayName, ThemePreferenceResult, ThemePreferenceSource };