Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
idris-maps committed Jan 9, 2022
1 parent 816b7ec commit 6f1b6bd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
4 changes: 3 additions & 1 deletion pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export const pipe = <T = any>(
func: PipeFunction[],
rid?: number,
) =>
(input: AsyncIterableIterator<any>): { iterable: AsyncIterableIterator<T>, rid?: number } => ({
(
input: AsyncIterableIterator<any>,
): { iterable: AsyncIterableIterator<T>; rid?: number } => ({
iterable: func.reduce((r, f) => f(r), input),
rid,
});
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Opens a file and creates a stream of lines
type fromFile = (
path: string,
funcs?: PipeFunction[],
) => Promise<{ iterator: AsyncIterableIterator<any>, rid: number }>;
) => Promise<{ iterator: AsyncIterableIterator<any>; rid: number }>;
```

### fromStdin
Expand All @@ -34,7 +34,7 @@ objects
type fromNdjsonFile = (
path: string,
funcs?: PipeFunction[],
) => Promise<{ iterator: AsyncIterableIterator<any>, rid: number }>;
) => Promise<{ iterator: AsyncIterableIterator<any>; rid: number }>;
```

### fromNdjsonStdin
Expand Down Expand Up @@ -63,7 +63,7 @@ type fromDsvFile = (
bool?: string[]; // list of boolean columns
},
funcs?: PipeFunction[],
) => Promise<{ iterator: AsyncIterableIterator<any>, rid: number }>;
) => Promise<{ iterator: AsyncIterableIterator<any>; rid: number }>;
```

### fromDsvStdin
Expand All @@ -76,9 +76,9 @@ type fromDsvStdin = (
delimiter?: string; // default "," (csv)
numeric?: string[]; // list of numeric columns
bool?: string[]; // list of boolean columns
},
funcs?: PipeFunction[]
) => { iterator: AsyncIterableIterator<any> }
},
funcs?: PipeFunction[],
) => { iterator: AsyncIterableIterator<any> };
```

## Transforms
Expand Down
34 changes: 17 additions & 17 deletions to.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
const closeFile = async (rid?: number) => {
if (rid) {
try {
await Deno.close(rid)
await Deno.close(rid);
} catch (e) {
// do nothing
}
}
return undefined
}
return undefined;
};

interface Stream<T = any> {
iterable: AsyncIterableIterator<T>
rid?: number
iterable: AsyncIterableIterator<T>;
rid?: number;
}

export const toArray = async <T = any>({ iterable, rid }: Stream<T>) => {
let r: T[] = [];
for await (const d of iterable) {
r.push(d);
}
await closeFile(rid)
await closeFile(rid);
return r;
};

Expand All @@ -29,14 +29,14 @@ export const find = async <T = any>(
return async ({ iterable, rid }: Stream<T>) => {
for await (const d of iterable) {
if (func(d)) {
await closeFile(rid)
return d
await closeFile(rid);
return d;
}
}
await closeFile(rid)
return undefined
}
}
await closeFile(rid);
return undefined;
};
};

export const reduce = <A = any, B = any>(
func: (r: B, d: A, i: number) => B,
Expand All @@ -49,7 +49,7 @@ export const reduce = <A = any, B = any>(
i++;
r = func(r, d, i);
}
await closeFile(rid)
await closeFile(rid);
return r;
};
};
Expand All @@ -73,7 +73,7 @@ export const toNdjsonStdout = async <T = any>({ iterable, rid }: Stream<T>) => {
for await (const d of iterable) {
log(JSON.stringify(d));
}
await closeFile(rid)
await closeFile(rid);
};

export const toNdjsonFile = async <T = any>(
Expand All @@ -92,7 +92,7 @@ export const toNdjsonFile = async <T = any>(
});
}
}
await closeFile(rid)
await closeFile(rid);
};

const isString = (d: any): d is string => d === String(d);
Expand Down Expand Up @@ -120,7 +120,7 @@ export const toDsvStdout = async <T = any>(
log(toDsvLine(head, delimiter, d));
}
}
await closeFile(rid)
await closeFile(rid);
};

export const toDsvFile = async <T = any>(
Expand All @@ -140,5 +140,5 @@ export const toDsvFile = async <T = any>(
});
}
}
await closeFile(rid)
await closeFile(rid);
};
6 changes: 4 additions & 2 deletions transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ export const limit = <T = any>(n: number) =>
}
};

export const parseJson = async function* (iterable: AsyncIterableIterator<string>) {
export const parseJson = async function* (
iterable: AsyncIterableIterator<string>,
) {
for await (const d of iterable) {
try {
yield JSON.parse(d);
} catch (e) {
// do nothing
}
}
}
};

const splitCsvRow = (delimiter: string, line: string) => {
let inQuote = false;
Expand Down

0 comments on commit 6f1b6bd

Please sign in to comment.