Skip to content

Commit

Permalink
Added json-parse and json-stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
mojir committed Apr 22, 2024
1 parent 983936e commit d5e783f
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 263 deletions.
12 changes: 12 additions & 0 deletions __tests__/builtin/normalExpressions/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,5 +349,17 @@ describe(`misc functions`, () => {
expect(lits.run(`(equal? {:a 10} {:a 10, :b 20})`)).toBe(false)
})
})

describe(`json-stringify`, () => {
test(`samples`, () => {
expect(lits.run(`(json-stringify {:a 10 :b 20})`)).toBe(`{"a":10,"b":20}`)
expect(lits.run(`(json-stringify {:a 10 :b 20} 2)`)).toBe(`{\n "a": 10,\n "b": 20\n}`)
})
})
describe(`json-parse`, () => {
test(`samples`, () => {
expect(lits.run(`(json-parse "[1,2,3]")`)).toEqual([1, 2, 3])
})
})
}
})
39 changes: 39 additions & 0 deletions cli/reference/categories/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,43 @@ module.exports = {
`(equal? 0.3 (+ 0.1 0.2))`,
],
},
'json-parse': {
name: `json-parse`,
category: `Misc`,
linkName: `json-parse`,
clojureDocs: null,
returns: {
type: `any`,
},
arguments: [
{
name: `value`,
type: `string`,
},
],
description: `Returns JSON.parse(\`value\`).`,
examples: [`(json-parse "[1, 2, 3]")`],
},
'json-stringify': {
name: `json-stringify`,
category: `Misc`,
linkName: `json-stringify`,
clojureDocs: null,
returns: {
type: `string`,
},
arguments: [
{
name: `value`,
type: `any`,
},
{
name: `indent`,
type: `number`,
description: `optional`,
},
],
description: `Returns JSON.stringify(\`value\`). If second argument is provided, returns JSON.stringify(\`value\`, null, \`indent\`).`,
examples: [`(json-stringify [1, 2, 3])`, `(json-stringify {:a {:b 10}} 2)`],
},
}
562 changes: 306 additions & 256 deletions docs/index.html

Large diffs are not rendered by default.

22 changes: 21 additions & 1 deletion docs/lits.iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ function stringifyValue(value) {
if (value === Number.NEGATIVE_INFINITY) {
return Number.NEGATIVE_INFINITY
}
if (isNaN(value)) {
if (typeof value === 'number' && isNaN(value)) {
return 'NaN'
}
return JSON.stringify(value)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@youcruit/lits",
"version": "1.0.61",
"version": "2.0.62",
"description": "A lisp implementation in Typescript",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
2 changes: 1 addition & 1 deletion scripts/docs/buildDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ function stringifyValue(value) {
if (value === Number.NEGATIVE_INFINITY) {
return Number.NEGATIVE_INFINITY
}
if (isNaN(value)) {
if (typeof value === 'number' && isNaN(value)) {
return 'NaN'
}
return JSON.stringify(value)
Expand Down
2 changes: 1 addition & 1 deletion scripts/docs/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ function stringifyValue(value) {
if (value === Number.NEGATIVE_INFINITY) {
return Number.NEGATIVE_INFINITY
}
if (isNaN(value)) {
if (typeof value === 'number' && isNaN(value)) {
return 'NaN'
}
return JSON.stringify(value)
Expand Down
20 changes: 19 additions & 1 deletion src/builtin/normalExpressions/categories/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Any } from '../../../interface'
import { compare, deepEqual } from '../../../utils'
import type { BuiltinNormalExpressions } from '../../interface'
import { version } from '../../../version'
import { asAny } from '../../../typeGuards/lits'
import { asAny, assertAny } from '../../../typeGuards/lits'
import { assertNumber } from '../../../typeGuards/number'
import { assertString } from '../../../typeGuards/string'
import { assertNumberOfParams } from '../../../typeGuards'
Expand Down Expand Up @@ -176,4 +176,22 @@ export const miscNormalExpression: BuiltinNormalExpressions = {
},
validate: node => assertNumberOfParams(0, node),
},
'json-parse': {
evaluate: ([first], sourceCodeInfo): Any => {
assertString(first, sourceCodeInfo)
return JSON.parse(first)
},
validate: node => assertNumberOfParams(1, node),
},
'json-stringify': {
evaluate: ([first, second], sourceCodeInfo): string => {
assertAny(first, sourceCodeInfo)
if (second === undefined) {
return JSON.stringify(first)
}
assertNumber(second, sourceCodeInfo)
return JSON.stringify(first, null, second)
},
validate: node => assertNumberOfParams({ min: 1, max: 2 }, node),
},
}
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = `1.0.61`
export const version = `2.0.62`

0 comments on commit d5e783f

Please sign in to comment.