Skip to content

Commit

Permalink
feat(csv): add oneOff() cell transform for enum like values
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 29, 2023
1 parent f69d46e commit 7c297db
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/csv/src/transforms.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { IObjectOf } from "@thi.ng/api";
import { padLeft } from "@thi.ng/strings/pad-left";
import { percent as $percent } from "@thi.ng/strings/percent";
import { maybeParseFloat, maybeParseInt } from "@thi.ng/strings/parse";
import { percent as $percent } from "@thi.ng/strings/percent";
import type { CellTransform } from "./api.js";

/**
Expand Down Expand Up @@ -89,6 +90,19 @@ export const date =
*/
export const url: CellTransform = (x) => new URL(x);

/**
* Cell parse value transform. Accepts an object of mappings with original cell
* values as keys which are then mapped to arbitrary new values in an enum like
* fashion.
*
* @param mappings
* @param defaultVal
*/
export const oneOf =
<T>(mappings: IObjectOf<T>, defaultVal: T): CellTransform =>
(x) =>
mappings[x] ?? defaultVal;

// formatters

export const zeroPad = (digits: number) => padLeft(digits, "0");
Expand Down

0 comments on commit 7c297db

Please sign in to comment.