Composable helpers for Ember that enables more declarative templating. These helpers can be composed together to form powerful ideas:
To install:
Ember 3.28+:
ember install ember-composable-helpers
Watch a free video overview presented by EmberMap:
- Compatability
- Configuration
- Argument ordering
- Upgrade Guide
- Available helpers
- Legal
- Contributors
- [ember-source][gh-ember-source] v3.28+
- [typescript][gh-typescript] v4.8+
- [ember-auto-import][gh-ember-auto-import] v2+
If you don't need all the helpers, you can specify which to include or remove from your build using only
or except
within your ember-cli-build.js
:
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
'ember-composable-helpers': {
only: ['inc', 'dec', 'pipe'],
except: ['filter-by']
}
});
}
Both only
and except
can be safely used together (the addon computes the diff), although it's best if you only use one for your own sanity.
except: ['pipe'] // imports all helpers except `pipe`
only: ['pipe'] // imports only `pipe`
This addon is built with composability in mind, and in order to faciliate that, the ordering of arguments is somewhat different then you might be used to.
For all non-unary helpers, the subject of the helper function will always be the last argument. This way the arguments are better readable if you compose together multiple helpers:
For action helpers, this will mean better currying semantics:
For help upgrading between major versions, check out the upgrading documentation.
Pipes the return values of actions in a sequence of actions. This is useful to compose a pipeline of actions, so each action can do only one thing.
The pipe
helper is Promise-aware, meaning that if any action in the pipeline returns a Promise, its return value will be piped into the next action. If the Promise rejects, the rest of the pipeline will be aborted.
The pipe
helper can also be used directly as a closure action (using pipe-action
) when being passed into a Component, which provides an elegant syntax for composing actions:
Calls the given function with arguments
Calls an action as a template helper.
Toggles a boolean value.
toggle
can also be used directly as a closure action using toggle-action
:
toggle
also accepts optional values to rotate through:
Returns an empty function.
Allows for the passed in action to not exist.
Like pipe
, this helper runs actions in a sequence (from left-to-right). The
difference is that this helper passes the original arguments to each action, not
the result of the previous action in the sequence.
If one of the actions in the sequence returns a promise, then it will wait for that promise to resolve before calling the next action in the sequence. If a promise is rejected it will stop the sequence and no further actions will be called.
Maps a callback on an array.
Maps an array on a property.
Sort an array by given properties.
You can append :desc
to properties to sort in reverse order.
You can also pass a method as the first argument:
Filters an array by a callback.
Filters an array by a property.
If you omit the second argument it will test if the property is truthy.
You can also pass an action as second argument:
The inverse of filter by.
If you omit the third argument it will test if the property is falsey.
You can also pass an action as third argument:
Returns the first entry matching the given value.
Creates an array of unique values that are included in all given arrays.
Invokes a method on an object, or on each object of an array.
Joins arrays to create an array of unique values. When applied to a single array, has the same behavior as uniq
.
Returns the first n
entries of a given array.
Returns an array with the first n
entries omitted.
Reduce an array to a value.
The last argument is initial value. If you omit it, undefined will be used.
Repeats n
times. This can be useful for making an n-length arbitrary list for iterating upon (you can think of this form as a times helper, a la Ruby's 5.times { ... }
):
You can also give it a value to repeat:
Reverses the order of the array.
Generates a range of numbers between a min
and max
value.
It can also be set to inclusive
:
And works with a negative range:
Joins the given array with an optional separator into a string.
Removes blank items from an array.
Checks if a given value or sub-array is included within an array.
Appends the given arrays and/or values into a single flat array.
Returns the given array split into sub-arrays the length of the given value.
Returns the given array without the given item(s).
Shuffles an array with a randomizer function, or with Math.random
as a default. Your randomizer function should return a number between 0 and 1.
Flattens an array to a single dimension.
Returns the object at the given index of an array.
Slices an array
Returns the next element in the array given the current element. Note: Accepts an optional boolean
parameter, useDeepEqual
, to flag whether a deep equal comparison should be performed.
Checks if the array has an element after the given element. Note: Accepts an optional boolean
parameter, useDeepEqual
, to flag whether a deep equal comparison should be performed.
Returns the previous element in the array given the current element. Note: Accepts an optional boolean
parameter, useDeepEqual
, to flag whether a deep equal comparison should be performed.
Checks if the array has an element before the given element. Note: Accepts an optional boolean
parameter, useDeepEqual
, to flag whether a deep equal comparison should be performed
Returns an array of a given object's own enumerable string-keyed property [key, value]
pairs
You can pair it with other array helpers too. For example
Converts a two-dimensional array of [key, value]
pairs into an Object
You can pair it with other array helpers too. For example, to copy only properties with non-falsey values:
Returns an object where the keys are the unique values of the given property, and the values are an array with all items of the array that have the same value of that property.
Returns an array of keys of given object.
Receives an object and picks a specified path off of it to pass on. Intended for use with {{on}}
modifiers placed on form elements.
It also supports an optional second argument to make common usage more ergonomic.
Returns an array of values from the given object.
Increments by 1
or step
.
Decrements by 1
or step
.
String helpers were extracted to the ember-cli-string-helpers addon.
DockYard, Inc © 2016
Licensed under the MIT license
We're grateful to these wonderful contributors who've contributed to ember-composable-helpers
: