Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
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 - npm Package Compare versions

Comparing version 0.1.4 to 0.1.5

readme/add.md

2

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

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

@@ -118,14 +118,81 @@ # cmd.js

## Structure of a Command
## Types of Commands
### "Each" Commands
```js
cmd.name(... args ...)(... vals ...);
cmd.add(...arguments)(...values);
cmd.add(100, 200)(7, 8, 9); // [307, 308, 309]
```
Some commands do not accept args, and you are given the command with empty args already provided.
These commands operate on each value passed in and thus do not have access to each value 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.
```js
cmd.sum(... vals ...);
cmd.exists(...values);
cmd.exists(0, null); // [true, false]
```
To chain these commands, just leave off the values until the very end. Some examples:
```js
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]
```
#### 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:
```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"
```
#### Map Command
What would you do if you needed to add 1 to each value in many arrays independently? Use `.map`:
```js
cmd.add(1).map([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.
```js
cmd.sum(...values);
cmd.sum(1, 2, 3); // 6
```
#### Map Command
What would you do if you needed to sum a bunch of arrays independently? Use `.map`:
```js
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!
```
### 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

@@ -151,20 +218,17 @@

## Get Raw Value
## All Modules
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:
### cmd.add
```js
cmd.use('case');
| name | return value | description |
|-------------|---------------|---------------|
| `add` | `[100, ...]` | Returns the sum of all arguments added to each given value. |
cmd.case.upper.raw('hello world');
// "HELLO WORLD"
The following example adds 10 to each value:
cmd.use('format');
cmd.format('my favorite number is {}').raw(100);
// "my favorite number is 100"
```js
cmd.add(10)(1, 2, 3, 4, 5);
// [11, 12, 13, 14, 15]
```
## All Modules
### cmd.alert

@@ -237,2 +301,15 @@

### cmd.divide
| name | return value | description |
|-------------|---------------|---------------|
| `divide` | `[100, ...]` | Returns the product of all arguments divided by each given value. |
The following example divides each value by 10:
```js
cmd.divide(10)(1, 2, 3, 4, 5);
// [0.1, 0.2, 0.3, 0.4, 0.5]
```
### cmd.equals

@@ -244,3 +321,3 @@

The following example checks for values that equal 50:
The following example checks for values that equal 50 or 30:

