Skip to content

Instantly share code, notes, and snippets.

@yhsiang
Created July 19, 2015 16:50
Show Gist options
  • Save yhsiang/d38867b653a367874273 to your computer and use it in GitHub Desktop.
Save yhsiang/d38867b653a367874273 to your computer and use it in GitHub Desktop.
js performance
var queryMatch = function (doc, query) {
return Object.keys(query).every(function(name) {
return propertyMatch(doc, name, query[name])
})
}
var propertyMatch = function (doc, name, prop) {
return Object.keys(prop).every(function(op) {
switch(op) {
case '$eq':
return doc[name] === prop[op]
case '$lt':
return doc[name] < prop[op]
case '$gt':
return doc[name] > prop[op]
case '$not':
return !propertyMatch(doc, name, prop[op])
}
return false
})
}
module.exports = function (query) {
return function (doc) {
return queryMatch(doc, query);
}
}
var interpeter = require('./interpeter')
var assert = require('assert')
var matcher = interpeter({
hello: {
$eq: 'world'
}
})
var bool = matcher({
hello: 'world'
})
assert(bool === true)
var bool2 = matcher({
hello: 'verden'
})
assert(bool2 === false)
var matcher2 = interpeter({
age: {
$lt: 10
}
})
var bool3 = matcher2({
age: 5
})
assert(bool3 === true)
var bool4 = matcher2({
age: 15
})
assert(bool4 === false)
var matcher3 = interpeter({
hello: {
$not: {
$eq: 'world'
}
}
})
var bool5 = matcher3({
hello: 'verden'
})
assert(bool5 === true)
var bool6 = matcher3({
hello: 'world'
})
assert(bool6 === false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment