Permalink
Browse files

new features and bug fixes.

* Added new comb.array.multiply method and added tests
* changed the uses of comb.when in comb.serial to use promise api
  • Loading branch information...
1 parent f3748d3 commit d415abcb36d4a80bd613cf2e42f3a8a3e542e777 @doug-martin doug-martin committed Jul 11, 2012
Showing with 73 additions and 41 deletions.
  1. +1 −1 index.js
  2. +54 −33 lib/base/array.js
  3. +7 −4 lib/define.js
  4. +1 −1 lib/promise.js
  5. +10 −2 test/base/array.test.js
View
@@ -1 +1 @@
-module.exports = exports = require("./lib");
+module.exports = require("./lib");
View
@@ -1,14 +1,14 @@
-var obj = require("./object"), misc = require("./misc");
+var obj = require("./object"), misc = require("./misc"), number = require("./number.js");
var argsToArray = misc.argsToArray;
-var isArray = exports.isArray = function(obj) {
+var isArray = exports.isArray = function (obj) {
return obj && obj instanceof Array;
};
-var cross = function(num, cross) {
- var ret = cross.reduceRight(function(a, b) {
+var cross = function (num, cross) {
+ var ret = cross.reduceRight(function (a, b) {
if (!isArray(b)) b = [b];
b.unshift(num)
a.unshift(b);
@@ -17,7 +17,7 @@ var cross = function(num, cross) {
return ret;
};
-var permute = function(num, cross, length) {
+var permute = function (num, cross, length) {
var ret = [];
for (var i = 0; i < cross.length; i++) {
ret.push([num].concat(array.rotate(cross, i)).slice(0, length));
@@ -26,7 +26,7 @@ var permute = function(num, cross, length) {
};
-var intersection = function(a, b) {
+var intersection = function (a, b) {
var ret = [];
if (isArray(a) && isArray(b)) {
if (a.length && b.length) {
@@ -59,7 +59,7 @@ comb.array = {
* comb.array.toArray() => [];
* comb.array.toArray("a", {a : "b"}) => ["a", ["a", "b"]];
*/
- toArray : function(o) {
+ toArray:function (o) {
var ret = [];
if (o != null) {
var args = argsToArray(arguments);
@@ -76,7 +76,7 @@ comb.array = {
ret.push(o);
}
} else {
- args.forEach(function(a) {
+ args.forEach(function (a) {
ret = ret.concat(array.toArray(a));
})
}
@@ -99,10 +99,10 @@ comb.array = {
*
* @param {Number[]} array the array of numbers to sum
*/
- sum : function(array) {
+ sum:function (array) {
array = array || [];
if (array.length) {
- return array.reduce(function(a, b) {
+ return array.reduce(function (a, b) {
return a + b;
});
} else {
@@ -121,9 +121,9 @@ comb.array = {
*
* @param {Aray} array the array of elements to remove duplicates from
*/
- removeDuplicates : function(arr) {
+ removeDuplicates:function (arr) {
if (isArray(arr)) {
- var ret = arr.reduce(function(a, b) {
+ var ret = arr.reduce(function (a, b) {
if (a.indexOf(b) == -1) {
return a.concat(b);
} else {
@@ -151,7 +151,7 @@ comb.array = {
* @param {Array} array the array of elements to remove duplicates from
* @param {Number} numberOfTimes the number of times to rotate the array
*/
- rotate : function(arr, numberOfTimes) {
+ rotate:function (arr, numberOfTimes) {
var ret = arr.slice();
if (typeof numberOfTimes != "number") {
numberOfTimes = 1;
@@ -185,7 +185,7 @@ comb.array = {
* @param {Array} arr the array to permute.
* @param {Number} length the number of elements to permute.
*/
- permutations : function(arr, length) {
+ permutations:function (arr, length) {
var ret = [];
if (isArray(arr)) {
var copy = arr.slice(0);
@@ -197,7 +197,7 @@ comb.array = {
[]
];
} else if (length <= arr.length) {
- ret = arr.reduce(function(a, b, i) {
+ ret = arr.reduce(function (a, b, i) {
if (length > 1) {
var ret = permute(b, array.rotate(copy, i).slice(1), length);
} else {
@@ -225,13 +225,13 @@ comb.array = {
*
* @param arrays variable number of arrays to zip together
*/
- zip : function() {
+ zip:function () {
var ret = [];
var arrs = argsToArray(arguments);
if (arrs.length > 1) {
var arr1 = arrs.shift();
if (isArray(arr1)) {
- ret = arr1.reduce(function(a, b, i) {
+ ret = arr1.reduce(function (a, b, i) {
var curr = [b];
for (var j = 0; j < arrs.length; j++) {
var currArr = arrs[j];
@@ -259,13 +259,13 @@ comb.array = {
*
* @param [Array[Array[]]] arr Array of arrays
*/
- transpose : function(arr) {
+ transpose:function (arr) {
var ret = [];
if (isArray(arr) && arr.length) {
var last;
- arr.forEach(function(a) {
+ arr.forEach(function (a) {
if (isArray(a) && (!last || a.length == last.length)) {
- a.forEach(function(b, i) {
+ a.forEach(function (b, i) {
!ret[i] && (ret[i] = []);
ret[i].push(b);
});
@@ -288,7 +288,7 @@ comb.array = {
* @param {Array} arr the array to retrieve values from
* @index {Number} index variable number of indexes to retrieve
*/
- valuesAt : function(arr, indexes) {
+ valuesAt:function (arr, indexes) {
var ret = [];
var indexes = argsToArray(arguments);
var arr = indexes.shift(), l = arr.length;
@@ -310,11 +310,11 @@ comb.array = {
*
* @param arrs variable number of arrays to union
*/
- union : function() {
+ union:function () {
var ret = [];
var arrs = argsToArray(arguments);
if (arrs.length > 1) {
- ret = array.removeDuplicates(arrs.reduce(function(a, b) {
+ ret = array.removeDuplicates(arrs.reduce(function (a, b) {
return a.concat(b);
}, []));
}
@@ -335,7 +335,7 @@ comb.array = {
* @param {Array} a
* @param {Array} b
*/
- intersect : function(a, b) {
+ intersect:function (a, b) {
var collect = [], set;
var args = argsToArray(arguments);
if (args.length > 1) {
@@ -346,7 +346,7 @@ comb.array = {
}
if (isArray(set)) {
var x = set.shift();
- var collect = set.reduce(function(a, b) {
+ var collect = set.reduce(function (a, b) {
return intersection(a, b);
}, x);
}
@@ -395,11 +395,11 @@ comb.array = {
*
* @param {Array} arr the array to find the powerset of
*/
- powerSet : function(arr) {
+ powerSet:function (arr) {
var ret = [];
if (isArray(arr) && arr.length) {
- var ret = arr.reduce(function(a, b) {
- var ret = a.map(function(c) {
+ var ret = arr.reduce(function (a, b) {
+ var ret = a.map(function (c) {
return c.concat(b);
})
return a.concat(ret);
@@ -444,7 +444,7 @@ comb.array = {
* @param {Array} a
* @param {Array} b
*/
- cartesian : function(a, b) {
+ cartesian:function (a, b) {
var ret = [];
if (isArray(a) && isArray(b) && a.length && b.length)
ret = cross(a[0], b).concat(array.cartesian(a.slice(1), b));
@@ -462,17 +462,38 @@ comb.array = {
*
* @param {Array} arr
*/
- compact : function(arr) {
+ compact:function (arr) {
var ret = [];
if (isArray(arr) && arr.length) {
- ret = arr.filter(function(item) {
+ ret = arr.filter(function (item) {
return !misc.isUndefinedOrNull(item);
})
}
return ret;
},
/**
+ * Creates a new array that is the result of the array concated the number of
+ * times. If times is not specified or it equals 0 then it defaults to 1.
+ * @param {Array} arr the array to multiply.
+ * @param {Number} [times=1] the number of times to multiple the array.
+ * @return {Array} a new array that is the result of the array multiplied the number of times specified.
+ */
+ multiply:function (arr, times) {
+ times = number.isNumber(times) ? times : 1;
+ if (!times) {
+ //make sure times is greater than zero if it is zero then dont multiply it
+ times = 1;
+ }
+ arr = array.toArray(arr || []);
+ var ret = [], i = 0;
+ while (++i <= times) {
+ ret = ret.concat(arr);
+ }
+ return ret;
+ },
+
+ /**
* Flatten multiple arrays into a single array
*
* @example
@@ -482,7 +503,7 @@ comb.array = {
*
* @param array
*/
- flatten : function(arr) {
+ flatten:function (arr) {
var set;
var args = argsToArray(arguments);
if (args.length > 1) {
@@ -491,7 +512,7 @@ comb.array = {
} else {
set = array.toArray(arr);
}
- return set.reduce(function(a, b) {
+ return set.reduce(function (a, b) {
return a.concat(b);
}, []);
}
View
@@ -94,11 +94,14 @@ var defineMixinProps = function (child, proto) {
var mixin = function () {
var args = Array.prototype.slice.call(arguments), l = args.length;
var child = this.prototype, childMeta = child.__meta, thisMeta = this.__meta, bases = child.__meta.bases, staticBases = bases.slice(),
- staticSupers = thisMeta.supers, supers = childMeta.supers;
+ staticSupers = thisMeta.supers || [], supers = childMeta.supers|| [];
for (var i = 0; i < l; i++) {
- var m = args[i];
- defineMixinProps(child, m.prototype.__meta.proto);
- defineMixinProps(this, m.__meta.proto);
+ var m = args[i], mProto = m.prototype;
+ var protoMeta = mProto.__meta, meta = m.__meta;
+ !protoMeta && (protoMeta = (mProto.__meta = {proto :mProto || {}}));
+ !meta && (meta = (m.__meta = {proto :m.__proto__ || {}}));
+ defineMixinProps(child, protoMeta.proto || {});
+ defineMixinProps(this, meta.proto || {});
//copy the bases for static,
mixinSupers(m.prototype, supers, bases);
View
@@ -583,7 +583,7 @@ var callNext = function (list, index, results, isErrored, ret) {
if (index < list.length) {
var item = list[index];
try {
- when(base.isFunction(item) ? item() : item,
+ when(base.isFunction(item) ? item() : item).then(
base.partial(callNextSuccess, list, index, results, isErrored, ret),
base.partial(callNextError, index, results, isErrored, ret));
} catch (e) {
@@ -1,7 +1,7 @@
"use strict";
var it = require('it'),
assert = require('assert'),
- comb = require("index");
+ comb = require("../../index");
it.describe("comb.array", function (it) {
@@ -231,6 +231,14 @@ it.describe("comb.array", function (it) {
assert.deepEqual(comb.array.compact([1, null, null, x, 2]), [1, 2]);
assert.deepEqual(comb.array.compact([1, 2]), [1, 2]);
});
- ;
+
+ it.should("multiply an array", function () {
+ assert.deepEqual(comb.array.multiply([1, 2, 3]), [1, 2, 3]);
+ assert.deepEqual(comb.array.multiply([1, 2, 3], 0), [1, 2, 3]);
+ assert.deepEqual(comb.array.multiply([1, 2, 3], 1), [1, 2, 3]);
+ assert.deepEqual(comb.array.multiply([1, 2, 3], 2), [1, 2, 3, 1, 2, 3]);
+ });
+
+ it.run();
});

0 comments on commit d415abc

Please sign in to comment.