Skip to content

Commit

Permalink
fix(legacy-preset-chart-nvd3): fix tokenizer input type (apache-super…
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro authored and NejcZdovc committed Apr 20, 2021
1 parent a4086f4 commit ee253c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions plugins/legacy-preset-chart-nvd3/src/utils/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*/
import { validateNumber } from '@superset-ui/core';

export function tokenizeToNumericArray(value: string): number[] | null {
if (!value.trim()) return null;
export function tokenizeToNumericArray(value?: string): number[] | null {
if (!value?.trim()) return null;
const tokens = value.split(',');
if (tokens.some(token => validateNumber(token))) throw new Error('All values should be numeric');
return tokens.map(token => parseFloat(token));
}

export function tokenizeToStringArray(value: string): string[] | null {
if (!value.trim()) return null;
export function tokenizeToStringArray(value?: string): string[] | null {
if (!value?.trim()) return null;
const tokens = value.split(',');
return tokens.map(token => token.trim());
}
8 changes: 8 additions & 0 deletions plugins/legacy-preset-chart-nvd3/test/utils/tokenize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe('tokenizeToNumericArray', () => {
expect(tokenizeToNumericArray(' 1, 2, 3, 4 ')).toStrictEqual([1, 2, 3, 4]);
});

it('evals undefined to null', () => {
expect(tokenizeToNumericArray(undefined)).toBeNull();
});

it('evals empty strings to null', () => {
expect(tokenizeToNumericArray('')).toBeNull();
expect(tokenizeToNumericArray(' ')).toBeNull();
Expand All @@ -43,6 +47,10 @@ describe('tokenizeToStringArray', () => {
expect(tokenizeToStringArray('1.1,a,3, bc ,d')).toStrictEqual(['1.1', 'a', '3', 'bc', 'd']);
});

it('evals undefined to null', () => {
expect(tokenizeToStringArray(undefined)).toBeNull();
});

it('evals empty string to null', () => {
expect(tokenizeToStringArray('')).toBeNull();
expect(tokenizeToStringArray(' ')).toBeNull();
Expand Down

0 comments on commit ee253c6

Please sign in to comment.