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

cmd.js

v0.2.2

Published

A chainable utility toolkit for JavaScript.

Downloads

32

Readme

cmd.js

JavaScript Logic Framework

cmd.js on npm

Github Stars Package Version Package License Code Climate Test Coverage Build Status

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.

Quickstart

Node.js

Install with npm:

npm install cmd.js
var cmd = require('cmd.js');

// Enable all cmd modules
cmd.use('*');

// Test
cmd.log.with('Hello World');

Browser

<script src="src/cmd.js"></script>
<script src="build/cmd.lib.js"></script>
<script>
    // Enable all cmd modules
    cmd.use('*');

    // Test
    cmd.log.with('Hello World');
</script>

Index of Commands

A

add alert

C

call case clone compare count

D

default divide

E

equals exists extend

F

filter format

G

get group

H

has

J

join

L

log logger

M

match max min multiply

N

not

O

obj

P

product push

R

reject reverse

S

sort subtract sum

T

tap

Chaining Example

Goal: 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? With cmd.js, it's even simpler:

cmd.js

// Enable all cmd modules
cmd.use('*');

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

sortAndPrint.with(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.

Types of Commands

"Each" Commands

cmd.add(...arguments).with(...values);

cmd.add(100, 200).with(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.with(...values);

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

To chain these commands, just leave off the values until the very end. Some examples:

cmd.filter(cmd.exists).with(1, 2, null, 3); // [1, 2, 3]

cmd.filter(function (x) {
    return typeof x !== 'string'
}).exists.with("1", 2, null, "3"); // [true, false]

Get Raw Value

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"

Map Command

What would you do if you needed to add 1 to each value in many arrays independently? Use .map:

cmd.add(1).map.with([1, 2, 3], [10, 20, 30], [100, 200, 300]);
// [[2, 3, 4], [11, 21, 31], [101, 201, 301]]

"All" Commands

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.with(...values);

cmd.sum.with(1, 2, 3); // 6

Map Command

What would you do if you needed to sum a bunch of arrays independently? Use .map:

cmd.sum.with([1, 2, 3], [4, 5, 6], [7, 8, 9]); // 45 - not what we want

cmd.sum.map.with([1, 2, 3], [4, 5, 6], [7, 8, 9]); // [6, 15, 24] - perfect!

Special Commands

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.

Argument Merging

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.with(1, 2, 3, 4, 5); // 5

cmd.max.with([1, 2, 3, 4, 5]); // 5

cmd.max.with(1, [2, 3], 4, 5); // 5

cmd.max.with([1], 2, [3, 4, 5]); // 5

cmd.max.with([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.

Tutorial

Let's start with some data:

var products = [
    {name: 'apple',  type: 'fruit',     q: 5, price: 1.99},
    {name: 'pear',   type: 'fruit',     q: 3, price: 2.59},
    {name: 'carrot', type: 'vegetable', q: 7, price: 0.59}
];

How many products are there in total? Luckily, .sum takes an array and returns a single number:

cmd.get('q').sum.with(products);
// 15

How many apples are there? Use .raw to get just the first result unwrapped:

cmd.filter(cmd.get('name').equals('apple')).get('q').raw(products);
// 5

How many fruits are there? Use .filter and .sum together:

cmd.filter(cmd.get('type').equals('fruit')).get('q').sum.with(products);
// 8

What is the total extended cost of all items?

cmd.do(cmd.get('q'), cmd.get('price')).map.product.sum.call('toFixed', 2).with(products);
// '21.85'

Developer Notes

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.