Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit 5f1d089

Browse files
author
Hans Kristian Flaatten
committed
refactor: move string parsing logic a separate function
1 parent c32f938 commit 5f1d089

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

index.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ module.exports.prototype.customAfter = function(field) {
100100
};
101101
};
102102

103+
module.exports.prototype.parseString = function(string) {
104+
if (!isNaN(string)) {
105+
return parseFloat(string, 10);
106+
} else {
107+
return string;
108+
}
109+
};
110+
103111
module.exports.prototype.parse = function(query) {
104112
var op, val;
105113
var res = {};
@@ -138,17 +146,16 @@ module.exports.prototype.parse = function(query) {
138146
res[key] = {$in: val.filter(function(element) {
139147
return element[0] !== '!';
140148
}).map(function(element) {
141-
return isNaN(element) ? element : parseFloat(element, 10);
142-
})};
149+
return this.parseString(element);
150+
}.bind(this))};
143151

144152
// $nin query
145153
} else {
146154
res[key] = {$nin: val.filter(function(element) {
147155
return element[0] === '!';
148156
}).map(function(element) {
149-
element = element.substr(1);
150-
return isNaN(element) ? element : parseFloat(element, 10);
151-
})};
157+
return this.parseString(element.substr(1));
158+
}.bind(this))};
152159
}
153160
}
154161

@@ -173,7 +180,7 @@ module.exports.prototype.parse = function(query) {
173180
switch (op) {
174181
case '!':
175182
if (val) {
176-
return { $ne: isNaN(val) ? val : parseFloat(val, 10) };
183+
return { $ne: this.parseString(val) };
177184
} else {
178185
return { $exists: false };
179186
}
@@ -193,9 +200,9 @@ module.exports.prototype.parse = function(query) {
193200
return { $regex: val, $options: 'i' };
194201
}
195202
}
196-
})();
203+
}.bind(this))();
197204
} else {
198-
res[key] = isNaN(val) ? val : parseFloat(val, 10);
205+
res[key] = this.parseString(val);
199206
}
200207
}
201208
return res;

0 commit comments

Comments
 (0)