json.encode & json.decode
This commit is contained in:
parent
68ec6f9f3e
commit
f4b1b96bc0
|
|
@ -6,6 +6,7 @@ import {
|
|||
} from 'reefvm'
|
||||
|
||||
import { dict } from './dict'
|
||||
import { json } from './json'
|
||||
import { load } from './load'
|
||||
import { list } from './list'
|
||||
import { math } from './math'
|
||||
|
|
@ -13,6 +14,7 @@ import { str } from './str'
|
|||
|
||||
export const globals = {
|
||||
dict,
|
||||
json,
|
||||
load,
|
||||
list,
|
||||
math,
|
||||
|
|
|
|||
7
src/prelude/json.ts
Normal file
7
src/prelude/json.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
export const json = {
|
||||
encode: (s: any) => JSON.stringify(s),
|
||||
decode: (s: string) => JSON.parse(s),
|
||||
}
|
||||
|
||||
; (json as any).parse = json.decode
|
||||
; (json as any).stringify = json.encode
|
||||
17
src/prelude/tests/json.test.ts
Normal file
17
src/prelude/tests/json.test.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { expect, describe, test } from 'bun:test'
|
||||
|
||||
describe('json', () => {
|
||||
test('json.decode', () => {
|
||||
expect(`json.decode '[1,2,3]'`).toEvaluateTo([1, 2, 3])
|
||||
expect(`json.decode '"heya"'`).toEvaluateTo('heya')
|
||||
expect(`json.decode '[true, false, null]'`).toEvaluateTo([true, false, null])
|
||||
expect(`json.decode '{"a": true, "b": false, "c": "yeah"}'`).toEvaluateTo({ a: true, b: false, c: "yeah" })
|
||||
})
|
||||
|
||||
test('json.encode', () => {
|
||||
expect(`json.encode [1 2 3]`).toEvaluateTo('[1,2,3]')
|
||||
expect(`json.encode 'heya'`).toEvaluateTo('"heya"')
|
||||
expect(`json.encode [true false null]`).toEvaluateTo('[true,false,null]')
|
||||
expect(`json.encode [a=true b=false c='yeah'] | json.decode`).toEvaluateTo({ a: true, b: false, c: "yeah" })
|
||||
})
|
||||
})
|
||||
Loading…
Reference in New Issue
Block a user