Skip to content

Commit

Permalink
feat: text step for js translator
Browse files Browse the repository at this point in the history
  • Loading branch information
davinov committed Aug 28, 2020
1 parent 0b663da commit 026ac18
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lib/translators/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ export class JavaScriptTranslator extends BaseTranslator {
return data.filter(filteringFunctionForCondition(filterStep.condition));
};
}

// transform a "text" step into corresponding function
text(step: Readonly<S.AddTextColumnStep>): JsStepFunction {
function addTextColumnToRow(row: Readonly<DataRow>) {
return {
...row,
[step.new_column]: step.text,
};
}

return function(data: Readonly<DataTable>, _dataDomains: Readonly<DataDomains>) {
return data.map(addTextColumnToRow);
};
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/js.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,18 @@ describe('Pipeline to js function translator', () => {
expect(() => invalidFilter(SAMPLE_DATA, {})).toThrow();
});
});

describe('text step', () => {
it('should add a column with text to each row', () => {
const addTextColumn = jsTranslator.text({
name: 'text',
text: 'some text',
new_column: 'text_new_column',
});
expect(addTextColumn(SAMPLE_DATA.domainA, {})).toEqual([
{ label: 'A', value: 1, text_new_column: 'some text' },
{ label: 'B', value: 2, text_new_column: 'some text' },
]);
});
});
});

0 comments on commit 026ac18

Please sign in to comment.