Skip to content

Commit

Permalink
When adding a record to an index, use JSON.stringify
Browse files Browse the repository at this point in the history
This lets you index non-string values
  • Loading branch information
scottmessinger committed May 3, 2014
1 parent d728d65 commit ef95a2f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions spec/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ asyncTest("it should add a doc to all the indexes", function(){
deepEqual(_.include(value.ids, 2), true, "ID should be added to the posts collection")
}))
promises.push(localforage.getItem('ks_blog_posts_by_title', function(index){
deepEqual(_.include(index.values["Lamps"], 2), true, "ID should be added to the posts index")
deepEqual(_.include(index.values[JSON.stringify("Lamps")], 2), true, "ID should be added to the posts index")
}))
promises.push(localforage.getItem('ks_blog_posts_2', function(value){
deepEqual({id: 2, title: 'Lamps'}, value, "ID should be added to the index")
deepEqual({id: 2, title: 'Lamps'}, value, "Object should be added")
}))
Promise.all(promises).then(start)
})
Expand Down Expand Up @@ -99,7 +99,7 @@ asyncTest("it should remove a record", function(){
equal(_.include(collectionBrain.ids, 2), false, "Should be removed from the collection brain")
return localforage.getItem('ks_blog_posts_by_title')
}).then(function(index){
equal(_.include(index.values["Lamps"], 2), false, "The id should be removed from the index")
equal(_.include(index.values[JSON.stringify("Lamps")], 2), false, "The id should be removed from the index")
start()
}).catch(function(e){ console.log(e, e.stack) })
})
Expand All @@ -121,8 +121,8 @@ asyncTest("it should update a record", function(){
return localforage.getItem('ks_blog_posts_by_title')
}).then(function(index){
console.log(index)
equal(_.include(index.values["Lamps"], 2), false, "The old value should be removed from the index")
equal(_.include(index.values["Turtles"], 2), true, "The new value should be added to the index")
equal(_.include(index.values[JSON.stringify("Lamps")], 2), false, "The old value should be removed from the index")
equal(_.include(index.values[JSON.stringify("Turtles")], 2), true, "The new value should be added to the index")
start()
})
.catch(function(e){ console.log(e, e.stack) })
Expand Down
8 changes: 4 additions & 4 deletions src/knapsack.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ KS.Collection.prototype._addToIndexes = function(indexes, doc){

KS.Collection.prototype._addToIndex = function(index, doc){
var self = this;
var docValue = doc[index.name]
var docValue = JSON.stringify(doc[index.name])
index.values[docValue] = index.values[docValue] || []
if (!_.include(index.values[docValue])){
index.values[docValue].push(doc.id)
Expand Down Expand Up @@ -172,7 +172,7 @@ KS.Collection.prototype._indexFind = function(index, value, cb){
return new Promise(function(resolve, reject){
localforage.getItem(self.indexBaseName + index)
.then(function(index, error){
var ids = index.values[value]
var ids = index.values[JSON.stringify(value)]
if (ids.length == 0){
if (cb) {cb([], error)}
if (error){reject(error)}
Expand Down Expand Up @@ -253,8 +253,8 @@ KS.Collection.prototype._removeFromIndexes = function(indexes, docToRemove){
}

KS.Collection.prototype._removeFromIndex = function(index, docToRemove){
var ids = index.values[docToRemove[index.name]]
var indexedValue = docToRemove[index.name]
var indexedValue = JSON.stringify(docToRemove[index.name])
var ids = index.values[indexedValue]
var self = this;
if (ids.length > 1){
index.values[indexedValue] = _.without(index.values[indexedValue], id)
Expand Down

0 comments on commit ef95a2f

Please sign in to comment.