@@ -356,2 +433,31 @@ ```js

### cmd.match
| name | return value | description |
|------------|-----------------|---------------|
| `match` | `[mixed, ...]` | Matches each item based on a condition and replaces the item with the value provided to `then`. |
The following example uses `cmd.match` to choose an appropriate sentence:
```js
var msgMatch = cmd.match(function (it, then) {
it > 5 && then('You have lots of messages');
it === 5 && then('You have five messages');
it > 1 && then('You have a few messages');
it === 1 && then('You have a message');
it === 0 && then('You have no messages');
then('Unknown');
});
msgMatch(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"]
```
### cmd.max

@@ -383,2 +489,15 @@

### cmd.multiply
| name | return value | description |
|-------------|---------------|---------------|
| `multiply` | `[100, ...]` | Returns the product of all arguments multiplied by each given value. |
The following example multiplies each value by 10:
```js
cmd.multiply(10)(1, 2, 3, 4, 5);
// [10, 20, 30, 40, 50]
```
### cmd.obj

@@ -521,42 +640,26 @@

### cmd.sum
### cmd.subtract
| name | return value | description |
|-------------|---------------|---------------|
| `sum` | `100` | Returns the sum of all given values. |
| `subtract` | `[100, ...]` | Returns the sum of all arguments subtracted from each given value. |
The following example returns the sum 1 + 2 + 3 + 4 + 5:
The following example subtracts 10 from each value:
```js
cmd.sum(1, 2, 3, 4, 5);
// 15
cmd.subtract(10)(1, 2, 3, 4, 5);
// [-9, -8, -7, -6, -5]
```
### cmd.switch
### cmd.sum
| name | return value | description |
|------------|-----------------|---------------|
| `switch` | `[mixed, ...]` | Switches based on a matching when condition. |
| name | return value | description |
|-------------|---------------|---------------|
| `sum` | `100` | Returns the sum of all given values. |
The following example uses `cmd.switch` to choose an appropriate sentence:
The following example returns the sum 1 + 2 + 3 + 4 + 5:
```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"]
cmd.sum(1, 2, 3, 4, 5);
// 15
```

@@ -118,14 +118,81 @@ # cmd.js

## Structure of a Command
## Types of Commands
### "Each" Commands
```js
cmd.name(... args ...)(... vals ...);
cmd.add(...arguments)(...values);
cmd.add(100, 200)(7, 8, 9); // [307, 308, 309]
```
Some commands do not accept args, and you are given the command with empty args already provided.
These commands operate on each value passed in and thus do not have access to each value 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.
```js
cmd.sum(... vals ...);
cmd.exists(...values);
cmd.exists(0, null); // [true, false]
```
To chain these commands, just leave off the values until the very end. Some examples:
```js
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]
```
#### 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:
```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"
```
#### Map Command
What would you do if you needed to add 1 to each value in many arrays independently? Use `.map`:
```js
cmd.add(1).map([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.
```js
cmd.sum(...values);
cmd.sum(1, 2, 3); // 6
```
#### Map Command
What would you do if you needed to sum a bunch of arrays independently? Use `.map`:
```js
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!
```
### 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

@@ -151,18 +218,2 @@

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

@@ -7,3 +7,3 @@ ### cmd.equals

The following example checks for values that equal 50:
The following example checks for values that equal 50 or 30:

@@ -10,0 +10,0 @@ ```js

@@ -25,4 +25,5 @@ /**

var libs = [
'alert',
'add', 'alert',
'case', 'clone', 'compare',
'divide',
'equals', 'exists', 'extend',

@@ -32,6 +33,6 @@ 'filter', 'format',

'log', 'logger',
'max', 'min',
'match', 'max', 'min', 'multiply',
'obj',
'pluck', 'product', 'push',
'sort', 'sum', 'switch'
'sort', 'subtract', 'sum'
];

@@ -67,9 +68,9 @@ return libs.forEach(function (name) {

if (mod.all) {
this.all(name, mod.all, mod);
this.registerAllFn(name, mod.all, mod);
}
else if (mod.each) {
this.each(name, mod.each, mod);
this.registerEachFn(name, mod.each, mod);
}
else if (mod.raw) {
this.raw(name, mod.raw);
this.registerRawFn(name, mod.raw);
}

@@ -79,8 +80,8 @@ };

/**
* Command.all() causes the function to be called on an array of values
* Command.registerAllFn() causes the function to be called on an array of values
* @author Nate Ferrero
*/
Command.prototype.all = function cmdAll(name, fn, plugin) {
Command.prototype.registerAllFn = function (name, fn, plugin) {
if (typeof fn !== 'function') {
throw new Error('cmd.all(name, fn), fn was not a function, got ' + typeof fn);
throw new Error('cmd.registerAllFn(name, fn), fn was not a function, got ' + typeof fn);
}

@@ -92,24 +93,44 @@

/**
* Handle the case where plugin.args is defined
*/
if (Array.isArray(plugin.args)) {
return self.getArgs(name, 'vals', function (vals) {
return fn(plugin.args, vals);
});
}
/**
* Values loader
*/
var valsLoader = function (args) {
return self.getArgs(name, 'vals', function (vals) {
var getVals = self.args(name, 'vals', function (vals) {
return fn(args, vals);
});
/**
* To syntax to avoid merging array values, just use raw values
*/
getVals.__defineGetter__('to', function () {
return function rawValsLoader() {
return fn.call(null, args, Array.prototype.slice.call(arguments));
};
});
/**
* Map to operate on multiple value sets
*/
getVals.__defineGetter__('map', function () {
return function mappedValsLoader() {
return Array.prototype.map.call(arguments, function (x) {
return getVals.apply(null, x);
});
}
});
return getVals;
};
/**
* Handle the case where plugin.args is defined
*/
if (typeof plugin.args !== 'undefined') {
return valsLoader(plugin.args);
}
/**
* Expect the arguments to be provided in the form cmd.x(...args...)(...vals...)
* but still allow default argSets to be used
*/
var argsLoader = self.getArgs(name, 'args', function (args) {
var argsLoader = self.args(name, 'args', function (args) {
return valsLoader(args);

@@ -142,8 +163,8 @@ });

/**
* Command.each() causes the function to be called on each value
* Command.registerEachFn() causes the function to be called on each value
* @author Nate Ferrero
*/
Command.prototype.each = function cmdEach(name, fn, plugin) {
Command.prototype.registerEachFn = function (name, fn, plugin) {
if (typeof fn !== 'function') {
throw new Error('cmd.each(name, fn), fn was not a function, got ' + typeof fn);
throw new Error('cmd.registerEachFn(name, fn), fn was not a function, got ' + typeof fn);
}

@@ -155,17 +176,6 @@

/**
* Handle the case where plugin.args is defined
*/
if (Array.isArray(plugin.args)) {
return self.getArgs(name, 'vals', function (vals) {
return vals.map(function (val) {
return fn(plugin.args, val);
});
});
}
/**
* Values loader
*/
var valsLoader = function (args) {
return self.getArgs(name, 'vals', function (vals) {
var getVals = self.args(name, 'vals', function (vals) {
return vals.map(function (val) {

@@ -175,9 +185,38 @@ return fn(args, val);

});
/**
* To syntax to avoid merging array values, just use raw values
*/
getVals.__defineGetter__('to', function () {
return function rawValsLoader() {
return Array.prototype.map.call(arguments, fn.bind(null, args));
};
});
/**
* Map to operate on multiple value sets
*/
getVals.__defineGetter__('map', function () {
return function mappedValsLoader() {
return Array.prototype.map.call(arguments, function (x) {
return getVals.apply(null, Array.isArray(x) ? x : [x]);
});
}
});
return getVals;
};
/**
* Handle the case where plugin.args is defined
*/
if (typeof plugin.args !== 'undefined') {
return valsLoader(plugin.args);
}
/**
* Expect the arguments to be provided in the form cmd.x(...args...)(...vals...)
* but still allow default argSets to be used
*/
var argsLoader = self.getArgs(name, 'args', function (args) {
var argsLoader = self.args(name, 'args', function (args) {
return valsLoader(args);

@@ -193,3 +232,3 @@ });

};
})
});

@@ -211,7 +250,7 @@ if (typeof plugin.argSets === 'object' && plugin.argSets) {

/**
* Command.raw() causes to function to be called exactly once with the given arguments
* Command.registerRawFn() causes to function to be called exactly once with the given arguments
*/
Command.prototype.raw = function cmdRaw(name, fn) {
Command.prototype.registerRawFn = function (name, fn) {
if (typeof fn !== 'function') {
throw new Error('cmd.raw(name, fn), fn was not a function, got ' + typeof fn);
throw new Error('cmd.registerRawFn(name, fn), fn was not a function, got ' + typeof fn);
}

@@ -224,12 +263,10 @@ Command.prototype.__defineGetter__(name, function () {

/**
* Command.getArgs()
* Command.args()
* @author Nate Ferrero
*/
Command.prototype.getArgs = function getArgsWrapper(name, purpose, done) {
Command.prototype.args = function argsWrapper(name, purpose, done) {
var getArgs = function getArgs() {
var args = Array.prototype.slice.apply(arguments);
var args = function args() {
var _args = [];
args.forEach(function (arg) {
Array.prototype.forEach.call(arguments, function (arg) {
if (Array.isArray(arg)) {

@@ -245,14 +282,15 @@ Array.prototype.push.apply(_args, arg);

getArgs.$name = name;
getArgs.$purpose = purpose;
args.$name = name;
args.$purpose = purpose;
if (purpose === 'vals') {
getArgs.__defineGetter__('and', function () {
return new Command(getArgs);
args.__defineGetter__('and', function () {
return new Command(args);
});
getArgs.raw = function () {
var args = Array.prototype.slice.apply(arguments);
var result = getArgs.apply(null, args);
return result.shift();
/**
* Get the first result unwrapped
*/
args.raw = function (first) {
return args(first).shift();
};

@@ -262,4 +300,4 @@

var context = this.context;
return cmd.getArgs(name, 'vals', function (vals) {
return getArgs.apply(null, context(vals));
return cmd.args(name, 'vals', function (vals) {
return args.apply(null, context(vals));
});

@@ -269,3 +307,3 @@ }

return getArgs;
return args;
};

@@ -272,0 +310,0 @@

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