Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get started on async functionality #157

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 100 additions & 32 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ function get(obj, key) {
return isFunction(obj.get) ? obj.get(key) : obj[key];
}

function identity(value) {
return value;
}

function promiseAll(promises) {
return Promise.all(promises).then(function(results) {
return !results.some(function(result) {
return !result;
});
});
}

function promiseSome(promises) {
return Promise.all(promises).then(function(results) {
return results.some(identity);
});
}

/**
*/

Expand Down Expand Up @@ -177,11 +195,26 @@ var defaultExpressions = {
*/

$or: function $or(a, b, k, o) {
var thenableResults = void 0;
for (var i = 0, n = a.length; i < n; i++) {
if (validate(get(a, i), b, k, o)) {
return true;
var result = validate(get(a, i), b, k, o);
if (result && result.then && !thenableResults) {
thenableResults = [];
}

if (thenableResults) {
thenableResults.push(result);
} else {
if (result) {
return true;
}
}
}

if (thenableResults) {
return promiseSome(thenableResults);
}

return false;
},

Expand All @@ -196,11 +229,27 @@ var defaultExpressions = {
*/

$and: function $and(a, b, k, o) {
var thenableResults = void 0;

for (var i = 0, n = a.length; i < n; i++) {
if (!validate(get(a, i), b, k, o)) {
return false;
var result = validate(get(a, i), b, k, o);
if (result && result.then && !thenableResults) {
thenableResults = [];
}

if (thenableResults) {
thenableResults.push(result);
} else {
if (!result) {
return false;
}
}
}

if (thenableResults) {
return promiseAll(thenableResults);
}

return true;
},

Expand All @@ -223,7 +272,7 @@ var defaultExpressions = {

$elemMatch: function $elemMatch(a, b, k, o) {
if (isArray(b)) {
return !!~search(b, a);
return search(b, a) !== -1;
}
return validate(a, b, k, o);
},
Expand All @@ -236,6 +285,29 @@ var defaultExpressions = {
}
};

function castTesterAsFn(a, comparable, compare) {
if (a instanceof RegExp) {
return function(b) {
return typeof b === "string" && a.test(b);
};
} else if (a instanceof Function) {
return a;
} else if (isArray(a) && !a.length) {
// Special case of a == []
return function(b) {
return isArray(b) && !b.length;
};
} else if (a === null) {
return function(b) {
//will match both null and undefined
return b == null;
};
}
return function(b) {
return compare(comparable(b), comparable(a)) === 0;
};
}

/**
*/

Expand All @@ -247,26 +319,7 @@ var prepare = {
var comparable = _ref.comparable,
compare = _ref.compare;

if (a instanceof RegExp) {
return or(function(b) {
return typeof b === "string" && a.test(b);
});
} else if (a instanceof Function) {
return or(a);
} else if (isArray(a) && !a.length) {
// Special case of a == []
return or(function(b) {
return isArray(b) && !b.length;
});
} else if (a === null) {
return or(function(b) {
//will match both null and undefined
return b == null;
});
}
return or(function(b) {
return compare(comparable(b), comparable(a)) === 0;
});
return or(castTesterAsFn(a, comparable, compare));
},

$gt: function $gt(a, query, _ref2) {
Expand Down Expand Up @@ -305,17 +358,21 @@ var prepare = {
},

$in: function $in(a, query, options) {
var comparable = options.comparable;
var comparable = options.comparable,
compare = options.compare;

return function(b) {
if (b instanceof Array) {
var thenableResults = void 0;

if (isArray(b)) {
for (var i = b.length; i--; ) {
if (~a.indexOf(comparable(get(b, i)))) {
if (a.indexOf(comparable(get(b, i))) !== -1) {
return true;
}
}
} else {
var comparableB = comparable(b);

if (
comparableB === b &&
(typeof b === "undefined" ? "undefined" : _typeof(b)) === "object"
Expand Down Expand Up @@ -343,9 +400,18 @@ var prepare = {
Handles the case of {'field': {$in: [/regexp1/, /regexp2/, ...]}}
*/
for (var i = a.length; i--; ) {
var tester = castTesterAsFn(get(a, i), comparable, compare);

var validator = createRootValidator(get(a, i), options);
var result = validate(validator, b, i, a);
if (
var result = validate(validator, comparableB, i, a);

if (result && result.then && !thenableResults) {
thenableResults = [];
}

if (thenableResults != null) {
thenableResults.push(result);
} else if (
result &&
String(result) !== "[object Object]" &&
String(b) !== "[object Object]"
Expand All @@ -354,7 +420,9 @@ var prepare = {
}
}

return !!~a.indexOf(comparableB);
if (thenableResults) {
return promiseSome(thenableResults);
}
}

return false;
Expand Down Expand Up @@ -453,7 +521,7 @@ var prepare = {
*/

$exists: function $exists(a) {
return !!a;
return Boolean(a);
}
};

Expand Down
Loading