From bfcb0754804c487d37866d33d25927824e043a19 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 27 Mar 2018 12:27:33 +0300 Subject: [PATCH] feature(for-each-key) add --- bin/cloudcmd.js | 13 ++++---- client/modules/operation/index.js | 14 ++++----- common/for-each-key.js | 11 +++++++ test/common/for-each-key.spec.js | 49 +++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 common/for-each-key.js create mode 100644 test/common/for-each-key.spec.js diff --git a/bin/cloudcmd.js b/bin/cloudcmd.js index 55305d93..327eb92a 100755 --- a/bin/cloudcmd.js +++ b/bin/cloudcmd.js @@ -204,6 +204,7 @@ function readConfig(name) { const fs = require('fs'); const tryCatch = require('try-catch'); const jju = require('jju'); + const forEachKey = require('../common/for-each-key'); const readjsonSync = (name) => jju.parse(fs.readFileSync(name, 'utf8'), { mode: 'json' @@ -216,23 +217,21 @@ function readConfig(name) { if (error) return exit(error.message); - Object.keys(data).forEach((item) => { - config(item, data[item]); - }); + forEachKey(config, data); } function help() { const bin = require('../json/help'); + const forEachKey = require('../common/for-each-key'); + const currify = require('currify/legacy'); const usage = 'Usage: cloudcmd [options]'; const url = Info.homepage; + const log = currify((a, b, c) => console.log(a, b, c)); console.log(usage); console.log('Options:'); - Object.keys(bin).forEach((name) => { - console.log(' %s %s', name, bin[name]); - }); - + forEachKey(log(' %s %s'), bin); console.log('\nGeneral help using Cloud Commander: <%s>', url); } diff --git a/client/modules/operation/index.js b/client/modules/operation/index.js index 99c4b012..0c14606d 100644 --- a/client/modules/operation/index.js +++ b/client/modules/operation/index.js @@ -14,6 +14,7 @@ CloudCmd.Operation = OperationProto; const currify = require('currify/legacy'); const wraptile = require('wraptile/legacy'); const exec = require('execon'); +const forEachKey = require('../../../common/for-each-key'); const RESTful = require('../../dom/rest'); const removeExtension = require('./remove-extension'); @@ -210,6 +211,9 @@ function OperationProto(operation, data) { let done; let lastError; + const removeListener = emitter.removeListener.bind(emitter); + const on = emitter.on.bind(emitter); + const listeners = { progress: (value) => { done = value === 100; @@ -221,9 +225,7 @@ function OperationProto(operation, data) { .hide() .clearProgress(); - Object.keys(listeners).forEach((name) => { - emitter.removeListener(name, listeners[name]); - }); + forEachKey(removeListener, listeners); if (lastError || done) callback(lastError); @@ -247,11 +249,7 @@ function OperationProto(operation, data) { } }; - const events = Object.keys(listeners); - - events.forEach((name) => { - emitter.on(name, listeners[name]); - }); + forEachKey(on, listeners); } this.hide = () => { diff --git a/common/for-each-key.js b/common/for-each-key.js new file mode 100644 index 00000000..eaae90f2 --- /dev/null +++ b/common/for-each-key.js @@ -0,0 +1,11 @@ +'use strict'; + +const currify = require('currify/legacy'); +const setValue = currify((fn, obj, key) => fn(key, obj[key])); + +module.exports = (fn, obj) => { + Object + .keys(obj) + .forEach(setValue(fn, obj)); +}; + diff --git a/test/common/for-each-key.spec.js b/test/common/for-each-key.spec.js new file mode 100644 index 00000000..3a952e52 --- /dev/null +++ b/test/common/for-each-key.spec.js @@ -0,0 +1,49 @@ +'use strict'; + +const test = require('tape'); +const diff = require('sinon-called-with-diff'); +const sinon = diff(require('sinon')); + +const forEachKey = require('../../common/for-each-key'); + +test('forEachKey: on property', (t) => { + const obj = { + a: 'hello', + }; + + const fn = sinon.stub(); + + forEachKey(fn, obj); + + t.ok(fn.calledWith('a', 'hello'), 'should call fn'); + t.end(); +}); + +test('forEachKey: a couple properties', (t) => { + const obj = { + a: 'hello', + b: 'world', + }; + + const fn = sinon.stub(); + + forEachKey(fn, obj); + + t.ok(fn.calledWith('b', 'world'), 'should call fn'); + t.end(); +}); + +test('forEachKey: count', (t) => { + const obj = { + a: 'hello', + b: 'world', + c: 'some', + }; + + const fn = sinon.stub(); + + forEachKey(fn, obj); + + t.equal(fn.callCount, 3, 'should '); + t.end(); +});