collectionize
Advanced tools
Comparing version 0.2.6 to 0.2.7
{ | ||
"name": "collectionize", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"main": "collectionize.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
(function () { | ||
// http://jsperf.com/jquery-each-vs-for-loop/40 | ||
function nativeEach(obj, cb) { | ||
if (obj && _.isFunction(cb)) { | ||
if (_.isPlainObject(obj)) { | ||
return eachObject(obj, cb); | ||
} else if (_.isArray(obj) || _.isString(obj)) { | ||
return eachArray(obj, cb); | ||
} | ||
} | ||
} | ||
function eachArray(obj, cb) { | ||
var i = 0, max = obj.length; | ||
for (; i < max; i++) { | ||
if (cb(obj[i], i, obj) === false) { | ||
break; | ||
} | ||
} | ||
} | ||
function eachObject(obj, cb) { | ||
var key; | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
if (cb(obj[key], key, obj) === false) { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
function arrayMove(arr, oldIndex, newIndex) { | ||
@@ -22,11 +53,11 @@ if (newIndex >= arr.length) { | ||
var lodashMethods = ['at', 'each', 'every', 'filter', 'find', 'findIndex', | ||
var lodashMethods = ['at', 'every', 'filter', 'find', 'findIndex', | ||
'findLastIndex', 'first', 'last', 'map', 'max', 'min', 'pluck', 'reduce', | ||
'reduceRight', 'reject', 'sample', 'size', 'shuffle', 'some', 'sortBy', 'where']; | ||
_.each(lodashMethods, function (fn) { | ||
self[fn] = function () { | ||
nativeEach(lodashMethods, function (methodName) { | ||
self[methodName] = function () { | ||
var args = _.toArray(arguments); | ||
args.unshift(self.db); | ||
return _[fn].apply(this, args); | ||
return _[methodName].apply(this, args); | ||
}; | ||
@@ -36,3 +67,3 @@ }); | ||
self.on = function (eventNames, fn) { | ||
_.each(eventNames.split(' '), function (eventName) { | ||
nativeEach(eventNames.split(' '), function (eventName) { | ||
self.listeners.push({ name: eventName, fn: fn }); | ||
@@ -45,3 +76,3 @@ }); | ||
var eventName = args.shift(); | ||
_.each(self.listeners, function (listener) { | ||
nativeEach(self.listeners, function (listener) { | ||
if (listener.name === eventName) { | ||
@@ -59,2 +90,6 @@ listener.fn.apply(this, args); | ||
self.each = function (cb) { | ||
nativeEach(self.db, cb); | ||
}; | ||
self.isEmpty = function (query) { | ||
@@ -66,3 +101,3 @@ return self.filter(query).length === 0; | ||
var matches = self.filter(query); | ||
_.each(matches, function (obj) { | ||
nativeEach(matches, function (obj) { | ||
if (obj) { | ||
@@ -105,3 +140,3 @@ if (_.isNumber(obj[prop])) { | ||
if (matches.length > 0) { | ||
_.each(matches, function (match) { | ||
nativeEach(matches, function (match) { | ||
_.extend(match, obj); | ||
@@ -122,3 +157,3 @@ self.trigger('beforeUpdate', match); | ||
self.db = self.reject(query); | ||
_.each(removed, function (obj) { | ||
nativeEach(removed, function (obj) { | ||
self.trigger('removed', obj); | ||
@@ -129,2 +164,3 @@ }); | ||
self.flush = function (db) { | ||
self.trigger('beforeFlush'); | ||
self.db = db || []; | ||
@@ -142,3 +178,3 @@ self.trigger('flushed'); | ||
data[index] = {}; | ||
_.each(item, function (value, key) { | ||
nativeEach(item, function (value, key) { | ||
if (_.isFunction(value)) { | ||
@@ -145,0 +181,0 @@ value = '(' + value + ');'; |
{ | ||
"name": "collectionize", | ||
"description": "A lightweight collection management library.", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"repository": "https://github.com/andrewchilds/collectionize.git", | ||
@@ -6,0 +6,0 @@ "author": { |
20744
277