@choksheak/ts-utils 0.3.2 → 0.3.4

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 (105) hide show
  1. package/{logging.cjs → asNumber.cjs} +13 -21
  2. package/asNumber.d.mts +6 -0
  3. package/asNumber.d.ts +6 -0
  4. package/asNumber.min.cjs +2 -0
  5. package/asNumber.min.cjs.map +1 -0
  6. package/asNumber.min.mjs +2 -0
  7. package/asNumber.min.mjs.map +1 -0
  8. package/asNumber.mjs +14 -0
  9. package/capLength.cjs +80 -0
  10. package/capLength.d.mts +3 -0
  11. package/capLength.d.ts +3 -0
  12. package/capLength.min.cjs +6 -0
  13. package/capLength.min.cjs.map +1 -0
  14. package/capLength.min.mjs +6 -0
  15. package/capLength.min.mjs.map +1 -0
  16. package/capLength.mjs +53 -0
  17. package/{iterators.cjs → concatIterators.cjs} +4 -4
  18. package/{iterators.min.cjs → concatIterators.min.cjs} +1 -1
  19. package/concatIterators.min.cjs.map +1 -0
  20. package/{iterators.min.mjs → concatIterators.min.mjs} +1 -1
  21. package/concatIterators.min.mjs.map +1 -0
  22. package/{iterators.mjs → concatIterators.mjs} +1 -1
  23. package/duration.min.cjs +1 -1
  24. package/duration.min.mjs +1 -1
  25. package/isPromise.cjs +40 -0
  26. package/isPromise.d.mts +19 -0
  27. package/isPromise.d.ts +19 -0
  28. package/isPromise.min.cjs +2 -0
  29. package/isPromise.min.cjs.map +1 -0
  30. package/isPromise.min.mjs +2 -0
  31. package/isPromise.min.mjs.map +1 -0
  32. package/isPromise.mjs +14 -0
  33. package/kvStore.min.cjs +1 -1
  34. package/kvStore.min.mjs +1 -1
  35. package/localStore.min.cjs +1 -1
  36. package/localStore.min.mjs +1 -1
  37. package/{average.cjs → mean.cjs} +24 -18
  38. package/mean.d.mts +7 -0
  39. package/mean.d.ts +7 -0
  40. package/mean.min.cjs +2 -0
  41. package/mean.min.cjs.map +1 -0
  42. package/mean.min.mjs +2 -0
  43. package/mean.min.mjs.map +1 -0
  44. package/mean.mjs +26 -0
  45. package/median.cjs +56 -0
  46. package/median.d.mts +7 -0
  47. package/median.d.ts +7 -0
  48. package/median.min.cjs +2 -0
  49. package/median.min.cjs.map +1 -0
  50. package/median.min.mjs +2 -0
  51. package/median.min.mjs.map +1 -0
  52. package/median.mjs +29 -0
  53. package/package.json +118 -37
  54. package/round.cjs +2 -7
  55. package/round.d.mts +2 -7
  56. package/round.d.ts +2 -7
  57. package/round.min.cjs +1 -1
  58. package/round.min.cjs.map +1 -1
  59. package/round.min.mjs +1 -1
  60. package/round.min.mjs.map +1 -1
  61. package/round.mjs +1 -5
  62. package/roundToString.cjs +43 -0
  63. package/roundToString.d.mts +8 -0
  64. package/roundToString.d.ts +8 -0
  65. package/roundToString.min.cjs +2 -0
  66. package/roundToString.min.cjs.map +1 -0
  67. package/roundToString.min.mjs +2 -0
  68. package/roundToString.min.mjs.map +1 -0
  69. package/roundToString.mjs +16 -0
  70. package/sum.cjs +17 -7
  71. package/sum.d.mts +3 -3
  72. package/sum.d.ts +3 -3
  73. package/sum.min.cjs +1 -1
  74. package/sum.min.cjs.map +1 -1
  75. package/sum.min.mjs +1 -1
  76. package/sum.min.mjs.map +1 -1
  77. package/sum.mjs +15 -7
  78. package/timer.min.cjs +1 -1
  79. package/timer.min.mjs +1 -1
  80. package/toReadableString.cjs +69 -0
  81. package/toReadableString.d.mts +10 -0
  82. package/toReadableString.d.ts +10 -0
  83. package/toReadableString.min.cjs +6 -0
  84. package/toReadableString.min.cjs.map +1 -0
  85. package/toReadableString.min.mjs +6 -0
  86. package/toReadableString.min.mjs.map +1 -0
  87. package/toReadableString.mjs +44 -0
  88. package/average.d.mts +0 -7
  89. package/average.d.ts +0 -7
  90. package/average.min.cjs +0 -2
  91. package/average.min.cjs.map +0 -1
  92. package/average.min.mjs +0 -2
  93. package/average.min.mjs.map +0 -1
  94. package/average.mjs +0 -20
  95. package/iterators.min.cjs.map +0 -1
  96. package/iterators.min.mjs.map +0 -1
  97. package/logging.d.mts +0 -4
  98. package/logging.d.ts +0 -4
  99. package/logging.min.cjs +0 -2
  100. package/logging.min.cjs.map +0 -1
  101. package/logging.min.mjs +0 -2
  102. package/logging.min.mjs.map +0 -1
  103. package/logging.mjs +0 -21
  104. /package/{iterators.d.mts → concatIterators.d.mts} +0 -0
  105. /package/{iterators.d.ts → concatIterators.d.ts} +0 -0
