@choksheak/ts-utils 0.3.2 → 0.3.3

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 (55) hide show
  1. package/asNumber.cjs +39 -0
  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/average.cjs +15 -7
  10. package/average.min.cjs +1 -1
  11. package/average.min.cjs.map +1 -1
  12. package/average.min.mjs +1 -1
  13. package/average.min.mjs.map +1 -1
  14. package/average.mjs +15 -7
  15. package/isPromise.cjs +40 -0
  16. package/isPromise.d.mts +19 -0
  17. package/isPromise.d.ts +19 -0
  18. package/isPromise.min.cjs +2 -0
  19. package/isPromise.min.cjs.map +1 -0
  20. package/isPromise.min.mjs +2 -0
  21. package/isPromise.min.mjs.map +1 -0
  22. package/isPromise.mjs +14 -0
  23. package/mean.cjs +53 -0
  24. package/mean.d.mts +7 -0
  25. package/mean.d.ts +7 -0
  26. package/mean.min.cjs +2 -0
  27. package/mean.min.cjs.map +1 -0
  28. package/mean.min.mjs +2 -0
  29. package/mean.min.mjs.map +1 -0
  30. package/mean.mjs +26 -0
  31. package/median.cjs +56 -0
  32. package/median.d.mts +7 -0
  33. package/median.d.ts +7 -0
  34. package/median.min.cjs +2 -0
  35. package/median.min.cjs.map +1 -0
  36. package/median.min.mjs +2 -0
  37. package/median.min.mjs.map +1 -0
  38. package/median.mjs +29 -0
  39. package/package.json +65 -1
  40. package/round.cjs +4 -1
  41. package/round.d.mts +2 -1
  42. package/round.d.ts +2 -1
  43. package/round.min.cjs +1 -1
  44. package/round.min.cjs.map +1 -1
  45. package/round.min.mjs +1 -1
  46. package/round.min.mjs.map +1 -1
  47. package/round.mjs +4 -1
  48. package/sum.cjs +17 -7
  49. package/sum.d.mts +3 -3
  50. package/sum.d.ts +3 -3
  51. package/sum.min.cjs +1 -1
  52. package/sum.min.cjs.map +1 -1
  53. package/sum.min.mjs +1 -1
  54. package/sum.min.mjs.map +1 -1
  55. package/sum.mjs +15 -7
