@dpkit/table
Version:
Data Package implementation in TypeScript.
24 lines (18 loc) • 630 B
text/typescript
import type { YearField } from "@dpkit/core"
import { DataType, lit, when } from "nodejs-polars"
import { col } from "nodejs-polars"
import type { Expr } from "nodejs-polars"
export function parseYearField(field: YearField, expr?: Expr) {
expr = expr ?? col(field.name)
expr = when(expr.str.lengths().eq(4))
.then(expr)
.otherwise(lit(null))
.cast(DataType.Int16)
return when(expr.gtEq(0).and(expr.ltEq(9999)))
.then(expr)
.otherwise(lit(null))
}
export function stringifyYearField(field: YearField, expr?: Expr) {
expr = expr ?? col(field.name)
return expr.cast(DataType.String).str.zFill(4)
}