Overview
Some general helpers for Meteor Blaze including comparison, logic and numeric operators, logging, objects manipulation.
Installation
meteor add imajus:common-helpers
Related packages
Contents
Package provides following global Blaze helpers:
log(...args)
– Just logs all argument passed to the browser console.field(object, path)
– Extract a field from object by path (dot delimeters can be used):
1{{field targetObject fieldName}} 2{{field user 'email.0.address'}}
not(val)
– Equivalent of!val
.eq(...args)
– Returnstrue
only if all arguments passed are equal (tested using==
operator).
Note: returns false
if only one argument is passed.
is(...args)
– Returnstrue
only if all arguments passed are identical (tested using===
operator).
Note: returns false
if only one argument is passed.
and(...args)
– Returns true-like value if all arguments passed are true-like, return false-like otherwise (for using in conditions). Strictly speaking, helper returns first false-like value found in arguments or the last argument if all are true-like:
1{{#if and currentUser someState somethingElse}} 2 <div class="{{and someStringVariable 'replacement'}}"> 3{{/if}}
or(...args)
– Returns true-like value if any argument passed is true-like, return false-like if all arguments are false-like (for using in conditions). Strictly speaking, helper returns first true-like value found in arguments or the last argument if all are false-like:
1{{#if or currentUser isEmulator}} 2 <div class="{{or someStringVariable 'fallback'}}"> 3{{/if}}
sum(...args)
– Sums all arguments passed using+=
operator (can also be used for string concatenation, but for that better to useconcat
from imajus:string-helpers):
1<div>{{sum price fee tax}}</div>
sub(...args)
- Subtract arguments starting from 2nd from 1st.
1<div>{{sub capacity used}}</div>
positive(...args)
– Returnstrue
only if all passed arguments are greater than zero, returnsfalse
otherwise.
1{{#if positive balance}}...{{/if}}
negative(...args)
– Returnstrue
only if all passed arguments are less than zero, returnsfalse
otherwise.
1{{#if negative balance}}...{{/if}}
gt(...args, { comp })
– Returnstrue
only if all passed arguments are greater thancomp
, returnsfalse
otherwise.
1{{#if gt 9 balance comp=price}}...{{/if}}
gt(...args, { comp })
– Returnstrue
only if all passed arguments are greater than or equal tocomp
, returnsfalse
otherwise.
1{{#if gte 10 balance comp=price}}...{{/if}}
lt(...args, { comp })
– Returnstrue
only if all passed arguments are less thancomp
, returnsfalse
otherwise.
1{{#if lt 100 price comp=balance}}...{{/if}}
lte(...args, { comp })
– Returnstrue
only if all passed arguments are less than or equal tocomp
, returnsfalse
otherwise.
1{{#if lte 99 price comp=balance}}...{{/if}}
nullOrUndefined(...args)
– Returnstrue
only if all passed arguments arenull
orundefined
.when(...)
- Analogue of ternary operator. Has two forms:when(cond, yes, no)
- Returnsyes
value whencond
is true-like, returnsno
value otherwise.when(...conds, { yes, no })
- Returnsyes
value when allconds
are true-like, returnsno
value if any ofconds
false-like. This form allows not to passyes
and/orno
at all, in which caseundefined
will be used for missing value.
1<div class="{{when loaded 'loaded' 'loading'}}"> 2<div class="{{when loaded 'loaded'}}"> 3<div class="{{when loaded no='loading'}}"> 4<div class="{{when loaded ready subsReady yes='ready' no='loading'}}"> 5<div class="{{when loaded ready subsReady yes='ready'}}"> 6<div class="{{when loaded ready subsReady no='loading'}}">
chunk(array, size)
– Splits array into chunks of equalsize
and return array of arrays. The latestchunk
may have less elements than previous depending on the length of original array.
1{{#each group in chunk items 3}} 2 <div class="row"> 3 {{#each item in group}} 4 <div class="col">...</div> 5 {{/each}} 6 </div> 7{{/each}}