Giter VIP home page Giter VIP logo

shackles's Introduction

shackles

Build Status NPM version

A minimal chaining library with tapping and logging

Install

$ npm install --save shackles

Basic Usage

Add chaining to a library:

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:

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:

var chain = shackles({
	inc: function(x) { return x+1 }
	pi: 3.141592654
})

var result = chain(0)
	.inc()
	.inc()
	.num()
	.inc()
	.value() // 4.141592654

Tapping

You can transform the value at any point in the chain:

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:

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:

var doubled = null

var chain = shackles({}, {
	logger: {
		log: function(value) {
			doubled = value * 2
		}
	}
})

var result = chain(10)
	.log()
	.value() // 10

console.log(doubled) // 20

You can enable/disable logging for longer sections of the chain:

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

shackles's People

Contributors

raineorshine avatar

Watchers

James Cloos avatar tx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.