Automagic creation of computed properties from enumerable chains.
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"]
Download the production version or the development version.
In your web page:
<script src="dist/ember-enumerology.min.js"></script>
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()
})
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
).
see: Ember.Enumerable#any
Removes any elements where a named property is empty, null
or undefined
.
key
- The name of the property to use for emptiness test.
see: Ember.Enumerable#contains
Returns true if the collection contains zero elements.
Shorthand for compactBy(key).empty()
key
- The name of the property to filter on.
see: Ember.Enumerable#filterBy
Returns the first element of the collection.
alias for empty
alias for emptyBy
Join a collection of strings with a given separator.
separator
- A string to use as the separator for each element. Defaults to' '
Returns the last element of the collection.
Returns the number of elements in the collection.
see: Ember.Enumerable#map
Returns true of the collection has one or more elements in it.
Shorthand for compactBy(key).nonEmpty()
key
- The name of the property to filter on.
see: Ember.Enumerable#rejectBy
see: Array#reverse
note: Enumerology never mutates the original collection, so a new copy of the array is created before being passed into reverse
.
note: Ember's setEach
method doesn't return the original collection, however Enumerology's does to enable chaining.
see: Array#slice
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
Sorts a collection of objects by the value of a named property.
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
Sorts a collection of objects numerically (instead of lexigraphically, as JavaScript does by default).
see: Array#sort
Sorts a collection of objects numerically by the value of a named property.
key
the property name to sort on.
Take the first n
elements from the collection.
howMany
- the number of elements to select from the collection.
Convert a collection of strings into a sentence.
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 tofalse
.
Copyright (c) 2013 Resistor Ltd. Licensed under the MIT license.