mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
feature(for-each-key) add
This commit is contained in:
parent
68e9bd813e
commit
bfcb075480
4 changed files with 72 additions and 15 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 = () => {
|
||||
|
|
|
|||
11
common/for-each-key.js
Normal file
11
common/for-each-key.js
Normal file
|
|
@ -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));
|
||||
};
|
||||
|
||||
49
test/common/for-each-key.spec.js
Normal file
49
test/common/for-each-key.spec.js
Normal file
|
|
@ -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();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue