Code Monkey home page Code Monkey logo

ember-macaroni's Introduction

ember-macaroni npm version Build Status

Delicious computed property macaronis (macros) for Ember.js 1.13x and greater.

Usage

First, import the macro(s):

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

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

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

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

Macro API

findFromCollectionByKey(collectionKey, propName, valueKey)

/** 
 * 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' }
 * });
 *
 * @param {String} collectionKey The key name for the collection
 * @param {String} propName The key name for the property to find by
 * @param {String} valueKey The key name that returns the value to find
*/

findFromCollectionByValue(collectionKey, propName, value)

/** 
 * Returns the first item with a property matching the passed value.
 *
 * Ember.Object.extend({
 *   items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
 *   selectedItem: findFromCollectionByKey('items', 'id', 1) // { id: 1, name: 'foo' }
 * });
 *
 * @param {String} collectionKey The key name for the collection
 * @param {String} propName The key name for the property to find by
 * @param {*} value The value to match
*/

rejectFromCollectionByKey(collectionKey, propName, valueKey)

/** 
 * 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' }]
 * });
 *
 * @param {String} collectionKey The key name for the collection
 * @param {String} propName The key name for the property to reject by
 * @param {String} valueKey The key name that returns the value to reject
*/

rejectFromCollectionByValue(collectionKey, propName, value)

/** 
 * 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' }]
 * });
 *
 * @param {String} collectionKey The key name for the collection
 * @param {String} propName The key name for the property to reject by
 * @param {*} value The value to reject
*/

filterFromCollectionByKey(collectionKey, propName, valueKey)

/** 
 * 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' }]
 * });
 * 
 * @param {String} collectionKey The key name for the collection
 * @param {String} propName The key name for the property to filter by
 * @param {String} valueKey The key name that returns the value to filter
*/

filterCollectionByContains(collectionKey, propName, values)

/** 
 * 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: filterCollectionByContains('items', 'id', [1]) // [{ id: 1, name: 'foo' }]
 * });
 * 
 * @param {String} collectionKey The key name for the collection
 * @param {String} propName The key name for the property to filter by
 * @param {Array} values The array of values to filter
*/

reduceCollectionByKey(collectionKey, dependentKey, startValue)

/** 
 * 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
 * });
 * 
 * @param {String} collectionKey The key name for the collection
 * @param {String} dependentKey The key name for the property to reduce
 * @param {*} startValue The initial value
*/

isEqualByKeys(firstKey, secondKey)

/** 
 * Strict equality using dependent keys.
 * 
 * Ember.Object.extend({
 *   employeeId: 1
 *   selectedId: 1,
 *   isSelected: isEqualByKeys('employeeId', 'selectedId') // true
 * });
 * 
 * @param {String} firstKey The key name for the first property
 * @param {String} secondKey The key name for the second property
*/

getPropertiesByKeys(...dependentKeys)

/** 
 * 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' }
 * });
 * 
 * @param {...rest} dependentKeys Argument list of dependent keys
*/

ifThenElseWithKeys(conditionalKey, trueKey, falseKey)

/** 
 * Ternary conditional with dependent keys.
 * 
 * Ember.Object.extend({
 *   isSelected: true,
 *   selectedText: 'Is Enabled',
*    deselectedText: 'Is Disabled',
 *   displayText: ifThenElseWithKeys('isSelected', 'selectedText', 'deselectedText') // 'Is Enabled'
 * });
 * 
 * @param {String} conditionalKey The key name for the conditional property
 * @param {String} trueKey The key name for the property to return when the conditional is true
 * @param {String} falseKey The key name for the property to return when the conditional is false
*/

ifThenElseWithValues(conditionalKey, trueValue, falseValue)

/** 
 * Ternary conditional.
 * 
 * Ember.Object.extend({
 *   isSelected: true,
 *   displayText: ifThenElseWithKeys('isSelected', 'Is Enabled', 'Is Disabled') // 'Is Enabled'
 * });
 * 
 * @param {String} conditionalKey The key name for the conditional property
 * @param {String} trueValue The value to return when the conditional is true
 * @param {String} falseValue The value to return when the conditional is false
*/

Installation

  • git clone this repository
  • npm install
  • bower install

Running

Running Tests

  • ember test
  • ember test --server

Building

  • ember build

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

ember-macaroni's People

Contributors

ember-tomster avatar poteto 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.