0.0.1 • Published 8 years ago

ninjs-utils v0.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

ninjs-utils

common utility methods

ninjs-core.argv -> minimist wrapper

https://github.com/substack/minimist

parse argument options

This module is the guts of optimist's argument parser without all the fanciful decoration.

browser support

build status

example

var argv = require('minimist')(process.argv.slice(2));
console.dir(argv);
$ node example/parse.js -a beep -b boop
{ _: [], a: 'beep', b: 'boop' }
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{ _: [ 'foo', 'bar', 'baz' ],
  x: 3,
  y: 4,
  n: 5,
  a: true,
  b: true,
  c: true,
  beep: 'boop' }

methods

var parseArgs = require('minimist')

var argv = parseArgs(args, opts={})

Return an argument object argv populated with the array arguments from args.

argv._ contains all the arguments that didn't have an option associated with them.

Numeric-looking arguments will be returned as numbers unless opts.string or opts.boolean is set for that argument name.

Any arguments after '--' will not be parsed and will end up in argv._.

options can be:

  • opts.string - a string or array of strings argument names to always treat as strings
  • opts.boolean - a boolean, string or array of strings to always treat as booleans. if true will treat all double hyphenated arguments without equal signs as boolean (e.g. affects --foo, not -f or --foo=bar)
  • opts.alias - an object mapping string names to strings or arrays of string argument names to use as aliases
  • opts.default - an object mapping string argument names to default values
  • opts.stopEarly - when true, populate argv._ with everything after the first non-option
  • opts['--'] - when true, populate argv._ with everything before the -- and argv['--'] with everything after the --. Here's an example:

    > require('./')('one two three -- four five --six'.split(' '), { '--': true })
    { _: [ 'one', 'two', 'three' ],
      '--': [ 'four', 'five', '--six' ] }

    Note that with opts['--'] set, parsing for arguments still stops after the --.

  • opts.unknown - a function which is invoked with a command line parameter not defined in the opts configuration object. If the function returns false, the unknown option is not added to argv.

ninjs-log

advanced logging using DEBUG env var with advanced typed formating, fs output, middleware mappings, and color mappings

ninja setup (Windows exmaple)

set NODE_ENV=development && set DEBUG="api:twitter"

usage

First, setup your environment variables, NODE_ENV and DEBUG.

NODE_ENV environment var

The NODE_ENV environment var specifies your runtime nodejs env and is used by several npm modules to alter configuration. For example, the 'express' server module changes caching and all sorts of optimations based on this value.

Typical values include : "development", "production" Advanced values include : "test", "staging", "local"

If NODE_ENV === "production", NO log messages will leak to output.

DEBUG environment var

DEBUG environment var specifies what keyed log messages are allowed to leak to the output (ie. stdout or log.txt or log.json) The express server module changes caching and all sorts of optimations based on this value.

Example on Windows:

set NODE_ENV=development
set DEBUG="api:twitter"

or

set NODE_ENV=development && set DEBUG="api:twitter"

process.env

NODE_ENV === "production" No logs leak DEBUG= All logs leak DEBUG=something:blah: Contains 'something:blah:' logs leak DEBUG=something:blah Equals 'something:blah' logs leak

  1. set DEBUG=*
  2. set DEBUG=specific:file

/*

snapp/lodash - lodash wrapper + js base classes utils (string, object, array, funcitons, dates, etc.)

mod: lodash info: Lodash modular utilities. version: 4.13.1 web: https://lodash.com/ git: https://github.com/lodash/lodash npm: https://www.npmjs.com/package/lodash

mod: numeral info: A javascript library for formatting and manipulating numbers. version: 1.5.3 web: http://numeraljs.com/ git: https://github.com/adamwdraper/Numeral-js npm: https://www.npmjs.com/package/numeral

mod: jquery-deferred info: jQuery deferred lib for nodeJS. version: 0.3.0 git: https://github.com/zzdhidden/node-jquery-deferred npm: https://www.npmjs.com/package/jquery-deferred

mod: moment info: Parse, validate, manipulate, and display dates in javascript. version: 2.13.0 web: http://momentjs.com/ git: https://github.com/moment/moment npm: https://www.npmjs.com/package/moment

mod: backbone -> see snapp/backbone

snapp

version: 0.0.1

global utils lib required by all other snapp-* libs snapp nodejs utils lib

drive

typed file system api

drive models

abstract models for reading/writing collections/models to disk (with most file formats)

pretty

mod: pretty info: Some tweaks for beautifying HTML with js-beautify version: 1.0.0 git: https://github.com/jonschlinkert/pretty npm: https://www.npmjs.com/package/pretty

stream

request(url) .on('error', function (err) { callback(err); }) .on('response', function (res) { console.log(res.statusCode) // 200 console.log(res.headers'content-type') // 'image/png' callback(null, res); }) .pipe(disk.createWriteStream(dest));