$ npm install ndtk
Returns the value assigned to process.env.NODE_ENV
when defined, otherwise "development".
Returns true
if current environment is set to "development".
Returns true
if current environment is set to "staging".
Returns true
if current environment is set to "test".
Returns true
if current environment is set to "production".
Returns true
if current environment is set to "qa".
Executes simple assertions and throw errors in negative cases.
condition
: The condition to be tested.content
: String or Error object to be thrown when a condition is not met.
const a = 1
const b = 2
assert(a === b, 'a and b are not equal.') // Throws an Error with message 'a and b are not equal.'
assert(a === b) // Throws an Error with a default message 'Something went wrong.'
assert(a === b, new Error('Oops')) // Throws the same Error with message 'Oops'
Returns the current caller directory.
// src/lib/lib.js
export const lib = (path) => {
const callerDir = path || ccd()
return callerDir
}
// src/app.js
import lib from '../lib/lib'
lib() // Returns /abs/path/to/src"
lib(__dirname) // Returns /abs/path/to/src"
Require optional modules without throwing unwanted errors. Intended to work only for local files.
name
: String representing the path to the module you are trying to require.defaultOnly
: Boolean. Defaults totrue
. Defines if you wish to return only thedefault
property or the whole module including named properties.
// src/lib.js
export const a = 'Foo'
export default 'Bar'
// src/app.js
req('./lib') // Returns 'Bar'
req('./lib', false) // Returns the object { default: 'Bar', a: 'Foo' }
req('./libb') // Returns null
Returns HTTP-friendly Error objects.
code
: String or number representing the status code. Invalid or not found error codes will fallback to500
.meta
: Object with extra information regarding the error. Possible options aremessage
,type
andattributes
.
httpError()
// Returns:
// Error Object {
// stack...,
// isHttp: true,
// message: 'Internal Server Error'
// output: {
// message: 'Internal Server Error',
// type: 'INTERNAL_SERVER_ERROR',
// status: 500,
// }
// }
httpError(400)
// Returns:
// Error Object {
// stack...,
// isHttp: true,
// message: 'Bad Request'
// output: {
// message: 'Bad Request',
// type: 'BAD_REQUEST',
// status: 400,
// }
// }
httpError(422, {
message: 'Something is wrong with this validation',
type: 'VALIDATION',
attributes: {
email: 'Email is invalid',
name: 'Name is required'
},
})
// Returns:
// Error Object {
// stack...,
// isHttp: true,
// message: 'Something is wrong with this validation'
// output: {
// message: 'Something is wrong with this validation',
// type: 'VALIDATION',
// attributes: {
// email: 'Email is invalid',
// name: 'Name is required'
// },
// status: 422,
// }
// }
Verifies if current path is a valid directory without throwing unwanted exceptions. Returns a boolean
.
dir
: String representing the path of the tested directory.
isDir(__dirname) // Return true
isDir('../') // Return true
isDir('bad-path') // Return false
Generates configuration objects based on the current environment.
object
: Raw object to be merged.defaults
: Optional fallback object.
const object = {
default: {
foo: true,
bar: { enabled: true },
},
development: {
bar: { enabled: false, name: 'dev' },
},
test: {
foo: false,
testOnly: true,
bar: { name: 'test' },
},
}
const defaults {
default: {
required: [],
},
test: {
bar: { description: 'test description' },
}
}
// On "development" environment
config(object)
// Returns:
// {
// foo: true,
// bar: { enabled: false, name: 'dev' }
// }
// On "test" environment
config(object)
// Returns:
// {
// foo: false,
// testOnly: true,
// bar: { enabled: true, name: 'test' }
// }
config(object, defaults)
// Returns:
// {
// foo: false,
// required: [],
// testOnly: true,
// bar: { enabled: true, name: 'test', description: 'test description' }
// }
The MIT License (MIT)
Copyright (c) 2018 Hugo W. - contact@hugw.io
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.