Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
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.
Install with npm:
npm install cmd.js
var cmd = require('cmd.js');
// Enable all cmd modules
cmd.use('*');
// Test
cmd.log('Hello World');
<script src="src/cmd.js"></script>
<script src="build/cmd.lib.js"></script>
<script>
// Enable all cmd modules
cmd.use('*');
// Test
cmd.log('Hello World');
</script>
Goal: 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? With cmd.js, it's even simpler:
// Enable all cmd modules
cmd.use('*');
var sortAndPrint = cmd.sort(cmd.get('age')).
and.logger(cmd.get('name'), cmd.get('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 chaining commands you create reusable logic isolated from specifc data variables.
cmd.add(...arguments)(...values);
cmd.add(100, 200)(7, 8, 9); // [307, 308, 309]
These commands operate on each value passed in and thus do not have access to other values during processing. This also means that every "each" command returns an array at all times. Both arguments and values are subject to argument merging as described below.
Some commands do not accept arguments, and only the values need to be provided.
cmd.exists(...values);
cmd.exists(0, null); // [true, false]
To chain these commands, just leave off the values until the very end. Some examples:
cmd.filter(cmd.exists)(1, 2, null, 3); // [1, 2, 3]
cmd.filter(function (x) {
return typeof x !== 'string'
}).and.exists("1", 2, null, "3"); // [true, false]
Each commands always return an array of values. To get the first value not wrapped in an array instead, just use .raw
immediately before passing in the values:
cmd.use('case');
cmd.case.upper.raw('hello world');
// "HELLO WORLD"
cmd.use('format');
cmd.format('my favorite number is {}').raw(100);
// "my favorite number is 100"
What would you do if you needed to add 1 to each value in many arrays independently? Use .map
:
cmd.add(1).map([1, 2, 3], [10, 20, 30], [100, 200, 300]);
// [[2, 3, 4], [11, 21, 31], [101, 201, 301]]
Every command is unique, but most all commands take all ...values
and perform some operation that includes all of them. Most "all" commands do not return an array, in direct contrast to "each" commands.
cmd.sum(...values);
cmd.sum(1, 2, 3); // 6
What would you do if you needed to sum a bunch of arrays independently? Use .map
:
cmd.sum([1, 2, 3], [4, 5, 6], [7, 8, 9]); // 45 - not what we want
cmd.sum.map([1, 2, 3], [4, 5, 6], [7, 8, 9]) // [6, 15, 24] - perfect!
Some commands do not fall under either of the above categories, and usually take and return very specific arguments. An example of this is cmd.compare
, which is described below.
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.
Development dependencies can be installed 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 src/lib/*.js
while running make build-watch
from the command line.
FAQs
A chainable utility toolkit for JavaScript.
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
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.