0.1.2 • Published 7 years ago

jewell v0.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

Jewell

Javascript Syntax Sugar for Higher-Order Messaging

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 musics = [...];
musics.forEach.play(); // 💎
musics.forEach(music => music.play()); // Tradicional

const animals = [...];
animals.filter.owned.map.name; // 💎
animals.filter(animal => animal.owned).map(animal => animal.name); // Tradicional

const employees = [...];
employees.filter.retired.forEach.sendPayment(); // 💎
employees.filter(employee => employee.retired).forEach(employee => employee.sendPayment()); // Tradicional

⚠ Warning: This is an experimental package and it's not meant to be used in production.

Installation

Using NPM:

npm install jewell --save

Using Yarn

yarn add jewell

Using CDN

<script src="https://unpkg.com/jewell/dist/index.js"></script>

Note: You may need a 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.

Jewelling a property

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

Jewelling a prototype property

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']

This is the same as fruits.map(fruit => fruit.name).

Mapping the results of functions

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

Calling function for each array item

const musics = [...];
musics.forEach.play();

Calling function with argument for each array item

const photos = [...];
photos.forEach.crop(150, 200);

Filtering items

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

Filtering items with argument

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

Filtering and mapping

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

Checking if all items comply to a rule

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

Accessing the original method

The original method is available through _fn

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