Skip to content

Commit 8ee869c

Browse files
committed
use the same stringify function
1 parent 2d9c1f2 commit 8ee869c

File tree

4 files changed

+33
-30
lines changed

4 files changed

+33
-30
lines changed

src/components/Cell.tsx

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { asyncBufferFromUrl, parquetMetadataAsync } from 'hyparquet'
22
import { useEffect, useState } from 'react'
33
import type { FileSource } from '../lib/sources/types.js'
44
import { parquetDataFrame } from '../lib/tableProvider.js'
5+
import { stringify } from '../lib/utils.js'
56
import Breadcrumb, { BreadcrumbConfig } from './Breadcrumb.js'
67
import Layout from './Layout.js'
78

@@ -75,30 +76,3 @@ export default function CellView({ source, row, col, config }: CellProps) {
7576
</Layout>
7677
)
7778
}
78-
79-
/**
80-
* Robust stringification of any value, including json and bigints.
81-
*/
82-
function stringify(value: unknown): string {
83-
if (typeof value === 'string') return value
84-
if (typeof value === 'number') return value.toLocaleString('en-US')
85-
if (Array.isArray(value)) {
86-
return `[\n${value.map((v) => indent(stringify(v), 2)).join(',\n')}\n]`
87-
}
88-
if (value === null || value === undefined) return JSON.stringify(value)
89-
if (value instanceof Date) return value.toISOString()
90-
if (typeof value === 'object') {
91-
return `{${Object.entries(value)
92-
.filter((d) => d[1] !== undefined)
93-
.map(([k, v]) => `${k}: ${stringify(v)}`)
94-
.join(', ')}}`
95-
}
96-
return '{}'
97-
}
98-
99-
function indent(text: string | undefined, spaces: number) {
100-
return text
101-
?.split('\n')
102-
.map((line) => ' '.repeat(spaces) + line)
103-
.join('\n')
104-
}

src/components/viewers/CellPanel.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { DataFrame, stringify } from 'hightable'
1+
import { DataFrame } from 'hightable'
22
import { useEffect, useState } from 'react'
3+
import { stringify } from '../../lib/utils.js'
34
import ContentHeader from './ContentHeader.js'
45

56
interface ViewerProps {
@@ -36,8 +37,7 @@ export default function CellPanel({ df, row, col, setProgress, setError, onClose
3637
if (asyncCell === undefined) {
3738
throw new Error(`Cell missing at column ${columnName}`)
3839
}
39-
/* TODO(SL): use the same implementation of stringify, here and in Cell.tsx */
40-
const text = await asyncCell.then(cell => stringify(cell as unknown) ?? '{}')
40+
const text = await asyncCell.then(stringify)
4141
setText(text)
4242
} catch (error) {
4343
setError(error as Error)

src/components/viewers/ParquetView.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, { useCallback, useEffect, useState } from 'react'
44
import { RoutesConfig, appendSearchParams } from '../../lib/routes.js'
55
import { FileSource } from '../../lib/sources/types.js'
66
import { parquetDataFrame } from '../../lib/tableProvider.js'
7+
import { stringify } from '../../lib/utils.js'
78
import classes from '../../styles/ParquetView.module.css'
89
import { Spinner } from '../Layout.js'
910
import CellPanel from './CellPanel.js'
@@ -103,6 +104,7 @@ export default function ParquetView({ source, setProgress, setError, config }: V
103104
onMouseDownCell={onMouseDownCell}
104105
onError={setError}
105106
className={classes.hightable}
107+
stringify={stringify}
106108
/>}
107109

108110
{isLoading && <div className='center'><Spinner /></div>}

src/lib/utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,30 @@ export const contentTypes: Record<string, string> = {
9797
}
9898

9999
export const imageTypes = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.tiff', '.webp']
100+
101+
/**
102+
* Robust stringification of any value, including json and bigints.
103+
*/
104+
export function stringify(value: unknown): string {
105+
if (typeof value === 'string') return value
106+
if (typeof value === 'number') return value.toLocaleString('en-US')
107+
if (Array.isArray(value)) {
108+
return `[\n${value.map((v) => indent(stringify(v), 2)).join(',\n')}\n]`
109+
}
110+
if (value === null || value === undefined) return JSON.stringify(value)
111+
if (value instanceof Date) return value.toISOString()
112+
if (typeof value === 'object') {
113+
return `{${Object.entries(value)
114+
.filter((d) => d[1] !== undefined)
115+
.map(([k, v]) => `${k}: ${stringify(v)}`)
116+
.join(', ')}}`
117+
}
118+
return '{}'
119+
}
120+
121+
function indent(text: string | undefined, spaces: number) {
122+
return text
123+
?.split('\n')
124+
.map((line) => ' '.repeat(spaces) + line)
125+
.join('\n')
126+
}

0 commit comments

Comments
 (0)