@raindrops-on-roses/number-cumulative-average 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # @raindrops-on-roses/number-cumulative-average
2
+
3
+ `cumulativeAverage` utility from [raindrops-on-roses](https://www.npmjs.com/package/raindrops-on-roses).
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @raindrops-on-roses/number-cumulative-average
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { cumulativeAverage } from "@raindrops-on-roses/number-cumulative-average";
15
+ ```
16
+
17
+ ## With the complete library
18
+
19
+ You can also install the complete library:
20
+
21
+ ```sh
22
+ npm install raindrops-on-roses
23
+ ```
24
+
25
+ and import the utility from the umbrella package:
26
+
27
+ ```ts
28
+ import { cumulativeAverage } from "raindrops-on-roses";
29
+ ```
30
+
31
+ ## Repository
32
+
33
+ [graphieros/raindrops-on-roses](https://github.com/graphieros/raindrops-on-roses)
@@ -0,0 +1,52 @@
1
+ export type CumulativeAverageConfig = {
2
+ keepInvalid?: boolean;
3
+ convertInvalidToZero?: boolean;
4
+ };
5
+ export type CumulativeAverageParams<T = unknown> = {
6
+ values: T[];
7
+ config?: CumulativeAverageConfig;
8
+ };
9
+ /**
10
+ * Calculates the cumulative average of the provided values.
11
+ *
12
+ * ---
13
+ *
14
+ * Invalid values are values for which `Number.isFinite()` returns `false`.
15
+ * Depending on the configuration, they can be preserved, discarded, or
16
+ * treated as zero when calculating the average.
17
+ *
18
+ * @param params - The cumulative average parameters.
19
+ * @param params.values - The values to process.
20
+ * @param params.config - Options controlling how invalid values are handled.
21
+ * @returns An array containing the cumulative averages and, when configured,
22
+ * preserved invalid values.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * cumulativeAverage({
27
+ * values: [1, 2, 3],
28
+ * }) // [1, 1.5, 2]
29
+ *```
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * cumulativeAverage({
34
+ * values: [1, null, 3],
35
+ * config: { keepInvalid: true },
36
+ * }) // [1, null, 2]
37
+ *```
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * cumulativeAverage({
42
+ * values: [1, null, 3],
43
+ * config: { keepInvalid: true, convertInvalidToZero: true },
44
+ * }) // [1, 0.5, 1.3333333333333333]
45
+ *```
46
+ * ---
47
+ *
48
+ * Lore:
49
+ * Every new experience changes ever so slightly the average that came before.
50
+ */
51
+ export declare function cumulativeAverage<T = unknown>({ values, config, }: CumulativeAverageParams<T>): Array<number | T>;
52
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,uBAAuB,GAAG;IACpC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAEF,MAAM,MAAM,uBAAuB,CAAC,CAAC,GAAG,OAAO,IAAI;IACjD,MAAM,EAAE,CAAC,EAAE,CAAC;IACZ,MAAM,CAAC,EAAE,uBAAuB,CAAC;CAClC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,GAAG,OAAO,EAAE,EAC7C,MAAM,EACN,MAAW,GACZ,EAAE,uBAAuB,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAsBhD"}
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ //#region src/index.ts
2
+ function e({ values: e, config: t = {} }) {
3
+ let { keepInvalid: n = !0, convertInvalidToZero: r = !1 } = t, i = [], a = 0, o = 0;
4
+ for (let t of e) {
5
+ let e = Number.isFinite(t);
6
+ if (e || n) {
7
+ if (!e && !r) {
8
+ i.push(t);
9
+ continue;
10
+ }
11
+ a += e ? t : 0, i.push(a / ++o);
12
+ }
13
+ }
14
+ return i;
15
+ }
16
+ //#endregion
17
+ export { e as cumulativeAverage };
18
+
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["export type CumulativeAverageConfig = {\n keepInvalid?: boolean;\n convertInvalidToZero?: boolean;\n};\n\nexport type CumulativeAverageParams<T = unknown> = {\n values: T[];\n config?: CumulativeAverageConfig;\n};\n\n/**\n * Calculates the cumulative average of the provided values.\n *\n * ---\n *\n * Invalid values are values for which `Number.isFinite()` returns `false`.\n * Depending on the configuration, they can be preserved, discarded, or\n * treated as zero when calculating the average.\n *\n * @param params - The cumulative average parameters.\n * @param params.values - The values to process.\n * @param params.config - Options controlling how invalid values are handled.\n * @returns An array containing the cumulative averages and, when configured,\n * preserved invalid values.\n *\n * @example\n * ```ts\n * cumulativeAverage({\n * values: [1, 2, 3],\n * }) // [1, 1.5, 2]\n *```\n *\n * @example\n * ```ts\n * cumulativeAverage({\n * values: [1, null, 3],\n * config: { keepInvalid: true },\n * }) // [1, null, 2]\n *```\n *\n * @example\n * ```ts\n * cumulativeAverage({\n * values: [1, null, 3],\n * config: { keepInvalid: true, convertInvalidToZero: true },\n * }) // [1, 0.5, 1.3333333333333333]\n *```\n * ---\n *\n * Lore:\n * Every new experience changes ever so slightly the average that came before.\n */\nexport function cumulativeAverage<T = unknown>({\n values,\n config = {},\n}: CumulativeAverageParams<T>): Array<number | T> {\n const { keepInvalid = true, convertInvalidToZero = false } = config;\n\n const result: Array<number | T> = [];\n let sum = 0;\n let count = 0;\n\n for (const value of values) {\n const valid = Number.isFinite(value);\n\n if (!valid && !keepInvalid) continue;\n\n if (!valid && !convertInvalidToZero) {\n result.push(value);\n continue;\n }\n\n sum += valid ? (value as number) : 0;\n result.push(sum / ++count);\n }\n\n return result;\n}\n"],"mappings":";AAoDA,SAAgB,EAA+B,EAC7C,WACA,YAAS,CAAC,KACsC;CAChD,IAAM,EAAE,iBAAc,IAAM,0BAAuB,OAAU,GAEvD,IAA4B,CAAC,GAC/B,IAAM,GACN,IAAQ;CAEZ,KAAK,IAAM,KAAS,GAAQ;EAC1B,IAAM,IAAQ,OAAO,SAAS,CAAK;EAE/B,IAAC,KAAU,GAEf;OAAI,CAAC,KAAS,CAAC,GAAsB;IACnC,EAAO,KAAK,CAAK;IACjB;GACF;GAGA,AADA,KAAO,IAAS,IAAmB,GACnC,EAAO,KAAK,IAAM,EAAE,CAAK;EAHzB;CAIF;CAEA,OAAO;AACT"}
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@raindrops-on-roses/number-cumulative-average",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "sideEffects": false,
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "build": "vite build && tsc -p tsconfig.json"
17
+ },
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "license": "MIT",
22
+ "homepage": "https://raindrops-on-roses.graphieros.com/number/cumulative-average",
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/graphieros/raindrops-on-roses.git",
26
+ "directory": "packages/number-cumulative-average"
27
+ }
28
+ }