@choksheak/ts-utils 0.3.3 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/README.md +9 -3
  2. package/asNumber.cjs +6 -6
  3. package/asNumber.d.mts +1 -1
  4. package/asNumber.d.ts +1 -1
  5. package/asNumber.min.cjs +1 -1
  6. package/asNumber.min.cjs.map +1 -1
  7. package/asNumber.min.mjs +1 -1
  8. package/asNumber.min.mjs.map +1 -1
  9. package/asNumber.mjs +6 -6
  10. package/capLength.cjs +80 -0
  11. package/capLength.d.mts +3 -0
  12. package/capLength.d.ts +3 -0
  13. package/capLength.min.cjs +6 -0
  14. package/capLength.min.cjs.map +1 -0
  15. package/capLength.min.mjs +6 -0
  16. package/capLength.min.mjs.map +1 -0
  17. package/capLength.mjs +53 -0
  18. package/{iterators.cjs → concatIterators.cjs} +4 -4
  19. package/{iterators.min.cjs → concatIterators.min.cjs} +1 -1
  20. package/concatIterators.min.cjs.map +1 -0
  21. package/{iterators.min.mjs → concatIterators.min.mjs} +1 -1
  22. package/concatIterators.min.mjs.map +1 -0
  23. package/{iterators.mjs → concatIterators.mjs} +1 -1
  24. package/duration.min.cjs +1 -1
  25. package/duration.min.mjs +1 -1
  26. package/kvStore.cjs +230 -239
  27. package/kvStore.d.mts +142 -44
  28. package/kvStore.d.ts +142 -44
  29. package/kvStore.min.cjs +1 -1
  30. package/kvStore.min.cjs.map +1 -1
  31. package/kvStore.min.mjs +1 -1
  32. package/kvStore.min.mjs.map +1 -1
  33. package/kvStore.mjs +229 -237
  34. package/localStore.cjs +187 -186
  35. package/localStore.d.mts +135 -34
  36. package/localStore.d.ts +135 -34
  37. package/localStore.min.cjs +1 -1
  38. package/localStore.min.cjs.map +1 -1
  39. package/localStore.min.mjs +1 -1
  40. package/localStore.min.mjs.map +1 -1
  41. package/localStore.mjs +186 -184
  42. package/mean.cjs +6 -6
  43. package/mean.min.cjs +1 -1
  44. package/mean.min.cjs.map +1 -1
  45. package/mean.min.mjs +1 -1
  46. package/mean.min.mjs.map +1 -1
  47. package/mean.mjs +6 -6
  48. package/median.cjs +7 -7
  49. package/median.min.cjs +1 -1
  50. package/median.min.cjs.map +1 -1
  51. package/median.min.mjs +1 -1
  52. package/median.min.mjs.map +1 -1
  53. package/median.mjs +7 -7
  54. package/package.json +67 -82
  55. package/round.cjs +2 -10
  56. package/round.d.mts +2 -8
  57. package/round.d.ts +2 -8
  58. package/round.min.cjs +1 -1
  59. package/round.min.cjs.map +1 -1
  60. package/round.min.mjs +1 -1
  61. package/round.min.mjs.map +1 -1
  62. package/round.mjs +1 -8
  63. package/{safeParseInt.cjs → roundToString.cjs} +19 -9
  64. package/roundToString.d.mts +8 -0
  65. package/roundToString.d.ts +8 -0
  66. package/roundToString.min.cjs +2 -0
  67. package/roundToString.min.cjs.map +1 -0
  68. package/roundToString.min.mjs +2 -0
  69. package/roundToString.min.mjs.map +1 -0
  70. package/roundToString.mjs +16 -0
  71. package/safeBtoa.d.mts +7 -0
  72. package/safeBtoa.d.ts +7 -0
  73. package/safeBtoa.min.cjs.map +1 -1
  74. package/safeBtoa.min.mjs.map +1 -1
  75. package/sum.cjs +6 -6
  76. package/sum.min.cjs +1 -1
  77. package/sum.min.cjs.map +1 -1
  78. package/sum.min.mjs +1 -1
  79. package/sum.min.mjs.map +1 -1
  80. package/sum.mjs +6 -6
  81. package/timer.cjs +19 -24
  82. package/timer.d.mts +9 -6
  83. package/timer.d.ts +9 -6
  84. package/timer.min.cjs +1 -1
  85. package/timer.min.cjs.map +1 -1
  86. package/timer.min.mjs +1 -1
  87. package/timer.min.mjs.map +1 -1
  88. package/timer.mjs +19 -23
  89. package/toReadableString.cjs +69 -0
  90. package/toReadableString.d.mts +10 -0
  91. package/toReadableString.d.ts +10 -0
  92. package/toReadableString.min.cjs +6 -0
  93. package/toReadableString.min.cjs.map +1 -0
  94. package/toReadableString.min.mjs +6 -0
  95. package/toReadableString.min.mjs.map +1 -0
  96. package/toReadableString.mjs +44 -0
  97. package/average.cjs +0 -55
  98. package/average.d.mts +0 -7
  99. package/average.d.ts +0 -7
  100. package/average.min.cjs +0 -2
  101. package/average.min.cjs.map +0 -1
  102. package/average.min.mjs +0 -2
  103. package/average.min.mjs.map +0 -1
  104. package/average.mjs +0 -28
  105. package/iterators.min.cjs.map +0 -1
  106. package/iterators.min.mjs.map +0 -1
  107. package/logging.cjs +0 -47
  108. package/logging.d.mts +0 -4
  109. package/logging.d.ts +0 -4
  110. package/logging.min.cjs +0 -2
  111. package/logging.min.cjs.map +0 -1
  112. package/logging.min.mjs +0 -2
  113. package/logging.min.mjs.map +0 -1
  114. package/logging.mjs +0 -21
  115. package/safeParseFloat.cjs +0 -33
  116. package/safeParseFloat.d.mts +0 -6
  117. package/safeParseFloat.d.ts +0 -6
  118. package/safeParseFloat.min.cjs +0 -2
  119. package/safeParseFloat.min.cjs.map +0 -1
  120. package/safeParseFloat.min.mjs +0 -2
  121. package/safeParseFloat.min.mjs.map +0 -1
  122. package/safeParseFloat.mjs +0 -8
  123. package/safeParseInt.d.mts +0 -6
  124. package/safeParseInt.d.ts +0 -6
  125. package/safeParseInt.min.cjs +0 -2
  126. package/safeParseInt.min.cjs.map +0 -1
  127. package/safeParseInt.min.mjs +0 -2
  128. package/safeParseInt.min.mjs.map +0 -1
  129. package/safeParseInt.mjs +0 -8
  130. /package/{iterators.d.mts → concatIterators.d.mts} +0 -0
  131. /package/{iterators.d.ts → concatIterators.d.ts} +0 -0
