Skip to content

Commit 3723e8a

Browse files
Copilotmathiasrw
andauthored
Fix negative numbers in JSON contexts to close #475 (#2298)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]> Co-authored-by: M. Wulff <[email protected]>
1 parent a6373df commit 3723e8a

File tree

4 files changed

+206
-133
lines changed

4 files changed

+206
-133
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ yarn.lock binary
1212
# paths that don't start with / are relative to the .gitattributes folder
1313
#relative/path/*.txt text eol=lf
1414

15+
src/alasqlparser.js merge=union
16+
1517

src/alasqlparser.jison

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,6 +2485,8 @@ JsonValue
24852485
JsonPrimitiveValue
24862486
: NumValue
24872487
{ $$ = +$1.value; }
2488+
| MINUS NumValue
2489+
{ $$ = -$2.value; }
24882490
| StringValue
24892491
{ $$ = ""+$1.value; }
24902492
| LogicValue
@@ -2534,6 +2536,12 @@ JsonProperty
25342536
{ $$ = {}; $$[$1] = $3; }
25352537
| Literal COLON JsonValue
25362538
{ $$ = {}; $$[$1] = $3; }
2539+
| STRING COLONDASH NumValue
2540+
{ $$ = {}; $$[$1.substr(1,$1.length-2)] = -$3.value; }
2541+
| NUMBER COLONDASH NumValue
2542+
{ $$ = {}; $$[$1] = -$3.value; }
2543+
| Literal COLONDASH NumValue
2544+
{ $$ = {}; $$[$1] = -$3.value; }
25372545
/* | STRING COLON ParamValue
25382546
{ $$ = {}; $$[$1.substr(1,$1.length-2)] = $3; }
25392547
| NUMBER COLON ParamValue

src/alasqlparser.js

Lines changed: 142 additions & 133 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test2201.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
describe('Test 2201 - JSON with negative numbers', function () {
7+
const test = '2201';
8+
9+
it('A) JSON array with negative numbers in objects', function () {
10+
var res = alasql('SELECT * FROM @[{delay:5},{delay:-7}]');
11+
assert.deepEqual(res, [{delay: 5}, {delay: -7}]);
12+
});
13+
14+
it('B) JSON object with negative number property', function () {
15+
var res = alasql('SELECT VALUE @{a:-5}');
16+
assert.deepEqual(res, {a: -5});
17+
});
18+
19+
it('C) JSON array with multiple negative numbers', function () {
20+
var res = alasql('SELECT * FROM @[{x:-1,y:-2},{x:-3,y:-4}]');
21+
assert.deepEqual(res, [
22+
{x: -1, y: -2},
23+
{x: -3, y: -4},
24+
]);
25+
});
26+
27+
it('D) JSON with mix of positive and negative numbers', function () {
28+
var res = alasql('SELECT * FROM @[{a:10,b:-10},{a:-5,b:5}]');
29+
assert.deepEqual(res, [
30+
{a: 10, b: -10},
31+
{a: -5, b: 5},
32+
]);
33+
});
34+
35+
it('E) JSON with negative decimal numbers', function () {
36+
var res = alasql('SELECT * FROM @[{val:-3.14},{val:-2.5}]');
37+
assert.deepEqual(res, [{val: -3.14}, {val: -2.5}]);
38+
});
39+
40+
it('F) Array literals with negative numbers', function () {
41+
var res = alasql('SELECT @[-1.7,-2,-3] AS arr');
42+
assert.deepEqual(res, [{arr: [-1.7, -2, -3]}]);
43+
});
44+
45+
it('G) Nested JSON with negative numbers', function () {
46+
var res = alasql('SELECT VALUE @{"outer":{"inner":-3.3}}');
47+
assert.deepEqual(res, {outer: {inner: -3.3}});
48+
});
49+
50+
it('H) Nested object in array with negative numbers', function () {
51+
var res = alasql('SELECT * FROM @[{"data":{"value":-10.5}}]');
52+
assert.deepEqual(res, [{data: {value: -10.5}}]);
53+
});
54+
});

0 commit comments

Comments
 (0)