Skip to content

Commit

Permalink
Allow return null/undefined for when allowReturnArray=true.
Browse files Browse the repository at this point in the history
  • Loading branch information
LesterLyu committed Jul 31, 2020
1 parent 27eb8a6 commit 0bc6ccf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion grammar/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class FormulaParser {
}
// Disallow union, and other unknown data types.
// e.g. `=(A1:C1, A2:E9)` -> #VALUE!
if (typeof result === 'object' && !Array.isArray(result)) {
if (typeof result === 'object' && !Array.isArray(result) && result != null) {
return FormulaError.VALUE;
}

Expand Down
21 changes: 21 additions & 0 deletions test/grammar/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const {DepParser} = require('../../grammar/dependency/hooks');

const parser = new FormulaParser({
onCell: ref => {
if (ref.row === 5 && ref.col === 5)
return null
return 1;
},
onRange: ref => {
Expand Down Expand Up @@ -107,6 +109,11 @@ describe('Dependency parser', () => {
});

describe('Basic parse', () => {
it('should parse null', function () {
let actual = parser.parse('E5', position);
expect(actual).to.deep.eq(null);
});

it('should parse whole column', function () {
let actual = parser.parse('SUM(A:A)', position);
expect(actual).to.deep.eq(6);
Expand All @@ -116,6 +123,7 @@ describe('Basic parse', () => {
let actual = parser.parse('SUM(1:1)', position);
expect(actual).to.deep.eq(1);
});

})

describe('Parser allows returning array or range', () => {
Expand All @@ -141,6 +149,19 @@ describe('Parser allows returning array or range', () => {
expect(actual).to.eq(1);
});

it('should return single value', function () {
let actual = parser.parse('E5', position, true);
expect(actual).to.eq(null);
});
});

describe('async parse', () => {
it('should return single value', async function () {
let actual = await parser.parseAsync('A1', position, true);
expect(actual).to.eq(1);
actual = await parser.parseAsync('E5', position, true);
expect(actual).to.eq(null);
});
});

describe('Custom async function', () => {
Expand Down

0 comments on commit 0bc6ccf

Please sign in to comment.