@choksheak/ts-utils 0.2.8 → 0.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/README.md +2 -0
- package/{arrayBuffer.js → arrayBuffer.cjs} +0 -1
- package/arrayBuffer.d.mts +10 -0
- package/arrayBuffer.min.cjs +2 -0
- package/arrayBuffer.min.cjs.map +1 -0
- package/arrayBuffer.min.mjs +2 -0
- package/{arrayBuffer.js.map → arrayBuffer.min.mjs.map} +1 -1
- package/arrayBuffer.mjs +14 -0
- package/{assert.js → assert.cjs} +3 -2
- package/assert.d.mts +7 -0
- package/assert.d.ts +1 -1
- package/assert.min.cjs +2 -0
- package/assert.min.cjs.map +1 -0
- package/assert.min.mjs +2 -0
- package/assert.min.mjs.map +1 -0
- package/assert.mjs +11 -0
- package/{average.js → average.cjs} +0 -1
- package/average.d.mts +7 -0
- package/average.min.cjs +2 -0
- package/{average.js.map → average.min.cjs.map} +1 -1
- package/average.min.mjs +2 -0
- package/average.min.mjs.map +1 -0
- package/average.mjs +20 -0
- package/base64Url.cjs +40 -0
- package/base64Url.d.mts +10 -0
- package/base64Url.d.ts +8 -0
- package/base64Url.min.cjs +2 -0
- package/base64Url.min.cjs.map +1 -0
- package/base64Url.min.mjs +2 -0
- package/base64Url.min.mjs.map +1 -0
- package/base64Url.mjs +14 -0
- package/{dateTimeStr.js → dateTimeStr.cjs} +0 -1
- package/dateTimeStr.d.mts +85 -0
- package/dateTimeStr.min.cjs +2 -0
- package/dateTimeStr.min.cjs.map +1 -0
- package/dateTimeStr.min.mjs +2 -0
- package/{dateTimeStr.js.map → dateTimeStr.min.mjs.map} +1 -1
- package/dateTimeStr.mjs +83 -0
- package/{duration.js → duration.cjs} +9 -11
- package/duration.d.mts +78 -0
- package/duration.min.cjs +2 -0
- package/duration.min.cjs.map +1 -0
- package/duration.min.mjs +2 -0
- package/duration.min.mjs.map +1 -0
- package/duration.mjs +154 -0
- package/{isEmpty.js → isEmpty.cjs} +0 -1
- package/isEmpty.d.mts +6 -0
- package/isEmpty.min.cjs +2 -0
- package/isEmpty.min.cjs.map +1 -0
- package/isEmpty.min.mjs +2 -0
- package/{isEmpty.js.map → isEmpty.min.mjs.map} +1 -1
- package/isEmpty.mjs +22 -0
- package/kvStore.cjs +333 -0
- package/kvStore.d.mts +97 -0
- package/kvStore.min.cjs +2 -0
- package/kvStore.min.cjs.map +1 -0
- package/kvStore.min.mjs +2 -0
- package/kvStore.min.mjs.map +1 -0
- package/kvStore.mjs +300 -0
- package/{localStorageCache.js → localStorageCache.cjs} +5 -7
- package/localStorageCache.d.mts +57 -0
- package/localStorageCache.min.cjs +2 -0
- package/localStorageCache.min.cjs.map +1 -0
- package/localStorageCache.min.mjs +2 -0
- package/localStorageCache.min.mjs.map +1 -0
- package/localStorageCache.mjs +92 -0
- package/{logging.js → logging.cjs} +0 -1
- package/logging.d.mts +4 -0
- package/logging.min.cjs +2 -0
- package/logging.min.cjs.map +1 -0
- package/logging.min.mjs +2 -0
- package/{logging.js.map → logging.min.mjs.map} +1 -1
- package/logging.mjs +21 -0
- package/{nonEmpty.js → nonEmpty.cjs} +0 -1
- package/nonEmpty.d.mts +10 -0
- package/nonEmpty.min.cjs +2 -0
- package/{nonEmpty.js.map → nonEmpty.min.cjs.map} +1 -1
- package/nonEmpty.min.mjs +2 -0
- package/nonEmpty.min.mjs.map +1 -0
- package/nonEmpty.mjs +30 -0
- package/{nonNil.js → nonNil.cjs} +0 -1
- package/{src/nonNil.ts → nonNil.d.mts} +3 -6
- package/nonNil.min.cjs +2 -0
- package/nonNil.min.cjs.map +1 -0
- package/nonNil.min.mjs +2 -0
- package/{nonNil.js.map → nonNil.min.mjs.map} +1 -1
- package/nonNil.mjs +10 -0
- package/package.json +414 -24
- package/{round.js → round.cjs} +0 -1
- package/{src/round.ts → round.d.mts} +4 -8
- package/round.min.cjs +2 -0
- package/round.min.cjs.map +1 -0
- package/round.min.mjs +2 -0
- package/{round.js.map → round.min.mjs.map} +1 -1
- package/round.mjs +12 -0
- package/{safeBtoa.js → safeBtoa.cjs} +0 -1
- package/safeBtoa.d.mts +6 -0
- package/safeBtoa.min.cjs +2 -0
- package/safeBtoa.min.cjs.map +1 -0
- package/safeBtoa.min.mjs +2 -0
- package/{safeBtoa.js.map → safeBtoa.min.mjs.map} +1 -1
- package/safeBtoa.mjs +9 -0
- package/{safeParseFloat.js → safeParseFloat.cjs} +0 -1
- package/safeParseFloat.d.mts +6 -0
- package/safeParseFloat.min.cjs +2 -0
- package/safeParseFloat.min.cjs.map +1 -0
- package/safeParseFloat.min.mjs +2 -0
- package/{safeParseFloat.js.map → safeParseFloat.min.mjs.map} +1 -1
- package/safeParseFloat.mjs +8 -0
- package/{safeParseInt.js → safeParseInt.cjs} +0 -1
- package/safeParseInt.d.mts +6 -0
- package/safeParseInt.min.cjs +2 -0
- package/safeParseInt.min.cjs.map +1 -0
- package/safeParseInt.min.mjs +2 -0
- package/{safeParseInt.js.map → safeParseInt.min.mjs.map} +1 -1
- package/safeParseInt.mjs +8 -0
- package/sha256.cjs +35 -0
- package/sha256.d.mts +6 -0
- package/sha256.min.cjs +2 -0
- package/sha256.min.cjs.map +1 -0
- package/sha256.min.mjs +2 -0
- package/{sha256.js.map → sha256.min.mjs.map} +1 -1
- package/sha256.mjs +10 -0
- package/{sleep.js → sleep.cjs} +0 -1
- package/{src/sleep.ts → sleep.d.mts} +3 -3
- package/sleep.min.cjs +2 -0
- package/sleep.min.cjs.map +1 -0
- package/sleep.min.mjs +2 -0
- package/{sleep.js.map → sleep.min.mjs.map} +1 -1
- package/sleep.mjs +7 -0
- package/{sum.js → sum.cjs} +0 -1
- package/sum.d.mts +7 -0
- package/sum.min.cjs +2 -0
- package/sum.min.cjs.map +1 -0
- package/sum.min.mjs +2 -0
- package/{sum.js.map → sum.min.mjs.map} +1 -1
- package/sum.mjs +13 -0
- package/{timeConstants.js → timeConstants.cjs} +0 -1
- package/timeConstants.d.mts +20 -0
- package/timeConstants.min.cjs +2 -0
- package/timeConstants.min.cjs.map +1 -0
- package/timeConstants.min.mjs +2 -0
- package/{timeConstants.js.map → timeConstants.min.mjs.map} +1 -1
- package/timeConstants.mjs +31 -0
- package/{timer.js → timer.cjs} +6 -6
- package/timer.d.mts +13 -0
- package/timer.min.cjs +2 -0
- package/timer.min.cjs.map +1 -0
- package/timer.min.mjs +2 -0
- package/timer.min.mjs.map +1 -0
- package/timer.mjs +161 -0
- package/assert.js.map +0 -1
- package/docs/.nojekyll +0 -1
- package/docs/assets/hierarchy.js +0 -1
- package/docs/assets/highlight.css +0 -22
- package/docs/assets/icons.js +0 -18
- package/docs/assets/icons.svg +0 -1
- package/docs/assets/main.js +0 -60
- package/docs/assets/navigation.js +0 -1
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1640
- package/docs/classes/kvStore.KVStore.html +0 -26
- package/docs/classes/kvStore.KVStoreField.html +0 -7
- package/docs/classes/timer.Timer.html +0 -8
- package/docs/functions/arrayBuffer.arrayBufferToBase64.html +0 -2
- package/docs/functions/arrayBuffer.arrayBufferToHex.html +0 -2
- package/docs/functions/assert.assert.html +0 -3
- package/docs/functions/average.average.html +0 -3
- package/docs/functions/dateTimeStr.getDisplayDateTime.html +0 -3
- package/docs/functions/dateTimeStr.getLongMonthNameOneIndexed.html +0 -4
- package/docs/functions/dateTimeStr.getLongMonthNameZeroIndexed.html +0 -4
- package/docs/functions/dateTimeStr.getShortMonthNameOneIndexed.html +0 -4
- package/docs/functions/dateTimeStr.getShortMonthNameZeroIndexed.html +0 -4
- package/docs/functions/dateTimeStr.hhMm.html +0 -4
- package/docs/functions/dateTimeStr.hhMmSs.html +0 -4
- package/docs/functions/dateTimeStr.hhMmSsMs.html +0 -5
- package/docs/functions/dateTimeStr.toDate.html +0 -4
- package/docs/functions/dateTimeStr.tzShort.html +0 -4
- package/docs/functions/dateTimeStr.yyyyMm.html +0 -4
- package/docs/functions/dateTimeStr.yyyyMmDd.html +0 -4
- package/docs/functions/duration.durationOrMsToMs.html +0 -2
- package/docs/functions/duration.durationToMs.html +0 -2
- package/docs/functions/duration.elapsed.html +0 -2
- package/docs/functions/duration.formatDuration.html +0 -4
- package/docs/functions/duration.msToDuration.html +0 -5
- package/docs/functions/duration.readableDuration.html +0 -4
- package/docs/functions/isEmpty.isEmpty.html +0 -2
- package/docs/functions/kvStore.kvStoreItem.html +0 -2
- package/docs/functions/localStorageCache.storeItem.html +0 -15
- package/docs/functions/logging.capLength.html +0 -1
- package/docs/functions/logging.stringify.html +0 -1
- package/docs/functions/nonEmpty.nonEmpty.html +0 -5
- package/docs/functions/nonNil.nonNil.html +0 -5
- package/docs/functions/round.round.html +0 -5
- package/docs/functions/round.roundS.html +0 -3
- package/docs/functions/safeBtoa.safeBtoa.html +0 -2
- package/docs/functions/safeParseFloat.safeParseFloat.html +0 -2
- package/docs/functions/safeParseInt.safeParseInt.html +0 -2
- package/docs/functions/sha256.sha256.html +0 -2
- package/docs/functions/sleep.sleep.html +0 -3
- package/docs/functions/sum.sum.html +0 -3
- package/docs/functions/timer.timer.html +0 -2
- package/docs/hierarchy.html +0 -1
- package/docs/index.html +0 -4
- package/docs/modules/arrayBuffer.html +0 -1
- package/docs/modules/assert.html +0 -1
- package/docs/modules/average.html +0 -1
- package/docs/modules/dateTimeStr.html +0 -1
- package/docs/modules/duration.html +0 -1
- package/docs/modules/isEmpty.html +0 -1
- package/docs/modules/kvStore.html +0 -1
- package/docs/modules/localStorageCache.html +0 -1
- package/docs/modules/logging.html +0 -1
- package/docs/modules/nonEmpty.html +0 -1
- package/docs/modules/nonNil.html +0 -1
- package/docs/modules/round.html +0 -1
- package/docs/modules/safeBtoa.html +0 -1
- package/docs/modules/safeParseFloat.html +0 -1
- package/docs/modules/safeParseInt.html +0 -1
- package/docs/modules/sha256.html +0 -1
- package/docs/modules/sleep.html +0 -1
- package/docs/modules/sum.html +0 -1
- package/docs/modules/timeConstants.html +0 -1
- package/docs/modules/timer.html +0 -1
- package/docs/modules.html +0 -1
- package/docs/types/dateTimeStr.AnyDateTime.html +0 -1
- package/docs/types/duration.Duration.html +0 -6
- package/docs/types/duration.DurationStyle.html +0 -6
- package/docs/types/duration.DurationSuffixMap.html +0 -6
- package/docs/types/duration.DurationSuffixType.html +0 -1
- package/docs/types/duration.DurationType.html +0 -2
- package/docs/types/localStorageCache.StoredItem.html +0 -4
- package/docs/variables/duration.DURATION_STYLE_SUFFIX_MAP.html +0 -1
- package/docs/variables/duration.DURATION_TYPE_SEQUENCE.html +0 -2
- package/docs/variables/kvStore.DEFAULT_EXPIRY_DELTA_MS.html +0 -2
- package/docs/variables/kvStore.GC_INTERVAL_MS.html +0 -2
- package/docs/variables/kvStore.MILLIS_PER_DAY.html +0 -2
- package/docs/variables/kvStore.kvStore.html +0 -3
- package/docs/variables/timeConstants.HOURS_PER_DAY.html +0 -1
- package/docs/variables/timeConstants.HOURS_PER_WEEK.html +0 -1
- package/docs/variables/timeConstants.MINUTES_PER_DAY.html +0 -1
- package/docs/variables/timeConstants.MINUTES_PER_HOUR.html +0 -1
- package/docs/variables/timeConstants.MINUTES_PER_WEEK.html +0 -1
- package/docs/variables/timeConstants.MS_PER_DAY.html +0 -1
- package/docs/variables/timeConstants.MS_PER_HOUR.html +0 -1
- package/docs/variables/timeConstants.MS_PER_MINUTE.html +0 -1
- package/docs/variables/timeConstants.MS_PER_SECOND.html +0 -3
- package/docs/variables/timeConstants.MS_PER_WEEK.html +0 -1
- package/docs/variables/timeConstants.SECONDS_PER_DAY.html +0 -1
- package/docs/variables/timeConstants.SECONDS_PER_HOUR.html +0 -1
- package/docs/variables/timeConstants.SECONDS_PER_MINUTE.html +0 -1
- package/docs/variables/timeConstants.SECONDS_PER_WEEK.html +0 -1
- package/duration.js.map +0 -1
- package/kvStore.js +0 -383
- package/kvStore.js.map +0 -1
- package/localStorageCache.js.map +0 -1
- package/sha256.js +0 -58
- package/src/arrayBuffer.ts +0 -29
- package/src/assert.ts +0 -12
- package/src/average.ts +0 -13
- package/src/dateTimeStr.ts +0 -184
- package/src/duration.ts +0 -260
- package/src/isEmpty.ts +0 -32
- package/src/kvStore.ts +0 -444
- package/src/localStorageCache.ts +0 -121
- package/src/logging.ts +0 -26
- package/src/nonEmpty.ts +0 -18
- package/src/safeBtoa.ts +0 -15
- package/src/safeParseFloat.ts +0 -10
- package/src/safeParseInt.ts +0 -10
- package/src/sha256.ts +0 -13
- package/src/sum.ts +0 -17
- package/src/timeConstants.ts +0 -22
- package/src/timer.ts +0 -33
- package/timer.js.map +0 -1
package/duration.d.mts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bunch of miscellaneous constants and utility functions related to handling
|
|
3
|
+
* date and time durations.
|
|
4
|
+
*
|
|
5
|
+
* Note that month and year do not have fixed durations, and hence are excluded
|
|
6
|
+
* from this file. Weeks have fixed durations, but are excluded because we
|
|
7
|
+
* use days as the max duration supported.
|
|
8
|
+
*/
|
|
9
|
+
type Duration = {
|
|
10
|
+
days?: number;
|
|
11
|
+
hours?: number;
|
|
12
|
+
minutes?: number;
|
|
13
|
+
seconds?: number;
|
|
14
|
+
milliseconds?: number;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* One of: days, hours, minutes, seconds, milliseconds
|
|
18
|
+
*/
|
|
19
|
+
type DurationType = keyof Duration;
|
|
20
|
+
/**
|
|
21
|
+
* Order in which the duration type appears in the duration string.
|
|
22
|
+
*/
|
|
23
|
+
declare const DURATION_TYPE_SEQUENCE: DurationType[];
|
|
24
|
+
/**
|
|
25
|
+
* Follows the same format as Intl.DurationFormat.prototype.format().
|
|
26
|
+
*
|
|
27
|
+
* Short: 1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns
|
|
28
|
+
* Long: 1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds,
|
|
29
|
+
* 7 milliseconds, 8 microseconds, 9 nanoseconds
|
|
30
|
+
* Narrow: 1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns
|
|
31
|
+
*/
|
|
32
|
+
type DurationStyle = "short" | "long" | "narrow";
|
|
33
|
+
type DurationSuffixMap = {
|
|
34
|
+
short: string;
|
|
35
|
+
shorts: string;
|
|
36
|
+
long: string;
|
|
37
|
+
longs: string;
|
|
38
|
+
narrow: string;
|
|
39
|
+
};
|
|
40
|
+
type DurationSuffixType = keyof DurationSuffixMap;
|
|
41
|
+
declare const DURATION_STYLE_SUFFIX_MAP: Record<DurationType, DurationSuffixMap>;
|
|
42
|
+
/**
|
|
43
|
+
* Convert a milliseconds duration into a Duration object. If the given ms is
|
|
44
|
+
* zero, then return an object with a single field of zero with duration type
|
|
45
|
+
* of durationTypeForZero.
|
|
46
|
+
*
|
|
47
|
+
* @param durationTypeForZero Defaults to 'milliseconds'
|
|
48
|
+
*/
|
|
49
|
+
declare function msToDuration(ms: number, durationTypeForZero?: DurationType): Duration;
|
|
50
|
+
/**
|
|
51
|
+
* Returns the number of milliseconds for the given duration.
|
|
52
|
+
*/
|
|
53
|
+
declare function durationToMs(duration: Duration): number;
|
|
54
|
+
/**
|
|
55
|
+
* Convenience function to return a duration given an ms or Duration.
|
|
56
|
+
*/
|
|
57
|
+
declare function durationOrMsToMs(duration: number | Duration): number;
|
|
58
|
+
/**
|
|
59
|
+
* Format a given Duration object into a string. If the object has no fields,
|
|
60
|
+
* then returns an empty string.
|
|
61
|
+
*
|
|
62
|
+
* @param style Defaults to 'short'
|
|
63
|
+
*/
|
|
64
|
+
declare function formatDuration(duration: Duration, style?: DurationStyle): string;
|
|
65
|
+
/**
|
|
66
|
+
* Convert a millisecond duration into a human-readable duration string.
|
|
67
|
+
*
|
|
68
|
+
* @param options.durationTypeForZero - Defaults to 'milliseconds'
|
|
69
|
+
* @param options.style - Defaults to 'short'
|
|
70
|
+
*/
|
|
71
|
+
declare function readableDuration(ms: number, options?: {
|
|
72
|
+
durationTypeForZero?: DurationType;
|
|
73
|
+
style?: DurationStyle;
|
|
74
|
+
}): string;
|
|
75
|
+
/** A shortened duration string useful for logging timings. */
|
|
76
|
+
declare function elapsed(ms: number): string;
|
|
77
|
+
|
|
78
|
+
export { DURATION_STYLE_SUFFIX_MAP, DURATION_TYPE_SEQUENCE, type Duration, type DurationStyle, type DurationSuffixMap, type DurationSuffixType, type DurationType, durationOrMsToMs, durationToMs, elapsed, formatDuration, msToDuration, readableDuration };
|
package/duration.min.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var c=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var P=(o,n)=>{for(var t in n)c(o,t,{get:n[t],enumerable:!0})},T=(o,n,t,e)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of y(n))!g.call(o,r)&&r!==t&&c(o,r,{get:()=>n[r],enumerable:!(e=R(n,r))||e.enumerable});return o};var U=o=>T(c({},"__esModule",{value:!0}),o);var H={};P(H,{DURATION_STYLE_SUFFIX_MAP:()=>D,DURATION_TYPE_SEQUENCE:()=>S,durationOrMsToMs:()=>w,durationToMs:()=>x,elapsed:()=>C,formatDuration:()=>M,msToDuration:()=>m,readableDuration:()=>d});module.exports=U(H);var S=["days","hours","minutes","seconds","milliseconds"],D={days:{short:"day",shorts:"days",long:"day",longs:"days",narrow:"d"},hours:{short:"hr",shorts:"hrs",long:"hour",longs:"hours",narrow:"h"},minutes:{short:"min",shorts:"mins",long:"minute",longs:"minutes",narrow:"m"},seconds:{short:"sec",shorts:"secs",long:"second",longs:"seconds",narrow:"s"},milliseconds:{short:"ms",shorts:"ms",long:"millisecond",longs:"milliseconds",narrow:"ms"}};function b(o){return o=="short"?"shorts":o==="long"?"longs":o}function I(o){return o==="narrow"?"":" "}function A(o){return o==="narrow"?" ":", "}function m(o,n){if(o===0)return n=n??"milliseconds",{[n]:0};let t={};for(let e=0;e<1;e++){let r=Math.floor(o/1e3),u=o-r*1e3;if(u>0&&(t.milliseconds=u),r===0)break;let i=Math.floor(r/60);if(r-=i*60,r>0&&(t.seconds=r),i===0)break;let s=Math.floor(i/60);if(i-=s*60,i>0&&(t.minutes=i),s===0)break;let a=Math.floor(s/24);s-=a*24,s>0&&(t.hours=s),a>0&&(t.days=a)}return t}function x(o){let n=(o.days??0)*864e5,t=(o.hours??0)*36e5,e=(o.minutes??0)*6e4,r=(o.seconds??0)*1e3,u=o.milliseconds??0;return n+t+e+r+u}function w(o){return typeof o=="number"?o:x(o)}function M(o,n){n=n??"short";let t=b(n),e=I(n),r=[];for(let i of S){let s=o[i];if(s===void 0)continue;let a=D[i],h=s===1?a[n]:a[t];r.push(s+e+h)}let u=A(n);return r.join(u)}function d(o,n){let t=m(o,n?.durationTypeForZero);return M(t,n?.style)}function C(o){return o>6e4?d(o):o>100?`${(o/1e3).toFixed(3)}s`:o+"ms"}0&&(module.exports={DURATION_STYLE_SUFFIX_MAP,DURATION_TYPE_SEQUENCE,durationOrMsToMs,durationToMs,elapsed,formatDuration,msToDuration,readableDuration});
|
|
2
|
+
//# sourceMappingURL=duration.min.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/duration.ts"],"sourcesContent":["/**\n * Bunch of miscellaneous constants and utility functions related to handling\n * date and time durations.\n *\n * Note that month and year do not have fixed durations, and hence are excluded\n * from this file. Weeks have fixed durations, but are excluded because we\n * use days as the max duration supported.\n */\n\nimport {\n HOURS_PER_DAY,\n MINUTES_PER_HOUR,\n MS_PER_DAY,\n MS_PER_HOUR,\n MS_PER_MINUTE,\n MS_PER_SECOND,\n SECONDS_PER_MINUTE,\n} from \"./timeConstants\";\n\nexport type Duration = {\n days?: number;\n hours?: number;\n minutes?: number;\n seconds?: number;\n milliseconds?: number;\n};\n\n/**\n * One of: days, hours, minutes, seconds, milliseconds\n */\nexport type DurationType = keyof Duration;\n\n/**\n * Order in which the duration type appears in the duration string.\n */\nexport const DURATION_TYPE_SEQUENCE: DurationType[] = [\n \"days\",\n \"hours\",\n \"minutes\",\n \"seconds\",\n \"milliseconds\",\n];\n\n/**\n * Follows the same format as Intl.DurationFormat.prototype.format().\n *\n * Short: 1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns\n * Long: 1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds,\n * 7 milliseconds, 8 microseconds, 9 nanoseconds\n * Narrow: 1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns\n */\nexport type DurationStyle = \"short\" | \"long\" | \"narrow\";\n\nexport type DurationSuffixMap = {\n short: string;\n shorts: string;\n long: string;\n longs: string;\n narrow: string;\n};\n\nexport type DurationSuffixType = keyof DurationSuffixMap;\n\nexport const DURATION_STYLE_SUFFIX_MAP: Record<\n DurationType,\n DurationSuffixMap\n> = {\n days: {\n short: \"day\",\n shorts: \"days\",\n long: \"day\",\n longs: \"days\",\n narrow: \"d\",\n },\n hours: {\n short: \"hr\",\n shorts: \"hrs\",\n long: \"hour\",\n longs: \"hours\",\n narrow: \"h\",\n },\n minutes: {\n short: \"min\",\n shorts: \"mins\",\n long: \"minute\",\n longs: \"minutes\",\n narrow: \"m\",\n },\n seconds: {\n short: \"sec\",\n shorts: \"secs\",\n long: \"second\",\n longs: \"seconds\",\n narrow: \"s\",\n },\n milliseconds: {\n short: \"ms\",\n shorts: \"ms\",\n long: \"millisecond\",\n longs: \"milliseconds\",\n narrow: \"ms\",\n },\n};\n\nfunction getDurationStyleForPlural(style: DurationStyle): DurationSuffixType {\n return style == \"short\" ? \"shorts\" : style === \"long\" ? \"longs\" : style;\n}\n\nfunction getValueAndUnitSeparator(style: DurationStyle): string {\n return style === \"narrow\" ? \"\" : \" \";\n}\n\nfunction getDurationTypeSeparator(style: DurationStyle): string {\n return style === \"narrow\" ? \" \" : \", \";\n}\n\n/**\n * Convert a milliseconds duration into a Duration object. If the given ms is\n * zero, then return an object with a single field of zero with duration type\n * of durationTypeForZero.\n *\n * @param durationTypeForZero Defaults to 'milliseconds'\n */\nexport function msToDuration(\n ms: number,\n durationTypeForZero?: DurationType,\n): Duration {\n if (ms === 0) {\n durationTypeForZero = durationTypeForZero ?? \"milliseconds\";\n return { [durationTypeForZero]: 0 };\n }\n\n const duration: Duration = {};\n\n for (let i = 0; i < 1; i++) {\n let seconds = Math.floor(ms / MS_PER_SECOND);\n const millis = ms - seconds * MS_PER_SECOND;\n\n if (millis > 0) {\n duration[\"milliseconds\"] = millis;\n }\n\n if (seconds === 0) {\n break;\n }\n\n let minutes = Math.floor(seconds / SECONDS_PER_MINUTE);\n seconds -= minutes * SECONDS_PER_MINUTE;\n\n if (seconds > 0) {\n duration[\"seconds\"] = seconds;\n }\n\n if (minutes === 0) {\n break;\n }\n\n let hours = Math.floor(minutes / MINUTES_PER_HOUR);\n minutes -= hours * MINUTES_PER_HOUR;\n\n if (minutes > 0) {\n duration[\"minutes\"] = minutes;\n }\n\n if (hours === 0) {\n break;\n }\n\n const days = Math.floor(hours / HOURS_PER_DAY);\n hours -= days * HOURS_PER_DAY;\n\n if (hours > 0) {\n duration[\"hours\"] = hours;\n }\n\n if (days > 0) {\n duration[\"days\"] = days;\n }\n }\n\n return duration;\n}\n\n/**\n * Returns the number of milliseconds for the given duration.\n */\nexport function durationToMs(duration: Duration): number {\n const daysMs = (duration.days ?? 0) * MS_PER_DAY;\n const hoursMs = (duration.hours ?? 0) * MS_PER_HOUR;\n const minsMs = (duration.minutes ?? 0) * MS_PER_MINUTE;\n const secsMs = (duration.seconds ?? 0) * MS_PER_SECOND;\n const msMs = duration.milliseconds ?? 0;\n\n return daysMs + hoursMs + minsMs + secsMs + msMs;\n}\n\n/**\n * Convenience function to return a duration given an ms or Duration.\n */\nexport function durationOrMsToMs(duration: number | Duration): number {\n return typeof duration === \"number\" ? duration : durationToMs(duration);\n}\n\n/**\n * Format a given Duration object into a string. If the object has no fields,\n * then returns an empty string.\n *\n * @param style Defaults to 'short'\n */\nexport function formatDuration(duration: Duration, style?: DurationStyle) {\n style = style ?? \"short\";\n const stylePlural = getDurationStyleForPlural(style);\n\n const space = getValueAndUnitSeparator(style);\n\n const a: string[] = [];\n\n for (const unit of DURATION_TYPE_SEQUENCE) {\n const value = duration[unit];\n if (value === undefined) continue;\n\n const suffixMap = DURATION_STYLE_SUFFIX_MAP[unit];\n const suffix = value === 1 ? suffixMap[style] : suffixMap[stylePlural];\n a.push(value + space + suffix);\n }\n\n const separator = getDurationTypeSeparator(style);\n return a.join(separator);\n}\n\n/**\n * Convert a millisecond duration into a human-readable duration string.\n *\n * @param options.durationTypeForZero - Defaults to 'milliseconds'\n * @param options.style - Defaults to 'short'\n */\nexport function readableDuration(\n ms: number,\n options?: { durationTypeForZero?: DurationType; style?: DurationStyle },\n): string {\n const duration = msToDuration(ms, options?.durationTypeForZero);\n\n return formatDuration(duration, options?.style);\n}\n\n/** A shortened duration string useful for logging timings. */\nexport function elapsed(ms: number): string {\n // Use long format for 1 minute or over.\n if (ms > MS_PER_MINUTE) {\n return readableDuration(ms);\n }\n\n // Use seconds format for over 100ms.\n if (ms > 100) {\n return `${(ms / 1000).toFixed(3)}s`;\n }\n\n // Use milliseconds format.\n return ms + \"ms\";\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,+BAAAE,EAAA,2BAAAC,EAAA,qBAAAC,EAAA,iBAAAC,EAAA,YAAAC,EAAA,mBAAAC,EAAA,iBAAAC,EAAA,qBAAAC,IAAA,eAAAC,EAAAV,GAmCO,IAAMW,EAAyC,CACpD,OACA,QACA,UACA,UACA,cACF,EAsBaC,EAGT,CACF,KAAM,CACJ,MAAO,MACP,OAAQ,OACR,KAAM,MACN,MAAO,OACP,OAAQ,GACV,EACA,MAAO,CACL,MAAO,KACP,OAAQ,MACR,KAAM,OACN,MAAO,QACP,OAAQ,GACV,EACA,QAAS,CACP,MAAO,MACP,OAAQ,OACR,KAAM,SACN,MAAO,UACP,OAAQ,GACV,EACA,QAAS,CACP,MAAO,MACP,OAAQ,OACR,KAAM,SACN,MAAO,UACP,OAAQ,GACV,EACA,aAAc,CACZ,MAAO,KACP,OAAQ,KACR,KAAM,cACN,MAAO,eACP,OAAQ,IACV,CACF,EAEA,SAASC,EAA0BC,EAA0C,CAC3E,OAAOA,GAAS,QAAU,SAAWA,IAAU,OAAS,QAAUA,CACpE,CAEA,SAASC,EAAyBD,EAA8B,CAC9D,OAAOA,IAAU,SAAW,GAAK,GACnC,CAEA,SAASE,EAAyBF,EAA8B,CAC9D,OAAOA,IAAU,SAAW,IAAM,IACpC,CASO,SAASG,EACdC,EACAC,EACU,CACV,GAAID,IAAO,EACT,OAAAC,EAAsBA,GAAuB,eACtC,CAAE,CAACA,CAAmB,EAAG,CAAE,EAGpC,IAAMC,EAAqB,CAAC,EAE5B,QAASC,EAAI,EAAGA,EAAI,EAAGA,IAAK,CAC1B,IAAIC,EAAU,KAAK,MAAMJ,EAAK,GAAa,EACrCK,EAASL,EAAKI,EAAU,IAM9B,GAJIC,EAAS,IACXH,EAAS,aAAkBG,GAGzBD,IAAY,EACd,MAGF,IAAIE,EAAU,KAAK,MAAMF,EAAU,EAAkB,EAOrD,GANAA,GAAWE,EAAU,GAEjBF,EAAU,IACZF,EAAS,QAAaE,GAGpBE,IAAY,EACd,MAGF,IAAIC,EAAQ,KAAK,MAAMD,EAAU,EAAgB,EAOjD,GANAA,GAAWC,EAAQ,GAEfD,EAAU,IACZJ,EAAS,QAAaI,GAGpBC,IAAU,EACZ,MAGF,IAAMC,EAAO,KAAK,MAAMD,EAAQ,EAAa,EAC7CA,GAASC,EAAO,GAEZD,EAAQ,IACVL,EAAS,MAAWK,GAGlBC,EAAO,IACTN,EAAS,KAAUM,EAEvB,CAEA,OAAON,CACT,CAKO,SAASO,EAAaP,EAA4B,CACvD,IAAMQ,GAAUR,EAAS,MAAQ,GAAK,MAChCS,GAAWT,EAAS,OAAS,GAAK,KAClCU,GAAUV,EAAS,SAAW,GAAK,IACnCW,GAAUX,EAAS,SAAW,GAAK,IACnCY,EAAOZ,EAAS,cAAgB,EAEtC,OAAOQ,EAASC,EAAUC,EAASC,EAASC,CAC9C,CAKO,SAASC,EAAiBb,EAAqC,CACpE,OAAO,OAAOA,GAAa,SAAWA,EAAWO,EAAaP,CAAQ,CACxE,CAQO,SAASc,EAAed,EAAoBN,EAAuB,CACxEA,EAAQA,GAAS,QACjB,IAAMqB,EAActB,EAA0BC,CAAK,EAE7CsB,EAAQrB,EAAyBD,CAAK,EAEtCuB,EAAc,CAAC,EAErB,QAAWC,KAAQ3B,EAAwB,CACzC,IAAM4B,EAAQnB,EAASkB,CAAI,EAC3B,GAAIC,IAAU,OAAW,SAEzB,IAAMC,EAAY5B,EAA0B0B,CAAI,EAC1CG,EAASF,IAAU,EAAIC,EAAU1B,CAAK,EAAI0B,EAAUL,CAAW,EACrEE,EAAE,KAAKE,EAAQH,EAAQK,CAAM,CAC/B,CAEA,IAAMC,EAAY1B,EAAyBF,CAAK,EAChD,OAAOuB,EAAE,KAAKK,CAAS,CACzB,CAQO,SAASC,EACdzB,EACA0B,EACQ,CACR,IAAMxB,EAAWH,EAAaC,EAAI0B,GAAS,mBAAmB,EAE9D,OAAOV,EAAed,EAAUwB,GAAS,KAAK,CAChD,CAGO,SAASC,EAAQ3B,EAAoB,CAE1C,OAAIA,EAAK,IACAyB,EAAiBzB,CAAE,EAIxBA,EAAK,IACA,IAAIA,EAAK,KAAM,QAAQ,CAAC,CAAC,IAI3BA,EAAK,IACd","names":["duration_exports","__export","DURATION_STYLE_SUFFIX_MAP","DURATION_TYPE_SEQUENCE","durationOrMsToMs","durationToMs","elapsed","formatDuration","msToDuration","readableDuration","__toCommonJS","DURATION_TYPE_SEQUENCE","DURATION_STYLE_SUFFIX_MAP","getDurationStyleForPlural","style","getValueAndUnitSeparator","getDurationTypeSeparator","msToDuration","ms","durationTypeForZero","duration","i","seconds","millis","minutes","hours","days","durationToMs","daysMs","hoursMs","minsMs","secsMs","msMs","durationOrMsToMs","formatDuration","stylePlural","space","a","unit","value","suffixMap","suffix","separator","readableDuration","options","elapsed"]}
|
package/duration.min.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var m=["days","hours","minutes","seconds","milliseconds"],x={days:{short:"day",shorts:"days",long:"day",longs:"days",narrow:"d"},hours:{short:"hr",shorts:"hrs",long:"hour",longs:"hours",narrow:"h"},minutes:{short:"min",shorts:"mins",long:"minute",longs:"minutes",narrow:"m"},seconds:{short:"sec",shorts:"secs",long:"second",longs:"seconds",narrow:"s"},milliseconds:{short:"ms",shorts:"ms",long:"millisecond",longs:"milliseconds",narrow:"ms"}};function M(o){return o=="short"?"shorts":o==="long"?"longs":o}function d(o){return o==="narrow"?"":" "}function h(o){return o==="narrow"?" ":", "}function R(o,n){if(o===0)return n=n??"milliseconds",{[n]:0};let t={};for(let i=0;i<1;i++){let r=Math.floor(o/1e3),u=o-r*1e3;if(u>0&&(t.milliseconds=u),r===0)break;let e=Math.floor(r/60);if(r-=e*60,r>0&&(t.seconds=r),e===0)break;let s=Math.floor(e/60);if(e-=s*60,e>0&&(t.minutes=e),s===0)break;let a=Math.floor(s/24);s-=a*24,s>0&&(t.hours=s),a>0&&(t.days=a)}return t}function y(o){let n=(o.days??0)*864e5,t=(o.hours??0)*36e5,i=(o.minutes??0)*6e4,r=(o.seconds??0)*1e3,u=o.milliseconds??0;return n+t+i+r+u}function N(o){return typeof o=="number"?o:y(o)}function g(o,n){n=n??"short";let t=M(n),i=d(n),r=[];for(let e of m){let s=o[e];if(s===void 0)continue;let a=x[e],f=s===1?a[n]:a[t];r.push(s+i+f)}let u=h(n);return r.join(u)}function P(o,n){let t=R(o,n?.durationTypeForZero);return g(t,n?.style)}function O(o){return o>6e4?P(o):o>100?`${(o/1e3).toFixed(3)}s`:o+"ms"}export{x as DURATION_STYLE_SUFFIX_MAP,m as DURATION_TYPE_SEQUENCE,N as durationOrMsToMs,y as durationToMs,O as elapsed,g as formatDuration,R as msToDuration,P as readableDuration};
|
|
2
|
+
//# sourceMappingURL=duration.min.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/duration.ts"],"sourcesContent":["/**\n * Bunch of miscellaneous constants and utility functions related to handling\n * date and time durations.\n *\n * Note that month and year do not have fixed durations, and hence are excluded\n * from this file. Weeks have fixed durations, but are excluded because we\n * use days as the max duration supported.\n */\n\nimport {\n HOURS_PER_DAY,\n MINUTES_PER_HOUR,\n MS_PER_DAY,\n MS_PER_HOUR,\n MS_PER_MINUTE,\n MS_PER_SECOND,\n SECONDS_PER_MINUTE,\n} from \"./timeConstants\";\n\nexport type Duration = {\n days?: number;\n hours?: number;\n minutes?: number;\n seconds?: number;\n milliseconds?: number;\n};\n\n/**\n * One of: days, hours, minutes, seconds, milliseconds\n */\nexport type DurationType = keyof Duration;\n\n/**\n * Order in which the duration type appears in the duration string.\n */\nexport const DURATION_TYPE_SEQUENCE: DurationType[] = [\n \"days\",\n \"hours\",\n \"minutes\",\n \"seconds\",\n \"milliseconds\",\n];\n\n/**\n * Follows the same format as Intl.DurationFormat.prototype.format().\n *\n * Short: 1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns\n * Long: 1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds,\n * 7 milliseconds, 8 microseconds, 9 nanoseconds\n * Narrow: 1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns\n */\nexport type DurationStyle = \"short\" | \"long\" | \"narrow\";\n\nexport type DurationSuffixMap = {\n short: string;\n shorts: string;\n long: string;\n longs: string;\n narrow: string;\n};\n\nexport type DurationSuffixType = keyof DurationSuffixMap;\n\nexport const DURATION_STYLE_SUFFIX_MAP: Record<\n DurationType,\n DurationSuffixMap\n> = {\n days: {\n short: \"day\",\n shorts: \"days\",\n long: \"day\",\n longs: \"days\",\n narrow: \"d\",\n },\n hours: {\n short: \"hr\",\n shorts: \"hrs\",\n long: \"hour\",\n longs: \"hours\",\n narrow: \"h\",\n },\n minutes: {\n short: \"min\",\n shorts: \"mins\",\n long: \"minute\",\n longs: \"minutes\",\n narrow: \"m\",\n },\n seconds: {\n short: \"sec\",\n shorts: \"secs\",\n long: \"second\",\n longs: \"seconds\",\n narrow: \"s\",\n },\n milliseconds: {\n short: \"ms\",\n shorts: \"ms\",\n long: \"millisecond\",\n longs: \"milliseconds\",\n narrow: \"ms\",\n },\n};\n\nfunction getDurationStyleForPlural(style: DurationStyle): DurationSuffixType {\n return style == \"short\" ? \"shorts\" : style === \"long\" ? \"longs\" : style;\n}\n\nfunction getValueAndUnitSeparator(style: DurationStyle): string {\n return style === \"narrow\" ? \"\" : \" \";\n}\n\nfunction getDurationTypeSeparator(style: DurationStyle): string {\n return style === \"narrow\" ? \" \" : \", \";\n}\n\n/**\n * Convert a milliseconds duration into a Duration object. If the given ms is\n * zero, then return an object with a single field of zero with duration type\n * of durationTypeForZero.\n *\n * @param durationTypeForZero Defaults to 'milliseconds'\n */\nexport function msToDuration(\n ms: number,\n durationTypeForZero?: DurationType,\n): Duration {\n if (ms === 0) {\n durationTypeForZero = durationTypeForZero ?? \"milliseconds\";\n return { [durationTypeForZero]: 0 };\n }\n\n const duration: Duration = {};\n\n for (let i = 0; i < 1; i++) {\n let seconds = Math.floor(ms / MS_PER_SECOND);\n const millis = ms - seconds * MS_PER_SECOND;\n\n if (millis > 0) {\n duration[\"milliseconds\"] = millis;\n }\n\n if (seconds === 0) {\n break;\n }\n\n let minutes = Math.floor(seconds / SECONDS_PER_MINUTE);\n seconds -= minutes * SECONDS_PER_MINUTE;\n\n if (seconds > 0) {\n duration[\"seconds\"] = seconds;\n }\n\n if (minutes === 0) {\n break;\n }\n\n let hours = Math.floor(minutes / MINUTES_PER_HOUR);\n minutes -= hours * MINUTES_PER_HOUR;\n\n if (minutes > 0) {\n duration[\"minutes\"] = minutes;\n }\n\n if (hours === 0) {\n break;\n }\n\n const days = Math.floor(hours / HOURS_PER_DAY);\n hours -= days * HOURS_PER_DAY;\n\n if (hours > 0) {\n duration[\"hours\"] = hours;\n }\n\n if (days > 0) {\n duration[\"days\"] = days;\n }\n }\n\n return duration;\n}\n\n/**\n * Returns the number of milliseconds for the given duration.\n */\nexport function durationToMs(duration: Duration): number {\n const daysMs = (duration.days ?? 0) * MS_PER_DAY;\n const hoursMs = (duration.hours ?? 0) * MS_PER_HOUR;\n const minsMs = (duration.minutes ?? 0) * MS_PER_MINUTE;\n const secsMs = (duration.seconds ?? 0) * MS_PER_SECOND;\n const msMs = duration.milliseconds ?? 0;\n\n return daysMs + hoursMs + minsMs + secsMs + msMs;\n}\n\n/**\n * Convenience function to return a duration given an ms or Duration.\n */\nexport function durationOrMsToMs(duration: number | Duration): number {\n return typeof duration === \"number\" ? duration : durationToMs(duration);\n}\n\n/**\n * Format a given Duration object into a string. If the object has no fields,\n * then returns an empty string.\n *\n * @param style Defaults to 'short'\n */\nexport function formatDuration(duration: Duration, style?: DurationStyle) {\n style = style ?? \"short\";\n const stylePlural = getDurationStyleForPlural(style);\n\n const space = getValueAndUnitSeparator(style);\n\n const a: string[] = [];\n\n for (const unit of DURATION_TYPE_SEQUENCE) {\n const value = duration[unit];\n if (value === undefined) continue;\n\n const suffixMap = DURATION_STYLE_SUFFIX_MAP[unit];\n const suffix = value === 1 ? suffixMap[style] : suffixMap[stylePlural];\n a.push(value + space + suffix);\n }\n\n const separator = getDurationTypeSeparator(style);\n return a.join(separator);\n}\n\n/**\n * Convert a millisecond duration into a human-readable duration string.\n *\n * @param options.durationTypeForZero - Defaults to 'milliseconds'\n * @param options.style - Defaults to 'short'\n */\nexport function readableDuration(\n ms: number,\n options?: { durationTypeForZero?: DurationType; style?: DurationStyle },\n): string {\n const duration = msToDuration(ms, options?.durationTypeForZero);\n\n return formatDuration(duration, options?.style);\n}\n\n/** A shortened duration string useful for logging timings. */\nexport function elapsed(ms: number): string {\n // Use long format for 1 minute or over.\n if (ms > MS_PER_MINUTE) {\n return readableDuration(ms);\n }\n\n // Use seconds format for over 100ms.\n if (ms > 100) {\n return `${(ms / 1000).toFixed(3)}s`;\n }\n\n // Use milliseconds format.\n return ms + \"ms\";\n}\n"],"mappings":"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","names":["DURATION_TYPE_SEQUENCE","DURATION_STYLE_SUFFIX_MAP","getDurationStyleForPlural","style","getValueAndUnitSeparator","getDurationTypeSeparator","msToDuration","ms","durationTypeForZero","duration","seconds","millis","minutes","hours","days","durationToMs","daysMs","hoursMs","minsMs","secsMs","msMs","durationOrMsToMs","formatDuration","stylePlural","space","a","unit","value","suffixMap","suffix","separator","readableDuration","options","elapsed"]}
|
package/duration.mjs
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// src/timeConstants.ts
|
|
2
|
+
var MS_PER_SECOND = 1e3;
|
|
3
|
+
var MS_PER_MINUTE = 6e4;
|
|
4
|
+
var MS_PER_HOUR = 36e5;
|
|
5
|
+
var MS_PER_DAY = 864e5;
|
|
6
|
+
var SECONDS_PER_MINUTE = 60;
|
|
7
|
+
var MINUTES_PER_HOUR = 60;
|
|
8
|
+
var HOURS_PER_DAY = 24;
|
|
9
|
+
|
|
10
|
+
// src/duration.ts
|
|
11
|
+
var DURATION_TYPE_SEQUENCE = [
|
|
12
|
+
"days",
|
|
13
|
+
"hours",
|
|
14
|
+
"minutes",
|
|
15
|
+
"seconds",
|
|
16
|
+
"milliseconds"
|
|
17
|
+
];
|
|
18
|
+
var DURATION_STYLE_SUFFIX_MAP = {
|
|
19
|
+
days: {
|
|
20
|
+
short: "day",
|
|
21
|
+
shorts: "days",
|
|
22
|
+
long: "day",
|
|
23
|
+
longs: "days",
|
|
24
|
+
narrow: "d"
|
|
25
|
+
},
|
|
26
|
+
hours: {
|
|
27
|
+
short: "hr",
|
|
28
|
+
shorts: "hrs",
|
|
29
|
+
long: "hour",
|
|
30
|
+
longs: "hours",
|
|
31
|
+
narrow: "h"
|
|
32
|
+
},
|
|
33
|
+
minutes: {
|
|
34
|
+
short: "min",
|
|
35
|
+
shorts: "mins",
|
|
36
|
+
long: "minute",
|
|
37
|
+
longs: "minutes",
|
|
38
|
+
narrow: "m"
|
|
39
|
+
},
|
|
40
|
+
seconds: {
|
|
41
|
+
short: "sec",
|
|
42
|
+
shorts: "secs",
|
|
43
|
+
long: "second",
|
|
44
|
+
longs: "seconds",
|
|
45
|
+
narrow: "s"
|
|
46
|
+
},
|
|
47
|
+
milliseconds: {
|
|
48
|
+
short: "ms",
|
|
49
|
+
shorts: "ms",
|
|
50
|
+
long: "millisecond",
|
|
51
|
+
longs: "milliseconds",
|
|
52
|
+
narrow: "ms"
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
function getDurationStyleForPlural(style) {
|
|
56
|
+
return style == "short" ? "shorts" : style === "long" ? "longs" : style;
|
|
57
|
+
}
|
|
58
|
+
function getValueAndUnitSeparator(style) {
|
|
59
|
+
return style === "narrow" ? "" : " ";
|
|
60
|
+
}
|
|
61
|
+
function getDurationTypeSeparator(style) {
|
|
62
|
+
return style === "narrow" ? " " : ", ";
|
|
63
|
+
}
|
|
64
|
+
function msToDuration(ms, durationTypeForZero) {
|
|
65
|
+
if (ms === 0) {
|
|
66
|
+
durationTypeForZero = durationTypeForZero ?? "milliseconds";
|
|
67
|
+
return { [durationTypeForZero]: 0 };
|
|
68
|
+
}
|
|
69
|
+
const duration = {};
|
|
70
|
+
for (let i = 0; i < 1; i++) {
|
|
71
|
+
let seconds = Math.floor(ms / MS_PER_SECOND);
|
|
72
|
+
const millis = ms - seconds * MS_PER_SECOND;
|
|
73
|
+
if (millis > 0) {
|
|
74
|
+
duration["milliseconds"] = millis;
|
|
75
|
+
}
|
|
76
|
+
if (seconds === 0) {
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
let minutes = Math.floor(seconds / SECONDS_PER_MINUTE);
|
|
80
|
+
seconds -= minutes * SECONDS_PER_MINUTE;
|
|
81
|
+
if (seconds > 0) {
|
|
82
|
+
duration["seconds"] = seconds;
|
|
83
|
+
}
|
|
84
|
+
if (minutes === 0) {
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
let hours = Math.floor(minutes / MINUTES_PER_HOUR);
|
|
88
|
+
minutes -= hours * MINUTES_PER_HOUR;
|
|
89
|
+
if (minutes > 0) {
|
|
90
|
+
duration["minutes"] = minutes;
|
|
91
|
+
}
|
|
92
|
+
if (hours === 0) {
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
const days = Math.floor(hours / HOURS_PER_DAY);
|
|
96
|
+
hours -= days * HOURS_PER_DAY;
|
|
97
|
+
if (hours > 0) {
|
|
98
|
+
duration["hours"] = hours;
|
|
99
|
+
}
|
|
100
|
+
if (days > 0) {
|
|
101
|
+
duration["days"] = days;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return duration;
|
|
105
|
+
}
|
|
106
|
+
function durationToMs(duration) {
|
|
107
|
+
const daysMs = (duration.days ?? 0) * MS_PER_DAY;
|
|
108
|
+
const hoursMs = (duration.hours ?? 0) * MS_PER_HOUR;
|
|
109
|
+
const minsMs = (duration.minutes ?? 0) * MS_PER_MINUTE;
|
|
110
|
+
const secsMs = (duration.seconds ?? 0) * MS_PER_SECOND;
|
|
111
|
+
const msMs = duration.milliseconds ?? 0;
|
|
112
|
+
return daysMs + hoursMs + minsMs + secsMs + msMs;
|
|
113
|
+
}
|
|
114
|
+
function durationOrMsToMs(duration) {
|
|
115
|
+
return typeof duration === "number" ? duration : durationToMs(duration);
|
|
116
|
+
}
|
|
117
|
+
function formatDuration(duration, style) {
|
|
118
|
+
style = style ?? "short";
|
|
119
|
+
const stylePlural = getDurationStyleForPlural(style);
|
|
120
|
+
const space = getValueAndUnitSeparator(style);
|
|
121
|
+
const a = [];
|
|
122
|
+
for (const unit of DURATION_TYPE_SEQUENCE) {
|
|
123
|
+
const value = duration[unit];
|
|
124
|
+
if (value === void 0) continue;
|
|
125
|
+
const suffixMap = DURATION_STYLE_SUFFIX_MAP[unit];
|
|
126
|
+
const suffix = value === 1 ? suffixMap[style] : suffixMap[stylePlural];
|
|
127
|
+
a.push(value + space + suffix);
|
|
128
|
+
}
|
|
129
|
+
const separator = getDurationTypeSeparator(style);
|
|
130
|
+
return a.join(separator);
|
|
131
|
+
}
|
|
132
|
+
function readableDuration(ms, options) {
|
|
133
|
+
const duration = msToDuration(ms, options?.durationTypeForZero);
|
|
134
|
+
return formatDuration(duration, options?.style);
|
|
135
|
+
}
|
|
136
|
+
function elapsed(ms) {
|
|
137
|
+
if (ms > MS_PER_MINUTE) {
|
|
138
|
+
return readableDuration(ms);
|
|
139
|
+
}
|
|
140
|
+
if (ms > 100) {
|
|
141
|
+
return `${(ms / 1e3).toFixed(3)}s`;
|
|
142
|
+
}
|
|
143
|
+
return ms + "ms";
|
|
144
|
+
}
|
|
145
|
+
export {
|
|
146
|
+
DURATION_STYLE_SUFFIX_MAP,
|
|
147
|
+
DURATION_TYPE_SEQUENCE,
|
|
148
|
+
durationOrMsToMs,
|
|
149
|
+
durationToMs,
|
|
150
|
+
elapsed,
|
|
151
|
+
formatDuration,
|
|
152
|
+
msToDuration,
|
|
153
|
+
readableDuration
|
|
154
|
+
};
|
package/isEmpty.d.mts
ADDED
package/isEmpty.min.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var t=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var o=Object.getOwnPropertyNames;var s=Object.prototype.hasOwnProperty;var l=(e,n)=>{for(var i in n)t(e,i,{get:n[i],enumerable:!0})},a=(e,n,i,u)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of o(n))!s.call(e,r)&&r!==i&&t(e,r,{get:()=>n[r],enumerable:!(u=f(n,r))||u.enumerable});return e};var c=e=>a(t({},"__esModule",{value:!0}),e);var b={};l(b,{isEmpty:()=>p});module.exports=c(b);function p(e){if(!e)return!0;if(typeof e!="object")return!1;if("length"in e)return e.length===0;if("size"in e)return e.size===0;for(let n in e)return!1;return!0}0&&(module.exports={isEmpty});
|
|
2
|
+
//# sourceMappingURL=isEmpty.min.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/isEmpty.ts"],"sourcesContent":["/**\n * Returns true if `t` is empty.\n */\nexport function isEmpty(t: unknown): boolean {\n // Anything falsy is considered empty.\n if (!t) {\n return true;\n }\n\n // Arrays are also of type `object`.\n if (typeof t !== \"object\") {\n return false;\n }\n\n // `length` includes arrays as well.\n if (\"length\" in t) {\n return t.length === 0;\n }\n\n // `size` is for Set, Map, Blob etc.\n if (\"size\" in t) {\n return t.size === 0;\n }\n\n // Super fast check for object emptiness.\n // https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object\n for (const k in t) {\n return false;\n }\n\n return true;\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAGO,SAASE,EAAQE,EAAqB,CAE3C,GAAI,CAACA,EACH,MAAO,GAIT,GAAI,OAAOA,GAAM,SACf,MAAO,GAIT,GAAI,WAAYA,EACd,OAAOA,EAAE,SAAW,EAItB,GAAI,SAAUA,EACZ,OAAOA,EAAE,OAAS,EAKpB,QAAWC,KAAKD,EACd,MAAO,GAGT,MAAO,EACT","names":["isEmpty_exports","__export","isEmpty","__toCommonJS","t","k"]}
|
package/isEmpty.min.mjs
ADDED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/isEmpty.ts"],"sourcesContent":["/**\n * Returns true if `t` is empty.\n */\nexport function isEmpty(t: unknown): boolean {\n // Anything falsy is considered empty.\n if (!t) {\n return true;\n }\n\n // Arrays are also of type `object`.\n if (typeof t !== \"object\") {\n return false;\n }\n\n // `length` includes arrays as well.\n if (\"length\" in t) {\n return t.length === 0;\n }\n\n // `size` is for Set, Map, Blob etc.\n if (\"size\" in t) {\n return t.size === 0;\n }\n\n // Super fast check for object emptiness.\n // https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object\n for (const k in t) {\n return false;\n }\n\n return true;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/isEmpty.ts"],"sourcesContent":["/**\n * Returns true if `t` is empty.\n */\nexport function isEmpty(t: unknown): boolean {\n // Anything falsy is considered empty.\n if (!t) {\n return true;\n }\n\n // Arrays are also of type `object`.\n if (typeof t !== \"object\") {\n return false;\n }\n\n // `length` includes arrays as well.\n if (\"length\" in t) {\n return t.length === 0;\n }\n\n // `size` is for Set, Map, Blob etc.\n if (\"size\" in t) {\n return t.size === 0;\n }\n\n // Super fast check for object emptiness.\n // https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object\n for (const k in t) {\n return false;\n }\n\n return true;\n}\n"],"mappings":"AAGO,SAASA,EAAQC,EAAqB,CAE3C,GAAI,CAACA,EACH,MAAO,GAIT,GAAI,OAAOA,GAAM,SACf,MAAO,GAIT,GAAI,WAAYA,EACd,OAAOA,EAAE,SAAW,EAItB,GAAI,SAAUA,EACZ,OAAOA,EAAE,OAAS,EAKpB,QAAWC,KAAKD,EACd,MAAO,GAGT,MAAO,EACT","names":["isEmpty","t","k"]}
|
package/isEmpty.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/isEmpty.ts
|
|
2
|
+
function isEmpty(t) {
|
|
3
|
+
if (!t) {
|
|
4
|
+
return true;
|
|
5
|
+
}
|
|
6
|
+
if (typeof t !== "object") {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
if ("length" in t) {
|
|
10
|
+
return t.length === 0;
|
|
11
|
+
}
|
|
12
|
+
if ("size" in t) {
|
|
13
|
+
return t.size === 0;
|
|
14
|
+
}
|
|
15
|
+
for (const k in t) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
isEmpty
|
|
22
|
+
};
|