ninjs-lodash
Super util library that exports extended Lodash object (_) via _.mixin({...Super Util Methods....})
Warning: The design of this module goes against standard coding practices. However, we are lazy, tired of import statements, and desire one core dependency module rather than thousands, hundreds of thousands, millions of module dependencies (each with overhead)
Overview
This module includes common utility methods that fall into the following groupings:
- JavaScript Types -> String, Object, Array, Function, Number, Regx, Boolean, Date ....
- Child Processes
- Configuration/Settings -> Env, User, Local, Global, .Folders, .Files
- Classes
- OS
- Paths
- Urls
- Debug
- File System
- Async
- Terminal/CLI Helpers
This is the least dependent module (or most depended upon) module in the ninjs-* namespace.
The purpose of packaging all this functionality into one module was to make it easier/faster to create new modules and reduce code duplication/bloat
We tried to keep it as tiny as possible with as few dependencies as possible.
Setup
Install
Install the module locally and add to ./package.json dependencies
npm install --save ninjs-lodash
Import
import 'ninjs-lodash' (instead of 'lodash')
Now you can use existing lodash utils AND/OR ninjs mixins
const _ = // ~~~~~ original lodash ~~~~~_ // ~~~~~ ninjs-lodash mixins ~~~~~_async _ // prints path of dir containing package.json
$ettings
Override/Add your own $ettings
- Create a ./.ninjs/settings.js(json) file within a module cwd that exports an object
- You can now access that object (and nested props) via _.$()
- internally uses _.get(settings, 'keypath'))
- *Be careful not to override _.$() default settings
_
Scripts/Debugging
Be sure to add '--' before args/switches when running cli scripts
Otherwise switches will not be passed to the spawned process created by 'npm run script' command
npm run cli -- settings --key=paths
$ettings - Global Settings Cache
Override/Add your own $ettings
- Create a ./.ninjs/settings.js(json) file within a module cwd that exports an object
- You can now access that object (and nested props) via _.$()
- internally uses _.get(settings, 'keypath'))
- *Be careful not to override _.$() default settings
_.$('keypath.to.list[3]')
CLI Helper - _.cli(map)
Maps a terminal command to a Command Object and invokes its 'handler'
Command Object
// command default options, args, switches "options": key: '' // command handler // options = _.merge(options, 'interpreted' options from command line args/switches) { /* Do Something -> run a command, invoke an api, .... */ } // command help (--help) "text": "This is printed when jav.key === settings && jav.isHelp" // command detailed help (--help -l) "body": "This is printed when jav.key === settings && jav.isHelp && jav.isLong.. If no body set -> obj.text is used instead"
Command Map
"ams": "module": "publish": "options": "name": "" { return lib } { return err ? _ : _ }
Example -> Invoke api.module.publish(options, cb) and print result
*** This is just an example api.module.publish does not exist ***
./package.json
./bin/cli.js
exports = moduleexports = // Command Map "ams": "module": "publish": "options": "name": "" { return lib } { return err ? _ : _ } "text": "--help text" "body": "--help -l long text" _ // Run Interpreter
Terminal . (Local) -> npm run cli -- [keypath.something.method] --switches --k=v ...
> npm run cli -- api.module.publish --name=ninjs-lodash
Terminal * (Global) -> %cli-alias% keypath.something.method --switches --k=v ...
> ninjs-lodash api.module.publish --name=ninjs-lodash
NOTE: args/switches after '--' proxied to spawned npm run-script process
String
_ // returns formatted byte string
Path
_path* // require('path') + extras_pathnamesrc_pathdirsrc_path_path
Url String
_url* // require('url') + extras
Plain Object
_ // _.assign with no mutation_ // _.merge with no mutation_ // deep merge
Array Object
_ // returns flattened array of args, args, string, ....
Async
_async* // require('async')_ // callback(err)_ // callback(null, result)_ // callback || function noop() {}
File System
_*fs // common fs-extra assigned to __fs* // _.fs = require('fs-extra')