A modern fork of the original utility library.
template tag gts/gjs
example:
// note that importing from this file includes *all* helpers
import { mapBy, pipe, mut } from '@nullvoxpopuli/ember-composable-helpers';
<template>
{{#each (mapBy "fullName" users) as |fullName|}}
<input type="text" value={{fullName}} onchange={{mut newName}}>
<button {{on 'click' (pipe updateFullName saveUser) newName}}>
Update and save {{fullName}} to {{newName}}
</button>
{{/each}}
</template>
If you only want one (or a few) utilit(y|ies), you may be interested in the individual exports instead:
import { mapBy } from '@nullvoxpopuli/ember-composable-helpers/helpers/map-by';
these are all the same paths as provided by the original ember-composable-helpers
class example:
To install:
Ember 3.28+:
npm add @nullvoxpopuli/ember-composable-helpers
To use in an existing project, to replace the original ember-composable-helpers:
under webpack, configure an alias:
resolve: {
alias: {
"ember-composable-helpers": "@nullvoxpopuli/ember-composable-helpers",
},
},
under classic builds, using ember-auto-import, the alias would be configured this way:
// ember-cli-build.js
autoImport: {
alias: {
"ember-composable-helpers": "@nullvoxpopuli/ember-composable-helpers",
},
},
Note that under auto-import, all helpers will be included. Under embroider, when static helpers/components are turned on, only what you use will be in the build output.
For Assessing impact, the whole library is 38K (no min, no gzip or brotly)
❯ du --depth 1 --reverse --apparent-size --no-percent-bars --filter ".js$" ember-composable-helpers/dist/
38K └─┬ dist
26K ├── helpers
4.4K ├── utils
3.8K ├── _app_
2.5K ├── index.js
1003B └── -private
Running thruogh terser, the whole library becames 24K
❯ du --depth 1 --reverse --apparent-size --no-percent-bars --filter ".min$" ember-composable-helpers/dist/
24K └─┬ dist
15K ├── helpers
3.5K ├── _app_
2.6K ├── utils
2.2K ├── index.js.min
658B └── -private
And then running through gzip (build -> min -> gzip)
❯ du --depth 1 --reverse --apparent-size --no-percent-bars --filter ".gz$" ember-composable-helpers/dist/
17K └─┬ dist
10K ├── helpers
4.7K ├── _app_
1.3K ├── utils
443B ├── -private
416B └── index.js.min.gzip
And then running through brotly (build -> min -> brotli)
❯ du --depth 1 --reverse --apparent-size --no-percent-bars --filter ".br$" ember-composable-helpers/dist/
13K └─┬ dist
8.3K ├── helpers
3.4K ├── _app_
1.0K ├── utils
370B ├── index.js.min.br
295B └── -private
- 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+
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.
We're grateful to these wonderful contributors who've contributed to ember-composable-helpers
: