Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chaingun

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chaingun - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0-beta

2

bower.json
{
"name": "chaingun",
"main": "chaingun.js",
"version": "0.1.2",
"version": "0.2.0-beta",
"homepage": "https://github.com/justinvdm/chaingun",

@@ -6,0 +6,0 @@ "authors": [

(function() {
function chaingun(obj) {
function chaingun(obj, opts) {
opts = opts || {};
var init = typeof obj == 'function'
? obj
: identity;
var get = opts.get || identity;
var set = opts.set || identity;
var exits = lookup(opts.exits || []);
function _chained_(v) {
function _chain_() { return curr; }
var _chain_ = function _chain_(v) {
if (!arguments.length) return get(curr);
curr = set(v);
return _chain_;
};
var curr = v;
if (typeof obj == 'function') curr = obj.apply(this, arguments);
return extend(_chain_, obj, function(fn) {
_chain_ = extend(_chain_, obj, function(fn, name) {
if (typeof fn != 'function') return;

@@ -14,6 +25,10 @@

Array.prototype.unshift.call(arguments, curr);
curr = fn.apply(this, arguments);
return this;
_chain_(fn.apply(this, arguments));
if (name in exits) return _chain_();
return _chain_;
};
});
_chain_(init.apply(_chain_, arguments));
return _chain_;
}

@@ -31,3 +46,3 @@

if (!source.hasOwnProperty(k)) return;
result = fn.call(this, source[k]);
result = fn.call(this, source[k], k);
if (typeof result != 'undefined') target[k] = result;

@@ -40,2 +55,10 @@ }

function lookup(arr) {
var result = {};
var i = arr.length;
while (i--) result[arr[i]] = 1;
return result;
}
function identity(v) { return v; }

@@ -42,0 +65,0 @@

@@ -26,3 +26,3 @@ var assert = require('assert');

it("should support chaining of the object's function properties", function() {
var chain = chaingun({
var thing = chaingun({
add: function(a, b) { return a + b; },

@@ -32,3 +32,3 @@ multiply: function(a, b) { return a * b; }

var result = chain(2)
var result = thing(2)
.add(3)

@@ -54,2 +54,36 @@ .multiply(5)

});
it("should allow its current value to be settable", function() {
var thing = chaingun({});
var t = thing(2);
assert.equal(t(), 2);
t(3);
assert.equal(t(), 3);
});
it("should support a get hook", function() {
var thing = chaingun({}, {get: function(v) { return v * 10; }});
var t = thing(2);
assert.equal(t(), 20);
});
it("should support a set hook", function() {
var thing = chaingun({}, {set: function(v) { return v * 10; }});
var t = thing(2);
assert.equal(t(), 20);
});
it("should support custom exits", function() {
var thing = chaingun({
add: function(a, b) { return a + b; },
multiply: function(a, b) { return a * b; }
}, {
exits: ['add', 'multiply']
});
var t = thing(2);
assert.equal(t.add(3), 5);
assert.equal(t.multiply(2), 10);
});
});
{
"name": "chaingun",
"version": "0.1.2",
"version": "0.2.0-beta",
"description": "make an object's functions chainable",

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

@@ -45,3 +45,3 @@ # chaingun

### `chaingun(obj)`
### `chaingun(obj[, opts])`

@@ -54,5 +54,5 @@ Returns a chainable version of the given object.

```javascript
var chain = chaingun({add: function(a, b) { return a + b; }});
var chained = chaingun({add: function(a, b) { return a + b; }});
chain(2)
chained(2)
.add(3)

@@ -63,2 +63,12 @@ .add(5);

The chain's value can be reset explicitly by invoking the chain directly with a value:
```javascript
var chained = chaingun({});
var chain = chain(2);
chain(); // 2
chain(3);
chain(); // 3
```
If `obj` is a function, it will be invoked at the start of the chain. Its return value will be used as the starting value of the chain:

@@ -71,5 +81,5 @@

var chain = chaingun(thing);
var chained = chaingun(thing);
chain(2, 3)
chained(2, 3)
.multiply(4)

@@ -79,2 +89,31 @@ (); // 20

if `'exits'` is provided as an option, the functions with the given names will return the chain's current value instead of returning the chain:
```javascript
var chained = chaingun(
{foo: function(v) { return v * 10; }},
{exits: ['foo']});
chained(2).foo(); // 20
```
if `'get'` is provided as an option, it will be used as a hook whenever the chain's value is requested:
```javascript
var chained = chaingun({}, {get: function(v) { return v * 10; }})
var chain = chained(2);
chain(); // 20
```
if `'set'` is provided as an option, it will be used as a hook whenever the chain's value is changed:
```javascript
var chained = chaingun({}, {set: function(v) { return v * 10; }})
var chain = thing(2);
chain(); // 20
chain(3);
chain(); // 30
```
`obj`'s properties can be accessed directly from the chainable:

@@ -90,7 +129,8 @@

var chain = chaingun(obj);
chain.foo === obj.foo; // true
chain.bar === obj.bar; // true
chain.baz === obj.baz; // true
chain.quux === obj.quux; // true
var chained = chaingun(obj);
chained.foo === obj.foo; // true
chained.bar === obj.bar; // true
chained.baz === obj.baz; // true
chained.quux === obj.quux; // true
```
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