Skip to content

Commit a628427

Browse files
committed
fix(api): better typings and api, and added some documentation
1 parent 103022c commit a628427

5 files changed

Lines changed: 80 additions & 12 deletions

File tree

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Data-pack
2+
3+
Utility pack for working with random data
4+
5+
## File utils
6+
7+
Simple wrapper around `fs` and `fs/promises`.
8+
9+
```typescript
10+
function read(path: string): Promise<string>;
11+
function readSync(path: string): string;
12+
function readJSON<T>(path: string): Promise<T>;
13+
function readJSONSync<T>(path: string): Promise<T>;
14+
15+
function write(path: string, content: any): Promise<void>;
16+
function writeSync(path: string, content: any): void;
17+
function withWriteStream(path: string, block: (stream: WriteStream) => void): void;
18+
```
19+
20+
## CSV
21+
22+
Utilities for parsing and writing CSV files
23+
24+
```typescript
25+
function* parseCSV(content: string, delimiter: string = ''): Generator<Array<string>, void>;
26+
function* parseCSVSync(content: string, delimiter: string = ''): Array<Array<string>>;
27+
function* parseCSVToFlow(content: string, delimiter: string = ''): GeneratorFlow<Array<string>>;
28+
```
29+
30+
## JSON
31+
32+
Utilities for parsing and writing JSON
33+
34+
```typescript
35+
function serialize(content: any): string;
36+
function deserialize<T>(content: string): T;
37+
```
38+
39+
## Generator flows
40+
41+
Utilities for working with generators as if they are arrays
42+
43+
```typescript
44+
// Creation
45+
const flow = GeneratorFlow.ofArray([1, 2, 3, 4]);
46+
const numFlow = GeneratorFlow.ofArray([5, 6]);
47+
const otherFlow = new GeneratorFlow(function* () {
48+
yield 'a';
49+
yield 'b';
50+
yield 'c';
51+
yield 'd';
52+
});
53+
54+
// Stream functions
55+
flow.skipWhile((it) => it < 2); // Flow[3,4]
56+
flow.takeWhile((it) => it < 2); // Flow[1]
57+
flow.skip(2); // Flow[3,4]
58+
flow.tail(); // Flow[2,3,4]
59+
flow.take(2); // Flow[1,2]
60+
flow.map((it) => it * 2); // Flow[2,4,6,8]
61+
flow.flatMap((it) => [it * 2]); // Flow[2,4,6,8]
62+
flow.filter((it) => it % 2 == 0); // Flow[2,4]
63+
flow.zip(otherFlow); // Flow[[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']]
64+
flow.cartesianProduct(otherFlow); // Flow[[1, 'a'], [1, 'b'], [1, 'c'], [1, 'd'], [2, 'a'], ... [4, 'd']]
65+
flow.concat(otherFlow); // Flow[1, 2, 3, 4, 5, 6]
66+
flow.peek(console.log); // Flow[1, 2, 3, 4]
67+
flow.reduce(add, 0); // 10
68+
flow.size(); // 4
69+
flow.toArray(); // [1, 2, 3, 4]
70+
flow.toSet(); // Set<1, 2, 3, 4>
71+
```

src/file.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ export async function read(path: string): Promise<string> {
99
export function readSync(path: string): string {
1010
return fs.readFileSync(path, 'utf8');
1111
}
12-
export async function readJSON(path: string): Promise<string> {
13-
return fsP.readFile(path, 'utf8').then(deserialize);
12+
export async function readJSON<T>(path: string): Promise<T> {
13+
const content = await read(path);
14+
return deserialize<T>(content);
1415
}
15-
export function readJSONSync(path: string): string {
16+
export function readJSONSync<T>(path: string): T {
1617
return deserialize(fs.readFileSync(path, 'utf8'));
1718
}
1819

src/generator-flow.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,11 @@ export class GeneratorFlow<T = unknown> {
144144
return count;
145145
}
146146

147-
peek() {
147+
peek(block: (t: T) => void) {
148148
const thisGenerator = this.generator();
149149
return new GeneratorFlow(function* () {
150150
for (const val of thisGenerator) {
151+
block(val);
151152
yield val;
152153
}
153154
});

src/json.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
export function serialize(content: any) {
1+
export function serialize(content: any): string {
22
return typeof content === 'string' ? content : JSON.stringify(content, null, 2);
33
}
44

5-
export function deserialize(content: string) {
6-
try {
7-
return JSON.parse(content);
8-
} catch (e: unknown) {
9-
return content;
10-
}
5+
export function deserialize<T>(content: string): T {
6+
return JSON.parse(content);
117
}

test/json.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ test('should serialize objects and arrays', () => {
1111
});
1212

1313
test('should deserialize json', () => {
14-
assertThat('content', deserialize('content'), 'array is deserialized');
1514
assertThat(['content'], deserialize('[\n "content"\n]'), 'array is deserialized');
1615
assertThat({ content: 'hei' }, deserialize('{\n "content": "hei"\n}'), 'object is deserialized');
1716
});

0 commit comments

Comments
 (0)