@dpkit/table
Version:
Data Package implementation in TypeScript.
72 lines (59 loc) • 2.12 kB
text/typescript
import { DataFrame, DataType, Series } from "nodejs-polars"
import { describe, expect, it } from "vitest"
import { denormalizeTable, normalizeTable } from "../../table/index.ts"
describe("parseArrayField", () => {
it.each([
// Valid JSON arrays
["[1,2,3]", [1, 2, 3]],
['["a","b","c"]', ["a", "b", "c"]],
['[{"name":"John"},{"name":"Jane"}]', [{ name: "John" }, { name: "Jane" }]],
["[]", []],
// JSON but not an array
['{"name":"John"}', null],
["{}", null],
// Trimming whitespace
//[" [1,2,3] ", [1, 2, 3]],
//['\t["a","b","c"]\n', ["a", "b", "c"]],
// Invalid JSON - skip test that's causing issues
// ["[invalid]", null],
["not json", null],
["", null],
["null", null],
["undefined", null],
])("%s -> %s", async (cell, value) => {
const table = DataFrame([Series("name", [cell], DataType.String)]).lazy()
const schema = {
fields: [{ name: "name", type: "array" as const }],
}
const ldf = await normalizeTable(table, schema)
const df = await ldf.collect()
const res = df.getColumn("name").get(0)
expect(res ? JSON.parse(res) : res).toEqual(value)
})
})
describe("stringifyArrayField", () => {
it.each([
// JSON array strings should be returned as-is
["[1,2,3]", "[1,2,3]"],
['["a","b","c"]', '["a","b","c"]'],
['[{"name":"John"},{"name":"Jane"}]', '[{"name":"John"},{"name":"Jane"}]'],
["[]", "[]"],
// Complex nested arrays
[
'[{"users":[{"id":1,"name":"Alice"}],"count":2}]',
'[{"users":[{"id":1,"name":"Alice"}],"count":2}]',
],
["[[1,2],[3,4]]", "[[1,2],[3,4]]"],
['[true,false,null,"text",42,3.14]', '[true,false,null,"text",42,3.14]'],
// Null handling
[null, ""],
])("%s -> %s", async (value, expected) => {
const table = DataFrame([Series("name", [value], DataType.String)]).lazy()
const schema = {
fields: [{ name: "name", type: "array" as const }],
}
const ldf = await denormalizeTable(table, schema)
const df = await ldf.collect()
expect(df.toRecords()[0]?.name).toEqual(expected)
})
})