Giter VIP home page Giter VIP logo

ember-enumerology's Introduction

ember-enumerology

Build Status

Automagic creation of computed properties from enumerable chains.

Getting Started

Enumerology is a bunch of useful Computed Property macros for working on enumerables.

Best I give you an example, eh?

var Movie = Em.Object.extend({
      castNames: Enumerology.create('cast').mapBy('name').sort().finalize(),
      characterNames: Enumerology.create('cast').mapBy('character').sort().finalize()
    });

var bttf = Movie.create({
      cast: [
        Em.Object.create({name: 'Michael J. Fox', character: 'Marty McFly'}),
        Em.Object.create({name: 'Christopher Lloyd', character: 'Doc Emmett Brown'}),
        Em.Object.create({name: 'Claudia Wells', character: 'Jennifer Parker'}),
      ]
    });

bttf.get('castNames')      // => ["Christopher Lloyd", "Claudia Wells", "Michael J. Fox"]
bttf.get('characterNames') // => ["Doc Emmett Brown", "Jennifer Parker", "Marty McFly"]

In the browser

Download the production version or the development version.

In your web page:

<script src="dist/ember-enumerology.min.js"></script>

Creating Enumerologies

In order to create an Enumerology-created computed property you call Enumerology.create and pass as an argument the name of the collection property to depend on (the part you would stick in the property() call in a normal computed property)

You can then chain on as many transformations as you like (eg map or uniq) and then call finalize() on the end of the chain to create and return the computed property.

For example:

Ember.Object.extend({
  fruit: ['apple', 'apple', 'banana', 'pear'],
  uniqueFruitCount: Enumerology.create('fruit').uniq().length().finalize()
})

Enumeration transforms

Enumerology supports a bunch of methods, which it calls "transforms" that can be applied to an enumerable. Most can be chained, although it obviously makes no sense to try and map the result of a method which returns an object, instead of an array (like first or any).

any

see: Ember.Enumerable#any

anyBy

see: Ember.Enumerable#anyBy

compact

see: Ember.Enumerable#compact

compactBy

Removes any elements where a named property is empty, null or undefined.

Parameters
  • key - The name of the property to use for emptiness test.

contains

see: Ember.Enumerable#contains

empty

Returns true if the collection contains zero elements.

emptyBy

Shorthand for compactBy(key).empty()

Parameters
  • key - The name of the property to filter on.

every

see: Ember.Enumerable#every

everyBy

see: Ember.Enumerable#everyBy

filter

see: Ember.Enumerable#filter

filterBy

see: Ember.Enumerable#filterBy

find

see: Ember.Enumerable#find

findBy

see: Ember.Enumerable#findBy

first

Returns the first element of the collection.

invoke

see: Ember.Enumerable#invoke

isEmpty

alias for empty

isEmptyBy

alias for emptyBy

join

Join a collection of strings with a given separator.

Parameters
  • separator - A string to use as the separator for each element. Defaults to ' '

last

Returns the last element of the collection.

length

Returns the number of elements in the collection.

map

see: Ember.Enumerable#map

mapBy

see: Ember.Enumerable#mapBy

nonEmpty

Returns true of the collection has one or more elements in it.

nonEmptyBy

Shorthand for compactBy(key).nonEmpty()

Parameters
  • key - The name of the property to filter on.

reduce

see: Ember.Enumerable#reduce

reject

see: Ember.Enumerable#reject

rejectBy

see: Ember.Enumerable#rejectBy

reverse

see: Array#reverse

note: Enumerology never mutates the original collection, so a new copy of the array is created before being passed into reverse.

setEach

see: Ember.Enumerable#setEach

note: Ember's setEach method doesn't return the original collection, however Enumerology's does to enable chaining.

slice

see: Array#slice

sort

see: Array#sort

notes:

  • Enumerology never mutates the original collection, so a new copy of the array is created before being passed into sort.
  • By default JavaScript sorts collections "lexigraphically", ie in alphabetical order (if no compareFunction is provided).

see also: sortNumerically

sortBy

Sorts a collection of objects by the value of a named property.

Parameters
  • key - the property name to sort on.
  • compareFunction - a function to define a custom sort operation. See: Array#sort

notes:

  • Enumerology never mutates the original collection, so a new copy of the array is created before being passed into sortBy.
  • By default JavaScript sorts collections "lexigraphically", ie in alphabetical order (if no compareFunction is provided).

see also: sortNumericallyBy

sortNumerically

Sorts a collection of objects numerically (instead of lexigraphically, as JavaScript does by default).

see: Array#sort

sortNumericallyBy

Sorts a collection of objects numerically by the value of a named property.

Parameters

  • key the property name to sort on.

take

Take the first n elements from the collection.

Parameters

  • howMany - the number of elements to select from the collection.

toSentence

Convert a collection of strings into a sentence.

Parameters

  • conjunction - The word to use to separate the last element of the list. Defaults to 'and'.
  • oxfordComma - Boolean. Whether or not to put a comma immediately before the conjunction. Defaults to false.

uniq

see: Ember.Enumerable#uniq

without

see: Ember.Enumerable#without

License

Copyright (c) 2013 Resistor Ltd. Licensed under the MIT license.

ember-enumerology's People

Watchers

 avatar  avatar

Forkers

glavin001

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.