Skip to content

Commit

Permalink
feat: add update function
Browse files Browse the repository at this point in the history
fixes #557
  • Loading branch information
kantord committed Oct 1, 2019
1 parent 81978e6 commit a61bc39
Show file tree
Hide file tree
Showing 6 changed files with 150 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 @@ -131,6 +131,18 @@ Parses a string containing a JSON value
[1, 2, 3]
```

## update

Performs a deep merge on two objects

```
{ 'a': [{ 'b': 2 }, { 'd': 4 }] } | update { 'a': [{ 'c': 3 }, { 'e': 5 }] }
```

```
{ 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }
```

## reduce \

Compresses multiple items into a single item using the provided function and
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"dependencies": {
"cartesian-product-generator": "^1.1.1",
"combinations-generator": "^1.0.1",
"lodash.merge": "^4.6.2",
"parsimmon": "^1.13.0"
},
"config": {
Expand Down
112 changes: 112 additions & 0 deletions src/__tests__/__snapshots__/interpreter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ exports[`interpreter correct target code sortBy \\.age | map \\.name 1`] = `"(fu
exports[`interpreter correct target code true 1`] = `"(function(_) { return (function(input) { return true})})"`;
exports[`interpreter correct target code update {'x': 3.14, 'deps': {'react': '4.5'}} 1`] = `"(function(_) { return (function(input) { return _.update((_.objectify([].concat(_.__spread__([['x',3.14], ['deps',(_.objectify([].concat(_.__spread__([['react','4.5']]))))]])))))(input)})})"`;
exports[`interpreter correct target tree
$ {
...Stuff
Expand Down Expand Up @@ -8973,3 +8975,113 @@ Object {
},
}
`;
exports[`interpreter correct target tree update {'x': 3.14, 'deps': {'react': '4.5'}} 1`] = `
Object {
"status": true,
"value": Object {
"name": "functionCall",
"value": Object {
"left": Object {
"name": "identifier",
"value": "update",
},
"right": Object {
"end": Object {
"column": 45,
"line": 1,
"offset": 44,
},
"name": "object",
"start": Object {
"column": 8,
"line": 1,
"offset": 7,
},
"value": Array [
Object {
"end": Object {
"column": 44,
"line": 1,
"offset": 43,
},
"name": "simpleList",
"start": Object {
"column": 9,
"line": 1,
"offset": 8,
},
"value": Array [
Object {
"name": "tuple",
"value": Array [
Object {
"name": "primitive",
"value": "'x'",
},
Object {
"name": "primitive",
"value": "3.14",
},
],
},
Object {
"name": "tuple",
"value": Array [
Object {
"name": "primitive",
"value": "'deps'",
},
Object {
"end": Object {
"column": 44,
"line": 1,
"offset": 43,
},
"name": "object",
"start": Object {
"column": 28,
"line": 1,
"offset": 27,
},
"value": Array [
Object {
"end": Object {
"column": 43,
"line": 1,
"offset": 42,
},
"name": "simpleList",
"start": Object {
"column": 29,
"line": 1,
"offset": 28,
},
"value": Array [
Object {
"name": "tuple",
"value": Array [
Object {
"name": "primitive",
"value": "'react'",
},
Object {
"name": "primitive",
"value": "'4.5'",
},
],
},
],
},
],
},
],
},
],
},
],
},
},
},
}
`;
16 changes: 16 additions & 0 deletions src/__tests__/interpreter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,22 @@ x`
sourceCode: `[{...$}]`,
input: { foo: 'bar' },
output: [{ foo: 'bar' }]
},
{
sourceCode: `update {'x': 3.14, 'deps': {'react': '4.5'}}`,
input: {
deps: {
react: '6. 5'
},
devDeps: []
},
output: {
x: 3.14,
deps: {
react: '4.5'
},
devDeps: []
}
}
]

Expand Down
5 changes: 4 additions & 1 deletion src/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import combinations from 'combinations-generator'
import { product } from 'cartesian-product-generator'
import type { ObjectProjectionItemType } from './types'
import merge from 'lodash.merge'

type ProjectableType = Array<mixed> & { [string]: mixed };
type ProjectionRuleType = number & string;
Expand Down Expand Up @@ -187,5 +188,7 @@ export default {
throw new Error(message)
},

parseJSON
parseJSON,
update: (updateWith: Object): (Object => Object) => (input: Object): Object =>
merge(input, updateWith)
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7056,6 +7056,11 @@ lodash.map@^4.5.1:
resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3"
integrity sha1-dx7Hg540c9nEzeKLGTlMNWL09tM=

lodash.merge@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
Expand Down

0 comments on commit a61bc39

Please sign in to comment.