From 1c3a117d02540c113a93301ad71cec2bd5ea98fe Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 21 Oct 2015 03:19:22 -0400 Subject: [PATCH] feature(cloudcmd) add execon from bower --- bower.json | 5 +- lib/client/cloudcmd.js | 14 ++- lib/util.js | 238 ++---------------------------------- modules/execon/.bower.json | 10 +- modules/execon/ChangeLog | 22 ++++ modules/execon/README.md | 6 +- modules/execon/bower.json | 4 +- modules/execon/lib/exec.js | 32 ++--- modules/execon/package.json | 10 +- test/lib/util.js | 56 --------- 10 files changed, 83 insertions(+), 314 deletions(-) diff --git a/bower.json b/bower.json index c4608a19..866244a9 100644 --- a/bower.json +++ b/bower.json @@ -29,7 +29,8 @@ "promise-polyfill": "~2.1.0", "rendy": "~1.1.0", "emitify": "~1.3.0", - "philip": "~1.3.1", - "smalltalk": "~1.5.3" + "smalltalk": "~1.5.3", + "execon": "~1.2.8", + "philip": "~1.3.1" } } diff --git a/lib/client/cloudcmd.js b/lib/client/cloudcmd.js index dc2938bc..f6e4d278 100644 --- a/lib/client/cloudcmd.js +++ b/lib/client/cloudcmd.js @@ -32,9 +32,10 @@ var CloudCmd; moduleFiles = [ window.Promise ? '' : 'promise-polyfill/Promise.min', - 'format-io/lib/format', - 'rendy/lib/rendy', - 'emitify/lib/emitify' + libDir('format', 'format-io'), + libDir('rendy'), + libDir('emitify'), + libDir('exec', 'execon') ].filter(function(name) { return name; }).map(function(name) { @@ -57,7 +58,14 @@ var CloudCmd; CloudCmd.init(prefix); }); } + + function libDir(name, dir) { + if (!dir) + dir = name; + return dir + '/lib/' + name; + } + function createScript(url, callback) { var script = document.createElement('script'); diff --git a/lib/util.js b/lib/util.js index 6460faf2..21867787 100644 --- a/lib/util.js +++ b/lib/util.js @@ -5,12 +5,17 @@ /* global rendy */ - if (typeof module === 'object' && module.exports) - module.exports = new UtilProto(); - else if (!Scope.Util) - Scope.Util = new UtilProto(); + var exec; - function UtilProto() { + if (typeof module === 'object' && module.exports) { + exec = require('execon'); + module.exports = new UtilProto(exec); + } else if (!Scope.Util) { + exec = window.exec; + Scope.Util = new UtilProto(exec); + } + + function UtilProto(exec) { var Util = this; this.check = new checkProto(); @@ -247,228 +252,7 @@ return type; } - this.exec = new ExecProto(); - - function ExecProto() { - /** - * function do save exec of function - * @param callback - * @param arg1 - * ... - * @param argN - */ - var exec = function(callback) { - var ret, - isFunc = Util.type.function(callback), - args = [].slice.call(arguments, 1); - - if (isFunc) - ret = callback.apply(null, args); - - return ret; - }; - - /* - * return function that calls callback with arguments - */ - exec.with = function(callback) { - var slice = Array.prototype.slice, - args = slice.call(arguments, 1); - - return function() { - var array = slice.call(arguments), - all = args.concat(array); - - callback.apply(null, all); - }; - }; - - /** - * return save exec function - * @param callback - */ - exec.ret = function() { - var result, - args = [].slice.call(arguments); - - args.unshift(exec); - result = exec.with.apply(null, args); - - return result; - }; - - /** - * function do conditional save exec of function - * @param condition - * @param callback - * @param func - */ - exec.if = function(condition, callback, func) { - var ret; - - if (condition) - exec(callback); - else - exec(func, callback); - - return ret; - }; - - /** - * exec function if it exist in object - * - * @param obj - * @param name - * @param arg - */ - exec.ifExist = function(obj, name, arg) { - var ret, - func = obj && obj[name]; - - if (func) - func = func.apply(obj, arg); - - return ret; - }; - - exec.parallel = function(funcs, callback) { - var keys = [], - callbackWas = false, - arr = [], - obj = {}, - count = 0, - countFuncs = 0, - type = Util.type(funcs); - - Util.check(arguments, ['funcs', 'callback']); - - switch(type) { - case 'array': - countFuncs = funcs.length; - - funcs.forEach(function(func, num) { - exec(func, function() { - checkFunc(num, arguments, arr); - }); - }); - break; - - case 'object': - keys = Object.keys(funcs); - countFuncs = keys.length; - - keys.forEach(function(name) { - var func = funcs[name]; - - exec(func, function() { - checkFunc(name, arguments, obj); - }); - }); - break; - } - - function checkFunc(num, data, all) { - var args = [].slice.call(data, 1), - isLast = false, - error = data[0], - length = args.length; - - ++count; - - isLast = count === countFuncs; - - if (!error) - if (length >= 2) - all[num] = args; - else - all[num] = args[0]; - - if (!callbackWas && (error || isLast)) { - callbackWas = true; - - if (type === 'array') - callback.apply(null, [error].concat(all)); - else - callback(error, all); - } - } - }; - - /** - * load functions thrue callbacks one-by-one - * @param funcs {Array} - array of functions - */ - exec.series = function(funcs, callback) { - var fn, - i = funcs.length, - check = function(error) { - var done; - - --i; - - if (!i || error) { - done = true; - exec(callback, error); - } - - return done; - }; - - if (!Array.isArray(funcs)) - throw(Error('funcs should be array!')); - - fn = funcs.shift(); - - exec(fn, function(error) { - if (!check(error)) - exec.series(funcs, callback); - }); - }; - - exec.each = function(array, iterator, callback) { - var listeners = array.map(function(item) { - return iterator.bind(null, item); - }); - - if (!listeners.length) - callback(); - else - exec.parallel(listeners, callback); - }; - - exec.eachSeries = function(array, iterator, callback) { - var listeners = array.map(function(item) { - return iterator.bind(null, item); - }); - - if (typeof callback !== 'function') - throw Error('callback should be function'); - - if (!listeners.length) - callback(); - else - exec.series(listeners, callback); - }; - - /** - * function execute param function in - * try...catch block - * - * @param callback - */ - exec.try = function(callback) { - var ret; - try { - ret = callback(); - } catch(error) { - ret = error; - } - - return ret; - }; - - return exec; - } + this.exec = exec; /** * function gets file extension diff --git a/modules/execon/.bower.json b/modules/execon/.bower.json index 08532c32..1f2b1ef2 100644 --- a/modules/execon/.bower.json +++ b/modules/execon/.bower.json @@ -1,5 +1,6 @@ { "name": "execon", + "version": "1.2.8", "homepage": "https://github.com/coderaiser/execon", "authors": [ "coderaiser " @@ -21,14 +22,13 @@ "test", "tests" ], - "version": "1.2.5", - "_release": "1.2.5", + "_release": "1.2.8", "_resolution": { "type": "version", - "tag": "v1.2.5", - "commit": "ea4e9ed95a445e84298ed670be59e42ce6fbe678" + "tag": "v1.2.8", + "commit": "7e8900c48fd9262a3a3acaa1c8b6c34ff17aa745" }, "_source": "git://github.com/coderaiser/execon.git", - "_target": "~1.2.2", + "_target": "~1.2.7", "_originalSource": "execon" } \ No newline at end of file diff --git a/modules/execon/ChangeLog b/modules/execon/ChangeLog index 09e254e6..73399e13 100644 --- a/modules/execon/ChangeLog +++ b/modules/execon/ChangeLog @@ -1,3 +1,25 @@ +2015.10.21, v1.2.8 + +fix: +- (exec) with do not return value + +feature: +- (travis) add +- (gitignore) add + + +2015.10.01, v1.2.7 + +fix: +- (exec) with: concat arguments + + +2015.10.01, v1.2.6 + +feature: +- (exec) with: bind.apply -> function apply + + 2015.07.21, v1.2.5 fix: diff --git a/modules/execon/README.md b/modules/execon/README.md index d6ffce9c..661484b4 100644 --- a/modules/execon/README.md +++ b/modules/execon/README.md @@ -1,4 +1,5 @@ -# Execon [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] +# Execon [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] [![BuildStatusIMGURL]][BuildStatusURL] + Patterns of function calls. ## Install @@ -149,4 +150,5 @@ MIT [BuildStatusURL]: https://travis-ci.org/coderaiser/execon "Build Status" [DependencyStatusURL]: https://gemnasium.com/coderaiser/execon "Dependency Status" [LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License" - +[BuildStatusIMGURL]: https://img.shields.io/travis/coderaiser/execon/master.svg?style=flat +[BuildStatusURL]: https://travis-ci.org/coderaiser/execon "Build Status" diff --git a/modules/execon/bower.json b/modules/execon/bower.json index ea77632a..d3eae338 100644 --- a/modules/execon/bower.json +++ b/modules/execon/bower.json @@ -1,5 +1,6 @@ { "name": "execon", + "version": "1.2.8", "homepage": "https://github.com/coderaiser/execon", "authors": [ "coderaiser " @@ -20,6 +21,5 @@ "bower_components", "test", "tests" - ], - "version": "1.2.5" + ] } diff --git a/modules/execon/lib/exec.js b/modules/execon/lib/exec.js index f3533bc8..829abdca 100644 --- a/modules/execon/lib/exec.js +++ b/modules/execon/lib/exec.js @@ -29,14 +29,16 @@ /* * return function that calls callback with arguments */ - exec.with = function(callback) { - var args = [].slice.call(arguments), - bind = Function.prototype.bind; + var slice = Array.prototype.slice, + args = slice.call(arguments, 1); - args[0] = null; - - return bind.apply(callback, args); + return function() { + var array = slice.call(arguments), + all = args.concat(array); + + return callback.apply(null, all); + }; }; /** @@ -98,10 +100,10 @@ type = getType(funcs); if (!funcs) - throw(Error('funcs' + ERROR)); + throw Error('funcs' + ERROR); if (!callback) - throw(Error('callback' + ERROR)); + throw Error('callback' + ERROR); switch(type) { case 'array': @@ -109,7 +111,7 @@ funcs.forEach(function(func, num) { exec(func, function() { - checkFunc(num, arguments, arr); + checkFunc(num, arguments); }); }); break; @@ -128,7 +130,7 @@ break; } - function checkFunc(num, data, all) { + function checkFunc(num, data) { var args = slice.call(data, 1), isLast = false, error = data[0], @@ -140,17 +142,17 @@ if (!error) if (length >= 2) - all[num] = args; + arr[num] = args; else - all[num] = args[0]; + arr[num] = args[0]; if (!callbackWas && (error || isLast)) { callbackWas = true; if (type === 'array') - callback.apply(null, [error].concat(all)); + callback.apply(null, [error].concat(arr)); else - callback(error, all); + callback(error, arr); } } }; @@ -176,7 +178,7 @@ }; if (!Array.isArray(funcs)) - throw(Error('funcs should be array!')); + throw Error('funcs should be array!'); fn = funcs.shift(); diff --git a/modules/execon/package.json b/modules/execon/package.json index 10e26a63..1ea0580f 100644 --- a/modules/execon/package.json +++ b/modules/execon/package.json @@ -1,9 +1,12 @@ { "name": "execon", - "version": "1.2.5", + "version": "1.2.8", "author": "coderaiser (https://github.com/coderaiser)", "description": "Patterns of function calls", "homepage": "http://github.com/coderaiser/execon", + "scripts": { + "test": "tape test/*.js" + }, "repository": { "type": "git", "url": "git://github.com/coderaiser/execon.git" @@ -13,5 +16,8 @@ "engines": { "node": ">=0.8" }, - "main": "lib/exec.js" + "main": "lib/exec.js", + "devDependencies": { + "tape": "~4.2.1" + } } diff --git a/test/lib/util.js b/test/lib/util.js index ae5635d0..7e760c76 100644 --- a/test/lib/util.js +++ b/test/lib/util.js @@ -25,62 +25,6 @@ should(ext).eql(EXT); }); }); - - describe('exec', function() { - it('should execute function with parameters', function() { - var WORD = 'hello', - func = function(word) { - return word; - }, - word = Util.exec(func, WORD); - - WORD.should.equal(word); - }); - - it('should not execute function, if type of first argument not function', function() { - var WORD = 'hello', - word = Util.exec(WORD); - - (word === undefined).should.be.true; - }); - }); - - describe('exec.ret', function() { - it('should return function that try to call callback', function() { - var STR = 'hello world', - func1 = function() { - var args = [].slice.call(arguments); - - return args.join(' '); - }, - func2 = Util.exec.ret(func1, 'hello'), - str = func2('world'); - - str.should.be.equal(STR); - }); - - }); - - describe('exec.parallel', function() { - it('should execute a couple functions async and return results in callback', function() { - var WORD = 'hello world', - funcSlow = function(callback) { - setTimeout(function() { - callback(null, 'hello'); - }, 10); - }, - funcFast = function(callback) { - setTimeout(function() { - callback(null, 'world'); - }, 1); - }; - - Util.exec.parallel([funcSlow, funcFast], function(error, hello, world) { - WORD.should.equal(hello + ' ' + world); - }); - }); - - }); }); })();