Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
[#546](https://github.com/plotly/dash-table/issues/546)
- New prop `export_columns` that takes values `all` or `visible` (default). This prop controls the columns used during export

[#597](https://github.com/plotly/dash-table/issues/597)
- Add `is blank` unary operator. Returns true for `undefined`, `null` and `''`.

### Changed
[#598](https://github.com/plotly/dash-table/issues/598)
- Allow values with whitespaces in column filters

### Fixed
[#460](https://github.com/plotly/dash-table/issues/460)
- The `datestartswith` relational operator now supports number comparison
Expand Down
2 changes: 1 addition & 1 deletion src/core/syntax-tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class SyntaxTree {

toQueryString() {
return this.lexerResult.valid ?
R.map(l => l.value, this.lexerResult.lexemes).join(' ') :
R.map(l => l.lexeme.transform ? l.lexeme.transform(l.value) : l.value, this.lexerResult.lexemes).join(' ') :
'';
}

Expand Down
1 change: 1 addition & 0 deletions src/core/syntax-tree/lexicon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IUnboundedLexeme {
regexp: RegExp;
regexpMatch?: number;
syntaxer?: (lexs: any[], pivot: any, pivotIndex: number) => any;
transform?: (value: any) => any;
}

export interface ILexeme extends IUnboundedLexeme {
Expand Down
63 changes: 39 additions & 24 deletions src/dash-table/syntax-tree/lexeme/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ISyntaxTree } from 'core/syntax-tree/syntaxer';
const FIELD_REGEX = /^{(([^{}\\]|\\.)+)}/;
const STRING_REGEX = /^(('([^'\\]|\\.)*')|("([^"\\]|\\.)*")|(`([^`\\]|\\.)*`))/;
const VALUE_REGEX = /^(([^\s'"`{}()\\]|\\.)+)(?:[\s)]|$)/;
const PERMISSIVE_VALUE_REGEX = /^(([^'"`{}()\\]|\\.)+)$/;

const getField = (
value: string
Expand All @@ -31,16 +32,6 @@ const getString = (
value: string
) => value.slice(1, value.length - 1).replace(/\\(.)/g, '$1');

const getValue = (
value: string
) => {
value = (value.match(VALUE_REGEX) as any)[1];

return isNumeric(value) ?
+value :
value.replace(/\\(.)/g, '$1');
};

export const stringExpression: IUnboundedLexeme = {
present: (tree: ISyntaxTree) => getString(tree.value),
resolve: (_target: any, tree: ISyntaxTree) => {
Expand All @@ -55,17 +46,41 @@ export const stringExpression: IUnboundedLexeme = {
type: LexemeType.Expression
};

export const valueExpression: IUnboundedLexeme = {
present: (tree: ISyntaxTree) => getValue(tree.value),
resolve: (_target: any, tree: ISyntaxTree) => {
if (VALUE_REGEX.test(tree.value)) {
return getValue(tree.value);
} else {
throw new Error();
}
},
regexp: VALUE_REGEX,
regexpMatch: 1,
subType: 'value',
type: LexemeType.Expression
};
const getValueFactory = (regex: RegExp) => (value: string) => {
value = (value.match(regex) as any)[1];

return isNumeric(value) ?
+value :
value.replace(/\\(.)/g, '$1');
};

const valueExpressionFactory = (
regex: RegExp,
transform?: (v: any) => any
): IUnboundedLexeme => {
const getValue = getValueFactory(regex);

return {
present: (tree: ISyntaxTree) => getValue(tree.value),
resolve: (_target: any, tree: ISyntaxTree) => {
if (regex.test(tree.value)) {
return getValue(tree.value);
} else {
throw new Error();
}
},
regexp: regex,
regexpMatch: 1,
subType: 'value',
transform,
type: LexemeType.Expression
};
};

export const valueExpression = valueExpressionFactory(VALUE_REGEX);
export const permissiveValueExpression = valueExpressionFactory(
PERMISSIVE_VALUE_REGEX,
(v: any) => typeof v === 'string' && v.indexOf(' ') !== -1 ?
`"${v}"` :
v
Comment thread
Marc-Andre-Rivet marked this conversation as resolved.
);
7 changes: 6 additions & 1 deletion src/dash-table/syntax-tree/lexeme/unary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function relationalSyntaxer([left, lexeme]: any[]) {
}

function relationalEvaluator(
fn: (opValue: any[]) => boolean
fn: (opValue: any) => boolean
) {
return (target: any, tree: ISyntaxTree) => fn(evaluator(target, tree));
}
Expand Down Expand Up @@ -75,6 +75,11 @@ export const isEven: IUnboundedLexeme = R.merge({
regexp: /^(is even)/i
}, LEXEME_BASE);

export const isBlank: IUnboundedLexeme = R.merge({
evaluate: relationalEvaluator(opValue => opValue === undefined || opValue === null || opValue === ''),
regexp: /^(is blank)/i
}, LEXEME_BASE);

export const isNil: IUnboundedLexeme = R.merge({
evaluate: relationalEvaluator(opValue => opValue === undefined || opValue === null),
regexp: /^(is nil)/i
Expand Down
10 changes: 6 additions & 4 deletions src/dash-table/syntax-tree/lexicon/column.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
fieldExpression,
stringExpression,
valueExpression
permissiveValueExpression
} from '../lexeme/expression';
import {
contains,
Expand All @@ -14,6 +14,7 @@ import {
notEqual
} from '../lexeme/relational';
import {
isBlank,
isBool,
isEven,
isNil,
Expand Down Expand Up @@ -45,7 +46,8 @@ const lexicon: ILexeme[] = [
if: ifLeading,
terminal: false
})),
...[isBool,
...[isBlank,
isBool,
isEven,
isNil,
isNum,
Expand All @@ -60,8 +62,8 @@ const lexicon: ILexeme[] = [
})),
...[
fieldExpression,
stringExpression,
valueExpression
permissiveValueExpression,
stringExpression
].map(exp => ({
...exp,
if: ifExpression,
Expand Down
4 changes: 3 additions & 1 deletion src/dash-table/syntax-tree/lexicon/columnMulti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
notEqual
} from '../lexeme/relational';
import {
isBlank,
isBool,
isEven,
isNil,
Expand Down Expand Up @@ -56,7 +57,8 @@ const lexicon: ILexeme[] = [
if: ifRelationalOperator,
terminal: false
})),
...[isBool,
...[isBlank,
isBool,
isEven,
isNil,
isNum,
Expand Down
4 changes: 3 additions & 1 deletion src/dash-table/syntax-tree/lexicon/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
notEqual
} from '../lexeme/relational';
import {
isBlank,
isBool,
isEven,
isNil,
Expand Down Expand Up @@ -89,7 +90,8 @@ const lexicon: ILexeme[] = [
if: ifRelationalOperator,
terminal: false
})),
...[isBool,
...[isBlank,
isBool,
isEven,
isNil,
isNum,
Expand Down
8 changes: 4 additions & 4 deletions tests/cypress/tests/standalone/filtering_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ describe('filter', () => {
DashTable.getFilterById('ccc').click();
DOM.focused.type(`gt`);
DashTable.getFilterById('ddd').click();
DOM.focused.type('20 a000');
DOM.focused.type('"20 a000');
DashTable.getFilterById('eee').click();
DOM.focused.type('is prime2');
DashTable.getFilterById('bbb').click();
DOM.focused.type('! !');
DOM.focused.type('! !"');
DashTable.getFilterById('ccc').click();

DashTable.getCellById(0, 'ccc').within(() => cy.get('.dash-cell-value').should('have.html', cell_0));
DashTable.getCellById(1, 'ccc').within(() => cy.get('.dash-cell-value').should('have.html', cell_1));

DashTable.getFilterById('bbb').within(() => cy.get('input').should('have.value', '! !'));
DashTable.getFilterById('bbb').within(() => cy.get('input').should('have.value', '! !"'));
DashTable.getFilterById('ccc').within(() => cy.get('input').should('have.value', 'gt'));
DashTable.getFilterById('ddd').within(() => cy.get('input').should('have.value', '20 a000'));
DashTable.getFilterById('ddd').within(() => cy.get('input').should('have.value', '"20 a000'));
DashTable.getFilterById('eee').within(() => cy.get('input').should('have.value', 'is prime2'));

DashTable.getFilterById('bbb').should('have.class', 'invalid');
Expand Down
10 changes: 10 additions & 0 deletions tests/cypress/tests/unit/query_syntactic_tree_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,16 @@ describe('Query Syntax Tree', () => {
expect(tree.evaluate(data3)).to.equal(false);
});

it('can check blank', () => {
const tree = new QuerySyntaxTree('{d} is blank');

expect(tree.isValid).to.equal(true);
expect(tree.evaluate(data0)).to.equal(true);
expect(tree.evaluate(data1)).to.equal(false);
expect(tree.evaluate(data2)).to.equal(true);
expect(tree.evaluate(data3)).to.equal(false);
});

it('can invert check nil', () => {
const tree = new QuerySyntaxTree('!({d} is nil)');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ describe('Single Column Syntax Tree', () => {
expect(tree.toQueryString()).to.equal('{a} = 1');
});

it.only('can be permissive value expression', () => {
const tree = new SingleColumnSyntaxTree('Hello world', COLUMN_TEXT);

expect(tree.isValid).to.equal(true);
expect(tree.evaluate({ a: 'Hello world' })).to.equal(true);
expect(tree.evaluate({ a: 'Helloworld' })).to.equal(false);
expect(tree.toQueryString()).to.equal('{a} contains "Hello world"');
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you include a toQueryString test for permissive as well?

@Marc-Andre-Rivet Marc-Andre-Rivet Sep 24, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 824e7df


it('`undefined` column type can use `contains`', () => {
const tree = new SingleColumnSyntaxTree('contains 1', COLUMN_UNDEFINED);

Expand Down