Skip to content

Commit

Permalink
Improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasrw committed Jan 16, 2024
1 parent 7f273b8 commit aada421
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 341 deletions.
8 changes: 1 addition & 7 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ branch=$(git rev-parse --abbrev-ref HEAD | rexreplace '[^0-9a-z-]' '.' | rexre
commit=$(git rev-parse --short HEAD)



echo '\nPrepare types'
cp 'types/alasql.d.ts' dist/

Expand All @@ -36,19 +35,14 @@ echo '\nPrepare echo plugin'
cp 'src/echo/alasql-echo.js' dist/



echo '\nPrepare prolog'
cp 'src/prolog/alasql-prolog.js' dist/






echo '\nBuild alasql.js files'
outfile_fs="dist/alasql.fs.js"
outfile="dist/alasql.js"
outfile_min="dist/alasql.min.js"
outfile_fs="dist/alasql.fs.js"

echo '# Concat all parts'
cat \
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"prettier": "^3.0.3",
"react-native-fetch-blob": "^0.10.8",
"react-native-fs": "^2.20.0",
"rexreplace": "7.1.3",
"strftime": "0.10.2",
"tabletop": "1.6.3",
"uglify-js": "3.17.4"
Expand Down Expand Up @@ -120,4 +121,4 @@
"pre-push": "yarn test-format || (echo please format using 'yarn format' && exit 1)"
}
}
}
}
6 changes: 5 additions & 1 deletion src/38query.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ function queryfn3(query) {

for (const key in query.groupColumns) {
// ony remove columns where the alias is also not a column in the result
if (query.groupColumns[key] !== key && d[query.groupColumns[key]] && !query.groupColumns[query.groupColumns[key]]){
if (
query.groupColumns[key] !== key &&
d[query.groupColumns[key]] &&
!query.groupColumns[query.groupColumns[key]]
) {
delete d[query.groupColumns[key]];
}
}
Expand Down
60 changes: 28 additions & 32 deletions src/50expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,10 @@
s += '.indexOf(';
s += 'alasql.utils.getValueOf(' + leftJS() + '))>-1)';
} else if (Array.isArray(this.right)) {
if (!alasql.options.cache || this.right.filter((value) => value instanceof yy.ParamValue).length > 0) {
if (
!alasql.options.cache ||
this.right.filter(value => value instanceof yy.ParamValue).length > 0
) {
// When not using cache, or when the array contains parameters, we need to re-create the Set
// leverage JS Set, which is faster for lookups than arrays
s =
Expand All @@ -493,17 +496,18 @@
leftJS() +
')))';
} else {
// Use a cache to not re-create the Set on every identitical query
// Use a cache to not re-create the Set on every identitical query
if (!alasql.sets) {
alasql.sets = {};
}
const allValues = this.right.map((value) => value.value);
const allValuesStr = allValues.join(",");
const allValues = this.right.map(value => value.value);
const allValuesStr = allValues.join(',');
if (!alasql.sets[allValuesStr]) {
// leverage JS Set, which is faster for lookups than arrays
alasql.sets[allValuesStr] = new Set(allValues);
}
s = 'alasql.sets["' + allValuesStr + '"].has(alasql.utils.getValueOf(' + leftJS() + '))';
s =
'alasql.sets["' + allValuesStr + '"].has(alasql.utils.getValueOf(' + leftJS() + '))';
}
} else {
s = '(' + rightJS() + '.indexOf(' + leftJS() + ')>-1)';
Expand All @@ -520,7 +524,10 @@
s += '.indexOf(';
s += 'alasql.utils.getValueOf(' + leftJS() + '))<0)';
} else if (Array.isArray(this.right)) {
if (!alasql.options.cache || this.right.filter((value) => value instanceof yy.ParamValue).length > 0) {
if (
!alasql.options.cache ||
this.right.filter(value => value instanceof yy.ParamValue).length > 0
) {
// When not using cache, or when the array contains parameters, we need to re-create the Set
// leverage JS Set, which is faster for lookups than arrays
s =
Expand All @@ -530,17 +537,18 @@
leftJS() +
'))))';
} else {
// Use a cache to not re-create the Set on every identitical query
// Use a cache to not re-create the Set on every identitical query
if (!alasql.sets) {
alasql.sets = {};
}
const allValues = this.right.map((value) => value.value);
const allValuesStr = allValues.join(",");
const allValues = this.right.map(value => value.value);
const allValuesStr = allValues.join(',');
if (!alasql.sets[allValuesStr]) {
// leverage JS Set, which is faster for lookups than arrays
alasql.sets[allValuesStr] = new Set(allValues);
}
s = '!alasql.sets["' + allValuesStr + '"].has(alasql.utils.getValueOf(' + leftJS() + '))';
s =
'!alasql.sets["' + allValuesStr + '"].has(alasql.utils.getValueOf(' + leftJS() + '))';
}
} else {
s = '(' + rightJS() + '.indexOf(';
Expand Down Expand Up @@ -783,32 +791,20 @@
}

toString() {
var s;
const { op, right } = this;
const res = right.toString();

if (op === '~') {
s = op + res;
}
if (op === '-') {
s = op + res;
}
if (op === '+') {
s = op + res;
switch (op) {
case '~':
case '-':
case '+':
case '#':
return op + res;
case 'NOT':
return op + '(' + res + ')';
default:
return '(' + res + ')';
}
if (op === '#') {
s = op + res;
}
if (op === 'NOT') {
s = op + '(' + res + ')';
}
if (op === null) {
s = '(' + res + ')';
}
if (!s) {
s = '(' + res + ')';
}
return s;
}

findAggregator(query) {
Expand Down
22 changes: 12 additions & 10 deletions test/test1820.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ if (typeof exports === 'object') {
}

describe('Test 1820 - SELECT query (a AS b, b AS c)', function () {

it('1. Select query where alias of one column is also a column name in the result set', function (done) {
let item1 = { a: 1, b: "hello" };
let item2 = { a: 2, b: "" };
let item1 = {a: 1, b: 'hello'};
let item2 = {a: 2, b: ''};

var res = alasql('SELECT a as b, b as c FROM ? GROUP BY a,b', [[item1, item2]]);

assert.deepEqual(res, [{
b: 1,
c: "hello"
}, {
b: 2,
c: ""
}]);
assert.deepEqual(res, [
{
b: 1,
c: 'hello',
},
{
b: 2,
c: '',
},
]);

done();
});
Expand Down
Loading

0 comments on commit aada421

Please sign in to comment.