Permalink
Browse files

Added array methods from MooTools

  • Loading branch information...
jpdery committed Feb 8, 2013
1 parent 93308dd commit faa6e55fce33db0b239a85388f19770a3a78589a
Showing with 228 additions and 0 deletions.
  1. +78 −0 shell/array.js
  2. +150 −0 test/shell/array.js
View
@@ -0,0 +1,78 @@
+/*
+array methods
+*/"use strict";
+
+var array = require('prime/es5/array')
+var number = require('prime/shell/number')
+
+array.implement({
+
+ clean: function(){
+ return array.filter(this, function(item){
+ return item !== null && item !== undefined
+ })
+ },
+
+ invoke: function(methodName){
+ var args = array.slice(arguments, 1)
+ return array.map(this, function(item){
+ return item[methodName].apply(item, args)
+ })
+ },
+
+ associate: function(keys){
+ var obj = {}, length = Math.min(this.length, keys.length)
+ for (var i = 0; i < length; i++) obj[keys[i]] = this[i]
+ return obj
+ },
+
+ contains: function(item, from){
+ return array.indexOf(this, item, from) != -1
+ },
+
+ append: function(items){
+ Array.prototype.push.apply(this, items)
+ return this
+ },
+
+ last: function(){
+ return (this.length) ? this[this.length - 1] : null
+ },
+
+ random: function(){
+ return (this.length) ? this[number.random(0, this.length - 1)] : null
+ },
+
+ include: function(item){
+ if (!array.contains(this, item)) array.push(this, item)
+ return this
+ },
+
+ combine: function(items){
+ for (var i = 0, l = items.length; i < l; i++) array.include(this, items[i])
+ return this
+ },
+
+ empty: function(){
+ this.length = 0
+ return this
+ },
+
+ pick: function(){
+ for (var i = 0, l = this.length; i < l; i++){
+ if (this[i] !== null && this[i] !== undefined) return this[i]
+ }
+ return null
+ },
+
+ find: function(fn, context){
+ for (var i = 0, l = this.length; i < l; i++){
+ var item = this[i]
+ if (fn.call(context, item, i, this)) return item
+ }
+ return null
+ }
+
+})
+
+module.exports = array;
View
@@ -0,0 +1,150 @@
+"use strict";
+
+var expect = require("expect.js")
+var array = require("../../shell/array")
+
+describe('Array', function() {
+
+ describe('clean', function() {
+
+ it('should clean an array from undefined and null values', function(){
+ var items = [null, 1, 0, true, false, "foo", undefined];
+ var arr = array.clean(items);
+ expect(arr).to.eql([1, 0, true, false, "foo"]);
+ });
+
+ })
+
+ describe('invoke', function(){
+
+ it('should invoke methods on the contained objects', function(){
+
+ var Item = function(i){
+ this.i = i
+ this.myMethod = function(){
+ return this.i
+ }
+ }
+
+ expect(array.invoke([new Item(3), new Item(1), new Item(2)], 'myMethod')).to.eql([3, 1, 2])
+
+ })
+
+ })
+
+ describe('associate', function(){
+
+ it('should associate an array with a specified array', function(){
+ var obj = array.associate([1, 2, 3, 0, 0, 0], ['a', 'b', 'c', 'd'])
+ expect(obj).to.eql({a:1, b:2, c:3, d:0})
+ })
+
+ })
+
+ describe('contains', function(){
+
+ it('should return false if the array does not contain the specified item', function(){
+ expect(array.contains([0,1,2], 'not found')).to.not.be.ok()
+ })
+
+ })
+
+ describe('append', function(){
+
+ it('should append to an array', function(){
+ var a = [1,2,4]
+ var b = [2,3,4,5]
+ array.append(a, b)
+ expect(a).to.eql([1,2,4,2,3,4,5])
+ expect(b).to.eql([2,3,4,5])
+ })
+
+ })
+
+ describe('last', function(){
+
+ it('should return the last item in the array', function(){
+ expect(array.last([1,2,3,0,0,0])).to.be(0)
+ expect(array.last([3])).to.be(3)
+ })
+
+ })
+
+ describe('random', function(){
+
+ it('should get a random element from an array', function(){
+ var a = [1]
+
+ expect(array.random(a)).to.be(1)
+
+ array.push(a, 2, 3)
+
+ // Let's try a few times
+ expect(a).to.contain(array.random(a))
+ expect(a).to.contain(array.random(a))
+ expect(a).to.contain(array.random(a))
+ expect(a).to.contain(array.random(a))
+ });
+
+ })
+
+ describe('include', function(){
+
+ it('should include only new items', function(){
+ var arr = array.include([1,2,3,4], 1)
+ arr = array.include([1,2,3,4], 5)
+ expect(arr).to.eql([1,2,3,4,5])
+ })
+
+ })
+
+ describe('combine', function(){
+
+ it('should combine an array', function(){
+ var arr = array.combine([1,2,3,4], [3,1,4,5,6,7])
+ expect(arr).to.eql([1,2,3,4,5,6,7])
+ })
+
+ })
+
+ describe('empty', function(){
+
+ it('should empty the array', function(){
+ var arr = array.empty([1,2,3,4])
+ expect(arr).to.eql([])
+ })
+
+ })
+
+ describe('pick', function(){
+
+ it('should pick a value that is not null from the array', function(){
+ expect(array.pick([null, undefined, true, 1])).to.be.ok()
+ })
+
+ it('should return null', function(){
+ expect(array.pick([])).to.be(null)
+ })
+
+ })
+
+ describe('find', function(){
+
+ var arr = [1,2,3,4,5,6,7,8]
+
+ it('should find a value from the array', function(){
+ expect(array.find(arr, function(item) {
+ return item === 8
+ })).to.be(8)
+ })
+
+ it('should return null', function(){
+ expect(array.find(arr, function(item) {
+ return item === 10
+ })).to.be(null)
+ })
+
+ })
+
+
+})

3 comments on commit faa6e55

having checks for !== null && !== undefined is a bit redundant. != null does the trick perfectly.

Owner

jpdery replied Feb 9, 2013

jslint has a lot of idiotic rules like this that you can disable.

Please sign in to comment.