Skip to content

Commit e687322

Browse files
committed
feat(exclude-properties): support excluding properties from the response
1 parent 70517dc commit e687322

4 files changed

Lines changed: 58 additions & 29 deletions

File tree

pnpm-lock.yaml

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/filter.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
11
import type { SDFRecord } from "./parser";
22

3+
export const filterExcludedProperties = (
4+
record: SDFRecord,
5+
excludedProperties: string[],
6+
): SDFRecord => {
7+
const filteredProperties = Object.keys(record.properties)
8+
.filter((key) => !excludedProperties.includes(key))
9+
.reduce<SDFRecord["properties"]>((obj, key) => {
10+
const val = record.properties[key];
11+
val !== undefined && (obj[key] = val);
12+
return obj;
13+
}, {});
14+
15+
return {
16+
...record,
17+
properties: filteredProperties,
18+
};
19+
};
20+
321
export type FilterFn = (record: SDFRecord) => boolean;
422

523
export type FilterRule = {
624
property: string;
725
min: number;
826
max: number;
9-
treatAs: "number" | "string" | "date";
27+
treatAs: "string" | "number" | "integer" | "object" | "array" | "boolean" | "null";
1028
};
1129

1230
export const filterRecord = (record: SDFRecord, rules: FilterRule[]): boolean => {
1331
for (const rule of rules) {
1432
const value = record.properties[rule.property];
15-
// skip a record that is missing a property to which we apply a filter
33+
// skip the filter check if the property is missing from the record
1634
if (value === undefined) continue;
1735

1836
// handle each type of filter
1937
switch (rule.treatAs) {
20-
case "number": {
38+
case "number":
39+
case "integer": {
2140
if (value === "") return false;
22-
// parseFloat has issues so should probably use a different library
41+
// parseFloat has issues so should probably use a library
2342
const numberValue = Number.parseFloat(value);
2443
// if the value is not parsable to a number, drop the record
2544
if (Number.isNaN(numberValue)) return false;
@@ -29,7 +48,10 @@ export const filterRecord = (record: SDFRecord, rules: FilterRule[]): boolean =>
2948
}
3049
// TODO: To implement
3150
case "string":
32-
case "date":
51+
case "object":
52+
case "array":
53+
case "boolean":
54+
case "null":
3355
continue;
3456
}
3557
}

src/node-stream.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Transform, type TransformCallback, type TransformOptions } from "node:stream";
22

3-
import type { FilterFn } from "./filter";
3+
import { filterExcludedProperties, type FilterFn } from "./filter";
44
import type { SDFRecord } from "./parser";
55
import { parseSdPart } from "./parser";
66
import { splitLines } from "./utils";
@@ -44,6 +44,7 @@ const countRecords = (buffer: string) => buffer.match(/\${4}.*/g)?.length ?? 0;
4444
export class NodeSDFTransformer extends Transform {
4545
constructor(
4646
private filter: FilterFn = () => true,
47+
private excludedProperties: string[] = [],
4748
options?: TransformOptions,
4849
// these shouldn't be in the constructor definition but how set these to this without ts complaining?
4950
private buffer = "",
@@ -74,7 +75,10 @@ export class NodeSDFTransformer extends Transform {
7475
const record = this.parse();
7576
if (this.filter(record)) {
7677
if (this.record) {
77-
const json = JSON.stringify(this.record);
78+
const json = JSON.stringify(
79+
filterExcludedProperties(this.record, this.excludedProperties),
80+
);
81+
7882
this.push(json + ",");
7983
this.record = record;
8084
} else {
@@ -90,7 +94,7 @@ export class NodeSDFTransformer extends Transform {
9094
const record = this.record;
9195

9296
if (record) {
93-
const json = JSON.stringify(record);
97+
const json = JSON.stringify(filterExcludedProperties(record, this.excludedProperties));
9498
this.push(json);
9599
}
96100

src/web-stream.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FilterFn } from "./filter";
1+
import { filterExcludedProperties, type FilterFn } from "./filter";
22
import { parseSdPart, type SDFRecord } from "./parser";
33
import { splitLines } from "./utils";
44

@@ -20,7 +20,10 @@ const RECORD_SEPARATOR = "$$$$";
2020
```
2121
* @returns instance of `TransformStream`
2222
*/
23-
export const createSDFTransformer = (filter: FilterFn = () => true) => {
23+
export const createSDFTransformer = (
24+
filter: FilterFn = () => true,
25+
excludedProperties: string[] = [],
26+
) => {
2427
let content = ""; // accumulator for the content of the current record
2528

2629
// TransformStream to be used with stream.pipeThrough()
@@ -37,7 +40,7 @@ export const createSDFTransformer = (filter: FilterFn = () => true) => {
3740
content = content.slice(recordEndIndex + RECORD_SEPARATOR.length + 1);
3841

3942
if (filter(record)) {
40-
controller.enqueue(record);
43+
controller.enqueue(filterExcludedProperties(record, excludedProperties));
4144
}
4245
}
4346
},

0 commit comments

Comments
 (0)