diff --git a/README.md b/README.md index 439cb23..f413c28 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![minzipped](https://img.shields.io/bundlephobia/minzip/hightable)](https://www.npmjs.com/package/hightable) [![workflow status](https://github.com/hyparam/hightable/actions/workflows/ci.yml/badge.svg)](https://github.com/hyparam/hightable/actions) [![mit license](https://img.shields.io/badge/License-MIT-orange.svg)](https://opensource.org/licenses/MIT) -![coverage](https://img.shields.io/badge/Coverage-93-darkred) +![coverage](https://img.shields.io/badge/Coverage-94-darkred) [![dependencies](https://img.shields.io/badge/Dependencies-0-blueviolet)](https://www.npmjs.com/package/hightable?activeTab=dependencies) HighTable is a virtualized table component for React, designed to efficiently display large datasets in the browser. It loads and renders only the rows necessary for the current viewport, enabling smooth scrolling and performance even with millions of rows. HighTable supports asynchronous data fetching, dynamic loading, and optional column sorting. diff --git a/src/HighTable.tsx b/src/HighTable.tsx index 82a224b..9545cde 100644 --- a/src/HighTable.tsx +++ b/src/HighTable.tsx @@ -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() 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() diff --git a/test/stringify.test.ts b/test/stringify.test.ts new file mode 100644 index 0000000..dc281a4 --- /dev/null +++ b/test/stringify.test.ts @@ -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') + }) +})