From d58e33063f7de387fabffc099bb46034d1e375d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20K=C3=A1ntor?= Date: Mon, 30 Sep 2019 19:12:07 +0200 Subject: [PATCH] feat: add parseJSON --- docs/functions.md | 12 ++++ .../__snapshots__/interpreter.test.js.snap | 57 +++++++++++++++++++ src/__tests__/builtins.test.js | 8 +++ src/__tests__/interpreter.test.js | 7 +++ src/builtins.js | 6 +- 5 files changed, 89 insertions(+), 1 deletion(-) diff --git a/docs/functions.md b/docs/functions.md index 39bfd377..7fb4eb56 100644 --- a/docs/functions.md +++ b/docs/functions.md @@ -105,6 +105,18 @@ Reverses the input: [3, 2, 1] ``` +## parseJSON + +Parses a string containing a JSON value + +``` +"[1, 2, 3]" | parseJSON +``` + +``` +[1, 2, 3] +``` + ## reduce \ Compresses multiple items into a single item using the provided function and diff --git a/src/__tests__/__snapshots__/interpreter.test.js.snap b/src/__tests__/__snapshots__/interpreter.test.js.snap index 07eef79e..35d6c28c 100644 --- a/src/__tests__/__snapshots__/interpreter.test.js.snap +++ b/src/__tests__/__snapshots__/interpreter.test.js.snap @@ -117,6 +117,8 @@ exports[`interpreter correct target code .foo | .bar 1`] = `"(function(_) { retu exports[`interpreter correct target code [ [ [ [ [ ] ]]] ] 1`] = `"(function(_) { return (function(input) { return Array.from([Array.from([Array.from([Array.from([Array.from([])])])])])})})"`; +exports[`interpreter correct target code ['{"foo": "bar"}'] | parseJSON 1`] = `"(function(_) { return (function(input) { return (function (input) {return _.parseJSON(input)})(Array.from(['{\\"foo\\": \\"bar\\"}']))})})"`; + exports[`interpreter correct target code [($ => error "asd" | "f")] 1`] = `"(function(_) { return (function(input) { return Array.from([((function (input) {return \\"f\\"})((function(input) {return _.error(\\"asd\\")(input)})))])})})"`; exports[`interpreter correct target code [...$foo] where $foo = "Hello" 1`] = `"(function(_) { return (function(input) { return ((function() {_ = _.__assign__('foo', (\\"Hello\\"), _); return (Array.from(_.get('foo')))})())})})"`; @@ -2623,6 +2625,61 @@ Object { } `; +exports[`interpreter correct target tree ['{"foo": "bar"}'] | parseJSON 1`] = ` +Object { + "status": true, + "value": Object { + "name": "pipe", + "value": Object { + "left": Object { + "end": Object { + "column": 19, + "line": 1, + "offset": 18, + }, + "name": "list", + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + "value": Array [ + Object { + "end": Object { + "column": 18, + "line": 1, + "offset": 17, + }, + "name": "simpleList", + "start": Object { + "column": 2, + "line": 1, + "offset": 1, + }, + "value": Array [ + Object { + "name": "primitive", + "value": "'{\\"foo\\": \\"bar\\"}'", + }, + ], + }, + ], + }, + "right": Object { + "name": "functionCall", + "value": Object { + "left": Object { + "name": "identifier", + "value": "parseJSON", + }, + "right": null, + }, + }, + }, + }, +} +`; + exports[`interpreter correct target tree [($ => error "asd" | "f")] 1`] = ` Object { "status": true, diff --git a/src/__tests__/builtins.test.js b/src/__tests__/builtins.test.js index 8cc6968e..02962e7a 100644 --- a/src/__tests__/builtins.test.js +++ b/src/__tests__/builtins.test.js @@ -6,6 +6,7 @@ const { map, sortBy, filter, + parseJSON, get, __assign__, reverse, @@ -242,6 +243,13 @@ describe('built ins', () => { expect(__spread__('ab')).toEqual(['a', 'b']) }) + describe('parseJSON', () => { + it('returns correct value', () => { + expect(parseJSON('{}')).toEqual({}) + expect(parseJSON('[0]')).toEqual([0]) + }) + }) + describe('join', () => { it('returns correct value', () => { expect(join(' ')(['Hello', 'World'])).toEqual('Hello World') diff --git a/src/__tests__/interpreter.test.js b/src/__tests__/interpreter.test.js index 7e753233..11da7574 100644 --- a/src/__tests__/interpreter.test.js +++ b/src/__tests__/interpreter.test.js @@ -640,6 +640,13 @@ x` 'Age of Joe': 13, 'Age of Marie': 14 } + }, + { + sourceCode: `['{"foo": "bar"}'] | parseJSON`, + input: null, + output: { + foo: 'bar' + } } ] diff --git a/src/builtins.js b/src/builtins.js index 1f6f4970..c9c9cbe6 100644 --- a/src/builtins.js +++ b/src/builtins.js @@ -17,6 +17,8 @@ const convertUndefined = (value: ?mixed): mixed | null => const handleOptional = (value: ?mixed, f: mixed => mixed): mixed => convertUndefined(value) === null ? null : f(value) +const parseJSON = JSON.parse + const objectify = (input: Array<[string, mixed]>): { [string]: mixed } => input.reduce(function ( a: { [string]: mixed }, @@ -179,5 +181,7 @@ export default { error: (message: string): (mixed => void) => (input: mixed) => { throw new Error(message) - } + }, + + parseJSON }