package/asNumber.cjs ADDED
@@ -0,0 +1,39 @@
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/asNumber.ts
21
+ var asNumber_exports = {};
22
+ __export(asNumber_exports, {
23
+ asNumber: () => asNumber
24
+ });
25
+ module.exports = __toCommonJS(asNumber_exports);
26
+ function asNumber(u) {
27
+ if (typeof u === "number" && isFinite(u)) {
28
+ return u;
29
+ }
30
+ const n = Number(u);
31
+ if (typeof n === "number" && isFinite(n)) {
32
+ return n;
33
+ }
34
+ return 0;
35
+ }
36
+ // Annotate the CommonJS export names for ESM import in node:
37
+ 0 && (module.exports = {
38
+ asNumber
39
+ });
package/asNumber.d.mts ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Coerce `u` into a number if possible, otherwise just return 0.
3
+ */
4
+ declare function asNumber(u: unknown): number;
5
+
6
+ export { asNumber };
package/asNumber.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Coerce `u` into a number if possible, otherwise just return 0.
3
+ */
4
+ declare function asNumber(u: unknown): number;
5
+
6
+ export { asNumber };
@@ -0,0 +1,2 @@
1
+ "use strict";var i=Object.defineProperty;var o=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var m=(e,n)=>{for(var t in n)i(e,t,{get:n[t],enumerable:!0})},s=(e,n,t,u)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of b(n))!f.call(e,r)&&r!==t&&i(e,r,{get:()=>n[r],enumerable:!(u=o(n,r))||u.enumerable});return e};var p=e=>s(i({},"__esModule",{value:!0}),e);var y={};m(y,{asNumber:()=>c});module.exports=p(y);function c(e){if(typeof e=="number"&&isFinite(e))return e;let n=Number(e);return typeof n=="number"&&isFinite(n)?n:0}0&&(module.exports={asNumber});
2
+ //# sourceMappingURL=asNumber.min.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/asNumber.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"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAAAE,IAAA,eAAAC,EAAAH,GAGO,SAASE,EAASE,EAAoB,CAE3C,GAAI,OAAOA,GAAM,UAAY,SAASA,CAAC,EACrC,OAAOA,EAIT,IAAM,EAAI,OAAOA,CAAC,EAClB,OAAI,OAAO,GAAM,UAAY,SAAS,CAAC,EAC9B,EAKF,CACT","names":["asNumber_exports","__export","asNumber","__toCommonJS","u"]}
@@ -0,0 +1,2 @@
1
+ function r(n){if(typeof n=="number"&&isFinite(n))return n;let e=Number(n);return typeof e=="number"&&isFinite(e)?e:0}export{r as asNumber};
2
+ //# sourceMappingURL=asNumber.min.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/asNumber.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"],"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","names":["asNumber","u","n"]}
package/asNumber.mjs ADDED
@@ -0,0 +1,14 @@
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
+ export {
13
+ asNumber
14
+ };
package/average.cjs CHANGED
@@ -24,15 +24,23 @@ __export(average_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(average_exports);
26
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
+
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
46
  // src/average.ts
package/average.min.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var i=Object.defineProperty;var o=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var d=(n,e)=>{for(var u in e)i(n,u,{get:e[u],enumerable:!0})},a=(n,e,u,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of m(e))!f.call(n,t)&&t!==u&&i(n,t,{get:()=>e[t],enumerable:!(r=o(e,t))||r.enumerable});return n};var b=n=>a(i({},"__esModule",{value:!0}),n);var s={};d(s,{average:()=>p});module.exports=b(s);function l(n){return n.reduce((e,u)=>{let r=typeof u=="number"&&!isNaN(u)&&isFinite(u)?u:0;return e+r},0)}function p(n){return n.length===0?0:l(n)/n.length}0&&(module.exports={average});
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
2
  //# sourceMappingURL=average.min.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/average.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 * Add all the numbers together in the given array. Treats null, undefined and\n * NaN as zero.\n */\nexport function sum(numbers: (number | null | undefined)[]): number {\n return numbers.reduce(\n (accumulated: number, current: number | null | undefined) => {\n const value =\n typeof current === \"number\" && !isNaN(current) && isFinite(current)\n ? current\n : 0;\n\n return accumulated + value;\n },\n 0,\n );\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GCIO,SAASI,EAAIC,EAAgD,CAClE,OAAOA,EAAQ,OACb,CAACC,EAAqBC,IAAuC,CAC3D,IAAMC,EACJ,OAAOD,GAAY,UAAY,CAAC,MAAMA,CAAO,GAAK,SAASA,CAAO,EAC9DA,EACA,EAEN,OAAOD,EAAcE,CACvB,EACA,CACF,CACF,CDVO,SAASC,EAAQC,EAAgD,CACtE,OAAIA,EAAQ,SAAW,EAAU,EAEnBC,EAAID,CAAO,EAEVA,EAAQ,MACzB","names":["average_exports","__export","average","__toCommonJS","sum","numbers","accumulated","current","value","average","numbers","sum"]}
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 CHANGED
@@ -1,2 +1,2 @@
1
- function t(n){return n.reduce((u,e)=>{let r=typeof e=="number"&&!isNaN(e)&&isFinite(e)?e:0;return u+r},0)}function o(n){return n.length===0?0:t(n)/n.length}export{o as average};
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
2
  //# sourceMappingURL=average.min.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/sum.ts","../src/average.ts"],"sourcesContent":["/**\n * Add all the numbers together in the given array. Treats null, undefined and\n * NaN as zero.\n */\nexport function sum(numbers: (number | null | undefined)[]): number {\n return numbers.reduce(\n (accumulated: number, current: number | null | undefined) => {\n const value =\n typeof current === \"number\" && !isNaN(current) && isFinite(current)\n ? current\n : 0;\n\n return accumulated + value;\n },\n 0,\n );\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":"AAIO,SAASA,EAAIC,EAAgD,CAClE,OAAOA,EAAQ,OACb,CAACC,EAAqBC,IAAuC,CAC3D,IAAMC,EACJ,OAAOD,GAAY,UAAY,CAAC,MAAMA,CAAO,GAAK,SAASA,CAAO,EAC9DA,EACA,EAEN,OAAOD,EAAcE,CACvB,EACA,CACF,CACF,CCVO,SAASC,EAAQC,EAAgD,CACtE,OAAIA,EAAQ,SAAW,EAAU,EAEnBC,EAAID,CAAO,EAEVA,EAAQ,MACzB","names":["sum","numbers","accumulated","current","value","average","numbers","sum"]}
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 CHANGED
@@ -1,12 +1,20 @@
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
+
1
13
  // src/sum.ts
2
14
  function sum(numbers) {
3
- return numbers.reduce(
4
- (accumulated, current) => {
5
- const value = typeof current === "number" && !isNaN(current) && isFinite(current) ? current : 0;
6
- return accumulated + value;
7
- },
8
- 0
9
- );
15
+ return numbers.reduce((accumulated, current) => {
16
+ return accumulated + asNumber(current);
17
+ }, 0);
10
18
  }
