Skip to content

Commit

Permalink
fix adapter matching, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
0x8890 committed Aug 20, 2015
1 parent 9a1f30c commit 74905f0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
3 changes: 2 additions & 1 deletion doc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog


##### 1.1.1 (2015-08-20)
##### 1.1.2 (2015-08-20)
- Fix: sorting implementation in built-in adapters.
- Fix: matching implementation in built-in adapters.


##### 1.1.0 (2015-08-20)
Expand Down
27 changes: 12 additions & 15 deletions lib/adapter/adapters/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,31 @@ export function applyOptions (count, fields, records, options) {
}


const primitiveTypes = new Set([ Number, String, Boolean, Symbol ])

const matchCheck = new WeakMap([
[ Date, (a, b) => a.getTime() === b.getTime() ],
[ Buffer, (a, b) => a.equals(toBuffer(b)) ],
[ Object, () => false ]
])

function check (a, b) {
return this.isPrimitive ? a === b : matchCheck.get(this.type)(a, b)

function check (type, a, b) {
if (b === null) return a === null
const matcher = matchCheck.get(type)
return matcher ? matcher(a, b) : a === b
}


function matchByField (fields, match, record) {
for (let field in match) {
if (record[field] === null) return false

const type = fields[field][keys.type]
const isPrimitive = fields[field][keys.link] || primitiveTypes.has(type)
const checkValue = (fieldDefinition, a, b) =>
fieldDefinition[keys.isArray] ?
a.some(check.bind(null, fieldDefinition[keys.type], b)) :
check(fieldDefinition[keys.type], b, a)

for (let field in match) {
let matches = match[field]
if (!Array.isArray(match[field])) matches = [ matches ]

for (let x of matches)
if (fields[field][keys.isArray] ?
!record[field].some(check.bind({ isPrimitive, type }, x)) :
!check.call({ isPrimitive, type }, x, record[field]))
return false
if (!matches.some(checkValue.bind(null, fields[field], record[field])))
return false
}

return true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fortune",
"description": "High-level I/O for web applications.",
"version": "1.1.1",
"version": "1.1.2",
"license": "MIT",
"author": {
"email": "[email protected]",
Expand Down
11 changes: 10 additions & 1 deletion test/unit/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default function () {
run(() => {
comment('find: match (string)')
return test(adapter =>
adapter.find(type, null, { match: { name: 'john' } })
adapter.find(type, null, { match: { name: [ 'john', 'xyz' ], age: 36 } })
.then(records => {
equal(records.length, 1, 'match length is correct')
equal(records[0].name, 'john', 'matched correct record')
Expand All @@ -128,6 +128,15 @@ export default function () {
}))
})

run(() => {
comment('find: match (nothing)')
return test(adapter =>
adapter.find(type, null, { match: { name: 'bob', age: 36 } })
.then(records => {
equal(records.length, 0, 'match length is correct')
}))
})

run(() => {
comment('find: sort ascending')
return test(adapter =>
Expand Down

0 comments on commit 74905f0

Please sign in to comment.