Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased](https://github.com/hyparam/hightable/compare/v0.13.1...HEAD)

### Refactor

- fix the return type of `stringify` ([#85](https://github.com/hyparam/hightable/pull/85)).

## [0.13.1](https://github.com/hyparam/hightable/compare/v0.13.0...v0.13.1) - 2025-03-27

### Changed
Expand Down
7 changes: 5 additions & 2 deletions src/utils/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/**
* Robust stringification of any value, including json and bigints.
*/
export function stringify(value: unknown): string {
export function stringify(value: unknown): string | undefined {
if (typeof value === 'string') return value
if (typeof value === 'number' || typeof value === 'bigint') return value.toLocaleString('en-US')
if (typeof value === 'boolean') return value.toString()
if (Array.isArray(value)) {
return `[\n${value.map((v) => indent(stringify(v), 2)).join(',\n')}\n]`
}
// Beware: JSON.stringify is not robustly typed since it returns undefined (not a string) for undefined:
// More details and corner cases here: https://github.com/microsoft/TypeScript/issues/18879#issuecomment-1399758565
if (value === null || value === undefined) return JSON.stringify(value)
if (value instanceof Date) return value.toISOString()
if (typeof value === 'object') {
Expand All @@ -16,7 +18,8 @@ export function stringify(value: unknown): string {
.map(([k, v]) => `${k}: ${stringify(v)}`)
.join(', ')}}`
}
return '{}'
// Fallback to JSON.stringify for unknown types or corner cases (e.g. functions)
return JSON.stringify(value)
}

function indent(text: string | undefined, spaces: number) {
Expand Down