Home

Awesome

<img src="http://i.imgur.com/XV4VFJl.jpg" title="Macaroni" align="right" width="33%">

ember-macaroni

npm version Build Status Ember Observer Score

Keep your app code DRY and copypasta free with computed property <strong>mac</strong>a<strong>ro</strong>ni<strong>s</strong> (macros) for Ember.js 1.13.x and greater.

Why

Computed property macros (CPM) are great for DRYing up your code, and Ember.js ships with a few handy computed macros. This addon adds a few more functional-style macros, and can be thought of as the "lodash equivalent" of Ember CPM libraries.

Chaining is not supported... yet.

Usage

First, import the macro(s) you need, or the whole thing:

import { findFromCollectionByKey } from 'ember-macaroni'; // imports a named macro
import macros from 'ember-macaroni'; // imports all the things
const { findFromCollectionByValue } = macros; // destructuring

export default Ember.Component.extend({
  items: null,
  selectedId: null,
  selectedItem: findFromCollectionByKey('items', 'id', 'selectedId'),
  hansel: findFromCollectionByValue('items', 'name', 'Hansel'),

  init() {
    this.items = [
      { id: 1, name: 'Derek Zoolander' },
      { id: 2, name: 'Hansel' },
      { id: 3, name: 'Mugatu' }
    ];
  },

  actions: {
    selectPerson(id) {
      Ember.set(this, 'selectedId', id);
    }
  }
});

Available macros


Collection

findFromCollectionByKey

Returns the first item with a property matching the passed value from a dependent key.

Ember.Object.extend({
  items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
  selectedId: 1,
  selectedItem: findFromCollectionByKey('items', 'id', 'selectedId') // { id: 1, name: 'foo' }
});

⬆ back to top

findFromCollectionByValue

Returns the first item with a property matching the passed value.

Ember.Object.extend({
  items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
  selectedItem: findFromCollectionByValue('items', 'id', 1) // { id: 1, name: 'foo' }
});

⬆ back to top

rejectFromCollectionByKey

Returns an array with the items that do not match the passed value from a dependent key.

Ember.Object.extend({
  items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
  selectedId: 2,
  selectedItem: rejectFromCollectionByKey('items', 'id', 'selectedId') // [{ id: 1, name: 'foo' }]
});

⬆ back to top

rejectFromCollectionByValue

Returns an array with the items that do not match the passed value.

Ember.Object.extend({
  items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
  selectedItem: rejectFromCollectionByValue('items', 'id', 2) // [{ id: 1, name: 'foo' }]
});

⬆ back to top

filterFromCollectionByKey

Returns an array with just the items with the matched property.

Ember.Object.extend({
  items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
  selectedId: 1,
  selectedItem: filterFromCollectionByKey('items', 'id', 'selectedId') // [{ id: 1, name: 'foo' }]
});

⬆ back to top

filterFromCollectionByContains

Returns an array with just the items that are contained in another array.

Ember.Object.extend({
  items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
  selectedId: 1,
  selectedItem: filterFromCollectionByContains('items', 'id', [1]) // [{ id: 1, name: 'foo' }]
});

⬆ back to top

collectionWithoutKey

Returns an array without an item by dependent key.

Ember.Object.extend({
  items: [1, 2, 3],
  selectedItem: 1,
  remainingItems: collectionWithoutKey('items', 'selectedItem') // [2, 3]
});

⬆ back to top

reduceCollectionByKey

Combines the values of the enumerator into a single value, using a dependent key.

Ember.Object.extend({
  items: [{ name: 'foo', age: 2 }, { name: 'bar', age: 5 }],
  selectedItem: reduceCollectionByKey('items', 'age', 0) // 7
});

⬆ back to top


Truth

isEqualByKeys

Strict equality using dependent keys.

Ember.Object.extend({
  employeeId: 1
  selectedId: 1,
  isSelected: isEqualByKeys('employeeId', 'selectedId') // true
});

⬆ back to top

ifThenElseWithKeys

Ternary conditional with dependent keys.

Ember.Object.extend({
  isSelected: true,
  selectedText: 'Is Enabled',
  deselectedText: 'Is Disabled',
  displayText: ifThenElseWithKeys('isSelected', 'selectedText', 'deselectedText') // 'Is Enabled'
});

⬆ back to top

ifThenElseWithValues

Ternary conditional.

Ember.Object.extend({
  isSelected: true,
  displayText: ifThenElseWithValues('isSelected', 'Is Enabled', 'Is Disabled') // 'Is Enabled'
});

⬆ back to top

gte

Greater than or equal to comparison between two dependent keys.

Ember.Object.extend({
  first: 5,
  second: 2,
  isFirstGreaterThanOrEqualToSecond: gte('first', 'second') // true
});

⬆ back to top

gt

Greater than comparison between two dependent keys.

Ember.Object.extend({
  first: 5,
  second: 2,
  isFirstGreaterThanSecond: gt('first', 'second') // true
});

⬆ back to top

lte

Lesser than or equal to comparison between two dependent keys.

Ember.Object.extend({
  first: 5,
  second: 2,
  isFirstLesserThanOrEqualToSecond: lte('first', 'second') // false
});

⬆ back to top

lt

Lesser than comparison between two dependent keys.

Ember.Object.extend({
  first: 5,
  second: 2,
  isFirstLessThanSecond: lt('first', 'second') // false
});

⬆ back to top


General

getPropertiesByKeys

Returns a POJO containing all the key-values that match the dependent keys.

Ember.Object.extend({
  age: 5,
  name: 'foo',
  props: getPropertiesByKeys('age', 'name') // { age: 5, name: 'foo' }
});

⬆ back to top

joinWith

Returns a string of values joined together with a separator.

Ember.Object.extend({
  firstName: 'Derek',
  lastName: 'Zoolander',
  fullName: joinWith(' ', 'firstName', 'lastName') // 'Derek Zoolander'
});

⬆ back to top

Installation

Running

Running Tests

Building

For more information on using ember-cli, visit http://www.ember-cli.com/.