Ver código fonte

added toArray method and test

mightyplow 8 anos atrás
pai
commit
cc28130655
2 arquivos alterados com 16 adições e 0 exclusões
  1. 2 0
      lib/array.js
  2. 14 0
      tests/array.js

+ 2 - 0
lib/array.js

@@ -1,6 +1,8 @@
 'use strict'
 
 module.exports = {
+    toArray: arraylike => Array.prototype.slice.call(arraylike || []),
+
     flatten: function flatten (ar) {
         return ar.reduce((acc, item) => {
             if (Array.isArray(item)) {

+ 14 - 0
tests/array.js

@@ -0,0 +1,14 @@
+'use strict'
+
+module.exports = {
+    toArray: test => {
+        const toArray = require('../lib/array').toArray
+
+        test.ok(typeof toArray === 'function', 'toArray should be a function')
+        test.ok(Array.isArray(toArray()), 'should return an array')
+        test.ok(!Array.isArray(arguments), 'arguments should not be an array')
+        test.ok(Array.isArray(toArray(arguments)), 'arguments should not be an array')
+
+        test.done()
+    }
+}