@gik/tools
v0.2.5
Published
A [tools suite](https://github.com/search?q=tools+user%3Agikmx) for Node / Javascript development.
Downloads
3
Readme
@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
member
Determine if given path is accessible. - fromStat
member
Determine statistics about a file system node. - fromSpawn
member
Spawn a shell command. - fromDirMake
member
Creates a directory. - fromDirRequire
member
Requires a directory path, if the directory does not exists, it's created. - fromDirRead
member
Get path of nodes in given directory (non recursively). - fromDirReadRecursive
member
Get path of nodes in given directory (recursively). - fromFileRead
member
Reads a file from the disk. - fromFileWrite
member
Writes 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
member
Determine if an element is an object and has no keys - string
member
Determines ifvalue
is really a string. - number
member
Determines ifvalue
is really a number. - array
member
Determines ifvalue
is really an array. - function
member
Determines ifvalue
is really a function. - regexp
member
Determines ifvalue
is really a regexp. - boolean
member
Determines ifvalue
is really a boolean. - object
member
Determines ifvalue
is really an object.
- objectEmpty
- props
function
Validates 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
error
will 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
CheckerPropParamError
when invalid parameters are passed.CheckerPropDefError
when a type definition is invalid.CheckerPropDefTypeError
when a type defintiion is not supported.CheckerPropReqError
when a required property is not found.CheckerPropTypeError
when 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 }