Skip to content

Commit

Permalink
feat: 🎸 add option default
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Apr 7, 2021
1 parent 59b5916 commit dd2dee4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/analytics/src/combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function sha1(input, salt) {
*
* @name combine
* @param {String} [path] the path to substitute
* @param {String} [default] value if no substitution (otherwise value stay unchanged)
* @param {String} [primer=auto] Data to send to the external pipeline
* @param {String} [file] the external pipeline is described in a file
* @param {String} [script] the external pipeline is described in a string of characters
Expand Down Expand Up @@ -115,6 +116,7 @@ export default function combine(data, feed) {
}
return whenReady
.then(async () => {
const defval = this.getParam('default', null);
const path = this.getParam('path');
const pathVal = get(data, path);
const keys = [].concat(pathVal).filter(Boolean);
Expand All @@ -128,6 +130,9 @@ export default function combine(data, feed) {
const val = values.shift();
if (val !== null) {
set(data, path, val);
} else if (defval !== null) {
const orig = get(data, path);
set(data, path, { id: orig, value: defval });
} else {
const orig = get(data, path);
set(data, path, { id: orig, value: orig });
Expand Down
84 changes: 84 additions & 0 deletions packages/analytics/test/combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,90 @@ describe('combine', () => {
done();
});
});
test('with script #1bis', (done) => {
ezs.use(statements);
const input = [
{ a: 1, b: 'a' },
{ a: 2, b: 'x' },
{ a: 3, b: 'c' },
{ a: 4, b: 'y' },
{ a: 5, b: 'e' },
{ a: 6, b: 'z' },
];
const output = [];
const script = `
[use]
plugin = analytics
[replace]
path = value
value = fix({id:'a', value:'aa'},{id:'b', value:'bb'},{id:'c', value:'cc'},{id:'d', value:'dd'},{id:'e', value:'ee'},{id:'f', value:'ff'})
[exploding]
[value]
`;

from(input)
.pipe(ezs('combine', { path: 'b', script }))
.pipe(ezs.catch())
.on('error', done)
.on('data', (chunk) => {
output.push(chunk);
})
.on('end', () => {
assert.equal(output.length, 6);
assert.equal(output[0].b.value, 'aa');
assert.equal(output[1].b.value, 'x');
assert.equal(output[2].b.value, 'cc');
assert.equal(output[3].b.value, 'y');
assert.equal(output[4].b.value, 'ee');
assert.equal(output[5].b.value, 'z');
done();
});
});

test('with script #1ter', (done) => {
ezs.use(statements);
const input = [
{ a: 1, b: 'a' },
{ a: 2, b: 'x' },
{ a: 3, b: 'c' },
{ a: 4, b: 'y' },
{ a: 5, b: 'e' },
{ a: 6, b: 'z' },
];
const output = [];
const script = `
[use]
plugin = analytics
[replace]
path = value
value = fix({id:'a', value:'aa'},{id:'b', value:'bb'},{id:'c', value:'cc'},{id:'d', value:'dd'},{id:'e', value:'ee'},{id:'f', value:'ff'})
[exploding]
[value]
`;

from(input)
.pipe(ezs('combine', { path: 'b', script, default: 'n/a' }))
.pipe(ezs.catch())
.on('error', done)
.on('data', (chunk) => {
output.push(chunk);
})
.on('end', () => {
assert.equal(output.length, 6);
assert.equal(output[0].b.value, 'aa');
assert.equal(output[1].b.value, 'n/a');
assert.equal(output[2].b.value, 'cc');
assert.equal(output[3].b.value, 'n/a');
assert.equal(output[4].b.value, 'ee');
assert.equal(output[5].b.value, 'n/a');
done();
});
});

test('with script #2', (done) => {
ezs.use(statements);
const input = [
Expand Down

0 comments on commit dd2dee4

Please sign in to comment.