mazey 5.2.0 → 5.3.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/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,118 @@ 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
+ * Result returned by {@link resolveThemePreference}.
3431
+ *
3432
+ * @category Browser Information
3433
+ */
3434
+ interface ThemePreferenceResult {
3435
+ /** Selected user-facing preference. */
3436
+ preference: ThemePreference;
3437
+ /** Effective light or dark theme. */
3438
+ resolvedTheme: ResolvedTheme;
3439
+ /** Human-readable name of the selected preference. */
3440
+ displayName: ThemePreferenceDisplayName;
3441
+ /** Highest-priority source that supplied the selected preference. */
3442
+ source: ThemePreferenceSource;
3443
+ }
3444
+ /**
3445
+ * Resolve a website theme preference without mutating the DOM or storage.
3446
+ *
3447
+ * Resolution uses four priority levels: a `light` or `dark` URL query
3448
+ * override, a stored `system`/`light`/`dark` preference, the current system
3449
+ * color scheme, and finally a configured `light` or `dark` fallback. The
3450
+ * default query parameter is `theme`, and the storage key must be supplied by
3451
+ * the consuming project. Query `system` and all unsupported values are ignored.
3452
+ *
3453
+ * The result separates the selected preference from the effective theme and
3454
+ * includes its display name (`System`, `Light`, or `Dark`) and source. A stored
3455
+ * `system` preference retains `source: "storage"` while its effective theme is
3456
+ * resolved from the current media query or fallback.
3457
+ *
3458
+ * Usage:
3459
+ *
3460
+ * ```javascript
3461
+ * import { resolveThemePreference } from "mazey";
3462
+ *
3463
+ * const theme = resolveThemePreference({
3464
+ * storageKey: "MY_WEBSITE_THEME",
3465
+ * });
3466
+ * console.log(theme.preference);
3467
+ * console.log(theme.resolvedTheme);
3468
+ * console.log(theme.displayName);
3469
+ * console.log(theme.source);
3470
+ * ```
3471
+ *
3472
+ * Possible output:
3473
+ *
3474
+ * ```text
3475
+ * system
3476
+ * dark
3477
+ * System
3478
+ * system
3479
+ * ```
3480
+ *
3481
+ * URL override:
3482
+ *
3483
+ * ```javascript
3484
+ * const theme = resolveThemePreference({
3485
+ * storageKey: "MY_WEBSITE_THEME",
3486
+ * url: "https://example.com/?theme=light",
3487
+ * });
3488
+ * ```
3489
+ *
3490
+ * @param options Project storage key and optional URL, storage, media-query, and fallback inputs.
3491
+ * @returns The selected preference, effective theme, display name, and preference source.
3492
+ * @throws {TypeError} If required options or explicitly supplied option values are invalid.
3493
+ * @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.
3494
+ * @category Browser Information
3495
+ */
3496
+ declare function resolveThemePreference(options: ResolveThemePreferenceOptions): ThemePreferenceResult;
3497
+
3242
3498
  /**
3243
3499
  * Create a configurable wrapper around the global `console`.
3244
3500
  *
@@ -3309,5 +3565,5 @@ declare const mazeyCon: Console;
3309
3565
  */
3310
3566
  declare const timeCon: Console;
3311
3567
 
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 };
3568
+ 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, sha256Hex, sleep, throttle, timeCon, toJavaScriptGlobalName, truncateZHString, unsanitize, unsanitizeInput, updateQueryParam, waitTime, windowLoaded, zAxiosIsValidRes };
3569
+ export type { GitHubRepositoryDetails, IsSafePWAEnvOptions, ResolveThemePreferenceOptions, ResolvedTheme, ThemePreference, ThemePreferenceDisplayName, ThemePreferenceResult, ThemePreferenceSource };