Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Tested for IE7.

  • Loading branch information...
commit 939f36c051e1ca78238191c5b2fc0d8f2053af66 1 parent bdd33b5
@airportyh authored
Showing with 100 additions and 10 deletions.
  1. +68 −0 ecma5arraymethods.js
  2. +13 −6 filtered.js
  3. +14 −0 filtered_tests.js
  4. +5 −4 index.js
View
68 ecma5arraymethods.js
@@ -0,0 +1,68 @@
+
+module.exports = {
+ forEach: function(arr, iter, context){
+ for (var i = 0; i < arr.length; i++){
+ iter.call(context, arr[i], i, arr);
+ }
+ }
+ ,
+ reduce: function(arr, accumlator, initVal){
+ var curr = initVal;
+ arr.forEach(function(item, i, arr){
+ curr = accumlator(curr, item, i, arr);
+ });
+ return curr;
+ }
+ ,
+ reduceRight: function(arr, accumlator, initVal){
+ var curr = initVal;
+ for (var i = arr.length - 1; i >= 0; i--){
+ var item = arr[i];
+ curr = accumlator(curr, item, i, arr);
+ }
+ return curr;
+ }
+ ,
+ map: function(arr, func, context){
+ var len = arr.length,
+ ret = []
+ for (var i = 0; i < len; i++){
+ if (i in arr)
+ ret[i] = func.call(context, arr[i], i, arr)
+ }
+ return ret
+ }
+ ,
+ filter: function(arr, accept, context){
+ return arr.reduce(function(curr, item, i, arr){
+ return accept.call(context, item, i, arr) ? curr.concat(item) : curr;
+ }, []);
+ }
+ ,
+ every: function(arr, predicate, context){
+ return arr.reduce(function(curr, item, i, arr){
+ return curr && predicate.call(context, item, i, arr);
+ }, true);
+ }
+ ,
+ some: function(arr, predicate, context){
+ return arr.reduce(function(curr, item, i, arr){
+ return curr || predicate.call(context, item, i, arr);
+ }, false);
+ }
+ ,
+ indexOf: function(arr, target, startFrom){
+ var len = arr.length
+ for (var i = startFrom || 0; i < len; i++){
+ if (i in arr && arr[i] === target) return i;
+ }
+ return -1;
+ }
+ ,
+ lastIndexOf: function(arr, target, startFrom){
+ var len = arr.length
+ for (var i = startFrom || len - 1; i >= 0; i--)
+ if (i in arr && arr[i] === target) return i;
+ return -1;
+ }
+}
View
19 filtered.js
@@ -1,4 +1,6 @@
var E = require('emmitt')
+var ecma5 = require('./ecma5arraymethods')
+
module.exports = FilteredArray
function FilteredArray(arr, fun){
@@ -11,8 +13,7 @@ function FilteredArray(arr, fun){
if (fun(item)){
self.indices.push(idx)
}
- if (typeof item !== 'object') return
- E.on(item, 'change', self.$onItemChange)
+ self._registerItemChange(item)
})
this.$onInsert = function(idx, item){
self._onInsert(idx, item)
@@ -57,6 +58,7 @@ FilteredArray.prototype = {
if (this.fun(item)){
this._insert(idx, item)
}
+ this._registerItemChange(item)
},
_onRemove: function(idx, item){
if (this.fun(item)){
@@ -74,7 +76,7 @@ FilteredArray.prototype = {
}
},
_insert: function(idx, item){
- this.indices = this.indices.map(function(i){
+ this.indices = ecma5.map(this.indices, function(i){
if (i >= idx) return i + 1
return i
})
@@ -83,8 +85,12 @@ FilteredArray.prototype = {
return one - other
})
},
+ _registerItemChange: function(item){
+ if (typeof item !== 'object') return
+ E.on(item, 'change', this.$onItemChange)
+ },
_remove: function(idx, item){
- var ii = this.indices.indexOf(idx)
+ var ii = ecma5.indexOf(this.indices, idx)
if (ii !== -1){
this.indices.splice(ii, 1)
}
@@ -92,9 +98,10 @@ FilteredArray.prototype = {
},
toArray: function(){
var source = this.source
- return this.indices.map(function(idx){
+ function item(idx){
return source.get(idx)
- })
+ }
+ return ecma5.map(this.indices, item)
},
destroy: function(){
this.source.forEach(function(item){
View
14 filtered_tests.js
@@ -107,6 +107,20 @@ test('items change, also changes', function(t){
t.end()
})
+test('listens to added items', function(t){
+ var bob = { name: 'bob', age: 1 }
+ var mary = { name: 'mary', age: 2 }
+ var source = new Array(bob)
+ var filtered = new FilteredArray(source, function(child){
+ return child.age % 2 === 1
+ })
+ source.add(mary)
+ t.equal(filtered.size(), 1)
+ set(mary, 'age', 3)
+ t.equal(filtered.size(), 2)
+ t.end()
+})
+
test('clean up', function(t){
var bob = { name: 'bob', age: 1 }
var mary = { name: 'mary', age: 2 }
View
9 index.js
@@ -1,4 +1,5 @@
var E = require('emmitt')
+var ecma5 = require('./ecma5arraymethods')
module.exports = MantaRay
@@ -30,7 +31,7 @@ MantaRay.prototype = {
E.emit(this, 'insert', i, obj)
},
remove: function(obj){
- var idx = this.arr.indexOf(obj)
+ var idx = ecma5.indexOf(this.arr, obj)
this.arr.splice(idx, 1)
E.emit(this, 'remove', idx, obj)
},
@@ -60,13 +61,13 @@ MantaRay.prototype = {
return this.arr
},
forEach: function(fun){
- this.arr.forEach(fun)
+ ecma5.forEach(this.arr, fun)
},
map: function(fun){
- return this.arr.map(fun)
+ return ecma5.map(this.arr, fun)
},
filter: function(fun){
- return this.arr.filter(fun)
+ return ecma5.filter(this.arr, fun)
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.