Skip to content

Commit

Permalink
fix(Set): Only use += in Set when value is an object
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesfer committed Jul 8, 2018
1 parent b87c0ff commit c61a37f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@
"@semantic-release/npm",
{
"path": "@semantic-release/git",
"assets": ["package.json", "CHANGELOG.md"],
"assets": [
"package.json",
"CHANGELOG.md"
],
"message": "chore(release): ${nextRelease.version}\n\n${nextRelease.notes}\n[skip ci]\n"
}
]
Expand Down
10 changes: 9 additions & 1 deletion src/clauses/set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,18 @@ describe('Set', () => {

it('should merge properties when override is false', () => {
const data = {
values: { node: 'value' },
values: { node: { name: 'complex value' } },
variables: { node2: 'variable' },
};
const query = new Set(data, { override: false });
expect(query.build()).to.equal('SET node += $node, node2 += variable');
});

it('should not merge plain values even when override is false', () => {
const data = {
values: { node: 'value' },
};
const query = new Set(data, { override: false });
expect(query.build()).to.equal('SET node = $node');
});
});
44 changes: 22 additions & 22 deletions src/clauses/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ export class Set extends Clause {
protected variables: Dictionary<string | Dictionary<string>>;
protected override: boolean;

protected makeLabelStatement = (labels: Many<string>, key: string) => {
return key + stringifyLabels(labels);
}

protected makeValueStatement = (value: any, key: string): string => {
const valueIsObject = value instanceof Parameter ? isObject(value.value) : isObject(value);
const op = this.override || !valueIsObject ? ' = ' : ' += ';
return key + op + value;
}

protected makeVariableStatement = (value: string | Dictionary<string>, key: string): string => {
const op = this.override ? ' = ' : ' += ';
if (isObject(value)) {
const operationStrings = map(value, (value, prop) => `${key}.${prop}${op}${value}`);
return join(operationStrings, ', ');
}
return key + op + value;
}

constructor(
{ labels, values, variables }: SetProperties,
inOptions?: SetOptions,
Expand All @@ -44,28 +63,9 @@ export class Set extends Clause {
}

build() {
const labels = map(this.labels, (labels, key) => {
return key + stringifyLabels(labels);
});
const values = this.makeSetStatements(this.values);
const variables = this.makeSetStatements(this.variables, true);

const labels = map(this.labels, this.makeLabelStatement);
const values = map(this.values, this.makeValueStatement);
const variables = map(this.variables, this.makeVariableStatement);
return 'SET ' + join(concat(labels, values, variables), ', ');
}

protected makeSetStatements(
obj: Dictionary<string | Parameter | Dictionary<string>>,
recursive: boolean = false,
) {
return map(obj, (value, key) => this.setStatement(value, key, recursive));
}

protected setStatement(value, key, recursive: boolean = false) {
const op = this.override ? ' = ' : ' += ';
if (isObject(value) && recursive) {
const operationStrings = map(value, (value, prop) => key + '.' + prop + op + value);
return join(operationStrings, ', ');
}
return key + op + value;
}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4576,6 +4576,12 @@ rx@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/rx/-/rx-4.1.0.tgz#a5f13ff79ef3b740fe30aa803fb09f98805d4782"

rxjs@^5.5.6:
version "5.5.11"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-5.5.11.tgz#f733027ca43e3bec6b994473be4ab98ad43ced87"
dependencies:
symbol-observable "1.0.1"

safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"
Expand Down Expand Up @@ -5034,6 +5040,10 @@ supports-color@^4.0.0, supports-color@^4.4.0:
dependencies:
has-flag "^2.0.0"

[email protected]:
version "1.0.1"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.0.1.tgz#8340fc4702c3122df5d22288f88283f513d3fdd4"

table@^3.7.8:
version "3.8.3"
resolved "https://registry.yarnpkg.com/table/-/table-3.8.3.tgz#2bbc542f0fda9861a755d3947fefd8b3f513855f"
Expand Down

0 comments on commit c61a37f

Please sign in to comment.