You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

cmd.js

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cmd.js

A chainable utility toolkit for JavaScript.

0.0.3
Source
npmnpm
Version published
Weekly downloads
22
120%
Maintainers
1
Weekly downloads
 
Created
Source

cmd.js

JavaScript Logic Framework

Ever find yourself handling complex data structures in JavaScript? With cmd.js, one can assemble small blocks of logic, and easily pass data through them for processing. Here's a sample task, and how it's made clearer with cmd.js:

The Task

Sort the users by increasing age, and display the name and id of each user.

Data Set

var users = [
    {name: 'John',     id: 1, age: 37},
    {name: 'Kimberly', id: 2, age: 35},
    {name: 'Janine',   id: 3, age: 33},
    {name: 'Justin',   id: 4, age: 31},
];

Vanilla JavaScript

users.sort(function (a, b) {
    return a.age > b.age;
});

users.forEach(function (user) {
    console.log(user.name, user.id);
});

// The output:
// Justin 4
// Janine 3
// Kimberly 2
// John 1

Pretty simple, right?

cmd.js

var pluck = cmd.pluck;
var sortAndPrint = cmd.sort(pluck('age')).
    and.logger(pluck('name'), pluck('id'));

sortAndPrint(users);

// The output:
// Justin 4
// Janine 3
// Kimberly 2
// John 1

The benefits of this style include reusability, clear logical flow, and less code in general. By composing commands you keep your functionality completely isolated from your data.

Developer Notes

This project uses gulp. Make all changes/additions in lib/*.js while running gulp from the command line.

API Reference

Structure of a Command

cmd.name(... args ...)(... vals ...);

Some commands do not accept args, and you are given the command with empty args already provided.

cmd.sum(... vals ...);

Alert

cmd.alert(val1, ...)

nameall or each?accepts args?return value
alerteachno[undefined, ...]

Example

The following example displays two alerts in sequence.

cmd.alert('Hello World!', 'Will Smith here.');

Compare

cmd.compare(val1, val2)

nameall or each?accepts args?return value
compareallno-1 or 0 or 1

Compare is a unique command in that it only accepts 2 values. Any further values will be ignored.

Example

The following example compares two values.

console.log(cmd.compare(8, 5)); // logs 3

Exists

cmd.exists(val1, ...)

nameall or each?accepts args?return value
existseachno[true or false, ...]

Example

The following example checks the existence of the values. Only null and undefined count as not existing.

cmd.exists(null, undefined, false, '', 0, true);
// [false, false, true, true, true, true]

Extend

cmd.extend(arg1, ...)(val1, ...)

nameall or each?accepts args?return value
extendeachyes[{...}, ...]

Extends each value with each argument, in order.

Example

cmd.extend({color: 'red'})({item: 'wrench'}, {item: 'apple'});
// [{item: 'wrench', color: 'red'}, {item: 'apple', color: 'red'}]

Keywords

underscore

FAQs

Package last updated on 28 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts