Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.

Commit 6b9adae

Browse files
author
Lucas Wojciechowski
committed
Refactoring
1 parent 2d1a33c commit 6b9adae

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

index.js

+32-25
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,41 @@ function compile(filter) {
2121
var op = filter[0];
2222
if (filter.length <= 1) return op === 'any' ? 'false' : 'true';
2323
var str =
24-
op === '==' ? compare(filter[1], filter[2], '===', false) :
25-
op === '!=' ? compare(filter[1], filter[2], '!==', false) :
24+
op === '==' ? compileComparisonOp(filter[1], filter[2], '===', false) :
25+
op === '!=' ? compileComparisonOp(filter[1], filter[2], '!==', false) :
2626
op === '<' ||
2727
op === '>' ||
2828
op === '<=' ||
29-
op === '>=' ? compare(filter[1], filter[2], op, true) :
30-
op === 'any' ? filter.slice(1).map(compile).join('||') :
31-
op === 'all' ? filter.slice(1).map(compile).join('&&') :
32-
op === 'none' ? '!(' + filter.slice(1).map(compile).join('||') + ')' :
33-
op === 'in' ? compileIn(filter[1], filter.slice(2)) :
34-
op === '!in' ? '!(' + compileIn(filter[1], filter.slice(2)) + ')' :
35-
op === 'has' ? compileHas(filter[1]) :
36-
op === '!has' ? negate(compileHas([filter[1]])) :
29+
op === '>=' ? compileComparisonOp(filter[1], filter[2], op, true) :
30+
op === 'any' ? compileLogicalOp(filter.slice(1), '||') :
31+
op === 'all' ? compileLogicalOp(filter.slice(1), '&&') :
32+
op === 'none' ? compileNegation(compileLogicalOp(filter.slice(1), '||')) :
33+
op === 'in' ? compileInOp(filter[1], filter.slice(2)) :
34+
op === '!in' ? compileNegation(compileInOp(filter[1], filter.slice(2))) :
35+
op === 'has' ? compileHasOp(filter[1]) :
36+
op === '!has' ? compileNegation(compileHasOp([filter[1]])) :
3737
'true';
3838
return '(' + str + ')';
3939
}
4040

41-
function valueExpr(key) {
42-
return key === '$type' ? 'f.type' : 'p[' + JSON.stringify(key) + ']';
41+
function compilePropertyReference(property) {
42+
return property === '$type' ? 'f.type' : 'p[' + JSON.stringify(property) + ']';
4343
}
44-
function compare(key, val, op, checkType) {
45-
var left = valueExpr(key);
46-
var right = key === '$type' ? types.indexOf(val) : JSON.stringify(val);
44+
45+
function compileComparisonOp(property, value, op, checkType) {
46+
var left = compilePropertyReference(property);
47+
var right = property === '$type' ? types.indexOf(value) : JSON.stringify(value);
4748
return (checkType ? 'typeof ' + left + '=== typeof ' + right + '&&' : '') + left + op + right;
4849
}
49-
function compileIn(key, values) {
50-
if (key === '$type') values = values.map(function(value) { return types.indexOf(value); });
51-
var left = JSON.stringify(values.sort(compareFn));
52-
var right = valueExpr(key);
50+
51+
function compileLogicalOp(expressions, op) {
52+
return expressions.map(compile).join(op);
53+
}
54+
55+
function compileInOp(property, values) {
56+
if (property === '$type') values = values.map(function(value) { return types.indexOf(value); });
57+
var left = JSON.stringify(values.sort(compare));
58+
var right = compilePropertyReference(property);
5359

5460
if (values.length <= 200) return left + '.indexOf(' + right + ') !== -1';
5561

@@ -60,14 +66,15 @@ function compileIn(key, values) {
6066
'return false; }(' + right + ', ' + left + ',0,' + (values.length - 1) + ')';
6167
}
6268

63-
function compileHas(key) {
64-
return JSON.stringify(key) + ' in p';
69+
function compileHasOp(property) {
70+
return JSON.stringify(property) + ' in p';
6571
}
6672

67-
function compareFn(a, b) {
68-
return a < b ? -1 : a > b ? 1 : 0;
73+
function compileNegation(expression) {
74+
return '!(' + expression + ')';
6975
}
7076

71-
function negate(expression) {
72-
return '!(' + expression + ')';
77+
// Comparison function to sort numbers and strings
78+
function compare(a, b) {
79+
return a < b ? -1 : a > b ? 1 : 0;
7380
}

0 commit comments

Comments
 (0)