Socket
Socket
Sign inDemoInstall

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 - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

readme/case.md

2

package.json
{
"name": "cmd.js",
"version": "0.1.1",
"version": "0.1.2",
"description": "A chainable utility toolkit for JavaScript.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -5,4 +5,4 @@ # cmd.js

[![NPM](https://nodei.co/npm/cmd.js.png?downloads=true&stars=true)](https://nodei.co/npm/cmd.js/)
[![NPM](https://nodei.co/npm-dl/cmd.js.png?months=6&height=2)](https://nodei.co/npm/cmd.js/)
[![cmd.js on npm](https://nodei.co/npm/cmd.js.png?downloads=true&stars=true)](https://nodei.co/npm/cmd.js/)
[![cmd.js on npm](https://nodei.co/npm-dl/cmd.js.png?months=6&height=2)](https://nodei.co/npm/cmd.js/)

@@ -16,11 +16,45 @@ [![Github Stars](https://img.shields.io/github/stars/NateFerrero/cmd.js.svg)](https://github.com/NateFerrero/cmd.js)

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:
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.
### The Task
## Quickstart
Sort the users by increasing age, and display the name and id of each user.
### Node.js
### Data Set
Install with [npm](https://www.npmjs.com/package/cmd.js):
```bash
npm install cmd.js
```
```js
var cmd = require('cmd.js');
// Enable all cmd modules
cmd.use('*');
// Test
cmd.log('Hello World');
```
### Browser
```html
<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>
```
## Chaining Example
Goal: sort the users by increasing age, and display the name and id of each user.
#### Data Set
```js
var users = [

@@ -34,3 +68,3 @@ {name: 'John', id: 1, age: 37},

### Vanilla JavaScript
#### Vanilla JavaScript

@@ -53,11 +87,12 @@ ```js

Pretty simple, right?
Pretty simple, right? With cmd.js, it's even simpler:
### cmd.js
#### cmd.js
```js
// Enable all cmd plugins
// Enable all cmd modules
cmd.use('*');
var pluck = cmd.pluck;
var sortAndPrint = cmd.sort(pluck('age')).

@@ -75,3 +110,3 @@ and.logger(pluck('name'), pluck('id'));

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.
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.

@@ -120,48 +155,95 @@ ### Developer Notes

## Alert
## Get Raw Value
### `cmd.alert(val1, ...)`
Most commands normally 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:
| name | all or each? | accepts args? | return value |
|----------|---------------|----------------|---------------------|
| `alert` | each | no | `[undefined, ...]` |
```js
cmd.use('case');
#### Example
cmd.case.upper.raw('hello world');
// "HELLO WORLD"
The following example displays two alerts in sequence.
cmd.use('format');
cmd.format('my favorite number is {}').raw(100);
// "my favorite number is 100"
```
## All Modules
### cmd.alert
| name | return value | description |
|----------|---------------------|---------------|
| `alert` | `[undefined, ...]` | Causes a browser alert for each value passed in. Does nothing in a Node.js environment. |
The following example displays two alerts in sequence:
```js
cmd.alert('Hello World!', 'Will Smith here.');
// two alerts displayed (only in browser)
```
## Compare
### cmd.case.*
### `cmd.compare(val1, val2)`
| name | return value | description |
|---------------|--------------------------------|---------------|
| `case.lower` | `['change string case', ...]` | Convert strings to lower case. |
| `case.title` | `['Change String Case', ...]` | Convert strings to title case. |
| `case.upper` | `['CHANGE STRING CASE', ...]` | Convert strings to upper case. |
| name | all or each? | accepts args? | return value |
|------------|---------------|----------------|-----------------|
| `compare` | all | no | `-1 or 0 or 1` |
The following example converts strings to lowercase:
Compare is a unique command in that it only accepts 2 values. Any further values will be ignored.
```js
cmd.case.lower('Hello World!', 'Will Smith here.');
// ["hello world!", "will smith here."]
```
#### Example
### cmd.clone
The following example compares two values.
| name | return value | description |
|----------|-----------------|---------------|
| `clone` | `[mixed, ...]` | Clone any JavaScript variable not containing a circular reference. |
The following example clones an object:
```js
console.log(cmd.compare(8, 5)); // logs 3
var answer = {data: 42};
var cloned = cmd.clone.raw(answer); // raw returns non-wrapped first response
[cloned === answer, cloned.data === answer.data];
// [false, true]
```
## Exists
### cmd.compare
### `cmd.exists(val1, ...)`
| name | return value | description |
|------------|-----------------|---------------|
| `compare` | `-1 or 0 or 1` | Compare is a unique command in that it only accepts 2 values. Any further values will be ignored. It is used internally for `cmd.sort` but available for custom sorting as well. |
| name | all or each? | accepts args? | return value |
|------------|---------------|----------------|-------------------------|
| `exists` | each | no | `[true or false, ...]` |
#### Example
The following examples compare two values. Compare defines a sort order for any two JavaScript types:
The following example checks the existence of the values. Only null and undefined count as not existing.
```js
cmd.compare(8, 5);
// 3
cmd.compare(1000, 'a');
// -1
cmd.compare('boo', 'apple');
// 1
cmd.compare('hello', false);
// 1
```
### cmd.exists
| name | return value | description |
|------------|-------------------------|---------------|
| `exists` | `[true or false, ...]` | Checks if each value passed in exists (not null or undefined). |
The following example checks the existence of the values. Only null and undefined count as not existing:
```js

@@ -172,14 +254,10 @@ cmd.exists(null, undefined, false, '', 0, true);

## Extend
### cmd.extend
### `cmd.extend(arg1, ...)(val1, ...)`
| name | return value | description |
|------------|-----------------|---------------|
| `extend` | `[{...}, ...]` | Extends each value with each argument, in order. |
| name | all or each? | accepts args? | return value |
|------------|---------------|----------------|-----------------|
| `extend` | each | yes | `[{...}, ...]` |
The following example adds the color red to each value passed in:
Extends each value with each argument, in order.
#### Example
```js

@@ -190,12 +268,216 @@ cmd.extend({color: 'red'})({item: 'wrench'}, {item: 'apple'});

### cmd.filter
| name | return value | description |
|------------|-----------------|---------------|
| `filter` | `[mixed, ...]` | Filters out values based on arguments. |
The following example filters the values to only even numbers greater than 5:
```js
cmd.filter(function (x) {
return x % 2 === 0;
}, function (x) {
return x > 5;
})(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// [6, 8, 10]
```
### cmd.format
| name | return value | description |
|------------|------------------------------|---------------|
| `format` | `['formatted string', ...]` | Formats string arguments using positional `{}` targets. |
The following example formats two strings using positional targets:
```js
cmd.format('I love {}pples, {}lueberries, and {}ake', '{} + {} = {}')('a', 'b', 'c');
// ["I love apples, blueberries, and cake", "a + b = c"]
```
### cmd.join
| name | return value | description |
|----------|---------------------------|---------------|
| `join` | `['joined string', ...]` | Joins values provided with arguments as glue. |
The following example joins the values using the glue provided in initial arguments:
```js
cmd.join('-', '+')('a', 'b', 'c');
// ["a-b-c", "a+b+c"]
```
### cmd.log
| name | return value | description |
|---------|-----------------|---------------|
| `log` | `[mixed, ...]` | Logs values and passes them through unchanged. |
The following example logs each value to the console and returns the values:
```js
cmd.log(1, 2, 3);
// 1
// 2
// 3
// [1, 2, 3]
```
### cmd.logger
| name | return value | description |
|------------|-----------------|---------------|
| `logger` | `[mixed, ...]` | Logs values once per each argument and passes them through unchanged. |
The following example logs each value wrapped in a custom log format to the console and returns the values. If a string is passed it will use `cmd.format` to format the logs:
```js
var withDate = function (x) {
return 'Log at ' + (new Date()) + ': ' + x;
};
cmd.logger(withDate, 'and the number is: {}')(1, 2, 3);
// Log at Sat Jan 31 2015 23:05:59 GMT-0800 (PST): 1 and the number is: 1
// Log at Sat Jan 31 2015 23:05:59 GMT-0800 (PST): 2 and the number is: 2
// Log at Sat Jan 31 2015 23:05:59 GMT-0800 (PST): 3 and the number is: 3
// [1, 2, 3]
```
### cmd.obj
| name | return value | description |
|----------|------------------|---------------|
| `obj` | `[{ ... }, ...]` | Zips up an object using arguments as keys and values as values. |
The following example builds an object with keys and repeated values. Note the `[[wrapped array]]` syntax to avoid spreading the array as arguments:
```js
cmd.obj('name', 'age', 'city', 'interests')(
'Nate', 25, 'Los Angeles, CA', [['tech', 'javascript', 'node.js', 'space']]
);
// [{
// "name": "Nate",
// "age": 25,
// "city": "Los Angeles, CA",
// "interests": ["tech", "javascript", "node.js", "space"]
// }]
```
### cmd.pluck
| name | return value | description |
|------------|-----------------|---------------|
| `pluck` | `[mixed, ...]` | Surfaces data within a structure of objects or arrays, using arguments as keys. |
The following example plucks object properties:
```js
var people = [{
name: 'Adam',
pet: {
type: 'bird',
name: 'Sherlock'
}
}, {
name: 'Shannon',
pet: {
type: 'snake',
name: 'Rosa'
}
}, {
name: 'Upgrayyed',
pet: {
type: 'dog',
name: 'Maxximus'
}
}];
cmd.pluck('pet', 'name')(people);
// ["Sherlock", "Rosa", "Maxximus"]
```
### cmd.push.to
| name | return value | description |
|------------|-----------------|---------------|
| `push.to` | `[mixed, ...]` | Pushes provided values to each argument array. The use of `.to` avoids having to `[[double wrap]]` array arguments. |
The following example pushes to an array:
```js
var people = [];
var add = cmd.push.to(people);
add({
name: 'Adam'
});
add({
name: 'Blake'
});
console.log(people);
// [{"name":"Adam"}, {"name":"Blake"}]
```
Push returns the value(s) passed in, so it can be used perfectly while chaining commands:
```js
add.and.log({name: 'Charlie'});
// Object {name: "Charlie"}
people.length;
// 3
```
### cmd.sort
| name | return value | description |
|---------------|-----------------|---------------|
| `sort` | `[mixed, ...]` | Sorts the array in custom order. |
| `sort.asc` | `[mixed, ...]` | Sorts the array in ascending order. |
| `sort.desc` | `[mixed, ...]` | Sorts the array in descending order. |
The following example sorts the values with various sort orders and parameters:
```js
cmd.sort.asc('c', 'a', 'b', 3, 1, 2);
// [1, 2, 3, "a", "b", "c"]
cmd.sort.desc('c', 'a', 'b', 3, 1, 2);
// ["c", "b", "a", 3, 2, 1]
// Sort by type, leaving order preserved within a type
cmd.sort(function (x) {
return typeof x;
})('c', 'a', 'b', 3, 1, 2);
// [3, 1, 2, "c", "a", "b"]
// Sort objects by a key
cmd.sort(function (x) {
return x.price;
})({name: 'TV', price: 899.00}, {name: 'Car', price: 16999.00}, {name: 'Spoon', price: 1.29});
// [{name: "Spoon", price: 1.29}, {name: "TV", price: 899}, {name: "Car", price: 16999}]
```
### cmd.switch
| name | return value | description |
|------------|-----------------|---------------|
| `switch` | `[mixed, ...]` | Switches based on a matching when condition. |
The following example uses `cmd.switch` to choose an appropriate sentence:
```js
var msgSwitch = cmd.switch(function (when, x) {
when(x > 5, 'You have lots of messages');
when(x === 5, 'You have five messages');
when(x > 1, 'You have a few messages');
when(x === 1, 'You have a message');
when(x === 0, 'You have no messages');
when(true, 'Unknown');
});
msgSwitch(0, 1, 2, 3, 4, 5, 6, 'x');
// ["You have no messages", "You have a message", "You have a few messages", "You have a few messages", "You have a few messages", "You have five messages", "You have lots of messages", "Unknown"]
```

@@ -5,4 +5,4 @@ # cmd.js

[![NPM](https://nodei.co/npm/cmd.js.png?downloads=true&stars=true)](https://nodei.co/npm/cmd.js/)
[![NPM](https://nodei.co/npm-dl/cmd.js.png?months=6&height=2)](https://nodei.co/npm/cmd.js/)
[![cmd.js on npm](https://nodei.co/npm/cmd.js.png?downloads=true&stars=true)](https://nodei.co/npm/cmd.js/)
[![cmd.js on npm](https://nodei.co/npm-dl/cmd.js.png?months=6&height=2)](https://nodei.co/npm/cmd.js/)

@@ -16,11 +16,45 @@ [![Github Stars](https://img.shields.io/github/stars/NateFerrero/cmd.js.svg)](https://github.com/NateFerrero/cmd.js)

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:
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.
### The Task
## Quickstart
Sort the users by increasing age, and display the name and id of each user.
### Node.js
### Data Set
Install with [npm](https://www.npmjs.com/package/cmd.js):
```bash
npm install cmd.js
```
```js
var cmd = require('cmd.js');
// Enable all cmd modules
cmd.use('*');
// Test
cmd.log('Hello World');
```
### Browser
```html
<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>
```
## Chaining Example
Goal: sort the users by increasing age, and display the name and id of each user.
#### Data Set
```js
var users = [

@@ -34,3 +68,3 @@ {name: 'John', id: 1, age: 37},

### Vanilla JavaScript
#### Vanilla JavaScript

@@ -53,11 +87,12 @@ ```js

Pretty simple, right?
Pretty simple, right? With cmd.js, it's even simpler:
### cmd.js
#### cmd.js
```js
// Enable all cmd plugins
// Enable all cmd modules
cmd.use('*');
var pluck = cmd.pluck;
var sortAndPrint = cmd.sort(pluck('age')).

@@ -75,3 +110,3 @@ and.logger(pluck('name'), pluck('id'));

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.
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.

@@ -119,1 +154,19 @@ ### Developer Notes

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.
## Get Raw Value
Most commands normally 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:
```js
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"
```
## All Modules

@@ -1,15 +0,12 @@

## Alert
### cmd.alert
### `cmd.alert(val1, ...)`
| name | return value | description |
|----------|---------------------|---------------|
| `alert` | `[undefined, ...]` | Causes a browser alert for each value passed in. Does nothing in a Node.js environment. |
| name | all or each? | accepts args? | return value |
|----------|---------------|----------------|---------------------|
| `alert` | each | no | `[undefined, ...]` |
The following example displays two alerts in sequence:
#### Example
The following example displays two alerts in sequence.
```js
cmd.alert('Hello World!', 'Will Smith here.');
// two alerts displayed (only in browser)
```

@@ -1,17 +0,22 @@

## Compare
### cmd.compare
### `cmd.compare(val1, val2)`
| name | return value | description |
|------------|-----------------|---------------|
| `compare` | `-1 or 0 or 1` | Compare is a unique command in that it only accepts 2 values. Any further values will be ignored. It is used internally for `cmd.sort` but available for custom sorting as well. |
| 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 examples compare two values. Compare defines a sort order for any two JavaScript types:
#### Example
```js
cmd.compare(8, 5);
// 3
The following example compares two values.
cmd.compare(1000, 'a');
// -1
```js
console.log(cmd.compare(8, 5)); // logs 3
cmd.compare('boo', 'apple');
// 1
cmd.compare('hello', false);
// 1
```

@@ -1,13 +0,9 @@

## Exists
### cmd.exists
### `cmd.exists(val1, ...)`
| name | return value | description |
|------------|-------------------------|---------------|
| `exists` | `[true or false, ...]` | Checks if each value passed in exists (not null or undefined). |
| 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:
#### Example
The following example checks the existence of the values. Only null and undefined count as not existing.
```js

@@ -14,0 +10,0 @@ cmd.exists(null, undefined, false, '', 0, true);

@@ -1,13 +0,9 @@

## Extend
### cmd.extend
### `cmd.extend(arg1, ...)(val1, ...)`
| name | return value | description |
|------------|-----------------|---------------|
| `extend` | `[{...}, ...]` | Extends each value with each argument, in order. |
| name | all or each? | accepts args? | return value |
|------------|---------------|----------------|-----------------|
| `extend` | each | yes | `[{...}, ...]` |
The following example adds the color red to each value passed in:
Extends each value with each argument, in order.
#### Example
```js

@@ -14,0 +10,0 @@ cmd.extend({color: 'red'})({item: 'wrench'}, {item: 'apple'});

@@ -24,8 +24,8 @@ /**

if (name === '*') {
var plugins = [
'alert', 'compare', 'copy', 'exists', 'extend', 'filter', 'format', 'join',
'log', 'logger', 'lower', 'max', 'min', 'obj', 'pluck', 'product',
'push', 'sort', 'sum', 'switch', 'title', 'upper', 'view'
var libs = [
'alert', 'case', 'clone', 'compare', 'exists', 'extend', 'filter', 'format', 'join',
'log', 'logger', 'max', 'min', 'obj', 'pluck', 'product',
'push', 'sort', 'sum', 'switch', 'view'
];
return plugins.forEach(function (name) {
return libs.forEach(function (name) {
self.use(name);

@@ -38,21 +38,12 @@ });

*/
var plugin = typeof module === 'object' ? require('./lib/' + name) : scope['cmd:lib'][name];
var mod = typeof module === 'object' ? require('./lib/' + name) : scope['cmd:lib'][name];
if (!plugin || typeof plugin !== 'object') {
throw new Error('cmd.js plugin ' + name + ' is not available');
if (!mod || typeof mod !== 'object') {
throw new Error('cmd.js module ' + name + ' is not available');
}
if (plugin.export) {
plugin.export(self);
}
if (plugin.all) {
self.all(name, plugin.all, plugin);
}
else if (plugin.each) {
self.each(name, plugin.each, plugin);
}
else if (plugin.raw) {
self.raw(name, plugin.raw);
}
/**
* Register the module scope as cmd.name
*/
self.module(name, mod);
});

@@ -62,2 +53,20 @@ };

/**
* Command.module(name, mod) registers a new module as cmd.name
*/
Command.prototype.module = function (name, mod) {
if (mod.export) {
mod.export(this);
}
if (mod.all) {
this.all(name, mod.all, mod);
}
else if (mod.each) {
this.each(name, mod.each, mod);
}
else if (mod.raw) {
this.raw(name, mod.raw);
}
};
/**
* Command.all() causes the function to be called on an array of values

@@ -64,0 +73,0 @@ * @author Nate Ferrero

@@ -5,2 +5,6 @@ (function () {

this.export = function (cmd) {
/**
* Dependencies
*/
cmd.use('format');

@@ -19,3 +23,5 @@ /**

args.forEach(function (arg) {
var log = typeof arg === 'function' ? arg(val) : arg;
var log = typeof arg === 'function' ? arg(val) : (
typeof arg === 'string' ? cmd.format(arg).raw(val) : arg
);
if (Array.isArray(log)) {

@@ -22,0 +28,0 @@ Array.prototype.push.apply(logs, log);

@@ -10,8 +10,9 @@ (function () {

*/
this.all = function (args, vals) {
return args.map(function (arg) {
arg.push.apply(arg, vals);
this.each = function (args, val) {
args.map(function (arg) {
arg.push(val);
});
return val;
};
};
}).call(typeof module === 'undefined' ? this['cmd:lib'].push = {} : this);

@@ -8,3 +8,3 @@ (function () {

*/
cmd.use('compare', 'copy');
cmd.use('compare', 'clone');

@@ -19,3 +19,3 @@ /**

var direction = 1;
var local = cmd.copy(args);
var local = cmd.clone(args);

@@ -22,0 +22,0 @@ if (local && local.length && typeof local[0] === 'number') {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc