-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -341,6 +341,7 @@ export default function HighTable({ | |
export function stringify(value: any): string | undefined { | ||
if (typeof value === 'string') return value | ||
if (typeof value === 'number') return value.toLocaleString() | ||
if (typeof value === 'bigint') return value.toLocaleString() | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
severo
Contributor
|
||
if (Array.isArray(value)) return `[${value.map(stringify).join(', ')}]` | ||
if (value === null || value === undefined) return JSON.stringify(value) | ||
if (value instanceof Date) return value.toISOString() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { stringify } from '../src/HighTable.js' | ||
|
||
describe('stringify', () => { | ||
test('returns the same string if input is a string', () => { | ||
expect(stringify('Hello')).toBe('Hello') | ||
}) | ||
|
||
test('returns the localized string if input is a number', () => { | ||
expect(stringify(4200)).toBe('4,200') | ||
}) | ||
|
||
test('stringifies null and undefined as JSON', () => { | ||
expect(stringify(null)).toBe('null') | ||
expect(stringify(undefined)).toBe(undefined) | ||
}) | ||
|
||
test('returns ISO string if input is a Date', () => { | ||
const date = new Date('2020-01-01T00:00:00.000Z') | ||
expect(stringify(date)).toBe('2020-01-01T00:00:00.000Z') | ||
}) | ||
|
||
test('handles arrays', () => { | ||
expect(stringify([1, 2, 3])).toBe('[1, 2, 3]') | ||
expect(stringify(['a', 'b'])).toBe('[a, b]') | ||
}) | ||
|
||
test('handles objects', () => { | ||
const obj = { a: 1, b: 'two' } | ||
expect(stringify(obj)).toBe('{a: 1, b: two}') | ||
}) | ||
|
||
test('handles nested objects and arrays', () => { | ||
const nested = { | ||
a: [1, 2, { x: 'x' }], | ||
b: { c: 'hello' }, | ||
} | ||
expect(stringify(nested)).toBe('{a: [1, 2, {x: x}], b: {c: hello}}') | ||
}) | ||
|
||
test('handles booleans', () => { | ||
expect(stringify(true)).toBe('true') | ||
expect(stringify(false)).toBe('false') | ||
}) | ||
|
||
test('handles bigints', () => { | ||
expect(stringify(BigInt(123))).toBe('123') | ||
expect(stringify(BigInt(4200))).toBe('4,200') | ||
}) | ||
}) |
I have two comments after reading the docs for toLocaleString().
Is this what we want? Or do we want to force 'en-US'? (eg: running the tests on a machine with another default language would fail)
So, maybe we want to setup a NumberFormat object to (prematurely?) optimize