Comparing version 1.0.4 to 1.0.5
@@ -9,6 +9,6 @@ { | ||
"dependencies": { | ||
"trailpack-core": "^1.0.0-beta-3", | ||
"trailpack-repl": "^1.0.0-beta-3", | ||
"trailpack-router": "^1.0.0-beta-4", | ||
"trails": "^1.0.0-beta-1", | ||
"trailpack-core": "^1.0.0", | ||
"trailpack-repl": "^1.0.8", | ||
"trailpack-router": "^1.0.2", | ||
"trails": "^1.0.4", | ||
"trails-controller": "^1.0.0-beta-2", | ||
@@ -21,5 +21,5 @@ "trails-model": "^1.0.0-beta-2", | ||
"devDependencies": { | ||
"eslint": "^2.8", | ||
"eslint": "^2.11", | ||
"eslint-config-trails": "latest", | ||
"mocha": "^2.4", | ||
"mocha": "^2.5", | ||
"supertest": "^1.2" | ||
@@ -26,0 +26,0 @@ }, |
29
index.js
@@ -164,2 +164,21 @@ /*eslint no-console: 0 */ | ||
/** | ||
* Extend the once emiter reader for accept multi valid events | ||
*/ | ||
onceAny (events, handler) { | ||
if (!events) | ||
return | ||
if (!(events instanceof Array)) | ||
events = [events] | ||
const cb = (e) => { | ||
this.removeListener(e, cb) | ||
handler.apply(this, Array.prototype.slice.call(arguments, 0)) | ||
} | ||
events.forEach(e => { | ||
this.addListener(e, cb) | ||
}) | ||
} | ||
/** | ||
* Resolve Promise once all events in the list have emitted | ||
@@ -172,5 +191,11 @@ * @return Promise | ||
} | ||
return Promise.all(events.map(eventName => { | ||
return new Promise(resolve => this.once(eventName, resolve)) | ||
return new Promise(resolve => { | ||
if (eventName instanceof Array){ | ||
this.onceAny(eventName, resolve) | ||
} | ||
else { | ||
this.once(eventName, resolve) | ||
} | ||
}) | ||
})) | ||
@@ -177,0 +202,0 @@ } |
@@ -10,2 +10,28 @@ /*eslint no-console: 0 */ | ||
/** | ||
* Detect if the item is a Object. | ||
*/ | ||
isObject (item) { | ||
return (item && typeof item === 'object' && | ||
!Array.isArray(item) && item !== null) | ||
}, | ||
/** | ||
* Deep Merge Objects | ||
*/ | ||
deepMerge (target, source) { | ||
if (this.isObject(target) && this.isObject(source)) { | ||
Object.keys(source).forEach(key => { | ||
if (this.isObject(source[key])) { | ||
if (!target[key]) Object.assign(target, { [key]: {} }) | ||
this.deepMerge(target[key], source[key]) | ||
} | ||
else { | ||
Object.assign(target, { [key]: source[key] }) | ||
} | ||
}) | ||
} | ||
return target | ||
}, | ||
/** | ||
* Copy and merge the provided configuration into a new object, decorated with | ||
@@ -30,7 +56,12 @@ * necessary default and environment-specific values. | ||
} | ||
const envConfig = (config.env && config.env[process.env.NODE_ENV]) || { } | ||
// merge config.env | ||
config = this.deepMerge( | ||
(config || { }), | ||
((config.env && config.env[process.env.NODE_ENV]) || { }) | ||
) | ||
// merge config.main.paths | ||
Object.assign( | ||
configTemplate.main.paths, (config.main || { }).paths, (envConfig.main || { }).paths | ||
configTemplate.main.paths, (config.main || { }).paths | ||
) | ||
@@ -40,10 +71,7 @@ | ||
Object.assign( | ||
configTemplate.main.timeouts, (config.main || { }).timeouts, (envConfig.main || { }).timeouts | ||
configTemplate.main.timeouts, (config.main || { }).timeouts | ||
) | ||
// override config.main.packs with env config | ||
configTemplate.main.packs = (envConfig.main || { }).packs || (config.main || { }).packs || [ ] | ||
// merge application log | ||
Object.assign(configTemplate.log, config.log, envConfig.log) | ||
Object.assign(configTemplate.log, config.log) | ||
@@ -50,0 +78,0 @@ // merge remaining environment-specific config properties |
{ | ||
"name": "trails", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Modern Web Application Framework for Node.js", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -210,4 +210,11 @@ 'use strict' | ||
}) | ||
it('should invoke listener when listening for multiple possible events', () => { | ||
const eventPromise = app.after([['test1', 'test2'], 'test3']) | ||
app.emit('test1') | ||
app.emit('test3') | ||
return eventPromise | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -21,2 +21,10 @@ 'use strict' | ||
extraneous: 'assigned' | ||
}, | ||
customObject: { | ||
string: 'b', | ||
int: 2, | ||
array: [2, 3, 4], | ||
subobj: { | ||
attr: 'b' | ||
} | ||
} | ||
@@ -32,2 +40,8 @@ }, | ||
extraneous: 'assigned' | ||
}, | ||
customObject: { | ||
subobj: { | ||
attr: 'b' | ||
}, | ||
int2: 2 | ||
} | ||
@@ -59,2 +73,10 @@ }, | ||
normal: 'yes' | ||
}, | ||
customObject: { | ||
string: 'a', | ||
int: 1, | ||
array: [1, 2, 3], | ||
subobj: { | ||
attr: 'a' | ||
} | ||
} | ||
@@ -118,2 +140,45 @@ } | ||
it('should merge full custom env config', () => { | ||
process.env.NODE_ENV = 'envTest1' | ||
const config = lib.Trails.buildConfig(testConfig) | ||
assert(config) | ||
assert(typeof config.customObject === 'object') | ||
assert.equal(config.customObject.string, 'b') | ||
assert.equal(config.customObject.int, 2) | ||
assert(Array.isArray(config.customObject.array)) | ||
assert.equal(config.customObject.array[0], 2) | ||
assert(typeof config.customObject.subobj === 'object') | ||
assert.equal(config.customObject.subobj.attr, 'b') | ||
}) | ||
it('should merge partial custom env config', () => { | ||
process.env.NODE_ENV = 'envTest2' | ||
const config = lib.Trails.buildConfig(testConfig) | ||
assert(config) | ||
assert(typeof config.customObject === 'object') | ||
assert.equal(config.customObject.string, 'a') | ||
assert.equal(config.customObject.int, 1) | ||
assert(Array.isArray(config.customObject.array)) | ||
assert.equal(config.customObject.array[0], 1) | ||
assert(typeof config.customObject.subobj === 'object') | ||
assert.equal(config.customObject.subobj.attr, 'b') | ||
}) | ||
it('should merge new custom attr in env config', () => { | ||
process.env.NODE_ENV = 'envTest2' | ||
const config = lib.Trails.buildConfig(testConfig) | ||
assert(config) | ||
assert(typeof config.customObject === 'object') | ||
assert.equal(config.customObject.string, 'a') | ||
assert.equal(config.customObject.int, 1) | ||
assert(Array.isArray(config.customObject.array)) | ||
assert.equal(config.customObject.array[0], 1) | ||
assert(typeof config.customObject.subobj === 'object') | ||
assert.equal(config.customObject.subobj.attr, 'b') | ||
assert.equal(config.customObject.int2, 2) | ||
}) | ||
it('should not override any configs if NODE_ENV matches no env', () => { | ||
@@ -126,2 +191,3 @@ process.env.NODE_ENV = 'notconfigured' | ||
assert.equal(config.log.normal, 'yes') | ||
assert.equal(config.customObject, testConfig.customObject) | ||
assert(!config.log.extraneous) | ||
@@ -128,0 +194,0 @@ assert(config.env) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82884
1944
17