diff --git a/snippets/xProd.md b/snippets/xProd.md index 332d9f76e..d5ff74b61 100644 --- a/snippets/xProd.md +++ b/snippets/xProd.md @@ -2,10 +2,10 @@ Creates a new array out of the two supplied by creating each possible pair from the arrays. -Use `Array.map()` to produce every possible pair from the elements of the two arrays. +Use `Array.reduce()`, `Array.map()` and `Array.concat()` to produce every possible pair from the elements of the two arrays and save them in an array. ```js -const xProd = (a, b) => a.map(x => b.map(y => [x, y])); +const xProd = (a, b) => a.reduce((acc,x) => acc.concat(b.map(y => [x, y])),[]); ``` ```js diff --git a/test/is/is.test.js b/test/is/is.test.js index 78d7b1e3a..922cc0f8c 100644 --- a/test/is/is.test.js +++ b/test/is/is.test.js @@ -5,9 +5,25 @@ test('Testing is', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof is === 'function', 'is is a Function'); + t.true(is(Array, [1]), `Works for arrays with data`); + t.true(is(Array, []), `Works for empty arrays`); + t.false(is(Array, {}), `Works for arrays, not objects`); + t.true(is(Object, {}), `Works for objects`); + t.true(is(Map, new Map()), `Works for maps`); + t.true(is(RegExp, /./g), `Works for regular expressions`); + t.true(is(Set, new Set()), `Works for sets`); + t.true(is(WeakMap, new WeakMap()), `Works for weak maps`); + t.true(is(WeakSet, new WeakSet()), `Works for weak sets`); + t.false(is(String, ''), `Works for strings - returns false for primitive`); + t.true(is(String, new String('')), `Works for strings - returns true when using constructor`); + t.false(is(Number, 1), `Works for numbers - returns false for primitive`); + t.true(is(Number, new Number('10')), `Works for numbers - returns true when using constructor`); + t.false(is(Boolean, false), `Works for booleans - returns false for primitive`); + t.true(is(Boolean, new Boolean(false)), `Works for booleans - returns true when using constructor`); + t.true(is(Function, () => null), `Works for functions`); //t.deepEqual(is(args..), 'Expected'); //t.equal(is(args..), 'Expected'); //t.false(is(args..), 'Expected'); //t.throws(is(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/testlog b/test/testlog index ec65e3ba3..051197040 100644 --- a/test/testlog +++ b/test/testlog @@ -1,4 +1,4 @@ -Test log for: Wed Jan 24 2018 16:47:50 GMT+0200 (GTB Standard Time) +Test log for: Wed Jan 24 2018 17:16:12 GMT+0200 (GTB Standard Time) > 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code > tape test/**/*.test.js | tap-spec @@ -555,6 +555,22 @@ Test log for: Wed Jan 24 2018 16:47:50 GMT+0200 (GTB Standard Time) Testing is √ is is a Function + √ Works for arrays with data + √ Works for empty arrays + √ Works for arrays, not objects + √ Works for objects + √ Works for maps + √ Works for regular expressions + √ Works for sets + √ Works for weak maps + √ Works for weak sets + √ Works for strings - returns false for primitive + √ Works for strings - returns true when using constructor + √ Works for numbers - returns false for primitive + √ Works for numbers - returns true when using constructor + √ Works for booleans - returns false for primitive + √ Works for booleans - returns true when using constructor + √ Works for functions Testing isAbsoluteURL @@ -1323,10 +1339,13 @@ Test log for: Wed Jan 24 2018 16:47:50 GMT+0200 (GTB Standard Time) Testing unzip √ unzip is a Function + √ unzip([['a', 1, true], ['b', 2, false]]) equals [['a', 'b'], [1, 2], [true, false]] + √ unzip([['a', 1, true], ['b', 2]]) equals [['a', 'b'], [1, 2], [true]] Testing unzipWith √ unzipWith is a Function + √ unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc + v, 0)) equals [3, 30, 300] Testing URLJoin @@ -1387,6 +1406,7 @@ Test log for: Wed Jan 24 2018 16:47:50 GMT+0200 (GTB Standard Time) Testing xProd √ xProd is a Function + √ xProd([1, 2], ['a', 'b']) returns [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']] Testing yesNo @@ -1434,8 +1454,8 @@ Test log for: Wed Jan 24 2018 16:47:50 GMT+0200 (GTB Standard Time) √ zipWith is a Function - total: 598 - passing: 598 - duration: 1.3s + total: 618 + passing: 618 + duration: 412ms diff --git a/test/unzip/unzip.test.js b/test/unzip/unzip.test.js index 17efbc0a4..76be27786 100644 --- a/test/unzip/unzip.test.js +++ b/test/unzip/unzip.test.js @@ -5,9 +5,11 @@ test('Testing unzip', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof unzip === 'function', 'unzip is a Function'); + t.deepEqual(unzip([['a', 1, true], ['b', 2, false]]), [['a', 'b'], [1, 2], [true, false]], `unzip([['a', 1, true], ['b', 2, false]]) equals [['a', 'b'], [1, 2], [true, false]]`); + t.deepEqual(unzip([['a', 1, true], ['b', 2]]), [['a', 'b'], [1, 2], [true]], `unzip([['a', 1, true], ['b', 2]]) equals [['a', 'b'], [1, 2], [true]]`); //t.deepEqual(unzip(args..), 'Expected'); //t.equal(unzip(args..), 'Expected'); //t.false(unzip(args..), 'Expected'); //t.throws(unzip(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/unzipWith/unzipWith.test.js b/test/unzipWith/unzipWith.test.js index 09f3d2429..8c3cbea6a 100644 --- a/test/unzipWith/unzipWith.test.js +++ b/test/unzipWith/unzipWith.test.js @@ -5,9 +5,9 @@ test('Testing unzipWith', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof unzipWith === 'function', 'unzipWith is a Function'); - //t.deepEqual(unzipWith(args..), 'Expected'); + t.deepEqual(unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc + v, 0)), [3, 30, 300], `unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc + v, 0)) equals [3, 30, 300]`); //t.equal(unzipWith(args..), 'Expected'); //t.false(unzipWith(args..), 'Expected'); //t.throws(unzipWith(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/xProd/xProd.js b/test/xProd/xProd.js index 07ea52dfa..37b3556a6 100644 --- a/test/xProd/xProd.js +++ b/test/xProd/xProd.js @@ -1,2 +1,2 @@ -const xProd = (a, b) => a.map(x => b.map(y => [x, y])); +const xProd = (a, b) => a.reduce((acc,x) => acc.concat(b.map(y => [x, y])),[]); module.exports = xProd \ No newline at end of file diff --git a/test/xProd/xProd.test.js b/test/xProd/xProd.test.js index a164210a2..9d650e65c 100644 --- a/test/xProd/xProd.test.js +++ b/test/xProd/xProd.test.js @@ -5,9 +5,10 @@ test('Testing xProd', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof xProd === 'function', 'xProd is a Function'); + t.deepEqual(xProd([1, 2], ['a', 'b']), [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']], `xProd([1, 2], ['a', 'b']) returns [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]`); //t.deepEqual(xProd(args..), 'Expected'); //t.equal(xProd(args..), 'Expected'); //t.false(xProd(args..), 'Expected'); //t.throws(xProd(args..), 'Expected'); t.end(); -}); \ No newline at end of file +});