瀏覽代碼

added function helper and tests

mightyplow 8 年之前
父節點
當前提交
f4b0044675
共有 2 個文件被更改,包括 26 次插入2 次删除
  1. 9 2
      lib/function.js
  2. 17 0
      tests/function.js

+ 9 - 2
lib/function.js

@@ -1,7 +1,14 @@
 'use strict'
 
-const fs = require('fs')
+const toArray = require('./array').toArray
+
+const argsToArray = fn => {
+    return function () {
+        return fn(toArray(arguments))
+    }
+}
 
 module.exports = {
-    combine: fns => fns.reverse().reduce((fnc, fn) => val => fnc(fn(val)))
+    argsAsArray: argsToArray,
+    combine: argsToArray(fns => fns.reverse().reduce((fnc, fn) => val => fnc(fn(val))))
 }

+ 17 - 0
tests/function.js

@@ -0,0 +1,17 @@
+'use strict'
+
+module.exports = {
+    combine: test => {
+        const combine = require('../lib/function').combine
+
+        test.ok(typeof combine === 'function', 'combine should be a function')
+
+        const a = val => val * 10
+        const b = val => val * 10
+
+        const fn = combine(a, b)
+        test.ok(fn(3) === 300, 'combine should return of all applied functions')
+
+        test.done();
+    }
+}