@gik/tools v0.2.5
@gik/tools 0.2.5
A tools suite for Node / Javascript development.
Contributors
Supported platforms
- darwin
- linux
Table of contents
- thrower Errors with pretty stack and customizable name.
- streamer An utility belt for our most common operations with RXJS's Observables.
- fromAccess
memberDetermine if given path is accessible. - fromStat
memberDetermine statistics about a file system node. - fromSpawn
memberSpawn a shell command. - fromDirMake
memberCreates a directory. - fromDirRequire
memberRequires a directory path, if the directory does not exists, it's created. - fromDirRead
memberGet path of nodes in given directory (non recursively). - fromDirReadRecursive
memberGet path of nodes in given directory (recursively). - fromFileRead
memberReads a file from the disk. - fromFileWrite
memberWrites a file on the disk.
- fromAccess
- server A minimal webserver using RxJS Observables.
- populator Allows properties in an object to inherit values from sibling properties.
- mapper Generates a flattened object containing a map for all the properties
- logger A wrapper around pino.
- checker A minimal type-checker for JavaScript purists.
- is Determine if given value really belongs to the corresponding type.
- objectEmpty
memberDetermine if an element is an object and has no keys - string
memberDetermines ifvalueis really a string. - number
memberDetermines ifvalueis really a number. - array
memberDetermines ifvalueis really an array. - function
memberDetermines ifvalueis really a function. - regexp
memberDetermines ifvalueis really a regexp. - boolean
memberDetermines ifvalueis really a boolean. - object
memberDetermines ifvalueis really an object.
- objectEmpty
- props
functionValidates properties of given object.
- is Determine if given value really belongs to the corresponding type.
thrower
Errors with pretty stack and customizable name.
Parameters
Returns
Error - A custom error instance with a pretty stack.
Example
Thrower('test'); // A standard Error with prettified stack
Thrower(new TypeError('test2')); // Standard TypeError with prettified stack
Thrower('test3', 'TestError'); // Custom TestError with 'test3' as message
Thrower(['hola %s', 'mundo'], 'HelloError'); // HelloError with 'hola mundo' as message
const Err = Thrower('bad boy', 'CanineError', false); // Returns CanineError instance.streamer
An utility belt for our most common operations with RXJS's Observables.
To do
- Add unit tests for all operators.
Members
- fromAccess
- fromStat
- fromSpawn
- fromDirMake
- fromDirRequire
- fromDirRead
- fromDirReadRecursive
- fromFileRead
- fromFileWrite
fromAccess
static property of
streamer
Determine if given path is accessible.
Parameters
Returns
StreamBoolean - Wether the file is accessible or not.
fromStat
static property of
streamer
Determine statistics about a file system node.
Parameters
Returns
StreamStat - stat object for the node.
Throws
Error- When given an invalid node.
fromSpawn
static property of
streamer
Spawn a shell command.
Parameters
Returns
StreamOutput - Each chunk of either stdout or stderr data.
fromDirMake
static property of
streamer
Creates a directory.
Parameters
Returns
StreamString - The path of the directory that was just created.
Throws
Error- When directory cannot be created.
fromDirRequire
static property of
streamer
Requires a directory path, if the directory does not exists, it's created.
Parameters
Returns
Array.<StreamString> - The path of the directory.
Throws
Error- When requested path exists and is not a directory.
fromDirRead
static property of
streamer
Get path of nodes in given directory (non recursively).
Parameters
Returns
Array.<StreamDirNode> - The path of the directory.
Throws
Error- When requested path exists and is not a directory.
fromDirReadRecursive
static property of
streamer
Get path of nodes in given directory (recursively).
Parameters
Returns
StreamPath - The path of the directory.
Throws
Error- When requested path exists and is not a directory.
fromFileRead
static property of
streamer
Reads a file from the disk.
Parameters
Returns
Observable.<string> - The contents of the file.
fromFileWrite
static property of
streamer
Writes a file on the disk.
Parameters
Returns
Observable.<string> - The future value true if write was succesful.
Throws
Error- When the file cannot be written.
server
A minimal webserver using RxJS Observables.
Parameters
Returns
Subscription Observable - if subscription mode was enabled
a subscription would be returned, otherwise an observable is returned.
populator
Allows properties in an object to inherit values from sibling properties. This specially useful when creating JSON configuration files.
Parameters
Returns
Object - An object copy with references replaced.
Example
const subject = {
a: { b: { c: 'world' } },
d: "hello ${a.b.c}${e}",
e: "!!!",
f: ["${e}", "${a.b.c}"]
};
const result = Populator(subject);
// result:
// { a: { b: { c: 'world' } }, d: "hello world!!!", e: "!!!", f: ["!!!", "world"] };mapper
Generates a flattened object containing a map for all the properties
available on subject.
Parameters
Returns
mapperResult - A flattened object.
Throws
mapperTypeError
Example
const subject = {
a: {
b: {
d: true,
e: {
g: 'foo'
}
},
f: undefined,
}
};
const result = Mapper(subject);
// result:
// { 'a.b.d': true, 'a.b.e.g': 'foo', 'a.f': undefined }logger
A wrapper around pino.
Behaviour
When the environment is non-production it will output prettier logs.
All calls to
errorwill use @gik/tools-thrower halting the execution when the environment is non-production (ie: development) however, when in production, it will fallback to Pino's default logger.When the environment is set as production it will load extreme-mode adding an even faster approach to logging. (make sure to read the documentation about the caveats)
Parameters
Returns
Types.Instance - A function that you can use for logging.
Throws
Types.ParamTypeError- When parameters are not valid.
checker
A minimal type-checker for JavaScript purists.
Members
is
Determine if given value really belongs to the corresponding type.
Members
objectEmpty
static property of
checker.is
Determine if an element is an object and has no keys
Parameters
Returns
boolean - Whether the object is empty or not.
string
static property of
checker.is
Determines if value is really a string.
Parameters
Returns
boolean - Wheter value is string or not.
number
static property of
checker.is
Determines if value is really a number.
Parameters
Returns
boolean - Wheter value is number or not.
array
static property of
checker.is
Determines if value is really an array.
Parameters
Returns
boolean - Wheter value is array or not.
function
static property of
checker.is
Determines if value is really a function.
Parameters
Returns
boolean - Wheter value is function or not.
regexp
static property of
checker.is
Determines if value is really a regexp.
Parameters
Returns
boolean - Wheter value is regexp or not.
boolean
static property of
checker.is
Determines if value is really a boolean.
Parameters
Returns
boolean - Wheter value is boolean or not.
object
static property of
checker.is
Determines if value is really an object.
Parameters
Returns
boolean - Wheter value is object or not.
props
static method of
checker
Validates properties of given object.
Parameters
Returns
Object - The validated subject extended with default values (when applies).
Throws
CheckerPropParamErrorwhen invalid parameters are passed.CheckerPropDefErrorwhen a type definition is invalid.CheckerPropDefTypeErrorwhen a type defintiion is not supported.CheckerPropReqErrorwhen a required property is not found.CheckerPropTypeErrorwhen a property does not match the defintion.
Example
const subject = { a: 1, b: 'hello' z: undefined };
const result = props(subject, {
a: { type:'number', required:true },
b: 'string',
c: { default: new Date() },
d: { required: false, default: null, map: value => [value, true] },
})
// result:
// { a: 1, b: 'hello', c: '1981-06-23 10:06:08', d: [null, true], z: undefined }