Javascript Syntax Sugar for Higher-Order Messaging
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
assets
src
test
typings
.codeclimate.yml
.gitignore
.npmignore
.travis.yml
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
README.md
_config.yml
package.json
tsconfig.json
yarn.lock

README.md

Jewell

npm version Build Status Code Climate Test Coverage

Jewell leverages the power of ES6 Proxy to provide syntax sugar, allowing you to write cleaner code.

The best way to showcase Jewell is through code samples:

import {jewellPrototype} from 'jewell';
jewellPrototype(Array);

const diamonds = [...];

// Get price: [2k, 100k, 300k]
diamonds.map.price; // 💎
diamonds.map(diamond => diamond.price); // Traditional

// Buy all pink diamonds
diamonds.filter.pink.forEach.buy(); // 💎
diamonds.filter(diamond => diamond.pink).forEach(diamond => diamond.buy()); // Traditional

Getting Started

Installation

npm install jewell --save

And to import:

import jewell, {jewellPrototype} from 'jewell';

Note: You may need an ES6 Proxy polyfill to support older browsers.

API

  • jewell(object: object, propertyName: string): Proxies function property. Bear in mind the property must already exist.
  • jewellPrototype(class: object, except: string[]): Proxies all functions with zero or one argument in a class prototype.
  • ._fn: After proxying, the original method can be accessed through ._fn.

⚠ Warning: Be aware that jewell(...) and jewellPrototype(...) replace the original methods with proxies. This package was created for experimental purposes, but if you decide to use it in production, make sure you're not creating unintendend behaviors or performance issues.

Jewelling an instance property

This means only a single instance will be jewelled.

const object = [{name: 'John'}, {name: 'Jane'}];
jewell(object, 'map');

Jewelling a prototype property

This means all instances of a class will the property jewelled.

jewell(Array.prototype, 'map');

Jewelling a class

jewellPrototype(Array, ['shift']); // Proxy functions with one or zero args except 'shift

Examples

The examples below expect jewellPrototype(Array) to have been called.

Mapping attributes

const fruits = [{name: 'Apple'}, {name: 'Grape'}];
fruits.map.name; // ['Apple', 'Grape']

Equivalent to fruits.map(fruit => fruit.name).

Mapping the results of functions

const photos = [...];
photos.map.crop(0, 0, 50, 50);

Equivalent to photos.map(photo => photo.crop(0, 0, 50, 50))

Calling function for each array item

const diamonds = [...];
diamonds.forEach.sell();

Equivalent to diamonds.forEach(diamond => diamond.sell())

Calling function with argument for each array item

const kittens = [...];
kittens.forEach.feed('fish', 3);

Equivalent to kittens.forEach(kitten => kitten.feed('fish', 3))

Filtering items

const employees = [...];
employees.filter.retired;

Filtering items with argument

const employees = [...];
employees.filter.compliesTo('rule');

Equivalent to employees.filter(employee => employee.compliesTo('rule'))

Filtering and mapping

const animals = [...];
animals.filter.friendly.map.name;

Equivalent to animals.filter(animal => animal.friendly).map(animal => animal.name)

Checking if all items comply to a rule

const employees = [...];
employees.every.retired; // true or false

Equivalent to employees.every(employee => employee.retired)

Accessing the original method

jewell(array, 'shift');
array.shift._fn // Original method

More examples

If you have some other cool examples, you're very welcome to open a PR.

Resources

Motivation

Jewell is inspired by @franzliedke's implementation of higher-order messaging for Laravel and by Nat Pryce's article on higher-order messaging with Ruby.

Ultimately it would be very nice to have higher-order messaging implemented as a native Javascript feature, but until then, there's Jewell.

License

MIT