package/timer.d.mts CHANGED
@@ -1,13 +1,16 @@
1
- declare class Timer {
1
+ /**
2
+ * Create a new timer and starts the timing right away. Returns a closed object
3
+ * instead of a class to make sure the variables are bound correctly.
4
+ */
5
+ declare function timer(): {
2
6
  startMs: number;
3
7
  endMs: number;
4
- constructor();
5
8
  stop(): void;
6
9
  restart(): void;
7
10
  elapsedMs(): number;
8
11
  toString(): string;
9
- }
10
- /** Shorthand for `new Timer()` to make this easier to use. */
11
- declare function timer(): Timer;
12
+ };
13
+ /** Defines the type of the Timer object. */
14
+ type Timer = ReturnType<typeof timer>;
12
15
 
13
- export { Timer, timer };
16
+ export { type Timer, timer };
package/timer.d.ts CHANGED
@@ -1,13 +1,16 @@
1
- declare class Timer {
1
+ /**
2
+ * Create a new timer and starts the timing right away. Returns a closed object
3
+ * instead of a class to make sure the variables are bound correctly.
4
+ */
5
+ declare function timer(): {
2
6
  startMs: number;
3
7
  endMs: number;
4
- constructor();
5
8
  stop(): void;
6
9
  restart(): void;
7
10
  elapsedMs(): number;
8
11
  toString(): string;
9
- }
10
- /** Shorthand for `new Timer()` to make this easier to use. */
11
- declare function timer(): Timer;
12
+ };
13
+ /** Defines the type of the Timer object. */
14
+ type Timer = ReturnType<typeof timer>;
12
15
 
