Generally useful utilities I tend to use in most of my projects.
This module is a drop-in replacement for node's util
module as it includes all of its functions plus several pre-required modules, and a few generally useful custom functions.
Preloaded standard node modules:
- assert
- buffer
- events
- EventEmitter (alias,shortcut)
- fs
- path
- stream
- url
Note: There is no overhead loading node's intrinsic modules, so why not?
Pre-required modules:
Custom functions (see below):
- defineCustomError
- debug
- unload
As well as these functions from node's standard util
module:
- debuglog
- format
- inherits
- inspect
Lodash is extended with:
-
isNonEmptyString(obj)
- shorthand for
_.isString(obj) && !_.isEmpty(obj)
- shorthand for
-
isNonEmptyArray(obj)
- shorthand for
_.isArray(obj) && !_.isEmpty(obj)
- shorthand for
-
isNonEmptyObject(obj)
- shorthand for
_.isPlainObject(obj) && !_.isEmpty(obj)
- shorthand for
-
setProps(target,...list) - sets properties from props on target, as in:
- for each props in list
- for each key in props
- delete target[key]
- target['_'+key] = props[key]
- adds new 'key' getter and setter to target
- for each key in props
- return target
- for each props in list
For example:
const defaults={
b: false
};
class Foo {
constructor(opts) {
_.setProps(this,defaults,opts);
}
}
let f = new Foo({ a:1, isFoo:true });
f.a // returns 1
f.a=32
f.a // returns 32
stringify(f) // returns '{"_b":false,"_a":32,"_isFoo":true}'
-
defineCustomError(name, fmt=defineCustomError.FORMAT)
- Factory function used to create new error classes
defineCustomError.FORMAT
- default output format:${msg}\n\t${stack}
- Factory function used to create new error classes
-
debug(...args)
- Provides simple debugging output
- args are the same as
util.format
debug.DEBUGGING
if true, debug messages are printed to console.logdebug.DATEMASK
- default date format:hhMMss.l
- see dateformat
- args are the same as
- Provides simple debugging output
-
unload(path)
- Allows required modules to be reloaded - EXPERIMENTAL