Skip to content

Commit

Permalink
Export toJson
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Jan 5, 2024
1 parent 02c2505 commit 954671f
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<body>
<div id="dropzone">Drop .parquet file here</div>
<script type="module">
import { parquetMetadata, toJson } from './src/metadata.js'
import { parquetMetadata, toJson } from './src/hyparquet.js'

const dropZone = document.getElementById('dropzone')

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "parquet file parser for javascript",
"keywords": [
"parquet",
"parser"
"parser",
"snappy",
"thrift"
],
"license": "MIT",
"repository": {
Expand Down
10 changes: 10 additions & 0 deletions src/hyparquet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ export function parquetMetadata(arrayBuffer: ArrayBuffer): any
* @returns {boolean} true if successful
*/
export function snappyUncompress(inputArray: Uint8Array, outputArray: Uint8Array): boolean

/**
* Replace bigints with numbers.
* When parsing parquet files, bigints are used to represent 64-bit integers.
* However, JSON does not support bigints, so it's helpful to convert to numbers.
*
* @param {any} obj object to convert
* @returns {unknown} converted object
*/
export function toJson(obj: any): unknown
3 changes: 3 additions & 0 deletions src/hyparquet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export { parquetMetadata }
import { snappyUncompress } from './snappy.js'
export { snappyUncompress }

import { toJson } from './toJson.js'
export { toJson }

/**
* Read parquet data rows from a file
*
Expand Down
25 changes: 0 additions & 25 deletions src/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,3 @@ export function parquetMetadata(arrayBuffer) {
created_by,
}
}

/**
* Replace bigints with numbers.
* When parsing parquet files, bigints are used to represent 64-bit integers.
* However, JSON does not support bigints, so it's helpful to convert to numbers.
*
* @param {any} obj object to convert
* @returns {unknown} converted object
*/
export function toJson(obj) {
if (typeof obj === 'bigint') {
return Number(obj)
} else if (Array.isArray(obj)) {
return obj.map(toJson)
} else if (obj instanceof Object) {
/** @type {Record<string, unknown>} */
const newObj = {}
for (const key of Object.keys(obj)) {
newObj[key] = toJson(obj[key])
}
return newObj
} else {
return obj
}
}
24 changes: 24 additions & 0 deletions src/toJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Replace bigints with numbers.
* When parsing parquet files, bigints are used to represent 64-bit integers.
* However, JSON does not support bigints, so it's helpful to convert to numbers.
*
* @param {any} obj object to convert
* @returns {unknown} converted object
*/
export function toJson(obj) {
if (typeof obj === 'bigint') {
return Number(obj)
} else if (Array.isArray(obj)) {
return obj.map(toJson)
} else if (obj instanceof Object) {
/** @type {Record<string, unknown>} */
const newObj = {}
for (const key of Object.keys(obj)) {
newObj[key] = toJson(obj[key])
}
return newObj
} else {
return obj
}
}
4 changes: 2 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export enum ParquetType {
BOOLEAN = 0,
INT32 = 1,
INT64 = 2,
INT96 = 3,
INT96 = 3, // deprecated
FLOAT = 4,
DOUBLE = 5,
BYTE_ARRAY = 6,
Expand Down Expand Up @@ -93,7 +93,7 @@ export enum Encoding {
PLAIN = 0,
PLAIN_DICTIONARY = 2,
RLE = 3,
BIT_PACKED = 4,
BIT_PACKED = 4, // deprecated
DELTA_BINARY_PACKED = 5,
DELTA_LENGTH_BYTE_ARRAY = 6,
DELTA_BYTE_ARRAY = 7,
Expand Down
3 changes: 2 additions & 1 deletion test/metadata.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { promises as fs } from 'fs'
import { describe, expect, it } from 'vitest'
import { parquetMetadata, toJson } from '../src/metadata.js'
import { parquetMetadata } from '../src/metadata.js'
import { toJson } from '../src/toJson.js'

/**
* Helper function to read .parquet file into ArrayBuffer
Expand Down

0 comments on commit 954671f

Please sign in to comment.