Skip to content

Commit

Permalink
feat: add parseJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Sep 30, 2019
1 parent 6d75094 commit d58e330
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions src/__tests__/__snapshots__/interpreter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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')))})())})})"`;
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/builtins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
map,
sortBy,
filter,
parseJSON,
get,
__assign__,
reverse,
Expand Down Expand Up @@ -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')
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/interpreter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,13 @@ x`
'Age of Joe': 13,
'Age of Marie': 14
}
},
{
sourceCode: `['{"foo": "bar"}'] | parseJSON`,
input: null,
output: {
foo: 'bar'
}
}
]

Expand Down
6 changes: 5 additions & 1 deletion src/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -179,5 +181,7 @@ export default {

error: (message: string): (mixed => void) => (input: mixed) => {
throw new Error(message)
}
},

parseJSON
}

0 comments on commit d58e330

Please sign in to comment.