@raindrops-on-roses/number-lttb 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-lttb
2
+
3
+ `lttb` 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-lttb
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { lttb } from "@raindrops-on-roses/number-lttb";
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 { lttb } from "raindrops-on-roses";
29
+ ```
30
+
31
+ ## Repository
32
+
33
+ [graphieros/raindrops-on-roses](https://github.com/graphieros/raindrops-on-roses)
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Downsamples a numeric data series using the Largest-Triangle-Three-Buckets
3
+ * (LTTB) algorithm while preserving the first and last data points.
4
+ *
5
+ * ---
6
+ *
7
+ * @param data - The numeric data series to downsample
8
+ * @param threshold - The maximum number of data points to retain
9
+ * @returns The downsampled data series, or the original data if the threshold
10
+ * is greater than or equal to the data length or less than 3
11
+ *
12
+ * ---
13
+ *
14
+ * Lore:
15
+ * A point appears.
16
+ * A second point as a mirror sends a distance.
17
+ * There love makes a triangle.
18
+ */
19
+ export declare function lttb({ data, threshold, }: {
20
+ data: number[];
21
+ threshold: number;
22
+ }): number[];
23
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,IAAI,CAAC,EACnB,IAAI,EACJ,SAAS,GACV,EAAE;IACD,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB,GAAG,MAAM,EAAE,CAiCX"}
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ //#region src/index.ts
2
+ function e({ data: e, threshold: t }) {
3
+ if (t >= e.length || t < 3) return e;
4
+ let n = [], r = (e.length - 2) / (t - 2), i = 0;
5
+ n.push(e[i]);
6
+ for (let a = 0; a < t - 2; a += 1) {
7
+ let t = Math.floor((a + 1) * r) + 1, o = Math.min(Math.floor((a + 2) * r) + 1, e.length), s = e.slice(t, o), c = s.reduce((e, t) => e + t, 0) / s.length, l = -1, u = i;
8
+ for (let n = t; n < o; n += 1) {
9
+ let t = Math.abs((e[i] - c) * (n - i));
10
+ t > l && (l = t, u = n);
11
+ }
12
+ n.push(e[u]), i = u;
13
+ }
14
+ return n.push(e[e.length - 1]), n;
15
+ }
16
+ //#endregion
17
+ export { e as lttb };
18
+
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * Downsamples a numeric data series using the Largest-Triangle-Three-Buckets\n * (LTTB) algorithm while preserving the first and last data points.\n *\n * ---\n *\n * @param data - The numeric data series to downsample\n * @param threshold - The maximum number of data points to retain\n * @returns The downsampled data series, or the original data if the threshold\n * is greater than or equal to the data length or less than 3\n *\n * ---\n *\n * Lore:\n * A point appears.\n * A second point as a mirror sends a distance.\n * There love makes a triangle.\n */\nexport function lttb({\n data,\n threshold,\n}: {\n data: number[];\n threshold: number;\n}): number[] {\n if (threshold >= data.length || threshold < 3) {\n return data;\n }\n const sampled = [];\n const bucketSize = (data.length - 2) / (threshold - 2);\n let a = 0;\n // First point as is\n sampled.push(data[a]);\n for (let i = 0; i < threshold - 2; i += 1) {\n const bucketStart = Math.floor((i + 1) * bucketSize) + 1;\n const bucketEnd = Math.min(\n Math.floor((i + 2) * bucketSize) + 1,\n data.length,\n );\n const bucket = data.slice(bucketStart, bucketEnd);\n const average = bucket.reduce((a, b) => a + b, 0) / bucket.length;\n let maxArea = -1;\n let nextA = a;\n\n for (let j = bucketStart; j < bucketEnd; j += 1) {\n const area = Math.abs((data[a] - average) * (j - a));\n if (area > maxArea) {\n maxArea = area;\n nextA = j;\n }\n }\n sampled.push(data[nextA]);\n a = nextA;\n }\n // Last point as is\n sampled.push(data[data.length - 1]);\n return sampled;\n}\n"],"mappings":";AAkBA,SAAgB,EAAK,EACnB,SACA,gBAIW;CACX,IAAI,KAAa,EAAK,UAAU,IAAY,GAC1C,OAAO;CAET,IAAM,IAAU,CAAC,GACX,KAAc,EAAK,SAAS,MAAM,IAAY,IAChD,IAAI;CAER,EAAQ,KAAK,EAAK,EAAE;CACpB,KAAK,IAAI,IAAI,GAAG,IAAI,IAAY,GAAG,KAAK,GAAG;EACzC,IAAM,IAAc,KAAK,OAAO,IAAI,KAAK,CAAU,IAAI,GACjD,IAAY,KAAK,IACrB,KAAK,OAAO,IAAI,KAAK,CAAU,IAAI,GACnC,EAAK,MACP,GACM,IAAS,EAAK,MAAM,GAAa,CAAS,GAC1C,IAAU,EAAO,QAAQ,GAAG,MAAM,IAAI,GAAG,CAAC,IAAI,EAAO,QACvD,IAAU,IACV,IAAQ;EAEZ,KAAK,IAAI,IAAI,GAAa,IAAI,GAAW,KAAK,GAAG;GAC/C,IAAM,IAAO,KAAK,KAAK,EAAK,KAAK,MAAY,IAAI,EAAE;GACnD,AAAI,IAAO,MACT,IAAU,GACV,IAAQ;EAEZ;EAEA,AADA,EAAQ,KAAK,EAAK,EAAM,GACxB,IAAI;CACN;CAGA,OADA,EAAQ,KAAK,EAAK,EAAK,SAAS,EAAE,GAC3B;AACT"}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@raindrops-on-roses/number-lttb",
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
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/graphieros/raindrops-on-roses.git",
25
+ "directory": "packages/number-lttb"
26
+ }
27
+ }