Skip to content

Commit

Permalink
fix: lint, better format
Browse files Browse the repository at this point in the history
Signed-off-by: Neko Ayaka <[email protected]>
  • Loading branch information
nekomeowww committed Sep 8, 2024
1 parent 90d9afb commit d1e465c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/neuri/src/formats/json/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './jsonParser'
export * from './jsonschema'
export * from './streamJsonParser'
39 changes: 32 additions & 7 deletions packages/neuri/src/formats/json/streamJsonParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { JSONSchema7 } from 'json-schema'
import Ajv from 'ajv'
import type { Token } from './jsonParser'
import { TokenType, createJSONParser } from './jsonParser'
import { createJSONParser } from './jsonParser'

type JSONValue = string | number | boolean | null | { [key: string]: JSONValue } | JSONValue[]

Expand Down Expand Up @@ -42,10 +42,14 @@ export function createStreamableJSONParser(schema: JSONSchema7, callback: Update

current = current[path[i]]
}

const lastKey = path[path.length - 1]
const previousValue = current[lastKey]

current[lastKey] = value

callback({ path, value, previousValue })

return currentObject
}

Expand All @@ -56,20 +60,32 @@ export function createStreamableJSONParser(schema: JSONSchema7, callback: Update
case 'JSONObject':
case 'JSONArray':
if (currentPath.length > 0) {
stack = [...stack, { object: currentObject, key: currentPath[currentPath.length - 1] }]
stack = [...stack, {
object: currentObject,
key: currentPath[currentPath.length - 1],
}]

const newValue: JSONValue = token.type === 'JSONObject' ? {} : []
currentObject = updateValue(currentPath, newValue, currentObject)
}
break
case 'JSONField':
currentPath = [...currentPath, token.content]
currentString = ''
if (token.children.length > 0)
({ currentObject, stack, currentPath, currentString } = processToken(token.children[0], { currentObject, stack, currentPath, currentString }))
if (token.children.length > 0) {
// Override
({
currentObject,
stack,
currentPath,
currentString,
} = processToken(token.children[0], { currentObject, stack, currentPath, currentString }))
}
break
case 'JSONString':
if (currentPath.length > 0) {
const content = token.content.replace(/^"|"$/g, '')

for (const char of content) {
currentString += char
currentObject = updateValue(currentPath, currentString, currentObject)
Expand All @@ -82,12 +98,20 @@ export function createStreamableJSONParser(schema: JSONSchema7, callback: Update
// eslint-disable-next-line no-case-declarations
let value: JSONValue
switch (token.type) {
case 'JSONNumber': value = Number(token.content); break
case 'JSONBoolean': value = token.content === 'true'; break
case 'JSONNull': value = null; break
case 'JSONNumber':
value = Number(token.content)
break
case 'JSONBoolean':
value = token.content === 'true'
break
case 'JSONNull':
value = null
break
}

currentObject = updateValue(currentPath, value, currentObject)
currentPath = currentPath.slice(0, -1)

break
}

Expand All @@ -96,6 +120,7 @@ export function createStreamableJSONParser(schema: JSONSchema7, callback: Update
if (child.type === 'JSONField')
({ currentObject, stack, currentPath, currentString } = processToken(child, { currentObject, stack, currentPath, currentString }))
}

if (stack.length > 0) {
const { object, key } = stack[stack.length - 1]
object[key] = currentObject
Expand Down

0 comments on commit d1e465c

Please sign in to comment.