GithubHelp home page GithubHelp logo

gum's Introduction

gum

Build Status

Small method chaining library, that can be used to create jQuery like DSLs from arbitrary functions.

Usage

Lets define few utility functions that operate on hashes that we'll create DSL for (feel free to skip to a next session)

// General funciton definitions (You can skip this part)

function descriptor(source) {
  return Object.getOwnPropertyNames(source).reduce(function(result, name) {
    result[name] = Object.getOwnPropertyDescriptor(source, name)
    return result
  }, {})
}

function supplement() {
  var sources = Array.prototype.slice.call(arguments)
  return merge.apply(merge, sources.reverse())
}

function merge() {
  var sources = Array.prototype.slice.call(arguments)
  var whitelist = {}
  sources.forEach(function(source) {
    var properties = source ? descriptor(source) : {}
    Object.keys(properties).forEach(function(name) {
      whitelist[name] = properties[name]
    })
  })
  return Object.create(Object.getPrototypeOf(sources[0]), whitelist)
}

function pick() {
  var names = Array.prototype.slice.call(arguments)
  var source = names.shift()
  var properties = descriptor(source)
  var whitelist = {}
  names.forEach(function(name) {
    whitelist[name] = properties[name]
  })
  return Object.create(Object.getPrototypeOf(source), whitelist)
}

Hash of functions can be used to assemble DSL providing method chaining interface.

var gum = require("gum")
var hash = gum({ merge: merge, pick: pick, supplement: supplement })

Chained operations result in a simple functions that execute chained tasks when invoked:

var setDefaults = hash.
    supplement({ x: 0, y: 0 })

setDefaults()           // => { x: 0, y: 0 }
setDefaults({ x: 1 })   // => { x: 1, y: 0 }

Each composed function has is aware of the DSL it's part of, so new compostions can be created by further chaining:

var adjust = setDefualts.
  merge({ x: 11, z: 17 }).
  pick("y", "z")

adjust()              // => { y: 0, z: 17 }
adjust({ y: 15 })     // => { y: 15, z: 17 }

Composed chains could also be bound to the input known up front:

var value = hash({ a: 1, b: 2, c: 3, d: 4 }).
  merge({ x: 12, y: 13 }).
  pick('a', 'b', 'x')

value()             // => { x: 12, a: 1, b: 2 }

Install

npm install gum

gum's People

Contributors

gozala avatar

Watchers

 avatar James Cloos avatar  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.