npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

clean

v4.0.2

Published

clean parses and sanitize argv for node, supporting fully extendable types, shorthands, validatiors and setters.

Downloads

121,019

Readme

clean NPM version Build Status Dependency Status

Clean is small but powerful node.js module that parses and sanitize argv for node, supporting:

  • fully extendable types
  • shorthands
  • validatiors
  • setters

Installation and Usage

npm install clean --save
var clean = require('clean');

Usage: clean(options)

Argv Shorthands

We can define shorthands with the option options.shorthands.

var shorthands = {
	c: 'cwd',
	n: 'no-recursive'
};

clean({
	shorthands: shorthands
}).argv(['node', 'xxx', '-c', 'abc', '-n']);
// The result is:
// {
//		cwd: 'abc',
//		recursive: false
// }

Types

clean({
	schema: {
		cwd: {
			type: require('path')
		},
		
		retry: {
			type: Boolean
		}
	}
}).parse(
	['node', 'xxx', '--cwd', 'abc', '--retry', 'false'], 
	function(err, results){
		console.log(results.cwd); // the `path.resolved()`d 'abc'
		console.log(results.retry === false); // is a boolean, not a string
	}
);

Validators and Setters

Validators and setters of clean is implemented by [checker](https://github.com/kaelzhang/node-checker), check the apis of checker for details.

You could check out the demo located at "example/clean.js". That is a very complicated situation of usage.

node example/clean.js --username guest

Programatical Details

constructor: clean(options)

  • options Object=
    • schema Object schema to define the argv
    • offset Number= The offset from which the parser should start to parse. Optional. Default to 2.
    • shorthands Object= The schema used to clean the given object or the parsred argv
    • parallel Boolean=false whether should check the argv in parallel, default to false

options.schema

name: {
  // If `required == true` and `--name` is not specified in argv, there will be an error
  required: true,
  validate: function(value){
    return /[a-z]/i.test(value);
  },
  set: function(value){
    return value.replace(/^[a-z]/, function(m){
      return m.toUpperCase();
    });
  }
}
  • required Boolean
  • default * if required is true, this property will be ignored.
  • validate function(v, is_default)|Array function for validation, or array of functions
  • set function(v, is_default)|Array setter function, or array of functions.

There are three methods available for this object of validator and setter.

  • this.async() returns function done, and turns the current validator or setter into an asynchronous method. We can also use done to define better error messages.
  • this.get(key) could fetch the value of other properties.
  • this.set(key, value) could set values of other properties.

.argv(argv)

  • argv Array process.argv or something like that.

Parses the argument vector, without cleaning the data.

Returns Object The parsed object with shorthand rules applied.

.clean(data, callback)

  • data Object The given data.
  • callback function(err, results)

Cleans the given data according to the schema.

.parse(argv, callback)

Parses argument vector (argv) or something like process.argv, and cleans the parsed data according to the schema.

This method is equivalent to c.clean(c.argv(argv), callback).

Advanced Section

.registerType(type, typeDef)

Registers a custom type.