-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0a7477
commit 085efc0
Showing
6 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": [ "es2015" ] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,4 @@ node_modules | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
src | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "ht-utils", | ||
"version": "1.0.0", | ||
"description": "Hudson-Taylor utils", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "semistandard && mocha -R spec -r babel-register --bail", | ||
"build": "babel src -d lib", | ||
"prepublish": "npm run build" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/hudson-taylor/utils.git" | ||
}, | ||
"keywords": [ | ||
"hudson-taylor" | ||
], | ||
"author": "Adam Brady <[email protected]>", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/hudson-taylor/utils/issues" | ||
}, | ||
"homepage": "https://github.com/hudson-taylor/utils#readme", | ||
"devDependencies": { | ||
"babel-cli": "^6.5.1", | ||
"babel-preset-es2015": "^6.5.0", | ||
"babel-register": "^6.5.2", | ||
"mocha": "^2.4.5", | ||
"semistandard": "^7.0.5" | ||
}, | ||
"dependencies": { | ||
"async": "^1.5.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import async from 'async'; | ||
|
||
export function merge () { | ||
// Merge objects passed as arguments, left to right precidence. | ||
|
||
let result = {}; | ||
|
||
for (let i = 0; i < arguments.length; i++) { | ||
let keys = Object.keys(arguments[i]); | ||
for (let k = 0; k < keys.length; k++) { | ||
let key = keys[k]; | ||
if (!result.hasOwnProperty(key)) { | ||
result[key] = arguments[i][key]; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export function formatError (err) { | ||
if (err.error) return err; | ||
if (err instanceof Error) { | ||
err = err.message || err.toString(); | ||
} | ||
return { error: err }; | ||
} | ||
|
||
export function getLastResult (methods, callback, isService) { | ||
async.reduce(methods, undefined, (lastResult, stored, done) => { | ||
let { | ||
service, | ||
method, | ||
data = lastResult | ||
} = stored; | ||
|
||
let call = [ | ||
service, | ||
method, | ||
data, | ||
function (err, result) { | ||
if (err) { | ||
let r = { | ||
error: err, | ||
method | ||
}; | ||
if (!isService) { | ||
r.service = service; | ||
} | ||
return done(r); | ||
} | ||
|
||
if (!isService) { | ||
var index = methods.indexOf(stored); | ||
var next = methods[index + 1]; | ||
|
||
if (next && next.data && !next.data[0].data) { | ||
methods[index + 1].data[0].data = result; | ||
} | ||
} | ||
|
||
return done(null, result); | ||
} | ||
]; | ||
|
||
if (isService) { | ||
call.shift(); | ||
} | ||
|
||
this.call(...call); | ||
}, callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* global describe, it */ | ||
import assert from 'assert'; | ||
|
||
import { | ||
merge, | ||
formatError | ||
} from '../src'; | ||
|
||
describe('Utilities', function () { | ||
describe('merge', function () { | ||
it('should merge', function () { | ||
let data1 = { a: 5 }; | ||
let data2 = { a: 15, b: 10 }; | ||
|
||
let result = merge(data1, data2); | ||
|
||
assert.deepEqual(result, { | ||
a: data1.a, | ||
b: data2.b | ||
}); | ||
}); | ||
}); | ||
|
||
describe('formatError', function () { | ||
it('should return object', function () { | ||
let str = 'err1234'; | ||
|
||
let data = formatError(str); | ||
|
||
assert.deepEqual(data, { | ||
error: str | ||
}); | ||
}); | ||
|
||
it('should convert Error instance to object', function () { | ||
let str = 'err1234'; | ||
|
||
let err = new Error(str); | ||
|
||
let data = formatError(err); | ||
|
||
assert.deepEqual(data, { | ||
error: str | ||
}); | ||
|
||
let err2 = new Error(); | ||
|
||
let data2 = formatError(err2); | ||
|
||
assert.deepEqual(data2, { | ||
error: 'Error' | ||
}); | ||
}); | ||
|
||
it('should not do anything if data has an error variable', function () { | ||
let _data = { | ||
error: 'something' | ||
}; | ||
|
||
let data = formatError(_data); | ||
|
||
assert.deepEqual(data, _data); | ||
}); | ||
}); | ||
}); |