https://github.com/raineorshine/shackles
A minimal chaining library with tapping and logging
https://github.com/raineorshine/shackles
Last synced: 3 months ago
JSON representation
A minimal chaining library with tapping and logging
- Host: GitHub
- URL: https://github.com/raineorshine/shackles
- Owner: raineorshine
- License: isc
- Created: 2014-12-10T10:28:01.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-06-14T01:58:09.000Z (about 10 years ago)
- Last Synced: 2025-03-19T20:02:29.014Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 143 KB
- Stars: 74
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# shackles
[](https://travis-ci.org/metaraine/shackles)
[](http://badge.fury.io/js/shackles)> A minimal chaining library with tapping and logging
## Install
```sh
$ npm install --save shackles
```## Basic Usage
Add chaining to a library:
```js
var stringlib = {
prepend: function(str, chr) {
return chr + str
},
append: function(str, chr) {
return str + chr
}
}var chain = shackles(stringlib)
var result = chain('Hello')
.prepend('(')
.append('!')
.append(')')
.value() // (Hello!)
```If underscore didn't have chaining, we could easily add it:
```js
var chain = shackles(_)var result = chain([1,2,3])
.map(function (x) { return x*x })
.filter(function (x) { return x > 2 })
.value() // [4,9]
```Scalar properties become chainable methods that override the underlying value:
```js
var chain = shackles({
inc: function(x) { return x+1 }
pi: 3.141592654
})var result = chain(0)
.inc()
.inc()
.pi()
.inc()
.value() // 4.141592654
```## Tapping
You can transform the value at any point in the chain:
```js
var chain = shackles(/* lib */)var result = chain(10)
.tap(function(value) {
return value * 2;
})
.value() // 20
```## Logging
You can log the value at any point in the chain:
The default logger method is `console`:```js
var chain = shackles({
inc: function(x) { return x+1 }
})var result = chain(0)
.inc()
.log() // 1
.inc()
.log() // 2
.inc()
.inc()
.value() // 4
```You can override the default logger:
```js
var doubled = nullvar chain = shackles({}, {
logger: {
log: function(value) {
doubled = value * 2
}
}
})var result = chain(10)
.log()
.value() // 10console.log(doubled) // 20
```You can enable/disable logging for longer sections of the chain:
```js
var history = []var stringlib = {
prepend: function(str, chr) {
return chr + str
},
append: function(str, chr) {
return str + chr
}
}var chain = shackles(stringlib, {
logger: {
log: function(value) {
history.push(value)
}
}
})var result = chain('Hello')
.log(true)
.prepend('(')
.append('!')
.append(')')
.log(false)
.append('?')
.append('?')
.append('?')
.value() // (Hello!)???console.log(history)
/* [
'Hello',
'(Hello',
'(Hello!',
'(Hello!)'
]) */
```## License
ISC © [Raine Lourie](https://github.com/metaraine)