11
19
 
12
20
  // src/average.ts
package/isPromise.cjs ADDED
@@ -0,0 +1,40 @@
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/isPromise.ts
21
+ var isPromise_exports = {};
22
+ __export(isPromise_exports, {
23
+ isNativePromise: () => isNativePromise,
24
+ isPromise: () => isPromise
25
+ });
26
+ module.exports = __toCommonJS(isPromise_exports);
27
+ function isPromise(obj) {
28
+ if (!obj || typeof obj !== "object" && typeof obj !== "function") {
29
+ return false;
30
+ }
31
+ return "then" in obj && typeof obj.then === "function";
32
+ }
33
+ function isNativePromise(obj) {
34
+ return obj instanceof Promise;
35
+ }
36
+ // Annotate the CommonJS export names for ESM import in node:
37
+ 0 && (module.exports = {
38
+ isNativePromise,
39
+ isPromise
40
+ });
@@ -0,0 +1,19 @@
1
+ /**
2
+ * A type guard to check if an object is a Promise (or a "thenable"). It checks
3
+ * if the object is not null, is an object, and has a callable .then method.
4
+ *
5
+ * Note that if the Promise expects a certain type like `Promise<T>`, there is
6
+ * no way to validate the type of T unless we resolve the promise. This function
7
+ * does not attempt to typecheck for T in any way.
8
+ */
9
+ declare function isPromise(obj: unknown): obj is Promise<unknown>;
10
+ /**
11
+ * A type guard to check if an object is a native Promise.
12
+ *
13
+ * Note that if the Promise expects a certain type like `Promise<T>`, there is
14
+ * no way to validate the type of T unless we resolve the promise. This function
15
+ * does not attempt to typecheck for T in any way.
16
+ */
17
+ declare function isNativePromise(obj: unknown): obj is Promise<unknown>;
18
+
19
+ export { isNativePromise, isPromise };
package/isPromise.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * A type guard to check if an object is a Promise (or a "thenable"). It checks
3
+ * if the object is not null, is an object, and has a callable .then method.
4
+ *
5
+ * Note that if the Promise expects a certain type like `Promise<T>`, there is
6
+ * no way to validate the type of T unless we resolve the promise. This function
7
+ * does not attempt to typecheck for T in any way.
8
+ */
9
+ declare function isPromise(obj: unknown): obj is Promise<unknown>;
10
+ /**
11
+ * A type guard to check if an object is a native Promise.
12
+ *
13
+ * Note that if the Promise expects a certain type like `Promise<T>`, there is
14
+ * no way to validate the type of T unless we resolve the promise. This function
15
+ * does not attempt to typecheck for T in any way.
16
+ */
17
+ declare function isNativePromise(obj: unknown): obj is Promise<unknown>;
18
+
19
+ export { isNativePromise, isPromise };
@@ -0,0 +1,2 @@
1
+ "use strict";var t=Object.defineProperty;var s=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var c=(n,e)=>{for(var i in e)t(n,i,{get:e[i],enumerable:!0})},m=(n,e,i,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of u(e))!f.call(n,o)&&o!==i&&t(n,o,{get:()=>e[o],enumerable:!(r=s(e,o))||r.enumerable});return n};var p=n=>m(t({},"__esModule",{value:!0}),n);var w={};c(w,{isNativePromise:()=>k,isPromise:()=>P});module.exports=p(w);function P(n){return!n||typeof n!="object"&&typeof n!="function"?!1:"then"in n&&typeof n.then=="function"}function k(n){return n instanceof Promise}0&&(module.exports={isNativePromise,isPromise});
2
+ //# sourceMappingURL=isPromise.min.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/isPromise.ts"],"sourcesContent":["/**\n * A type guard to check if an object is a Promise (or a \"thenable\"). It checks\n * if the object is not null, is an object, and has a callable .then method.\n *\n * Note that if the Promise expects a certain type like `Promise<T>`, there is\n * no way to validate the type of T unless we resolve the promise. This function\n * does not attempt to typecheck for T in any way.\n */\nexport function isPromise(obj: unknown): obj is Promise<unknown> {\n // Check if the object is defined and not null.\n if (!obj || (typeof obj !== \"object\" && typeof obj !== \"function\")) {\n return false;\n }\n\n // Check if the .then property is a function (callable).\n return \"then\" in obj && typeof obj.then === \"function\";\n}\n\n/**\n * A type guard to check if an object is a native Promise.\n *\n * Note that if the Promise expects a certain type like `Promise<T>`, there is\n * no way to validate the type of T unless we resolve the promise. This function\n * does not attempt to typecheck for T in any way.\n */\nexport function isNativePromise(obj: unknown): obj is Promise<unknown> {\n return obj instanceof Promise;\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,qBAAAE,EAAA,cAAAC,IAAA,eAAAC,EAAAJ,GAQO,SAASG,EAAUE,EAAuC,CAE/D,MAAI,CAACA,GAAQ,OAAOA,GAAQ,UAAY,OAAOA,GAAQ,WAC9C,GAIF,SAAUA,GAAO,OAAOA,EAAI,MAAS,UAC9C,CASO,SAASH,EAAgBG,EAAuC,CACrE,OAAOA,aAAe,OACxB","names":["isPromise_exports","__export","isNativePromise","isPromise","__toCommonJS","obj"]}
@@ -0,0 +1,2 @@
1
+ function e(n){return!n||typeof n!="object"&&typeof n!="function"?!1:"then"in n&&typeof n.then=="function"}function o(n){return n instanceof Promise}export{o as isNativePromise,e as isPromise};
2
+ //# sourceMappingURL=isPromise.min.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/isPromise.ts"],"sourcesContent":["/**\n * A type guard to check if an object is a Promise (or a \"thenable\"). It checks\n * if the object is not null, is an object, and has a callable .then method.\n *\n * Note that if the Promise expects a certain type like `Promise<T>`, there is\n * no way to validate the type of T unless we resolve the promise. This function\n * does not attempt to typecheck for T in any way.\n */\nexport function isPromise(obj: unknown): obj is Promise<unknown> {\n // Check if the object is defined and not null.\n if (!obj || (typeof obj !== \"object\" && typeof obj !== \"function\")) {\n return false;\n }\n\n // Check if the .then property is a function (callable).\n return \"then\" in obj && typeof obj.then === \"function\";\n}\n\n/**\n * A type guard to check if an object is a native Promise.\n *\n * Note that if the Promise expects a certain type like `Promise<T>`, there is\n * no way to validate the type of T unless we resolve the promise. This function\n * does not attempt to typecheck for T in any way.\n */\nexport function isNativePromise(obj: unknown): obj is Promise<unknown> {\n return obj instanceof Promise;\n}\n"],"mappings":"AAQO,SAASA,EAAUC,EAAuC,CAE/D,MAAI,CAACA,GAAQ,OAAOA,GAAQ,UAAY,OAAOA,GAAQ,WAC9C,GAIF,SAAUA,GAAO,OAAOA,EAAI,MAAS,UAC9C,CASO,SAASC,EAAgBD,EAAuC,CACrE,OAAOA,aAAe,OACxB","names":["isPromise","obj","isNativePromise"]}
package/isPromise.mjs ADDED
@@ -0,0 +1,14 @@
1
+ // src/isPromise.ts
2
+ function isPromise(obj) {
3
+ if (!obj || typeof obj !== "object" && typeof obj !== "function") {
4
+ return false;
5
+ }
6
+ return "then" in obj && typeof obj.then === "function";
7
+ }
8
+ function isNativePromise(obj) {
9
+ return obj instanceof Promise;
10
+ }
11
+ export {
12
+ isNativePromise,
13
+ isPromise
14
+ };
package/mean.cjs ADDED
@@ -0,0 +1,53 @@
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/mean.ts
21
+ var mean_exports = {};
22
+ __export(mean_exports, {
23
+ mean: () => mean
24
+ });
25
+ module.exports = __toCommonJS(mean_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/mean.ts
47
+ function mean(numbers) {
48
+ return numbers.length > 0 ? sum(numbers) / numbers.length : 0;
49
+ }
50
+ // Annotate the CommonJS export names for ESM import in node:
51
+ 0 && (module.exports = {
52
+ mean
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 u=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var c=(n,r)=>{for(var e in r)u(n,e,{get:r[e],enumerable:!0})},s=(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 k=n=>s(u({},"__esModule",{value:!0}),n);var a={};c(a,{mean:()=>w});module.exports=k(a);function m(n){if(typeof n=="number"&&isFinite(n))return n;let r=Number(n);return typeof r=="number"&&isFinite(r)?r: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 // 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,UAAAE,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,EAAKC,EAA4B,CAC/C,OAAOA,EAAQ,OAAS,EAAIC,EAAID,CAAO,EAAIA,EAAQ,OAAS,CAC9D","names":["mean_exports","__export","mean","__toCommonJS","asNumber","u","n","sum","numbers","accumulated","current","asNumber","mean","numbers","sum"]}
package/mean.min.mjs ADDED
@@ -0,0 +1,2 @@
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?t(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 // 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 * 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,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,EAAKC,EAA4B,CAC/C,OAAOA,EAAQ,OAAS,EAAIC,EAAID,CAAO,EAAIA,EAAQ,OAAS,CAC9D","names":["asNumber","u","n","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" && 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/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" && 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/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 m=Object.getOwnPropertyDescriptor;var s=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var c=(e,n)=>{for(var t in n)i(e,t,{get:n[t],enumerable:!0})},l=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of s(n))!f.call(e,r)&&r!==t&&i(e,r,{get:()=>n[r],enumerable:!(o=m(n,r))||o.enumerable});return e};var a=e=>l(i({},"__esModule",{value:!0}),e);var p={};c(p,{median:()=>b});module.exports=a(p);function u(e){if(typeof e=="number"&&isFinite(e))return e;let n=Number(e);return typeof n=="number"&&isFinite(n)?n:0}function b(e){if(e.length===0)return 0;let n=e.map(u).slice().sort(),t=Math.floor(n.length/2);if(n.length%2===1)return n[t];let o=n[t-1],r=n[t];return(o+r)/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 // 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"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,YAAAE,IAAA,eAAAC,EAAAH,GCGO,SAASI,EAASC,EAAoB,CAE3C,GAAI,OAAOA,GAAM,UAAY,SAASA,CAAC,EACrC,OAAOA,EAIT,IAAM,EAAI,OAAOA,CAAC,EAClB,OAAI,OAAO,GAAM,UAAY,SAAS,CAAC,EAC9B,EAKF,CACT,CDZO,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 r(e){if(typeof e=="number"&&isFinite(e))return e;let n=Number(e);return typeof n=="number"&&isFinite(n)?n:0}function s(e){if(e.length===0)return 0;let n=e.map(r).slice().sort(),t=Math.floor(n.length/2);if(n.length%2===1)return n[t];let o=n[t-1],i=n[t];return(o+i)/2}export{s 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 // 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 * 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,GAAI,OAAOA,GAAM,UAAY,SAASA,CAAC,EACrC,OAAOA,EAIT,IAAM,EAAI,OAAOA,CAAC,EAClB,OAAI,OAAO,GAAM,UAAY,SAAS,CAAC,EAC9B,EAKF,CACT,CCZO,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" && 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/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,7 +1,7 @@
1
1
  {
2
2
  "name": "@choksheak/ts-utils",
3
3
  "license": "The Unlicense",
4
- "version": "0.3.2",
4
+ "version": "0.3.3",
5
5
  "description": "Random Typescript utilities with support for full tree-shaking",
6
6
  "private": false,
7
7
  "scripts": {
@@ -98,6 +98,19 @@
98
98
  "default": "./arrayBuffer.cjs"
99
99
  }
100
100
  },
101
+ "./asNumber": {
102
+ "types": "./asNumber.d.ts",
103
+ "import": {
104
+ "production": "./asNumber.min.mjs",
105
+ "development": "./asNumber.mjs",
106
+ "default": "./asNumber.mjs"
107
+ },
108
+ "require": {
109
+ "production": "./asNumber.min.cjs",
110
+ "development": "./asNumber.cjs",
111
+ "default": "./asNumber.cjs"
112
+ }
113
+ },
101
114
  "./assert": {
102
115
  "types": "./assert.d.ts",
103
116
  "import": {
@@ -176,6 +189,19 @@
176
189
  "default": "./isEmpty.cjs"
177
190
  }
178
191
  },
192
+ "./isPromise": {
193
+ "types": "./isPromise.d.ts",
194
+ "import": {
195
+ "production": "./isPromise.min.mjs",
196
+ "development": "./isPromise.mjs",
197
+ "default": "./isPromise.mjs"
198
+ },
199
+ "require": {
200
+ "production": "./isPromise.min.cjs",
201
+ "development": "./isPromise.cjs",
202
+ "default": "./isPromise.cjs"
203
+ }
204
+ },
179
205
  "./iterators": {
180
206
  "types": "./iterators.d.ts",
181
207
  "import": {
@@ -228,6 +254,32 @@
228
254
  "default": "./logging.cjs"
229
255
  }
230
256
  },
257
+ "./mean": {
258
+ "types": "./mean.d.ts",
259
+ "import": {
260
+ "production": "./mean.min.mjs",
261
+ "development": "./mean.mjs",
262
+ "default": "./mean.mjs"
263
+ },
264
+ "require": {
265
+ "production": "./mean.min.cjs",
266
+ "development": "./mean.cjs",
267
+ "default": "./mean.cjs"
268
+ }
269
+ },
270
+ "./median": {
271
+ "types": "./median.d.ts",
272
+ "import": {
273
+ "production": "./median.min.mjs",
274
+ "development": "./median.mjs",
275
+ "default": "./median.mjs"
276
+ },
277
+ "require": {
278
+ "production": "./median.min.cjs",
279
+ "development": "./median.cjs",
280
+ "default": "./median.cjs"
281
+ }
282
+ },
231
283
  "./nonEmpty": {
232
284
  "types": "./nonEmpty.d.ts",
233
285
  "import": {
@@ -390,6 +442,9 @@
390
442
  "arrayBuffer": [
391
443
  "./arrayBuffer.d.ts"
392
444
  ],
445
+ "asNumber": [
446
+ "./asNumber.d.ts"
447
+ ],
393
448
  "assert": [
394
449
  "./assert.d.ts"
395
450
  ],
@@ -408,6 +463,9 @@
408
463
  "isEmpty": [
409
464
  "./isEmpty.d.ts"
410
465
  ],
466
+ "isPromise": [
467
+ "./isPromise.d.ts"
468
+ ],
411
469
  "iterators": [
412
470
  "./iterators.d.ts"
413
471
  ],
@@ -420,6 +478,12 @@
420
478
  "logging": [
421
479
  "./logging.d.ts"
422
480
  ],
481
+ "mean": [
482
+ "./mean.d.ts"
483
+ ],
484
+ "median": [
485
+ "./median.d.ts"
486
+ ],
423
487
  "nonEmpty": [
424
488
  "./nonEmpty.d.ts"
425
489
  ],
package/round.cjs CHANGED
@@ -29,7 +29,10 @@ function round(n, numDecimalPlaces = 0) {
29
29
  return Math.round(n * multipler) / multipler;
30
30
  }
31
31
  function roundS(n, numDecimalPlaces = 0) {
32
- return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);
32
+ return round(n, numDecimalPlaces).toLocaleString("en-US", {
33
+ minimumFractionDigits: numDecimalPlaces,
34
+ maximumFractionDigits: numDecimalPlaces
35
+ });
33
36
  }
34
37
  // Annotate the CommonJS export names for ESM import in node:
35
38
  0 && (module.exports = {
package/round.d.mts CHANGED
@@ -7,7 +7,8 @@
7
7
  declare function round(n: number, numDecimalPlaces?: number): number;
8
8
  /**
9
9
  * Returns a string with the number in the exact number of decimal places
10
- * specified, in case the number ends with zeroes.
10
+ * specified, in case the number ends with zeroes, and adding commas for each
11
+ * group of 3 significant digits.
11
12
  */
12
13
  declare function roundS(n: number, numDecimalPlaces?: number): string;
13
14
 
package/round.d.ts CHANGED
@@ -7,7 +7,8 @@
7
7
  declare function round(n: number, numDecimalPlaces?: number): number;
8
8
  /**
9
9
  * Returns a string with the number in the exact number of decimal places
10
- * specified, in case the number ends with zeroes.
10
+ * specified, in case the number ends with zeroes, and adding commas for each
11
+ * group of 3 significant digits.
11
12
  */
12
13
  declare function roundS(n: number, numDecimalPlaces?: number): string;
13
14
 
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 i=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var b=(r,n)=>{for(var t in n)i(r,t,{get:n[t],enumerable:!0})},d=(r,n,t,u)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of g(n))!p.call(r,o)&&o!==t&&i(r,o,{get:()=>n[o],enumerable:!(u=m(n,o))||u.enumerable});return r};var x=r=>d(i({},"__esModule",{value:!0}),r);var a={};b(a,{round:()=>e,roundS:()=>S});module.exports=x(a);function e(r,n=0){let t=Math.pow(10,n);return Math.round(r*t)/t}function S(r,n=0){return e(r,n).toLocaleString("en-US",{minimumFractionDigits:n,maximumFractionDigits:n})}0&&(module.exports={round,roundS});
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 * 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, and adding commas for each\n * group of 3 significant digits.\n */\nexport function roundS(n: number, numDecimalPlaces = 0): string {\n return round(n, numDecimalPlaces).toLocaleString(\"en-US\", {\n minimumFractionDigits: numDecimalPlaces,\n maximumFractionDigits: numDecimalPlaces,\n });\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,WAAAE,EAAA,WAAAC,IAAA,eAAAC,EAAAJ,GAMO,SAASE,EAAMG,EAAWC,EAAmB,EAAW,CAC7D,IAAMC,EAAY,KAAK,IAAI,GAAID,CAAgB,EAC/C,OAAO,KAAK,MAAMD,EAAIE,CAAS,EAAIA,CACrC,CAOO,SAASJ,EAAOE,EAAWC,EAAmB,EAAW,CAC9D,OAAOJ,EAAMG,EAAGC,CAAgB,EAAE,eAAe,QAAS,CACxD,sBAAuBA,EACvB,sBAAuBA,CACzB,CAAC,CACH","names":["round_exports","__export","round","roundS","__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 o(r,n=0){let t=Math.pow(10,n);return Math.round(r*t)/t}function i(r,n=0){return o(r,n).toLocaleString("en-US",{minimumFractionDigits:n,maximumFractionDigits:n})}export{o as round,i as roundS};
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 * 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, and adding commas for each\n * group of 3 significant digits.\n */\nexport function roundS(n: number, numDecimalPlaces = 0): string {\n return round(n, numDecimalPlaces).toLocaleString(\"en-US\", {\n minimumFractionDigits: numDecimalPlaces,\n maximumFractionDigits: numDecimalPlaces,\n });\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,CAOO,SAASC,EAAOH,EAAWC,EAAmB,EAAW,CAC9D,OAAOF,EAAMC,EAAGC,CAAgB,EAAE,eAAe,QAAS,CACxD,sBAAuBA,EACvB,sBAAuBA,CACzB,CAAC,CACH","names":["round","n","numDecimalPlaces","multipler","roundS"]}
package/round.mjs CHANGED
@@ -4,7 +4,10 @@ function round(n, numDecimalPlaces = 0) {
4
4
  return Math.round(n * multipler) / multipler;
5
5
  }
6
6
  function roundS(n, numDecimalPlaces = 0) {
7
- return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);
7
+ return round(n, numDecimalPlaces).toLocaleString("en-US", {
8
+ minimumFractionDigits: numDecimalPlaces,
9
+ maximumFractionDigits: numDecimalPlaces
10
+ });
8
11
  }
9
12
  export {
10
13
  round,
package/sum.cjs CHANGED
@@ -23,14 +23,24 @@ __export(sum_exports, {
23
23
  sum: () => sum
24
24
  });
25
25
  module.exports = __toCommonJS(sum_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
26
40
  function sum(numbers) {
27
- return numbers.reduce(
28
- (accumulated, current) => {
29
- const value = typeof current === "number" && !isNaN(current) && isFinite(current) ? current : 0;
30
- return accumulated + value;
31
- },
32
- 0
33
- );
41
+ return numbers.reduce((accumulated, current) => {
42
+ return accumulated + asNumber(current);
43
+ }, 0);
34
44
  }
35
45
  // Annotate the CommonJS export names for ESM import in node:
36
46
  0 && (module.exports = {
package/sum.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Add all the numbers together in the given array. Treats null, undefined and
3
- * NaN as zero.
2
+ * Add all the numbers together in the given array.
3
+ * Non-numbers will be coerced into numbers if possible.
4
4
  */
5
- declare function sum(numbers: (number | null | undefined)[]): number;
5
+ declare function sum(numbers: unknown[]): number;
6
6
 
7
7
  export { sum };
package/sum.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Add all the numbers together in the given array. Treats null, undefined and
3
- * NaN as zero.
2
+ * Add all the numbers together in the given array.
3
+ * Non-numbers will be coerced into numbers if possible.
4
4
  */
5
- declare function sum(numbers: (number | null | undefined)[]): number;
5
+ declare function sum(numbers: unknown[]): number;
6
6
 
7
7
  export { sum };
package/sum.min.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var b=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var r=Object.prototype.hasOwnProperty;var f=(n,e)=>{for(var u in e)b(n,u,{get:e[u],enumerable:!0})},o=(n,e,u,m)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of l(e))!r.call(n,i)&&i!==u&&b(n,i,{get:()=>e[i],enumerable:!(m=d(e,i))||m.enumerable});return n};var s=n=>o(b({},"__esModule",{value:!0}),n);var a={};f(a,{sum:()=>t});module.exports=s(a);function t(n){return n.reduce((e,u)=>{let m=typeof u=="number"&&!isNaN(u)&&isFinite(u)?u:0;return e+m},0)}0&&(module.exports={sum});
1
+ "use strict";var t=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var s=(r,n)=>{for(var e in n)t(r,e,{get:n[e],enumerable:!0})},p=(r,n,e,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let u of b(n))!f.call(r,u)&&u!==e&&t(r,u,{get:()=>n[u],enumerable:!(o=m(n,u))||o.enumerable});return r};var c=r=>p(t({},"__esModule",{value:!0}),r);var w={};s(w,{sum:()=>k});module.exports=c(w);function i(r){if(typeof r=="number"&&isFinite(r))return r;let n=Number(r);return typeof n=="number"&&isFinite(n)?n:0}function k(r){return r.reduce((n,e)=>n+i(e),0)}0&&(module.exports={sum});
2
2
  //# sourceMappingURL=sum.min.cjs.map
package/sum.min.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/sum.ts"],"sourcesContent":["/**\n * Add all the numbers together in the given array. Treats null, undefined and\n * NaN as zero.\n */\nexport function sum(numbers: (number | null | undefined)[]): number {\n return numbers.reduce(\n (accumulated: number, current: number | null | undefined) => {\n const value =\n typeof current === \"number\" && !isNaN(current) && isFinite(current)\n ? current\n : 0;\n\n return accumulated + value;\n },\n 0,\n );\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,SAAAE,IAAA,eAAAC,EAAAH,GAIO,SAASE,EAAIE,EAAgD,CAClE,OAAOA,EAAQ,OACb,CAACC,EAAqBC,IAAuC,CAC3D,IAAMC,EACJ,OAAOD,GAAY,UAAY,CAAC,MAAMA,CAAO,GAAK,SAASA,CAAO,EAC9DA,EACA,EAEN,OAAOD,EAAcE,CACvB,EACA,CACF,CACF","names":["sum_exports","__export","sum","__toCommonJS","numbers","accumulated","current","value"]}
1
+ {"version":3,"sources":["../src/sum.ts","../src/asNumber.ts"],"sourcesContent":["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","/**\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"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,SAAAE,IAAA,eAAAC,EAAAH,GCGO,SAASI,EAASC,EAAoB,CAE3C,GAAI,OAAOA,GAAM,UAAY,SAASA,CAAC,EACrC,OAAOA,EAIT,IAAM,EAAI,OAAOA,CAAC,EAClB,OAAI,OAAO,GAAM,UAAY,SAAS,CAAC,EAC9B,EAKF,CACT,CDZO,SAASC,EAAIC,EAA4B,CAC9C,OAAOA,EAAQ,OAAO,CAACC,EAAqBC,IACnCD,EAAcE,EAASD,CAAO,EACpC,CAAC,CACN","names":["sum_exports","__export","sum","__toCommonJS","asNumber","u","sum","numbers","accumulated","current","asNumber"]}
package/sum.min.mjs CHANGED
@@ -1,2 +1,2 @@
1
- function m(n){return n.reduce((u,e)=>{let i=typeof e=="number"&&!isNaN(e)&&isFinite(e)?e:0;return u+i},0)}export{m as sum};
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 i(n){return n.reduce((r,u)=>r+e(u),0)}export{i as sum};
2
2
  //# sourceMappingURL=sum.min.mjs.map
package/sum.min.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/sum.ts"],"sourcesContent":["/**\n * Add all the numbers together in the given array. Treats null, undefined and\n * NaN as zero.\n */\nexport function sum(numbers: (number | null | undefined)[]): number {\n return numbers.reduce(\n (accumulated: number, current: number | null | undefined) => {\n const value =\n typeof current === \"number\" && !isNaN(current) && isFinite(current)\n ? current\n : 0;\n\n return accumulated + value;\n },\n 0,\n );\n}\n"],"mappings":"AAIO,SAASA,EAAIC,EAAgD,CAClE,OAAOA,EAAQ,OACb,CAACC,EAAqBC,IAAuC,CAC3D,IAAMC,EACJ,OAAOD,GAAY,UAAY,CAAC,MAAMA,CAAO,GAAK,SAASA,CAAO,EAC9DA,EACA,EAEN,OAAOD,EAAcE,CACvB,EACA,CACF,CACF","names":["sum","numbers","accumulated","current","value"]}
1
+ {"version":3,"sources":["../src/asNumber.ts","../src/sum.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"],"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","names":["asNumber","u","n","sum","numbers","accumulated","current","asNumber"]}
package/sum.mjs CHANGED
@@ -1,12 +1,20 @@
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
+
1
13
  // src/sum.ts
2
14
  function sum(numbers) {
3
- return numbers.reduce(
4
- (accumulated, current) => {
5
- const value = typeof current === "number" && !isNaN(current) && isFinite(current) ? current : 0;
6
- return accumulated + value;
7
- },
8
- 0
9
- );
15
+ return numbers.reduce((accumulated, current) => {
16
+ return accumulated + asNumber(current);
17
+ }, 0);
10
18
  }
11
19
  export {
12
20
  sum