Skip to content

Instantly share code, notes, and snippets.

@poteto
Last active February 29, 2016 08:32
Show Gist options
  • Save poteto/f857fbc4e65c5f717114 to your computer and use it in GitHub Desktop.
Save poteto/f857fbc4e65c5f717114 to your computer and use it in GitHub Desktop.
test pipe
import Ember from 'ember';
const { Controller } = Ember;
export default Controller.extend({
init() {
this._super(...arguments);
this.cart = [
{ id: 0, name: 'Socks', price: 5 },
{ id: 1, name: 'Shoes', price: 50 }
];
this.item = {
id: 2,
name: 'Dog Treats',
price: 12
}
}
});
<h1>Warning: Contrived Example</h1>
Cart value: {{totalPrice}}
<ul>
{{#each cart as |product|}}
<li>{{product.name}} - $ {{product.price}}</li>
{{/each}}
</ul>
<button {{action
(pipe
(append cart)
(reduceBy "price")
(curry-concat "$")
(action (mut totalPrice))
) item}}>
Add {{item.name}} - {{item.price}} to Cart
</button>
import Ember from 'ember';
export function append([b, a]/*, hash*/) {
let arity = arguments[0].length;
if (arity === 2) {
return [...a, ...b];
}
if (arity === 1) {
return function(curried) {
b.pushObject(curried);
return b;
}
}
}
export default Ember.Helper.helper(append);
import Ember from 'ember';
export function curryConcat([b, a]/*, hash*/) {
let arity = arguments[0].length;
if (arity === 2) {
return a.concat(b);
}
if (arity === 1) {
return function(curried) {
return b.concat(curried);
};
}
}
export default Ember.Helper.helper(curryConcat);
import Ember from 'ember';
const { Helper: { helper } } = Ember;
export function pipe(actions = []) {
return function(...args) {
return actions.reduce((acc, curr, idx) => {
if (idx === 0) {
return curr(...args);
}
return curr(acc);
}, undefined);
};
}
export default helper(pipe);
import Ember from 'ember';
const { get, isEmpty } = Ember;
function _reduce(key, array) {
array = isEmpty(array) ? [] : array;
return array.reduce((acc, item) => {
return acc + get(item, key);
}, 0);
}
export function reduceBy([reduceByKey, array]/*, hash*/) {
let arity = arguments[0].length;
if (arity === 2) {
return _reduce(reduceByKey, array);
}
if (arity === 1) {
return function(curried) {
return _reduce(reduceByKey, curried);
}
}
}
export default Ember.Helper.helper(reduceBy);
{
"version": "0.6.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.3.1/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.3.3/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment