@stacksjs/arrays 0.75.118 → 0.75.119
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/dist/arr.d.ts +1 -0
- package/dist/arr.js +1 -1
- package/dist/series.d.ts +25 -0
- package/dist/series.js +1 -0
- package/package.json +1 -1
package/dist/arr.d.ts
CHANGED
package/dist/arr.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export*from"./contains";export*from"./helpers";export*from"./math";export*from"./transform";
|
|
1
|
+
export*from"./contains";export*from"./helpers";export*from"./math";export*from"./transform";export*from"./series";
|
package/dist/series.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Reject absent and nonnumeric values without turning null, false or blanks into zero. */
|
|
2
|
+
export declare function finiteNumeric(value: unknown): number | null;
|
|
3
|
+
/** Prefer a direct source per field; a missing field can fall back to another source. */
|
|
4
|
+
export declare function mergeDailySeries(rows: readonly DailyObservation[], priority?: readonly string[]): DailySeriesPoint[];
|
|
5
|
+
/** Statistics over valid observations, including zero; latest is by date rather than input order. */
|
|
6
|
+
export declare function summarizeSeries(points: readonly NumericSeriesPoint[]): {
|
|
7
|
+
count: unknown.length;
|
|
8
|
+
latest: unknown;
|
|
9
|
+
average: unknown;
|
|
10
|
+
lowest: unknown;
|
|
11
|
+
highest: unknown
|
|
12
|
+
};
|
|
13
|
+
/** Numeric observations grouped by calendar day, with provenance per field. */
|
|
14
|
+
export declare interface DailyObservation {
|
|
15
|
+
day: string
|
|
16
|
+
source: string
|
|
17
|
+
values: Record<string, unknown>
|
|
18
|
+
updatedAt?: string
|
|
19
|
+
}
|
|
20
|
+
export declare interface DailySeriesPoint {
|
|
21
|
+
date: string
|
|
22
|
+
values: Record<string, number>
|
|
23
|
+
sources: Record<string, string>
|
|
24
|
+
}
|
|
25
|
+
export declare interface NumericSeriesPoint { date: string, value: number, source?: string }
|
package/dist/series.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function finiteNumeric(value){if(typeof value!=="number"&&typeof value!=="string")return null;if(typeof value==="string"&&!value.trim())return null;const number=Number(value);return Number.isFinite(number)?number:null}export function mergeDailySeries(rows,priority=[]){const rank=(source)=>{const at=priority.indexOf(source);return at<0?priority.length:at},sorted=[...rows].sort((a,b)=>rank(a.source)-rank(b.source)||String(b.updatedAt||"").localeCompare(String(a.updatedAt||""))||a.source.localeCompare(b.source)),days=new Map;for(const row of sorted){if(!/^\d{4}-\d{2}-\d{2}$/.test(row.day))continue;const parsed=new Date(`${row.day}T00:00:00Z`);if(!Number.isFinite(parsed.getTime())||parsed.toISOString().slice(0,10)!==row.day)continue;const point=days.get(row.day)||{date:row.day,values:{},sources:{}};for(const[key,raw]of Object.entries(row.values)){if(Object.hasOwn(point.values,key))continue;const value=finiteNumeric(raw);if(value===null)continue;Object.defineProperty(point.values,key,{value,enumerable:!0,configurable:!0,writable:!0});Object.defineProperty(point.sources,key,{value:row.source,enumerable:!0,configurable:!0,writable:!0})}if(Object.keys(point.values).length)days.set(row.day,point)}return[...days.values()].sort((a,b)=>a.date.localeCompare(b.date))}export function summarizeSeries(points){const valid=points.filter((point)=>Number.isFinite(point.value)).sort((a,b)=>a.date.localeCompare(b.date));let sum=0,lowest=1/0,highest=-1/0;for(const point of valid){sum+=point.value;lowest=Math.min(lowest,point.value);highest=Math.max(highest,point.value)}return{count:valid.length,latest:valid.at(-1)??null,average:valid.length?sum/valid.length:null,lowest:valid.length?lowest:null,highest:valid.length?highest:null}}
|