@@ -17,31 +17,37 @@ var __copyProps = (to, from, except, desc) => {
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
- // src/average.ts
21
- var average_exports = {};
22
- __export(average_exports, {
23
- average: () => average
20
+ // src/mean.ts
21
+ var mean_exports = {};
22
+ __export(mean_exports, {
23
+ mean: () => mean
24
24
  });
25
- module.exports = __toCommonJS(average_exports);
25
+ module.exports = __toCommonJS(mean_exports);
26
+
27
+ // src/asNumber.ts
28
+ function asNumber(u) {
29
+ if (typeof u === "number") {
30
+ return isFinite(u) ? u : 0;
31
+ }
32
+ u = Number(u);
33
+ if (typeof u === "number" && isFinite(u)) {
34
+ return u;
35
+ }
36
+ return 0;
37
+ }
26
38
 
27
39
  // src/sum.ts
28
40
  function sum(numbers) {
29
- return numbers.reduce(
30
- (accumulated, current) => {
31
- const value = typeof current === "number" && !isNaN(current) && isFinite(current) ? current : 0;
32
- return accumulated + value;
33
- },
34
- 0
35
- );
41
+ return numbers.reduce((accumulated, current) => {
42
+ return accumulated + asNumber(current);
43
+ }, 0);
36
44
  }
37
45
 
38
- // src/average.ts
39
- function average(numbers) {
40
- if (numbers.length === 0) return 0;
41
- const total = sum(numbers);
42
- return total / numbers.length;
46
+ // src/mean.ts
47
+ function mean(numbers) {
48
+ return numbers.length > 0 ? sum(numbers) / numbers.length : 0;
43
49
  }
44
50
  // Annotate the CommonJS export names for ESM import in node:
45
51
  0 && (module.exports = {
46
- average
52
+ mean
47
53
  });
package/mean.d.mts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Compute the mean (average) of all the numbers in the given array.
3
+ * Non-numbers will be coerced into numbers if possible.
4
+ */
5
+ declare function mean(numbers: unknown[]): number;
6
+
7
+ export { mean };
package/mean.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Compute the mean (average) of all the numbers in the given array.
3
+ * Non-numbers will be coerced into numbers if possible.
4
+ */
5
+ declare function mean(numbers: unknown[]): number;
6
+
7
+ export { mean };
package/mean.min.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";var o=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var c=(n,r)=>{for(var e in r)o(n,e,{get:r[e],enumerable:!0})},k=(n,r,e,u)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of b(r))!p.call(n,t)&&t!==e&&o(n,t,{get:()=>r[t],enumerable:!(u=f(r,t))||u.enumerable});return n};var s=n=>k(o({},"__esModule",{value:!0}),n);var a={};c(a,{mean:()=>w});module.exports=s(a);function m(n){return typeof n=="number"?isFinite(n)?n:0:(n=Number(n),typeof n=="number"&&isFinite(n)?n:0)}function i(n){return n.reduce((r,e)=>r+m(e),0)}function w(n){return n.length>0?i(n)/n.length:0}0&&(module.exports={mean});
2
+ //# sourceMappingURL=mean.min.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/mean.ts","../src/asNumber.ts","../src/sum.ts"],"sourcesContent":["import { sum } from \"./sum\";\n\n/**\n * Compute the mean (average) of all the numbers in the given array.\n * Non-numbers will be coerced into numbers if possible.\n */\nexport function mean(numbers: unknown[]): number {\n return numbers.length > 0 ? sum(numbers) / numbers.length : 0;\n}\n","/**\n * Coerce `u` into a number if possible, otherwise just return 0.\n */\nexport function asNumber(u: unknown): number {\n // If u is a valid number, return it.\n if (typeof u === \"number\") {\n return isFinite(u) ? u : 0;\n }\n\n // Try to make into a number if not already a number.\n u = Number(u);\n\n // If u is a valid number, return it.\n if (typeof u === \"number\" && isFinite(u)) {\n return u;\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,UAAAE,IAAA,eAAAC,EAAAH,GCGO,SAASI,EAASC,EAAoB,CAE3C,OAAI,OAAOA,GAAM,SACR,SAASA,CAAC,EAAIA,EAAI,GAI3BA,EAAI,OAAOA,CAAC,EAGR,OAAOA,GAAM,UAAY,SAASA,CAAC,EAC9BA,EAKF,EACT,CCdO,SAASC,EAAIC,EAA4B,CAC9C,OAAOA,EAAQ,OAAO,CAACC,EAAqBC,IACnCD,EAAcE,EAASD,CAAO,EACpC,CAAC,CACN,CFJO,SAASE,EAAKC,EAA4B,CAC/C,OAAOA,EAAQ,OAAS,EAAIC,EAAID,CAAO,EAAIA,EAAQ,OAAS,CAC9D","names":["mean_exports","__export","mean","__toCommonJS","asNumber","u","sum","numbers","accumulated","current","asNumber","mean","numbers","sum"]}
package/mean.min.mjs ADDED
@@ -0,0 +1,2 @@
1
+ function r(n){return typeof n=="number"?isFinite(n)?n:0:(n=Number(n),typeof n=="number"&&isFinite(n)?n:0)}function e(n){return n.reduce((t,o)=>t+r(o),0)}function b(n){return n.length>0?e(n)/n.length:0}export{b as mean};
2
+ //# sourceMappingURL=mean.min.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/asNumber.ts","../src/sum.ts","../src/mean.ts"],"sourcesContent":["/**\n * Coerce `u` into a number if possible, otherwise just return 0.\n */\nexport function asNumber(u: unknown): number {\n // If u is a valid number, return it.\n if (typeof u === \"number\") {\n return isFinite(u) ? u : 0;\n }\n\n // Try to make into a number if not already a number.\n u = Number(u);\n\n // If u is a valid number, return it.\n if (typeof u === \"number\" && isFinite(u)) {\n return u;\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 * Compute the mean (average) of all the numbers in the given array.\n * Non-numbers will be coerced into numbers if possible.\n */\nexport function mean(numbers: unknown[]): number {\n return numbers.length > 0 ? sum(numbers) / numbers.length : 0;\n}\n"],"mappings":"AAGO,SAASA,EAASC,EAAoB,CAE3C,OAAI,OAAOA,GAAM,SACR,SAASA,CAAC,EAAIA,EAAI,GAI3BA,EAAI,OAAOA,CAAC,EAGR,OAAOA,GAAM,UAAY,SAASA,CAAC,EAC9BA,EAKF,EACT,CCdO,SAASC,EAAIC,EAA4B,CAC9C,OAAOA,EAAQ,OAAO,CAACC,EAAqBC,IACnCD,EAAcE,EAASD,CAAO,EACpC,CAAC,CACN,CCJO,SAASE,EAAKC,EAA4B,CAC/C,OAAOA,EAAQ,OAAS,EAAIC,EAAID,CAAO,EAAIA,EAAQ,OAAS,CAC9D","names":["asNumber","u","sum","numbers","accumulated","current","asNumber","mean","numbers","sum"]}
package/mean.mjs ADDED
@@ -0,0 +1,26 @@
1
+ // src/asNumber.ts
2
+ function asNumber(u) {
3
+ if (typeof u === "number") {
4
+ return isFinite(u) ? u : 0;
5
+ }
6
+ u = Number(u);
7
+ if (typeof u === "number" && isFinite(u)) {
8
+ return u;
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/mean.ts
21
+ function mean(numbers) {
22
+ return numbers.length > 0 ? sum(numbers) / numbers.length : 0;
23
+ }
24
+ export {
25
+ mean
26
+ };
package/median.cjs ADDED
@@ -0,0 +1,56 @@
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/median.ts
21
+ var median_exports = {};
22
+ __export(median_exports, {
23
+ median: () => median
24
+ });
25
+ module.exports = __toCommonJS(median_exports);
26
+
27
+ // src/asNumber.ts
28
+ function asNumber(u) {
29
+ if (typeof u === "number") {
30
+ return isFinite(u) ? u : 0;
31
+ }
32
+ u = Number(u);
33
+ if (typeof u === "number" && isFinite(u)) {
34
+ return u;
35
+ }
36
+ return 0;
37
+ }
38
+
39
+ // src/median.ts
40
+ function median(numbers) {
41
+ if (numbers.length === 0) {
42
+ return 0;
43
+ }
44
+ const sorted = numbers.map(asNumber).slice().sort();
45
+ const middleIndex = Math.floor(sorted.length / 2);
46
+ if (sorted.length % 2 === 1) {
47
+ return sorted[middleIndex];
48
+ }
49
+ const value1 = sorted[middleIndex - 1];
50
+ const value2 = sorted[middleIndex];
51
+ return (value1 + value2) / 2;
52
+ }
53
+ // Annotate the CommonJS export names for ESM import in node:
54
+ 0 && (module.exports = {
55
+ median
56
+ });
package/median.d.mts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Compute the median (middle number) of all the numbers in the given array.
3
+ * Non-numbers will be coerced into numbers if possible.
4
+ */
5
+ declare function median(numbers: unknown[]): number;
6
+
7
+ export { median };
package/median.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Compute the median (middle number) of all the numbers in the given array.
3
+ * Non-numbers will be coerced into numbers if possible.
4
+ */
5
+ declare function median(numbers: unknown[]): number;
6
+
7
+ export { median };
package/median.min.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";var i=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var s=Object.prototype.hasOwnProperty;var l=(n,e)=>{for(var r in e)i(n,r,{get:e[r],enumerable:!0})},a=(n,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of f(e))!s.call(n,t)&&t!==r&&i(n,t,{get:()=>e[t],enumerable:!(o=u(e,t))||o.enumerable});return n};var c=n=>a(i({},"__esModule",{value:!0}),n);var p={};l(p,{median:()=>b});module.exports=c(p);function m(n){return typeof n=="number"?isFinite(n)?n:0:(n=Number(n),typeof n=="number"&&isFinite(n)?n:0)}function b(n){if(n.length===0)return 0;let e=n.map(m).slice().sort(),r=Math.floor(e.length/2);if(e.length%2===1)return e[r];let o=e[r-1],t=e[r];return(o+t)/2}0&&(module.exports={median});
2
+ //# sourceMappingURL=median.min.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/median.ts","../src/asNumber.ts"],"sourcesContent":["import { asNumber } from \"./asNumber\";\n\n/**\n * Compute the median (middle number) of all the numbers in the given array.\n * Non-numbers will be coerced into numbers if possible.\n */\nexport function median(numbers: unknown[]): number {\n if (numbers.length === 0) {\n return 0;\n }\n\n // Create a copy with slice() to avoid mutating the original array.\n const sorted = numbers.map(asNumber).slice().sort();\n\n const middleIndex = Math.floor(sorted.length / 2);\n\n // Is odd length -> return middle number.\n if (sorted.length % 2 === 1) {\n return sorted[middleIndex];\n }\n\n // Is even length -> return mean of middle 2 numbers.\n const value1 = sorted[middleIndex - 1];\n const value2 = sorted[middleIndex];\n return (value1 + value2) / 2;\n}\n","/**\n * Coerce `u` into a number if possible, otherwise just return 0.\n */\nexport function asNumber(u: unknown): number {\n // If u is a valid number, return it.\n if (typeof u === \"number\") {\n return isFinite(u) ? u : 0;\n }\n\n // Try to make into a number if not already a number.\n u = Number(u);\n\n // If u is a valid number, return it.\n if (typeof u === \"number\" && isFinite(u)) {\n return u;\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"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,YAAAE,IAAA,eAAAC,EAAAH,GCGO,SAASI,EAASC,EAAoB,CAE3C,OAAI,OAAOA,GAAM,SACR,SAASA,CAAC,EAAIA,EAAI,GAI3BA,EAAI,OAAOA,CAAC,EAGR,OAAOA,GAAM,UAAY,SAASA,CAAC,EAC9BA,EAKF,EACT,CDdO,SAASC,EAAOC,EAA4B,CACjD,GAAIA,EAAQ,SAAW,EACrB,MAAO,GAIT,IAAMC,EAASD,EAAQ,IAAIE,CAAQ,EAAE,MAAM,EAAE,KAAK,EAE5CC,EAAc,KAAK,MAAMF,EAAO,OAAS,CAAC,EAGhD,GAAIA,EAAO,OAAS,IAAM,EACxB,OAAOA,EAAOE,CAAW,EAI3B,IAAMC,EAASH,EAAOE,EAAc,CAAC,EAC/BE,EAASJ,EAAOE,CAAW,EACjC,OAAQC,EAASC,GAAU,CAC7B","names":["median_exports","__export","median","__toCommonJS","asNumber","u","median","numbers","sorted","asNumber","middleIndex","value1","value2"]}
package/median.min.mjs ADDED
@@ -0,0 +1,2 @@
1
+ function t(n){return typeof n=="number"?isFinite(n)?n:0:(n=Number(n),typeof n=="number"&&isFinite(n)?n:0)}function f(n){if(n.length===0)return 0;let e=n.map(t).slice().sort(),r=Math.floor(e.length/2);if(e.length%2===1)return e[r];let o=e[r-1],i=e[r];return(o+i)/2}export{f as median};
2
+ //# sourceMappingURL=median.min.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/asNumber.ts","../src/median.ts"],"sourcesContent":["/**\n * Coerce `u` into a number if possible, otherwise just return 0.\n */\nexport function asNumber(u: unknown): number {\n // If u is a valid number, return it.\n if (typeof u === \"number\") {\n return isFinite(u) ? u : 0;\n }\n\n // Try to make into a number if not already a number.\n u = Number(u);\n\n // If u is a valid number, return it.\n if (typeof u === \"number\" && isFinite(u)) {\n return u;\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 * Compute the median (middle number) of all the numbers in the given array.\n * Non-numbers will be coerced into numbers if possible.\n */\nexport function median(numbers: unknown[]): number {\n if (numbers.length === 0) {\n return 0;\n }\n\n // Create a copy with slice() to avoid mutating the original array.\n const sorted = numbers.map(asNumber).slice().sort();\n\n const middleIndex = Math.floor(sorted.length / 2);\n\n // Is odd length -> return middle number.\n if (sorted.length % 2 === 1) {\n return sorted[middleIndex];\n }\n\n // Is even length -> return mean of middle 2 numbers.\n const value1 = sorted[middleIndex - 1];\n const value2 = sorted[middleIndex];\n return (value1 + value2) / 2;\n}\n"],"mappings":"AAGO,SAASA,EAASC,EAAoB,CAE3C,OAAI,OAAOA,GAAM,SACR,SAASA,CAAC,EAAIA,EAAI,GAI3BA,EAAI,OAAOA,CAAC,EAGR,OAAOA,GAAM,UAAY,SAASA,CAAC,EAC9BA,EAKF,EACT,CCdO,SAASC,EAAOC,EAA4B,CACjD,GAAIA,EAAQ,SAAW,EACrB,MAAO,GAIT,IAAMC,EAASD,EAAQ,IAAIE,CAAQ,EAAE,MAAM,EAAE,KAAK,EAE5CC,EAAc,KAAK,MAAMF,EAAO,OAAS,CAAC,EAGhD,GAAIA,EAAO,OAAS,IAAM,EACxB,OAAOA,EAAOE,CAAW,EAI3B,IAAMC,EAASH,EAAOE,EAAc,CAAC,EAC/BE,EAASJ,EAAOE,CAAW,EACjC,OAAQC,EAASC,GAAU,CAC7B","names":["asNumber","u","median","numbers","sorted","asNumber","middleIndex","value1","value2"]}
package/median.mjs ADDED
@@ -0,0 +1,29 @@
1
+ // src/asNumber.ts
2
+ function asNumber(u) {
3
+ if (typeof u === "number") {
4
+ return isFinite(u) ? u : 0;
5
+ }
6
+ u = Number(u);
7
+ if (typeof u === "number" && isFinite(u)) {
8
+ return u;
9
+ }
10
+ return 0;
11
+ }
12
+
13
+ // src/median.ts
14
+ function median(numbers) {
15
+ if (numbers.length === 0) {
16
+ return 0;
17
+ }
18
+ const sorted = numbers.map(asNumber).slice().sort();
19
+ const middleIndex = Math.floor(sorted.length / 2);
20
+ if (sorted.length % 2 === 1) {
21
+ return sorted[middleIndex];
22
+ }
23
+ const value1 = sorted[middleIndex - 1];
24
+ const value2 = sorted[middleIndex];
25
+ return (value1 + value2) / 2;
26
+ }
27
+ export {
28
+ median
29
+ };
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@choksheak/ts-utils",
3
3
  "license": "The Unlicense",
4
- "version": "0.3.2",
4
+ "version": "0.3.4",
5
5
  "description": "Random Typescript utilities with support for full tree-shaking",
6
6
  "private": false,
7
+ "sideEffects": false,
7
8
  "scripts": {
8
9
  "lint": "eslint .",
9
10
  "fix": "eslint . --fix",
@@ -98,6 +99,19 @@
98
99
  "default": "./arrayBuffer.cjs"
99
100
  }
100
101
  },
102
+ "./asNumber": {
103
+ "types": "./asNumber.d.ts",
104
+ "import": {
105
+ "production": "./asNumber.min.mjs",
106
+ "development": "./asNumber.mjs",
107
+ "default": "./asNumber.mjs"
108
+ },
109
+ "require": {
110
+ "production": "./asNumber.min.cjs",
111
+ "development": "./asNumber.cjs",
112
+ "default": "./asNumber.cjs"
113
+ }
114
+ },
101
115
  "./assert": {
102
116
  "types": "./assert.d.ts",
103
117
  "import": {
@@ -111,19 +125,6 @@
111
125
  "default": "./assert.cjs"
112
126
  }
113
127
  },
114
- "./average": {
115
- "types": "./average.d.ts",
116
- "import": {
117
- "production": "./average.min.mjs",
118
- "development": "./average.mjs",
119
- "default": "./average.mjs"
120
- },
121
- "require": {
122
- "production": "./average.min.cjs",
123
- "development": "./average.cjs",
124
- "default": "./average.cjs"
125
- }
126
- },
127
128
  "./base64Url": {
128
129
  "types": "./base64Url.d.ts",
129
130
  "import": {
@@ -137,6 +138,32 @@
137
138
  "default": "./base64Url.cjs"
138
139
  }
139
140
  },
141
+ "./capLength": {
142
+ "types": "./capLength.d.ts",
143
+ "import": {
144
+ "production": "./capLength.min.mjs",
145
+ "development": "./capLength.mjs",
146
+ "default": "./capLength.mjs"
147
+ },
148
+ "require": {
149
+ "production": "./capLength.min.cjs",
150
+ "development": "./capLength.cjs",
151
+ "default": "./capLength.cjs"
152
+ }
153
+ },
154
+ "./concatIterators": {
155
+ "types": "./concatIterators.d.ts",
156
+ "import": {
157
+ "production": "./concatIterators.min.mjs",
158
+ "development": "./concatIterators.mjs",
159
+ "default": "./concatIterators.mjs"
160
+ },
161
+ "require": {
162
+ "production": "./concatIterators.min.cjs",
163
+ "development": "./concatIterators.cjs",
164
+ "default": "./concatIterators.cjs"
165
+ }
166
+ },
140
167
  "./dateTimeStr": {
141
168
  "types": "./dateTimeStr.d.ts",
142
169
  "import": {
@@ -176,17 +203,17 @@
176
203
  "default": "./isEmpty.cjs"
177
204
  }
178
205
  },
179
- "./iterators": {
180
- "types": "./iterators.d.ts",
206
+ "./isPromise": {
207
+ "types": "./isPromise.d.ts",
181
208
  "import": {
182
- "production": "./iterators.min.mjs",
183
- "development": "./iterators.mjs",
184
- "default": "./iterators.mjs"
209
+ "production": "./isPromise.min.mjs",
210
+ "development": "./isPromise.mjs",
211
+ "default": "./isPromise.mjs"
185
212
  },
186
213
  "require": {
187
- "production": "./iterators.min.cjs",
188
- "development": "./iterators.cjs",
189
- "default": "./iterators.cjs"
214
+ "production": "./isPromise.min.cjs",
215
+ "development": "./isPromise.cjs",
216
+ "default": "./isPromise.cjs"
190
217
  }
191
218
  },
192
219
  "./kvStore": {
@@ -215,17 +242,30 @@
215
242
  "default": "./localStore.cjs"
216
243
  }
217
244
  },
218
- "./logging": {
219
- "types": "./logging.d.ts",
245
+ "./mean": {
246
+ "types": "./mean.d.ts",
247
+ "import": {
248
+ "production": "./mean.min.mjs",
249
+ "development": "./mean.mjs",
250
+ "default": "./mean.mjs"
251
+ },
252
+ "require": {
253
+ "production": "./mean.min.cjs",
254
+ "development": "./mean.cjs",
255
+ "default": "./mean.cjs"
256
+ }
257
+ },
258
+ "./median": {
259
+ "types": "./median.d.ts",
220
260
  "import": {
221
- "production": "./logging.min.mjs",
222
- "development": "./logging.mjs",
223
- "default": "./logging.mjs"
261
+ "production": "./median.min.mjs",
262
+ "development": "./median.mjs",
263
+ "default": "./median.mjs"
224
264
  },
225
265
  "require": {
226
- "production": "./logging.min.cjs",
227
- "development": "./logging.cjs",
228
- "default": "./logging.cjs"
266
+ "production": "./median.min.cjs",
267
+ "development": "./median.cjs",
268
+ "default": "./median.cjs"
229
269
  }
230
270
  },
231
271
  "./nonEmpty": {
@@ -267,6 +307,19 @@
267
307
  "default": "./round.cjs"
268
308
  }
269
309
  },
310
+ "./roundToString": {
311
+ "types": "./roundToString.d.ts",
312
+ "import": {
313
+ "production": "./roundToString.min.mjs",
314
+ "development": "./roundToString.mjs",
315
+ "default": "./roundToString.mjs"
316
+ },
317
+ "require": {
318
+ "production": "./roundToString.min.cjs",
319
+ "development": "./roundToString.cjs",
320
+ "default": "./roundToString.cjs"
321
+ }
322
+ },
270
323
  "./safeBtoa": {
271
324
  "types": "./safeBtoa.d.ts",
272
325
  "import": {
@@ -383,6 +436,19 @@
383
436
  "development": "./timer.cjs",
384
437
  "default": "./timer.cjs"
385
438
  }
439
+ },
440
+ "./toReadableString": {
441
+ "types": "./toReadableString.d.ts",
442
+ "import": {
443
+ "production": "./toReadableString.min.mjs",
444
+ "development": "./toReadableString.mjs",
445
+ "default": "./toReadableString.mjs"
446
+ },
447
+ "require": {
448
+ "production": "./toReadableString.min.cjs",
449
+ "development": "./toReadableString.cjs",
450
+ "default": "./toReadableString.cjs"
451
+ }
386
452
  }
387
453
  },
388
454
  "typesVersions": {
@@ -390,15 +456,21 @@
390
456
  "arrayBuffer": [
391
457
  "./arrayBuffer.d.ts"
392
458
  ],
459
+ "asNumber": [
460
+ "./asNumber.d.ts"
461
+ ],
393
462
  "assert": [
394
463
  "./assert.d.ts"
395
464
  ],
396
- "average": [
397
- "./average.d.ts"
398
- ],
399
465
  "base64Url": [
400
466
  "./base64Url.d.ts"
401
467
  ],
468
+ "capLength": [
469
+ "./capLength.d.ts"
470
+ ],
471
+ "concatIterators": [
472
+ "./concatIterators.d.ts"
473
+ ],
402
474
  "dateTimeStr": [
403
475
  "./dateTimeStr.d.ts"
404
476
  ],
@@ -408,8 +480,8 @@
408
480
  "isEmpty": [
409
481
  "./isEmpty.d.ts"
410
482
  ],
411
- "iterators": [
412
- "./iterators.d.ts"
483
+ "isPromise": [
484
+ "./isPromise.d.ts"
413
485
  ],
414
486
  "kvStore": [
415
487
  "./kvStore.d.ts"
@@ -417,8 +489,11 @@
417
489
  "localStore": [
418
490
  "./localStore.d.ts"
419
491
  ],
420
- "logging": [
421
- "./logging.d.ts"
492
+ "mean": [
493
+ "./mean.d.ts"
494
+ ],
495
+ "median": [
496
+ "./median.d.ts"
422
497
  ],
423
498
  "nonEmpty": [
424
499
  "./nonEmpty.d.ts"
@@ -429,6 +504,9 @@
429
504
  "round": [
430
505
  "./round.d.ts"
431
506
  ],
507
+ "roundToString": [
508
+ "./roundToString.d.ts"
509
+ ],
432
510
  "safeBtoa": [
433
511
  "./safeBtoa.d.ts"
434
512
  ],
@@ -455,6 +533,9 @@
455
533
  ],
456
534
  "timer": [
457
535
  "./timer.d.ts"
536
+ ],
537
+ "toReadableString": [
538
+ "./toReadableString.d.ts"
458
539
  ]
459
540
  }
460
541
  }
package/round.cjs CHANGED
@@ -20,19 +20,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/round.ts
21
21
  var round_exports = {};
22
22
  __export(round_exports, {
23
- round: () => round,
24
- roundS: () => roundS
23
+ round: () => round
25
24
  });
26
25
  module.exports = __toCommonJS(round_exports);
27
26
  function round(n, numDecimalPlaces = 0) {
28
27
  const multipler = Math.pow(10, numDecimalPlaces);
29
28
  return Math.round(n * multipler) / multipler;
30
29
  }
31
- function roundS(n, numDecimalPlaces = 0) {
32
- return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);
33
- }
34
30
  // Annotate the CommonJS export names for ESM import in node:
35
31
  0 && (module.exports = {
36
- round,
37
- roundS
32
+ round
38
33
  });
package/round.d.mts CHANGED
@@ -2,13 +2,8 @@
2
2
  * Simplifies rounding of floating point numbers. Note that if your number ends
3
3
  * with zeros, and you convert the number to string, you will lose the zeroes
4
4
  * at the end. To show the exact number of decimal places, you'll need to use
5
- * toFixed(). E.g. round(1.20, 2).toFixed(2).
5
+ * roundToString() or toFixed(). E.g. round(1.20, 2).toFixed(2).
6
6
  */
7
7
  declare function round(n: number, numDecimalPlaces?: number): number;
8
- /**
9
- * Returns a string with the number in the exact number of decimal places
10
- * specified, in case the number ends with zeroes.
11
- */
12
- declare function roundS(n: number, numDecimalPlaces?: number): string;
13
8
 
14
- export { round, roundS };
9
+ export { round };
package/round.d.ts CHANGED
@@ -2,13 +2,8 @@
2
2
  * Simplifies rounding of floating point numbers. Note that if your number ends
3
3
  * with zeros, and you convert the number to string, you will lose the zeroes
4
4
  * at the end. To show the exact number of decimal places, you'll need to use
5
- * toFixed(). E.g. round(1.20, 2).toFixed(2).
5
+ * roundToString() or toFixed(). E.g. round(1.20, 2).toFixed(2).
6
6
  */
7
7
  declare function round(n: number, numDecimalPlaces?: number): number;
8
- /**
9
- * Returns a string with the number in the exact number of decimal places
10
- * specified, in case the number ends with zeroes.
11
- */
12
- declare function roundS(n: number, numDecimalPlaces?: number): string;
13
8
 
14
- export { round, roundS };
9
+ export { round };
package/round.min.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var u=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var x=(n,r)=>{for(var t in r)u(n,t,{get:r[t],enumerable:!0})},f=(n,r,t,e)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of p(r))!b.call(n,o)&&o!==t&&u(n,o,{get:()=>r[o],enumerable:!(e=i(r,o))||e.enumerable});return n};var h=n=>f(u({},"__esModule",{value:!0}),n);var M={};x(M,{round:()=>d,roundS:()=>m});module.exports=h(M);function d(n,r=0){let t=Math.pow(10,r);return Math.round(n*t)/t}function m(n,r=0){return d(n,r).toFixed(r)}0&&(module.exports={round,roundS});
1
+ "use strict";var o=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var a=Object.prototype.hasOwnProperty;var b=(r,n)=>{for(var t in n)o(r,t,{get:n[t],enumerable:!0})},c=(r,n,t,e)=>{if(n&&typeof n=="object"||typeof n=="function")for(let u of p(n))!a.call(r,u)&&u!==t&&o(r,u,{get:()=>n[u],enumerable:!(e=m(n,u))||e.enumerable});return r};var d=r=>c(o({},"__esModule",{value:!0}),r);var i={};b(i,{round:()=>h});module.exports=d(i);function h(r,n=0){let t=Math.pow(10,n);return Math.round(r*t)/t}0&&(module.exports={round});
2
2
  //# sourceMappingURL=round.min.cjs.map
package/round.min.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/round.ts"],"sourcesContent":["/**\n * Simplifies rounding of floating point numbers. Note that if your number ends\n * with zeros, and you convert the number to string, you will lose the zeroes\n * at the end. To show the exact number of decimal places, you'll need to use\n * toFixed(). E.g. round(1.20, 2).toFixed(2).\n */\nexport function round(n: number, numDecimalPlaces = 0): number {\n const multipler = Math.pow(10, numDecimalPlaces);\n return Math.round(n * multipler) / multipler;\n}\n\n/**\n * Returns a string with the number in the exact number of decimal places\n * specified, in case the number ends with zeroes.\n */\nexport function roundS(n: number, numDecimalPlaces = 0): string {\n return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,WAAAE,EAAA,WAAAC,IAAA,eAAAC,EAAAJ,GAMO,SAASE,EAAM,EAAWG,EAAmB,EAAW,CAC7D,IAAMC,EAAY,KAAK,IAAI,GAAID,CAAgB,EAC/C,OAAO,KAAK,MAAM,EAAIC,CAAS,EAAIA,CACrC,CAMO,SAASH,EAAO,EAAWE,EAAmB,EAAW,CAC9D,OAAOH,EAAM,EAAGG,CAAgB,EAAE,QAAQA,CAAgB,CAC5D","names":["round_exports","__export","round","roundS","__toCommonJS","numDecimalPlaces","multipler"]}
1
+ {"version":3,"sources":["../src/round.ts"],"sourcesContent":["/**\n * Simplifies rounding of floating point numbers. Note that if your number ends\n * with zeros, and you convert the number to string, you will lose the zeroes\n * at the end. To show the exact number of decimal places, you'll need to use\n * roundToString() or toFixed(). E.g. round(1.20, 2).toFixed(2).\n */\nexport function round(n: number, numDecimalPlaces = 0): number {\n const multipler = Math.pow(10, numDecimalPlaces);\n return Math.round(n * multipler) / multipler;\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,WAAAE,IAAA,eAAAC,EAAAH,GAMO,SAASE,EAAME,EAAWC,EAAmB,EAAW,CAC7D,IAAMC,EAAY,KAAK,IAAI,GAAID,CAAgB,EAC/C,OAAO,KAAK,MAAMD,EAAIE,CAAS,EAAIA,CACrC","names":["round_exports","__export","round","__toCommonJS","n","numDecimalPlaces","multipler"]}
package/round.min.mjs CHANGED
@@ -1,2 +1,2 @@
1
- function o(n,r=0){let t=Math.pow(10,r);return Math.round(n*t)/t}function u(n,r=0){return o(n,r).toFixed(r)}export{o as round,u as roundS};
1
+ function u(r,t=0){let n=Math.pow(10,t);return Math.round(r*n)/n}export{u as round};
2
2
  //# sourceMappingURL=round.min.mjs.map
package/round.min.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/round.ts"],"sourcesContent":["/**\n * Simplifies rounding of floating point numbers. Note that if your number ends\n * with zeros, and you convert the number to string, you will lose the zeroes\n * at the end. To show the exact number of decimal places, you'll need to use\n * toFixed(). E.g. round(1.20, 2).toFixed(2).\n */\nexport function round(n: number, numDecimalPlaces = 0): number {\n const multipler = Math.pow(10, numDecimalPlaces);\n return Math.round(n * multipler) / multipler;\n}\n\n/**\n * Returns a string with the number in the exact number of decimal places\n * specified, in case the number ends with zeroes.\n */\nexport function roundS(n: number, numDecimalPlaces = 0): string {\n return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);\n}\n"],"mappings":"AAMO,SAASA,EAAM,EAAWC,EAAmB,EAAW,CAC7D,IAAMC,EAAY,KAAK,IAAI,GAAID,CAAgB,EAC/C,OAAO,KAAK,MAAM,EAAIC,CAAS,EAAIA,CACrC,CAMO,SAASC,EAAO,EAAWF,EAAmB,EAAW,CAC9D,OAAOD,EAAM,EAAGC,CAAgB,EAAE,QAAQA,CAAgB,CAC5D","names":["round","numDecimalPlaces","multipler","roundS"]}
1
+ {"version":3,"sources":["../src/round.ts"],"sourcesContent":["/**\n * Simplifies rounding of floating point numbers. Note that if your number ends\n * with zeros, and you convert the number to string, you will lose the zeroes\n * at the end. To show the exact number of decimal places, you'll need to use\n * roundToString() or toFixed(). E.g. round(1.20, 2).toFixed(2).\n */\nexport function round(n: number, numDecimalPlaces = 0): number {\n const multipler = Math.pow(10, numDecimalPlaces);\n return Math.round(n * multipler) / multipler;\n}\n"],"mappings":"AAMO,SAASA,EAAMC,EAAWC,EAAmB,EAAW,CAC7D,IAAMC,EAAY,KAAK,IAAI,GAAID,CAAgB,EAC/C,OAAO,KAAK,MAAMD,EAAIE,CAAS,EAAIA,CACrC","names":["round","n","numDecimalPlaces","multipler"]}
package/round.mjs CHANGED
@@ -3,10 +3,6 @@ function round(n, numDecimalPlaces = 0) {
3
3
  const multipler = Math.pow(10, numDecimalPlaces);
4
4
  return Math.round(n * multipler) / multipler;
5
5
  }
6
- function roundS(n, numDecimalPlaces = 0) {
7
- return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);
8
- }
9
6
  export {
10
- round,
11
- roundS
7
+ round
12
8
  };
@@ -0,0 +1,43 @@
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/roundToString.ts
21
+ var roundToString_exports = {};
22
+ __export(roundToString_exports, {
23
+ roundToString: () => roundToString
24
+ });
25
+ module.exports = __toCommonJS(roundToString_exports);
26
+
27
+ // src/round.ts
28
+ function round(n, numDecimalPlaces = 0) {
29
+ const multipler = Math.pow(10, numDecimalPlaces);
30
+ return Math.round(n * multipler) / multipler;
31
+ }
32
+
33
+ // src/roundToString.ts
34
+ function roundToString(n, numDecimalPlaces = 0) {
35
+ return round(n, numDecimalPlaces).toLocaleString("en-US", {
36
+ minimumFractionDigits: numDecimalPlaces,
37
+ maximumFractionDigits: numDecimalPlaces
38
+ });
39
+ }
40
+ // Annotate the CommonJS export names for ESM import in node:
41
+ 0 && (module.exports = {
42
+ roundToString
43
+ });