Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
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:
Sort the users by increasing age, and display the name and id of each user.
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},
];
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?
// Enable all cmd plugins
cmd.use('*');
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.
Installation is done with npm install
or make install
for convenience.
Testing is accomplished with mocha, and can be run with npm test
or make test
. There's also a handy make test-watch
to see live test results during development.
This project is built with gulp. Make all changes/additions in lib/*.js
while running make build-watch
from the command line.
cmd.name(... args ...)(... vals ...);
Some commands do not accept args, and you are given the command with empty args already provided.
cmd.sum(... vals ...);
Arguments are automatically merged one level deep for maximum convenience. For example, you can provide an array of arguments or individual arguments, or any combination thereof. The following are all identical:
cmd.use('max');
cmd.max(1, 2, 3, 4, 5); // [5]
cmd.max([1, 2, 3, 4, 5]); // [5]
cmd.max(1, [2, 3], 4, 5); // [5]
cmd.max([1], 2, [3, 4, 5]); // [5]
cmd.max([1], [2], [3], [4], [5]); // [5]
Because of this, if you absolutely need to work with an array as-is, pass it in like [[1, 2, 3]]
to avoid automatic argument merging.
cmd.alert(val1, ...)
name | all or each? | accepts args? | return value |
---|---|---|---|
alert | each | no | [undefined, ...] |
The following example displays two alerts in sequence.
cmd.alert('Hello World!', 'Will Smith here.');
cmd.compare(val1, val2)
name | all or each? | accepts args? | return value |
---|---|---|---|
compare | all | no | -1 or 0 or 1 |
Compare is a unique command in that it only accepts 2 values. Any further values will be ignored.
The following example compares two values.
console.log(cmd.compare(8, 5)); // logs 3
cmd.exists(val1, ...)
name | all or each? | accepts args? | return value |
---|---|---|---|
exists | each | no | [true or false, ...] |
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]
cmd.extend(arg1, ...)(val1, ...)
name | all or each? | accepts args? | return value |
---|---|---|---|
extend | each | yes | [{...}, ...] |
Extends each value with each argument, in order.
cmd.extend({color: 'red'})({item: 'wrench'}, {item: 'apple'});
// [{item: 'wrench', color: 'red'}, {item: 'apple', color: 'red'}]
FAQs
A chainable utility toolkit for JavaScript.
The npm package cmd.js receives a total of 1 weekly downloads. As such, cmd.js popularity was classified as not popular.
We found that cmd.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.