13
- export { Timer, timer };
16
+ export { type Timer, timer };
package/timer.min.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var p=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var h=(t,o)=>{for(var r in o)p(t,r,{get:o[r],enumerable:!0})},x=(t,o,r,i)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of d(o))!m.call(t,n)&&n!==r&&p(t,n,{get:()=>o[n],enumerable:!(i=S(o,n))||i.enumerable});return t};var R=t=>x(p({},"__esModule",{value:!0}),t);var A={};h(A,{Timer:()=>c,timer:()=>I});module.exports=R(A);var y=["days","hours","minutes","seconds","milliseconds"],b={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 P(t){return t=="short"?"shorts":t==="long"?"longs":t}function T(t){return t==="narrow"?"":" "}function U(t){return t==="narrow"?" ":", "}function N(t,o){if(t===0)return o=o??"milliseconds",{[o]:0};let r={};for(let i=0;i<1;i++){let n=Math.floor(t/1e3),a=t-n*1e3;if(a>0&&(r.milliseconds=a),n===0)break;let e=Math.floor(n/60);if(n-=e*60,n>0&&(r.seconds=n),e===0)break;let s=Math.floor(e/60);if(e-=s*60,e>0&&(r.minutes=e),s===0)break;let u=Math.floor(s/24);s-=u*24,s>0&&(r.hours=s),u>0&&(r.days=u)}return r}function O(t,o){o=o??"short";let r=P(o),i=T(o),n=[];for(let e of y){let s=t[e];if(s===void 0)continue;let u=b[e],M=s===1?u[o]:u[r];n.push(s+i+M)}let a=U(o);return n.join(a)}function w(t,o){let r=N(t,o?.durationTypeForZero);return O(r,o?.style)}function D(t){return t>6e4?w(t):t>100?`${(t/1e3).toFixed(3)}s`:t+"ms"}var c=class{startMs;endMs=0;constructor(){this.startMs=Date.now()}stop(){this.endMs=Date.now()}restart(){this.endMs=0,this.startMs=Date.now()}elapsedMs(){return(this.endMs||Date.now())-this.startMs}toString(){return D(this.elapsedMs())}};function I(){return new c}0&&(module.exports={Timer,timer});
1
+ "use strict";var l=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var D=(o,r)=>{for(var n in r)l(o,n,{get:r[n],enumerable:!0})},M=(o,r,n,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of d(r))!m.call(o,t)&&t!==n&&l(o,t,{get:()=>r[t],enumerable:!(i=p(r,t))||i.enumerable});return o};var h=o=>M(l({},"__esModule",{value:!0}),o);var O={};D(O,{timer:()=>N});module.exports=h(O);var T=["days","hours","minutes","seconds","milliseconds"],_={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 E(o){return o==="narrow"?"":" "}function R(o){return o==="narrow"?" ":", "}function w(o,r){if(o===0)return r=r??"milliseconds",{[r]:0};let n={};for(let i=0;i<1;i++){let t=Math.floor(o/1e3),a=o-t*1e3;if(a>0&&(n.milliseconds=a),t===0)break;let e=Math.floor(t/60);if(t-=e*60,t>0&&(n.seconds=t),e===0)break;let s=Math.floor(e/60);if(e-=s*60,e>0&&(n.minutes=e),s===0)break;let u=Math.floor(s/24);s-=u*24,s>0&&(n.hours=s),u>0&&(n.days=u)}return n}function P(o,r){r=r??"short";let n=b(r),i=E(r),t=[];for(let e of T){let s=o[e];if(s===void 0)continue;let u=_[e],f=s===1?u[r]:u[n];t.push(s+i+f)}let a=R(r);return t.join(a)}function U(o,r){let n=w(o,r?.durationTypeForZero);return P(n,r?.style)}function c(o){return o>6e4?U(o):o>100?`${(o/1e3).toFixed(3)}s`:o+"ms"}function N(){let o={startMs:Date.now(),endMs:0,stop(){o.endMs=Date.now()},restart(){o.endMs=0,o.startMs=Date.now()},elapsedMs(){return(o.endMs||Date.now())-o.startMs},toString(){return c(o.elapsedMs())}};return o}0&&(module.exports={timer});
2
2
  //# sourceMappingURL=timer.min.cjs.map
package/timer.min.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/timer.ts","../src/duration.ts"],"sourcesContent":["import { elapsed } from \"./duration\";\n\nexport class Timer {\n public startMs: number;\n public endMs = 0;\n\n public constructor() {\n this.startMs = Date.now();\n }\n\n public stop(): void {\n this.endMs = Date.now();\n }\n\n public restart(): void {\n this.endMs = 0;\n this.startMs = Date.now();\n }\n\n public elapsedMs(): number {\n const stopMs = this.endMs || Date.now();\n return stopMs - this.startMs;\n }\n\n public toString(): string {\n return elapsed(this.elapsedMs());\n }\n}\n\n/** Shorthand for `new Timer()` to make this easier to use. */\nexport function timer(): Timer {\n return new Timer();\n}\n","/**\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\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GACtB,KAAK,OACvB,CAEO,UAAmB,CACxB,OAAOC,EAAQ,KAAK,UAAU,CAAC,CACjC,CACF,EAGO,SAASC,GAAe,CAC7B,OAAO,IAAIF,CACb","names":["timer_exports","__export","Timer","timer","__toCommonJS","DURATION_TYPE_SEQUENCE","DURATION_STYLE_SUFFIX_MAP","getDurationStyleForPlural","style","getValueAndUnitSeparator","getDurationTypeSeparator","msToDuration","ms","durationTypeForZero","duration","seconds","millis","minutes","hours","days","formatDuration","duration","style","stylePlural","getDurationStyleForPlural","space","getValueAndUnitSeparator","a","unit","DURATION_TYPE_SEQUENCE","value","suffixMap","DURATION_STYLE_SUFFIX_MAP","suffix","separator","getDurationTypeSeparator","readableDuration","ms","options","msToDuration","elapsed","Timer","elapsed","timer"]}
1
+ {"version":3,"sources":["../src/timer.ts","../src/duration.ts"],"sourcesContent":["import { elapsed } from \"./duration\";\n\n/**\n * Create a new timer and starts the timing right away. Returns a closed object\n * instead of a class to make sure the variables are bound correctly.\n */\nexport function timer() {\n const obj = {\n startMs: Date.now(),\n endMs: 0,\n\n stop(): void {\n obj.endMs = Date.now();\n },\n\n restart(): void {\n obj.endMs = 0;\n obj.startMs = Date.now();\n },\n\n elapsedMs(): number {\n const stopMs = obj.endMs || Date.now();\n return stopMs - obj.startMs;\n },\n\n toString(): string {\n return elapsed(obj.elapsedMs());\n },\n };\n\n return obj;\n}\n\n/** Defines the type of the Timer object. */\nexport type Timer = ReturnType<typeof timer>;\n","/**\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,WAAAE,IAAA,eAAAC,EAAAH,GCmCO,IAAMI,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,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AAyBJ,CAAK,EAEtCK,EAAc,CAAC,EAErB,QAAWC,KAAQC,EAAwB,CACzC,IAAMC,EAAQT,EAASO,CAAI,EAC3B,GAAIE,IAAU,OAAW,SAEzB,IAAMC,EAAYC,EAA0BJ,CAAI,EAC1CK,EAASH,IAAU,EAAIC,EAAUT,CAAK,EAAIS,EAAUR,CAAW,EACrEI,EAAE,KAAKG,EAAQL,EAAQQ,CAAM,CAC/B,CAEA,IAAMC,EAAYC,EAAyBb,CAAK,EAChD,OAAOK,EAAE,KAAKO,CAAS,CACzB,CAQO,SAASE,EACdC,EACAC,EACQ,CACR,IAAMjB,EAAWkB,EAAaF,EAAIC,GAAS,mBAAmB,EAE9D,OAAOlB,EAAeC,EAAUiB,GAAS,KAAK,CAChD,CAGO,SAASE,EAAQH,EAAoB,CAE1C,OAAIA,EAAK,IACAD,EAAiBC,CAAE,EAIxBA,EAAK,IACA,IAAIA,EAAK,KAAM,QAAQ,CAAC,CAAC,IAI3BA,EAAK,IACd,CD7PO,SAASI,GAAQ,CACtB,IAAMC,EAAM,CACV,QAAS,KAAK,IAAI,EAClB,MAAO,EAEP,MAAa,CACXA,EAAI,MAAQ,KAAK,IAAI,CACvB,EAEA,SAAgB,CACdA,EAAI,MAAQ,EACZA,EAAI,QAAU,KAAK,IAAI,CACzB,EAEA,WAAoB,CAElB,OADeA,EAAI,OAAS,KAAK,IAAI,GACrBA,EAAI,OACtB,EAEA,UAAmB,CACjB,OAAOC,EAAQD,EAAI,UAAU,CAAC,CAChC,CACF,EAEA,OAAOA,CACT","names":["timer_exports","__export","timer","__toCommonJS","DURATION_TYPE_SEQUENCE","DURATION_STYLE_SUFFIX_MAP","getDurationStyleForPlural","style","getValueAndUnitSeparator","getDurationTypeSeparator","msToDuration","ms","durationTypeForZero","duration","seconds","millis","minutes","hours","days","formatDuration","duration","style","stylePlural","getDurationStyleForPlural","space","getValueAndUnitSeparator","a","unit","DURATION_TYPE_SEQUENCE","value","suffixMap","DURATION_STYLE_SUFFIX_MAP","suffix","separator","getDurationTypeSeparator","readableDuration","ms","options","msToDuration","elapsed","timer","obj","elapsed"]}
package/timer.min.mjs CHANGED
@@ -1,2 +1,2 @@
1
- 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 m(t){return t=="short"?"shorts":t==="long"?"longs":t}function h(t){return t==="narrow"?"":" "}function x(t){return t==="narrow"?" ":", "}function R(t,o){if(t===0)return o=o??"milliseconds",{[o]:0};let r={};for(let u=0;u<1;u++){let s=Math.floor(t/1e3),a=t-s*1e3;if(a>0&&(r.milliseconds=a),s===0)break;let e=Math.floor(s/60);if(s-=e*60,s>0&&(r.seconds=s),e===0)break;let n=Math.floor(e/60);if(e-=n*60,e>0&&(r.minutes=e),n===0)break;let i=Math.floor(n/24);n-=i*24,n>0&&(r.hours=n),i>0&&(r.days=i)}return r}function g(t,o){o=o??"short";let r=m(o),u=h(o),s=[];for(let e of S){let n=t[e];if(n===void 0)continue;let i=d[e],D=n===1?i[o]:i[r];s.push(n+u+D)}let a=x(o);return s.join(a)}function y(t,o){let r=R(t,o?.durationTypeForZero);return g(r,o?.style)}function E(t){return t>6e4?y(t):t>100?`${(t/1e3).toFixed(3)}s`:t+"ms"}var c=class{startMs;endMs=0;constructor(){this.startMs=Date.now()}stop(){this.endMs=Date.now()}restart(){this.endMs=0,this.startMs=Date.now()}elapsedMs(){return(this.endMs||Date.now())-this.startMs}toString(){return E(this.elapsedMs())}};function w(){return new c}export{c as Timer,w as timer};
1
+ var D=["days","hours","minutes","seconds","milliseconds"],M={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 h(o){return o=="short"?"shorts":o==="long"?"longs":o}function y(o){return o==="narrow"?"":" "}function g(o){return o==="narrow"?" ":", "}function S(o,r){if(o===0)return r=r??"milliseconds",{[r]:0};let n={};for(let u=0;u<1;u++){let s=Math.floor(o/1e3),a=o-s*1e3;if(a>0&&(n.milliseconds=a),s===0)break;let e=Math.floor(s/60);if(s-=e*60,s>0&&(n.seconds=s),e===0)break;let t=Math.floor(e/60);if(e-=t*60,e>0&&(n.minutes=e),t===0)break;let i=Math.floor(t/24);t-=i*24,t>0&&(n.hours=t),i>0&&(n.days=i)}return n}function x(o,r){r=r??"short";let n=h(r),u=y(r),s=[];for(let e of D){let t=o[e];if(t===void 0)continue;let i=M[e],c=t===1?i[r]:i[n];s.push(t+u+c)}let a=g(r);return s.join(a)}function T(o,r){let n=S(o,r?.durationTypeForZero);return x(n,r?.style)}function l(o){return o>6e4?T(o):o>100?`${(o/1e3).toFixed(3)}s`:o+"ms"}function R(){let o={startMs:Date.now(),endMs:0,stop(){o.endMs=Date.now()},restart(){o.endMs=0,o.startMs=Date.now()},elapsedMs(){return(o.endMs||Date.now())-o.startMs},toString(){return l(o.elapsedMs())}};return o}export{R as timer};
2
2
  //# sourceMappingURL=timer.min.mjs.map
package/timer.min.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/duration.ts","../src/timer.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","import { elapsed } from \"./duration\";\n\nexport class Timer {\n public startMs: number;\n public endMs = 0;\n\n public constructor() {\n this.startMs = Date.now();\n }\n\n public stop(): void {\n this.endMs = Date.now();\n }\n\n public restart(): void {\n this.endMs = 0;\n this.startMs = Date.now();\n }\n\n public elapsedMs(): number {\n const stopMs = this.endMs || Date.now();\n return stopMs - this.startMs;\n }\n\n public toString(): string {\n return elapsed(this.elapsedMs());\n }\n}\n\n/** Shorthand for `new Timer()` to make this easier to use. */\nexport function timer(): Timer {\n return new Timer();\n}\n"],"mappings":"AAmCO,IAAMA,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,IA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EO,WAAoB,CAEzB,OADe,KAAK,OAAS,KAAK,IAAI,GACtB,KAAK,OACvB,CAEO,UAAmB,CACxB,OAAOC,EAAQ,KAAK,UAAU,CAAC,CACjC,CACF,EAGO,SAASC,GAAe,CAC7B,OAAO,IAAIF,CACb","names":["DURATION_TYPE_SEQUENCE","DURATION_STYLE_SUFFIX_MAP","getDurationStyleForPlural","style","getValueAndUnitSeparator","getDurationTypeSeparator","msToDuration","ms","durationTypeForZero","duration","i","seconds","millis","minutes","hours","days","formatDuration","duration","style","stylePlural","getDurationStyleForPlural","space","getValueAndUnitSeparator","a","unit","DURATION_TYPE_SEQUENCE","value","suffixMap","DURATION_STYLE_SUFFIX_MAP","suffix","separator","getDurationTypeSeparator","readableDuration","ms","options","msToDuration","elapsed","Timer","elapsed","timer"]}
1
+ {"version":3,"sources":["../src/duration.ts","../src/timer.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","import { elapsed } from \"./duration\";\n\n/**\n * Create a new timer and starts the timing right away. Returns a closed object\n * instead of a class to make sure the variables are bound correctly.\n */\nexport function timer() {\n const obj = {\n startMs: Date.now(),\n endMs: 0,\n\n stop(): void {\n obj.endMs = Date.now();\n },\n\n restart(): void {\n obj.endMs = 0;\n obj.startMs = Date.now();\n },\n\n elapsedMs(): number {\n const stopMs = obj.endMs || Date.now();\n return stopMs - obj.startMs;\n },\n\n toString(): string {\n return elapsed(obj.elapsedMs());\n },\n };\n\n return obj;\n}\n\n/** Defines the type of the Timer object. */\nexport type Timer = ReturnType<typeof timer>;\n"],"mappings":"AAmCO,IAAMA,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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,KAAQC,EAAwB,CACzC,IAAMC,EAAQT,EAASO,CAAI,EAC3B,GAAIE,IAAU,OAAW,SAEzB,IAAMC,EAAYC,EAA0BJ,CAAI,EAC1CK,EAASH,IAAU,EAAIC,EAAUT,CAAK,EAAIS,EAAUR,CAAW,EACrEI,EAAE,KAAKG,EAAQL,EAAQQ,CAAM,CAC/B,CAEA,IAAMC,EAAYC,EAAyBb,CAAK,EAChD,OAAOK,EAAE,KAAKO,CAAS,CACzB,CAQO,SAASE,EACdC,EACAC,EACQ,CACR,IAAMjB,EAAWkB,EAAaF,EAAIC,GAAS,mBAAmB,EAE9D,OAAOlB,EAAeC,EAAUiB,GAAS,KAAK,CAChD,CAGO,SAASE,EAAQH,EAAoB,CAE1C,OAAIA,EAAK,IACAD,EAAiBC,CAAE,EAIxBA,EAAK,IACA,IAAIA,EAAK,KAAM,QAAQ,CAAC,CAAC,IAI3BA,EAAK,IACd,CC7PO,SAASI,GAAQ,CACtB,IAAMC,EAAM,CACV,QAAS,KAAK,IAAI,EAClB,MAAO,EAEP,MAAa,CACXA,EAAI,MAAQ,KAAK,IAAI,CACvB,EAEA,SAAgB,CACdA,EAAI,MAAQ,EACZA,EAAI,QAAU,KAAK,IAAI,CACzB,EAEA,WAAoB,CAElB,OADeA,EAAI,OAAS,KAAK,IAAI,GACrBA,EAAI,OACtB,EAEA,UAAmB,CACjB,OAAOC,EAAQD,EAAI,UAAU,CAAC,CAChC,CACF,EAEA,OAAOA,CACT","names":["DURATION_TYPE_SEQUENCE","DURATION_STYLE_SUFFIX_MAP","getDurationStyleForPlural","style","getValueAndUnitSeparator","getDurationTypeSeparator","msToDuration","ms","durationTypeForZero","duration","i","seconds","millis","minutes","hours","days","formatDuration","duration","style","stylePlural","getDurationStyleForPlural","space","getValueAndUnitSeparator","a","unit","DURATION_TYPE_SEQUENCE","value","suffixMap","DURATION_STYLE_SUFFIX_MAP","suffix","separator","getDurationTypeSeparator","readableDuration","ms","options","msToDuration","elapsed","timer","obj","elapsed"]}
package/timer.mjs CHANGED
@@ -131,31 +131,27 @@ function elapsed(ms) {
131
131
  }
