Giter Site home page Giter Site logo

koleok / cliquer Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 43 KB

Creates a group of related functions from a naming function, a value deriver function, and an array of primitive values.

JavaScript 100.00%
clique namer primitive-values shorthand es2015 ramda sanctuary

cliquer's Introduction

cliquer

Creates a group of related functions from a naming function, a value deriver function, and an array of primitive values.

Installation

yarn add cliquer

# or

npm i cliquer

API

clique()

clique :: (a -> String) -> (a -> Any) -> Array a -> Object

Takes a namer function, a function to derive a value, and an array of strings that becomes input for both functions, returns an object where each key value pair is generated by [namer, valueDeriver] respectively.

import { clique } from 'cliquer';

const concat = x => y => y.concat(x)
const always = x => () => x
const consts = clique(concat('Bar'), always)

const C = consts(['a', 'b'])
C.bBar() //=> 'b'

simpleClique()

simpleClique :: (a -> Any) -> Array a -> Object

This is simply clique partially applied with an identity function, so that each key is either a literal or toString representation of the input value.

import { simpleClique } from 'cliquer';

const always = x => () => x
const consts = simpleClique(always)

const C = consts(['a', 'b'])
C.b() //=> 'b'

How and why did this come to be?

Cliquer was originally inspired by a utility in drboolean's lenses library called makeLenses which takes an array of strings and returns an object of named lenses:

import { lensProp } from 'ramda'
import { makeLenses, view } from 'lenses'

const obj = {
  foe: { name: 'marley' },
  friend: { name: 'me' },
}

// makeLenses allows a DRY-er approach like
const L = makeLenses(['friend', 'name'])

// instead of
const friend = lensProp('friend')
const name = lensProp('name')

const friendName = compose(L.friend, L.name)
view(friendName, obj) // => 'me'

This got me thinking, how many things in javascript would benefit from a similar shortcut? So a simple curried factory function was born that took two functions, one to generate the key name, and one to define the function. Turns out this is a super useful pattern for creating utility groups.

import R from 'ramda'

const keys = ['beer', 'me']

// Pass an identity function so each key is named literally
const simpleClique = clique(x => x)

// Simple equality checks
const eqs = simpleClique(R.equals)
const E = eqs(keys)

E.beer('beer') //=> true
E.me('beer') //=> false


// Constant generators
const consts = simpleClique(R.always)
const C = consts(keys)

C.beer() //=> 'beer'


// Regex group
const firstChars = simpleClique(x => new RegExp(`^${x}`))
const F = firstChars(['a', 'b'])

R.test(F.a, 'abc') //=> true
R.test(F.b, 'abc') //=> false


/* Even more complex things! */

// Pass a function that names each key by appending `bird`
const birdClique =
  clique(x => x.concat('bird'))

const url =
  'https://isthisbirdathing.com/api/birds'

const birdFetchers =
  birdClique(x => fetch(`${url}/${x}bird`).then(x => x.json()) )

const B =
  birdFetchers(['blue', 'shoe', 'fackle'])

B.facklebird()
  .then(console.log) //=> { isAThing: 'um.. no' }

B.bluebird()
  .then(console.log) //=> { isAThing: 'yep ๐Ÿฆ' }

Get the picture? If not submit a PR to help me explain this better or to add more imaginative/hilarious examples ๐Ÿ’–

What are you waiting for? Start some cliques ๐Ÿป

cliquer's People

Contributors

koleok avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.