Skip to content

Commit 703ed99

Browse files
committed
Add functions for async operations
1 parent 6472b9f commit 703ed99

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/Util.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* JSONDB - JSON Database Manager
33
*
4-
* Manage JSON files as databases with JSON Query Language (JQL)
4+
* Manage JSON files as databases with JSONDB Query Language (JQL)
55
*
66
* This content is released under the GPL License (GPL-3.0)
77
*
@@ -307,13 +307,14 @@ var Util = (function () {
307307
length = length || l;
308308

309309
var ret = {};
310-
var i = 0;
310+
var i = j = 0;
311311
for (var key in object) {
312312
if (object.hasOwnProperty(key)) {
313-
if (i > offset && i < length) {
313+
if (i >= offset && j < length) {
314314
ret[key] = object[key];
315+
++j;
315316
}
316-
i++;
317+
++i;
317318
}
318319
}
319320
return ret;
@@ -327,7 +328,9 @@ var Util = (function () {
327328
*/
328329
Util.prototype.extends = function (child, parent) {
329330
for (var key in parent) {
330-
if (parent.hasOwnProperty(key)) child[key] = parent[key];
331+
if (parent.hasOwnProperty(key)) {
332+
child[key] = parent[key];
333+
}
331334
}
332335
function Ctor() {
333336
this.constructor = child;
@@ -480,8 +483,41 @@ var Util = (function () {
480483
return server + '/' + database;
481484
};
482485

486+
/**
487+
* Add 0 to a number lower than 10
488+
* @param {number|string} number
489+
* @return {string}
490+
*/
491+
Util.prototype.zeropad = function (number) {
492+
number = parseInt(number);
493+
if (number < 10 && number >= 0) {
494+
number = '0' + number;
495+
}
496+
return number;
497+
};
498+
499+
/**
500+
* Asynchronous while
501+
* @param {function} condition The while condition
502+
* @param {function} bridge The function used for recursion
503+
* @param {function} callback The callback function
504+
*/
505+
Util.prototype.whilst = function(condition, bridge, callback) {
506+
try {
507+
if (condition() === false) {
508+
return callback(null);
509+
}
510+
return bridge(function() {
511+
this.whilst(condition, bridge, callback);
512+
}.bind(this));
513+
}
514+
catch (e) {
515+
return callback(e);
516+
}
517+
};
518+
483519
return Util;
484520
})();
485521

486522
// Exports the module
487-
module.exports = new Util();
523+
module.exports = new Util();

0 commit comments

Comments
 (0)