132
132
 
133
133
  // src/timer.ts
134
- var Timer = class {
135
- startMs;
136
- endMs = 0;
137
- constructor() {
138
- this.startMs = Date.now();
139
- }
140
- stop() {
141
- this.endMs = Date.now();
142
- }
143
- restart() {
144
- this.endMs = 0;
145
- this.startMs = Date.now();
146
- }
147
- elapsedMs() {
148
- const stopMs = this.endMs || Date.now();
149
- return stopMs - this.startMs;
150
- }
151
- toString() {
152
- return elapsed(this.elapsedMs());
153
- }
154
- };
155
134
  function timer() {
156
- return new Timer();
135
+ const obj = {
136
+ startMs: Date.now(),
137
+ endMs: 0,
138
+ stop() {
139
+ obj.endMs = Date.now();
140
+ },
141
+ restart() {
142
+ obj.endMs = 0;
143
+ obj.startMs = Date.now();
144
+ },
145
+ elapsedMs() {
146
+ const stopMs = obj.endMs || Date.now();
147
+ return stopMs - obj.startMs;
148
+ },
149
+ toString() {
150
+ return elapsed(obj.elapsedMs());
151
+ }
152
+ };
153
+ return obj;
157
154
  }
158
155
  export {
159
- Timer,
160
156
  timer
161
157
  };
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/toReadableString.ts
21
+ var toReadableString_exports = {};
22
+ __export(toReadableString_exports, {
23
+ toReadableString: () => toReadableString
24
+ });
25
+ module.exports = __toCommonJS(toReadableString_exports);
26
+ function toReadableString(u, options) {
27
+ if (typeof u === "string") {
28
+ return u;
29
+ }
30
+ if (u instanceof Error) {
31
+ const error = u;
32
+ let result = "";
33
+ const errorName = error.name || "Error";
34
+ const errorMessage = error.message || "An error occurred with no message provided.";
35
+ result += `${errorName}: ${errorMessage}`;
36
+ if (options?.includeStack && error.stack) {
37
+ const stack = error.stack.replace(new RegExp(`^${errorName}:.*\\n?`), "").trim();
38
+ if (stack) {
39
+ result += `
40
+ Stack Trace:
41
+ ${stack}`;
42
+ }
43
+ }
44
+ if (options?.includeErrorProps) {
45
+ const customProps = error;
46
+ const additionalInfo = Object.keys(customProps).filter(
47
+ (key) => key !== "name" && key !== "message" && key !== "stack" && typeof customProps[key] !== "function" && typeof customProps[key] !== "object"
48
+ ).map((key) => `
49
+ - ${key}: ${customProps[key]}`);
50
+ if (additionalInfo.length > 0) {
51
+ result += `
52
+ Additional Data:${additionalInfo.join("")}`;
53
+ }
54
+ }
55
+ return result;
56
+ }
57
+ if (u !== null && typeof u === "object" && u.toString !== Object.prototype.toString) {
58
+ return u.toString();
59
+ }
60
+ try {
61
+ return JSON.stringify(u);
62
+ } catch {
63
+ return String(u);
64
+ }
65
+ }
66
+ // Annotate the CommonJS export names for ESM import in node:
67
+ 0 && (module.exports = {
68
+ toReadableString
69
+ });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Make the given argument of unknown type into something human-readable.
3
+ * For Error objects, you can specify options to make the string more verbose.
4
+ */
5
+ declare function toReadableString(u: unknown, options?: {
6
+ includeStack?: boolean;
7
+ includeErrorProps?: boolean;
8
+ }): string;
9
+
10
+ export { toReadableString };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Make the given argument of unknown type into something human-readable.
3
+ * For Error objects, you can specify options to make the string more verbose.
4
+ */
5
+ declare function toReadableString(u: unknown, options?: {
6
+ includeStack?: boolean;
7
+ includeErrorProps?: boolean;
8
+ }): string;
9
+
10
+ export { toReadableString };
@@ -0,0 +1,6 @@
1
+ "use strict";var c=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var d=(r,t)=>{for(var n in t)c(r,n,{get:t[n],enumerable:!0})},m=(r,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of g(t))!p.call(r,e)&&e!==n&&c(r,e,{get:()=>t[e],enumerable:!(i=l(t,e))||i.enumerable});return r};var S=r=>m(c({},"__esModule",{value:!0}),r);var b={};d(b,{toReadableString:()=>u});module.exports=S(b);function u(r,t){if(typeof r=="string")return r;if(r instanceof Error){let n=r,i="",e=n.name||"Error",f=n.message||"An error occurred with no message provided.";if(i+=`${e}: ${f}`,t?.includeStack&&n.stack){let a=n.stack.replace(new RegExp(`^${e}:.*\\n?`),"").trim();a&&(i+=`
2
+ Stack Trace:
3
+ ${a}`)}if(t?.includeErrorProps){let a=n,s=Object.keys(a).filter(o=>o!=="name"&&o!=="message"&&o!=="stack"&&typeof a[o]!="function"&&typeof a[o]!="object").map(o=>`
4
+ - ${o}: ${a[o]}`);s.length>0&&(i+=`
5
+ Additional Data:${s.join("")}`)}return i}if(r!==null&&typeof r=="object"&&r.toString!==Object.prototype.toString)return r.toString();try{return JSON.stringify(r)}catch{return String(r)}}0&&(module.exports={toReadableString});
6
+ //# sourceMappingURL=toReadableString.min.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/toReadableString.ts"],"sourcesContent":["/**\n * Make the given argument of unknown type into something human-readable.\n * For Error objects, you can specify options to make the string more verbose.\n */\nexport function toReadableString(\n u: unknown,\n options?: { includeStack?: boolean; includeErrorProps?: boolean },\n): string {\n if (typeof u === \"string\") {\n return u;\n }\n\n if (u instanceof Error) {\n const error = u as Error;\n let result = \"\";\n\n // Always include the name and message\n const errorName = error.name || \"Error\";\n const errorMessage =\n error.message || \"An error occurred with no message provided.\";\n\n result += `${errorName}: ${errorMessage}`;\n\n // Optionally include the stack trace\n if (options?.includeStack && error.stack) {\n // Clean up the stack trace to start on a new line,\n // removing potential duplicate header lines if the browser adds them.\n const stack = error.stack\n // Remove the first line if it duplicates the name/message\n .replace(new RegExp(`^${errorName}:.*\\\\n?`), \"\")\n .trim();\n\n if (stack) {\n result += `\\nStack Trace:\\n${stack}`;\n }\n }\n\n // Add any potential custom error properties (e.g., HTTP status code)\n if (options?.includeErrorProps) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const customProps: { [key: string]: unknown } = error as any;\n\n const additionalInfo = Object.keys(customProps)\n .filter(\n (key) =>\n key !== \"name\" &&\n key !== \"message\" &&\n key !== \"stack\" &&\n typeof customProps[key] !== \"function\" &&\n typeof customProps[key] !== \"object\",\n )\n .map((key) => `\\n- ${key}: ${customProps[key]}`);\n\n if (additionalInfo.length > 0) {\n result += `\\nAdditional Data:${additionalInfo.join(\"\")}`;\n }\n }\n\n return result;\n }\n\n // If the object has a custom toString(), then use it.\n if (\n u !== null &&\n typeof u === \"object\" &&\n u.toString !== Object.prototype.toString\n ) {\n return u.toString();\n }\n\n try {\n // Attempt to JSON stringify the object for inspection.\n return JSON.stringify(u);\n } catch {\n // Fallback if the object cannot be stringified (e.g., circular references).\n return String(u);\n }\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,sBAAAE,IAAA,eAAAC,EAAAH,GAIO,SAASE,EACdE,EACAC,EACQ,CACR,GAAI,OAAOD,GAAM,SACf,OAAOA,EAGT,GAAIA,aAAa,MAAO,CACtB,IAAME,EAAQF,EACVG,EAAS,GAGPC,EAAYF,EAAM,MAAQ,QAC1BG,EACJH,EAAM,SAAW,8CAKnB,GAHAC,GAAU,GAAGC,CAAS,KAAKC,CAAY,GAGnCJ,GAAS,cAAgBC,EAAM,MAAO,CAGxC,IAAMI,EAAQJ,EAAM,MAEjB,QAAQ,IAAI,OAAO,IAAIE,CAAS,SAAS,EAAG,EAAE,EAC9C,KAAK,EAEJE,IACFH,GAAU;AAAA;AAAA,EAAmBG,CAAK,GAEtC,CAGA,GAAIL,GAAS,kBAAmB,CAE9B,IAAMM,EAA0CL,EAE1CM,EAAiB,OAAO,KAAKD,CAAW,EAC3C,OACEE,GACCA,IAAQ,QACRA,IAAQ,WACRA,IAAQ,SACR,OAAOF,EAAYE,CAAG,GAAM,YAC5B,OAAOF,EAAYE,CAAG,GAAM,QAChC,EACC,IAAKA,GAAQ;AAAA,IAAOA,CAAG,KAAKF,EAAYE,CAAG,CAAC,EAAE,EAE7CD,EAAe,OAAS,IAC1BL,GAAU;AAAA,kBAAqBK,EAAe,KAAK,EAAE,CAAC,GAE1D,CAEA,OAAOL,CACT,CAGA,GACEH,IAAM,MACN,OAAOA,GAAM,UACbA,EAAE,WAAa,OAAO,UAAU,SAEhC,OAAOA,EAAE,SAAS,EAGpB,GAAI,CAEF,OAAO,KAAK,UAAUA,CAAC,CACzB,MAAQ,CAEN,OAAO,OAAOA,CAAC,CACjB,CACF","names":["toReadableString_exports","__export","toReadableString","__toCommonJS","u","options","error","result","errorName","errorMessage","stack","customProps","additionalInfo","key"]}
@@ -0,0 +1,6 @@
1
+ function f(r,i){if(typeof r=="string")return r;if(r instanceof Error){let e=r,o="",a=e.name||"Error",s=e.message||"An error occurred with no message provided.";if(o+=`${a}: ${s}`,i?.includeStack&&e.stack){let n=e.stack.replace(new RegExp(`^${a}:.*\\n?`),"").trim();n&&(o+=`
2
+ Stack Trace:
3
+ ${n}`)}if(i?.includeErrorProps){let n=e,c=Object.keys(n).filter(t=>t!=="name"&&t!=="message"&&t!=="stack"&&typeof n[t]!="function"&&typeof n[t]!="object").map(t=>`
4
+ - ${t}: ${n[t]}`);c.length>0&&(o+=`
5
+ Additional Data:${c.join("")}`)}return o}if(r!==null&&typeof r=="object"&&r.toString!==Object.prototype.toString)return r.toString();try{return JSON.stringify(r)}catch{return String(r)}}export{f as toReadableString};
6
+ //# sourceMappingURL=toReadableString.min.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/toReadableString.ts"],"sourcesContent":["/**\n * Make the given argument of unknown type into something human-readable.\n * For Error objects, you can specify options to make the string more verbose.\n */\nexport function toReadableString(\n u: unknown,\n options?: { includeStack?: boolean; includeErrorProps?: boolean },\n): string {\n if (typeof u === \"string\") {\n return u;\n }\n\n if (u instanceof Error) {\n const error = u as Error;\n let result = \"\";\n\n // Always include the name and message\n const errorName = error.name || \"Error\";\n const errorMessage =\n error.message || \"An error occurred with no message provided.\";\n\n result += `${errorName}: ${errorMessage}`;\n\n // Optionally include the stack trace\n if (options?.includeStack && error.stack) {\n // Clean up the stack trace to start on a new line,\n // removing potential duplicate header lines if the browser adds them.\n const stack = error.stack\n // Remove the first line if it duplicates the name/message\n .replace(new RegExp(`^${errorName}:.*\\\\n?`), \"\")\n .trim();\n\n if (stack) {\n result += `\\nStack Trace:\\n${stack}`;\n }\n }\n\n // Add any potential custom error properties (e.g., HTTP status code)\n if (options?.includeErrorProps) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const customProps: { [key: string]: unknown } = error as any;\n\n const additionalInfo = Object.keys(customProps)\n .filter(\n (key) =>\n key !== \"name\" &&\n key !== \"message\" &&\n key !== \"stack\" &&\n typeof customProps[key] !== \"function\" &&\n typeof customProps[key] !== \"object\",\n )\n .map((key) => `\\n- ${key}: ${customProps[key]}`);\n\n if (additionalInfo.length > 0) {\n result += `\\nAdditional Data:${additionalInfo.join(\"\")}`;\n }\n }\n\n return result;\n }\n\n // If the object has a custom toString(), then use it.\n if (\n u !== null &&\n typeof u === \"object\" &&\n u.toString !== Object.prototype.toString\n ) {\n return u.toString();\n }\n\n try {\n // Attempt to JSON stringify the object for inspection.\n return JSON.stringify(u);\n } catch {\n // Fallback if the object cannot be stringified (e.g., circular references).\n return String(u);\n }\n}\n"],"mappings":"AAIO,SAASA,EACdC,EACAC,EACQ,CACR,GAAI,OAAOD,GAAM,SACf,OAAOA,EAGT,GAAIA,aAAa,MAAO,CACtB,IAAME,EAAQF,EACVG,EAAS,GAGPC,EAAYF,EAAM,MAAQ,QAC1BG,EACJH,EAAM,SAAW,8CAKnB,GAHAC,GAAU,GAAGC,CAAS,KAAKC,CAAY,GAGnCJ,GAAS,cAAgBC,EAAM,MAAO,CAGxC,IAAMI,EAAQJ,EAAM,MAEjB,QAAQ,IAAI,OAAO,IAAIE,CAAS,SAAS,EAAG,EAAE,EAC9C,KAAK,EAEJE,IACFH,GAAU;AAAA;AAAA,EAAmBG,CAAK,GAEtC,CAGA,GAAIL,GAAS,kBAAmB,CAE9B,IAAMM,EAA0CL,EAE1CM,EAAiB,OAAO,KAAKD,CAAW,EAC3C,OACEE,GACCA,IAAQ,QACRA,IAAQ,WACRA,IAAQ,SACR,OAAOF,EAAYE,CAAG,GAAM,YAC5B,OAAOF,EAAYE,CAAG,GAAM,QAChC,EACC,IAAKA,GAAQ;AAAA,IAAOA,CAAG,KAAKF,EAAYE,CAAG,CAAC,EAAE,EAE7CD,EAAe,OAAS,IAC1BL,GAAU;AAAA,kBAAqBK,EAAe,KAAK,EAAE,CAAC,GAE1D,CAEA,OAAOL,CACT,CAGA,GACEH,IAAM,MACN,OAAOA,GAAM,UACbA,EAAE,WAAa,OAAO,UAAU,SAEhC,OAAOA,EAAE,SAAS,EAGpB,GAAI,CAEF,OAAO,KAAK,UAAUA,CAAC,CACzB,MAAQ,CAEN,OAAO,OAAOA,CAAC,CACjB,CACF","names":["toReadableString","u","options","error","result","errorName","errorMessage","stack","customProps","additionalInfo","key"]}
@@ -0,0 +1,44 @@
1
+ // src/toReadableString.ts
2
+ function toReadableString(u, options) {
3
+ if (typeof u === "string") {
4
+ return u;
5
+ }
6
+ if (u instanceof Error) {
7
+ const error = u;
8
+ let result = "";
9
+ const errorName = error.name || "Error";
10
+ const errorMessage = error.message || "An error occurred with no message provided.";
11
+ result += `${errorName}: ${errorMessage}`;
12
+ if (options?.includeStack && error.stack) {
13
+ const stack = error.stack.replace(new RegExp(`^${errorName}:.*\\n?`), "").trim();
14
+ if (stack) {
15
+ result += `
16
+ Stack Trace:
17
+ ${stack}`;
18
+ }
19
+ }
20
+ if (options?.includeErrorProps) {
21
+ const customProps = error;
22
+ const additionalInfo = Object.keys(customProps).filter(
23
+ (key) => key !== "name" && key !== "message" && key !== "stack" && typeof customProps[key] !== "function" && typeof customProps[key] !== "object"
24
+ ).map((key) => `
25
+ - ${key}: ${customProps[key]}`);
26
+ if (additionalInfo.length > 0) {
27
+ result += `
28
+ Additional Data:${additionalInfo.join("")}`;
29
+ }
30
+ }
31
+ return result;
32
+ }
33
+ if (u !== null && typeof u === "object" && u.toString !== Object.prototype.toString) {
34
+ return u.toString();
35
+ }
36
+ try {
37
+ return JSON.stringify(u);
38
+ } catch {
39
+ return String(u);
40
+ }
41
+ }
42
+ export {
43
+ toReadableString
44
+ };
package/average.cjs DELETED
@@ -1,55 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/average.ts
21
- var average_exports = {};
22
- __export(average_exports, {
23
- average: () => average
24
- });
25
- module.exports = __toCommonJS(average_exports);
26
-
27
- // src/asNumber.ts
28
- function asNumber(u) {
29
- if (typeof u === "number" && isFinite(u)) {
30
- return u;
31
- }
32
- const n = Number(u);
33
- if (typeof n === "number" && isFinite(n)) {
34
- return n;
35
- }
36
- return 0;
37
- }
38
-
39
- // src/sum.ts
40
- function sum(numbers) {
41
- return numbers.reduce((accumulated, current) => {
42
- return accumulated + asNumber(current);
43
- }, 0);
44
- }
45
-
46
- // src/average.ts
47
- function average(numbers) {
48
- if (numbers.length === 0) return 0;
49
- const total = sum(numbers);
50
- return total / numbers.length;
51
- }
52
- // Annotate the CommonJS export names for ESM import in node:
53
- 0 && (module.exports = {
54
- average
55
- });
package/average.d.mts DELETED
@@ -1,7 +0,0 @@
1
- /**
2
- * Average all the numbers together in the given array. Treats null, undefined
3
- * and NaN as zero.
4
- */
5
- declare function average(numbers: (number | null | undefined)[]): number;
6
-
7
- export { average };
package/average.d.ts DELETED
@@ -1,7 +0,0 @@
1
- /**
2
- * Average all the numbers together in the given array. Treats null, undefined
3
- * and NaN as zero.
4
- */
5
- declare function average(numbers: (number | null | undefined)[]): number;
6
-
7
- export { average };
package/average.min.cjs DELETED
@@ -1,2 +0,0 @@
1
- "use strict";var u=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var a=(n,r)=>{for(var e in r)u(n,e,{get:r[e],enumerable:!0})},c=(n,r,e,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of b(r))!p.call(n,t)&&t!==e&&u(n,t,{get:()=>r[t],enumerable:!(o=f(r,t))||o.enumerable});return n};var l=n=>c(u({},"__esModule",{value:!0}),n);var d={};a(d,{average:()=>s});module.exports=l(d);function i(n){if(typeof n=="number"&&isFinite(n))return n;let r=Number(n);return typeof r=="number"&&isFinite(r)?r:0}function m(n){return n.reduce((r,e)=>r+i(e),0)}function s(n){return n.length===0?0:m(n)/n.length}0&&(module.exports={average});
2
- //# sourceMappingURL=average.min.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/average.ts","../src/asNumber.ts","../src/sum.ts"],"sourcesContent":["import { sum } from \"./sum\";\n\n/**\n * Average all the numbers together in the given array. Treats null, undefined\n * and NaN as zero.\n */\nexport function average(numbers: (number | null | undefined)[]): number {\n if (numbers.length === 0) return 0;\n\n const total = sum(numbers);\n\n return total / numbers.length;\n}\n","/**\n * Coerce `u` into a number if possible, otherwise just return 0.\n */\nexport function asNumber(u: unknown): number {\n // No transformation needed if u is already a number.\n if (typeof u === \"number\" && isFinite(u)) {\n return u;\n }\n\n // Try to make into a number if possible.\n const n = Number(u);\n if (typeof n === \"number\" && isFinite(n)) {\n return n;\n }\n\n // Return 0 for everything else. This is usually ok if want to just ignore\n // all other noise.\n return 0;\n}\n","import { asNumber } from \"./asNumber\";\n\n/**\n * Add all the numbers together in the given array.\n * Non-numbers will be coerced into numbers if possible.\n */\nexport function sum(numbers: unknown[]): number {\n return numbers.reduce((accumulated: number, current: unknown) => {\n return accumulated + asNumber(current);\n }, 0);\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GCGO,SAASI,EAASC,EAAoB,CAE3C,GAAI,OAAOA,GAAM,UAAY,SAASA,CAAC,EACrC,OAAOA,EAIT,IAAMC,EAAI,OAAOD,CAAC,EAClB,OAAI,OAAOC,GAAM,UAAY,SAASA,CAAC,EAC9BA,EAKF,CACT,CCZO,SAASC,EAAIC,EAA4B,CAC9C,OAAOA,EAAQ,OAAO,CAACC,EAAqBC,IACnCD,EAAcE,EAASD,CAAO,EACpC,CAAC,CACN,CFJO,SAASE,EAAQC,EAAgD,CACtE,OAAIA,EAAQ,SAAW,EAAU,EAEnBC,EAAID,CAAO,EAEVA,EAAQ,MACzB","names":["average_exports","__export","average","__toCommonJS","asNumber","u","n","sum","numbers","accumulated","current","asNumber","average","numbers","sum"]}
package/average.min.mjs DELETED
@@ -1,2 +0,0 @@
1
- function e(n){if(typeof n=="number"&&isFinite(n))return n;let r=Number(n);return typeof r=="number"&&isFinite(r)?r:0}function t(n){return n.reduce((r,u)=>r+e(u),0)}function b(n){return n.length===0?0:t(n)/n.length}export{b as average};
2
- //# sourceMappingURL=average.min.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/asNumber.ts","../src/sum.ts","../src/average.ts"],"sourcesContent":["/**\n * Coerce `u` into a number if possible, otherwise just return 0.\n */\nexport function asNumber(u: unknown): number {\n // No transformation needed if u is already a number.\n if (typeof u === \"number\" && isFinite(u)) {\n return u;\n }\n\n // Try to make into a number if possible.\n const n = Number(u);\n if (typeof n === \"number\" && isFinite(n)) {\n return n;\n }\n\n // Return 0 for everything else. This is usually ok if want to just ignore\n // all other noise.\n return 0;\n}\n","import { asNumber } from \"./asNumber\";\n\n/**\n * Add all the numbers together in the given array.\n * Non-numbers will be coerced into numbers if possible.\n */\nexport function sum(numbers: unknown[]): number {\n return numbers.reduce((accumulated: number, current: unknown) => {\n return accumulated + asNumber(current);\n }, 0);\n}\n","import { sum } from \"./sum\";\n\n/**\n * Average all the numbers together in the given array. Treats null, undefined\n * and NaN as zero.\n */\nexport function average(numbers: (number | null | undefined)[]): number {\n if (numbers.length === 0) return 0;\n\n const total = sum(numbers);\n\n return total / numbers.length;\n}\n"],"mappings":"AAGO,SAASA,EAASC,EAAoB,CAE3C,GAAI,OAAOA,GAAM,UAAY,SAASA,CAAC,EACrC,OAAOA,EAIT,IAAMC,EAAI,OAAOD,CAAC,EAClB,OAAI,OAAOC,GAAM,UAAY,SAASA,CAAC,EAC9BA,EAKF,CACT,CCZO,SAASC,EAAIC,EAA4B,CAC9C,OAAOA,EAAQ,OAAO,CAACC,EAAqBC,IACnCD,EAAcE,EAASD,CAAO,EACpC,CAAC,CACN,CCJO,SAASE,EAAQC,EAAgD,CACtE,OAAIA,EAAQ,SAAW,EAAU,EAEnBC,EAAID,CAAO,EAEVA,EAAQ,MACzB","names":["asNumber","u","n","sum","numbers","accumulated","current","asNumber","average","numbers","sum"]}
package/average.mjs DELETED
@@ -1,28 +0,0 @@
1
- // src/asNumber.ts
2
- function asNumber(u) {
3
- if (typeof u === "number" && isFinite(u)) {
4
- return u;
5
- }
6
- const n = Number(u);
7
- if (typeof n === "number" && isFinite(n)) {
8
- return n;
9
- }
10
- return 0;
11
- }
12
-
13
- // src/sum.ts
14
- function sum(numbers) {
15
- return numbers.reduce((accumulated, current) => {
16
- return accumulated + asNumber(current);
17
- }, 0);
18
- }
19
-
20
- // src/average.ts
21
- function average(numbers) {
22
- if (numbers.length === 0) return 0;
23
- const total = sum(numbers);
24
- return total / numbers.length;
25
- }
26
- export {
27
- average
28
- };
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/iterators.ts"],"sourcesContent":["/** Memory-efficient way to concat two or more iterators. */\nexport function* concatIterators<T>(...iterators: Generator<T>[]) {\n for (const iterator of iterators) {\n yield* iterator;\n }\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,qBAAAE,IAAA,eAAAC,EAAAH,GACO,SAAUE,KAAsBE,EAA2B,CAChE,QAAWC,KAAYD,EACrB,MAAOC,CAEX","names":["iterators_exports","__export","concatIterators","__toCommonJS","iterators","iterator"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/iterators.ts"],"sourcesContent":["/** Memory-efficient way to concat two or more iterators. */\nexport function* concatIterators<T>(...iterators: Generator<T>[]) {\n for (const iterator of iterators) {\n yield* iterator;\n }\n}\n"],"mappings":"AACO,SAAUA,KAAsBC,EAA2B,CAChE,QAAWC,KAAYD,EACrB,MAAOC,CAEX","names":["concatIterators","iterators","iterator"]}
package/logging.cjs DELETED
@@ -1,47 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/logging.ts
21
- var logging_exports = {};
22
- __export(logging_exports, {
23
- capLength: () => capLength,
24
- stringify: () => stringify
25
- });
26
- module.exports = __toCommonJS(logging_exports);
27
- function stringify(u) {
28
- if (typeof u === "string") {
29
- return u;
30
- }
31
- if (u !== null && typeof u === "object" && u.toString !== Object.prototype.toString) {
32
- return u.toString();
33
- }
34
- return JSON.stringify(u);
35
- }
36
- function capLength(u, maxLength = 400) {
37
- const s = stringify(u);
38
- if (s.length <= maxLength) {
39
- return s;
40
- }
41
- return s.slice(0, maxLength) + ` ... (${s.length - maxLength} more)`;
42
- }
43
- // Annotate the CommonJS export names for ESM import in node:
44
- 0 && (module.exports = {
45
- capLength,
46
- stringify
47
- });
package/logging.d.mts DELETED
@@ -1,4 +0,0 @@
1
- declare function stringify(u: unknown): string;
2
- declare function capLength(u: unknown, maxLength?: number): string;
3
-
4
- export { capLength, stringify };
package/logging.d.ts DELETED
@@ -1,4 +0,0 @@
1
- declare function stringify(u: unknown): string;
2
- declare function capLength(u: unknown, maxLength?: number): string;
3
-
4
- export { capLength, stringify };
package/logging.min.cjs DELETED
@@ -1,2 +0,0 @@
1
- "use strict";var o=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var s=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var p=(t,n)=>{for(var r in n)o(t,r,{get:n[r],enumerable:!0})},l=(t,n,r,e)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of s(n))!c.call(t,i)&&i!==r&&o(t,i,{get:()=>n[i],enumerable:!(e=g(n,i))||e.enumerable});return t};var y=t=>l(o({},"__esModule",{value:!0}),t);var S={};p(S,{capLength:()=>u,stringify:()=>f});module.exports=y(S);function f(t){return typeof t=="string"?t:t!==null&&typeof t=="object"&&t.toString!==Object.prototype.toString?t.toString():JSON.stringify(t)}function u(t,n=400){let r=f(t);return r.length<=n?r:r.slice(0,n)+` ... (${r.length-n} more)`}0&&(module.exports={capLength,stringify});
2
- //# sourceMappingURL=logging.min.cjs.map