Skip to content

Commit

Permalink
Create enum for 2D JSON data analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaster committed Oct 1, 2021
1 parent bde6a98 commit 6ccb3d8
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/run-it/src/components/ShowResponse/responseUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,22 @@ export const isColumnar = (data: any[]) => {
return !complex
}

type ItemType = 'a' | 'o' | 'v' | 'u'
enum ItemType {
Array = 'a',
Object = 'o',
Simple = 's',
Undefined = 'u',
}

/**
* Is this an array, an object, a value, or undefined
* @param value to check
*/
const itemType = (value: any): ItemType => {
if (!value) return 'u'
if (Array.isArray(value)) return 'a'
if (value instanceof Object) return 'o'
return 'v'
if (!value) return ItemType.Undefined
if (Array.isArray(value)) return ItemType.Array
if (value instanceof Object) return ItemType.Object
return ItemType.Simple
}

/**
Expand All @@ -93,16 +98,16 @@ const itemType = (value: any): ItemType => {
const getTypes = (json: any) => {
const types = [new Set<ItemType>(), new Set<ItemType>()]
if (!json) {
types[0].add('u')
types[0].add(ItemType.Undefined)
return types
}
for (const key of Object.keys(json)) {
const value = json[key]
const type = itemType(value)
types[0].add(type)
switch (type) {
case 'a':
case 'o':
case ItemType.Array:
case ItemType.Object:
Object.keys(value).forEach((k) => {
const v = value[k]
types[1].add(itemType(v))
Expand All @@ -121,7 +126,7 @@ export const canTabulate = (json: any) => {
const types = getTypes(json)
return (
types[0].size === 1 &&
(types[0].has('a') || types[0].has('o')) &&
(types[0].has(ItemType.Array) || types[0].has(ItemType.Object)) &&
types[1].size <= 1
)
}
Expand Down

0 comments on commit 6ccb3d8

Please sign in to comment.