A modern helper library. Much inspired by dash.el and s.el, f.el, handlebars-helpers.
Dashbars that is a helper collection be able to easily combined with built-in helpers:
{{-take 5 (-drop 3 (-range 0 10))}} // [3,4,5,6,7]
{{-slice (-range 0 10) 0 5}} // [0,1,2,3,4]
{{#each (-take 5 (-drop 3 (-range 0 10)))}} // [3,4,5,6,7]
...
{{else}}
...
{{/each}}
{{#if (-lt? 3 myVar)}} // 3 < myVar
...
{{else}}
...
{{/if}}
You can try this on trydashbars.
Functions return boolean. The name of predicate should end with question mark(?
).
Basically, You can use the predicate helper to combine with build-in block helpers(if, unless, each):
{{#if (-lt? 3 4)}}
...
{{/if}}
{{#if (-string? 'my string')}}
...
{{/if}}
You can chain helpers:
{{#if (-not? (-gt? 3 4))}}
...
{{/if}}
There are common predicates for object:
- -is?
(o)
- -and?
(...)
- -or?
(...)
- -not?
(boolean)
- -gt?
(left, right)
- -lt?
(left, right)
- -ge?
(left, right)
- -le?
(left, right)
- -ne?
(left, right)
- -equal?
(left, right)
- -deep-equal?
(left, right)
- -in?
(prop, o)
- -has?
(prop, o)
- -empty?
(o)
- -not-empty?
(o)
- -string?
(o)
- -array?
(o)
List and Dictionary are abstract term. List demonstrates Array and Set of ES6, somethings which are iterable like list. Dictionary demonstrates plain JavaScript Object and Map of ES6, something which are key-value data structure.
As of now, dashbars tested and supports ES5 only so that List is Array and Dictionary is plain Object.
Functions take a list return new list.
All functions which you need are also helper. You should register your helper. And then you can use the name of your helper:
Handlebars.registerHelper('-as-is', function(o){
return o;
});
{{-json (-map '-as-is' (-range 0 5))}} // [0,1,2,3,4]
Predicates are the same:
Handlebars.registerHelper('n-even?', function(n){
return n % 2 === 0;
});
{{-json (-take-while 'n-even?' (-range 0 5))}} // [0]
- -map
(fn, list)
- -sort
(list, compare)
- -take
(n, list)
- -drop
(n, list)
- -filter
(pred, list)
- -take-while
(pred, list)
- -drop-while
(pred, list)
- -slice
(list, begin, end)
- -flatten
(list)
- -deep-flatten
(list)
Functions construct new JavaScript object.
Functions reducing lists into single value.
- -size
(list)
- -find
(pred, list)
- -reduce
(fn, initial, list)
- -first
(list)
- -last
(list)
- -join
(list, sep)
- -sum
(list)
- -product
(list)
- -min
(list)
- -max
(list)
Functions partitioning the input list into multiple lists.
- -group-by
(fn, list)
Functions transforming the input list into a list of lists for easy iteration.
- -grouped
(size, list)
Operations pretending lists are sets.
- -union
(...)
- -difference
(...)
- -intersection
(...)
- -distinct
(list)
Operations on dictionaries.
- -json
(o)
Combinational functions, a bit experimental:
- n-add
(left, right)
- n-subtract
(left, right)
- n-multiply
(left, right)
- n-divide
(left, right)
- n-modulo
(left, right)
- n-bls
(left, right)
- n-brs
(left, right)
- n-brss
(left, right)
- n-band
(left, right)
- n-bor
(left, right)
- n-bxor
(left, right)
- s-size
(s)
- s-trim
(s)
- s-take
(n, s)
- s-drop
(n, s)
- s-repeat
(n, s)
- s-concat
(...)
- s-split
(sep, s)
- s-slice
(s, from, to)
- s-reverse
(s)
- s-replace
(old, new, s, regOpts)
- s-match
(regex, s, regOpts)
- s-lowercase
(s)
- s-uppercase
(s)
- s-lowercase?
(s)
- s-uppercase?
(s)
- s-match?
(regex, s, regOpts)
- s-contain?
(needle, s, ignoreCase)
- s-start-with?
(prefix, s, ignoreCase)
- s-end-with?
(suffix, s, ignoreCase)
Functions and Helpers
Template Data:
{
funct0: function(){},
funct1: function(){ return 1;},
objec0: {},
objec01: {},
objec1: {key:'value'},
objec11: {key:'value'},
objec2: {
key:"value",
key2:"value2"
},
array0: [],
array1: [1],
truee0: true,
false0: false,
strin0: '',
strin1: 'string',
numbe0: 0,
numbe1: 1
}
Function returns false when o
is falsey value, otherwise returns true.
{{-is? false}} // => false
{{-is? null}} // => false
{{-is? undefined}} // => false
{{-is? 0}} // => false
{{-is? ''}} // => false
{{-is? true}} // => true
{{-is? 'undefined'}} // => true
{{-is? 1}} // => true
{{-is? false0}} // => false
{{-is? strin0}} // => false
{{-is? numbe0}} // => false
{{-is? truee0}} // => true
{{-is? funct0}} // => true
{{-is? objec0}} // => true
{{-is? array0}} // => true
{{-is? strin1}} // => true
{{-and? true false}} // => false
{{-and? true true}} // => true
{{-and? true true 0}} // => false
{{-and? true true 1}} // => true
{{-or? true false}} // => true
{{-or? false false}} // => false
{{-or? false false 0}} // => false
{{-or? false false 1}} // => true
{{-not? true}} // => false
{{-not? false}} // => true
{{-not? 0}} // => true
{{-not? 1}} // => false
{{-gt? 1 2}} // => false
{{-gt? 2 2}} // => false
{{-gt? 2 1}} // => true
{{-gt? 'a' 'b'}} // => false
{{-gt? 'a' 'a'}} // => false
{{-gt? 'b' 'a'}} // => true
{{-lt? 1 2}} // => true
{{-lt? 2 2}} // => false
{{-lt? 2 1}} // => false
{{-lt? 'a' 'b'}} // => true
{{-lt? 'a' 'a'}} // => false
{{-lt? 'b' 'a'}} // => false
{{-ge? 1 2}} // => false
{{-ge? 2 2}} // => true
{{-ge? 2 1}} // => true
{{-ge? 'a' 'b'}} // => false
{{-ge? 'a' 'a'}} // => true
{{-ge? 'b' 'a'}} // => true
{{-le? 1 2}} // => true
{{-le? 2 2}} // => true
{{-le? 2 1}} // => false
{{-le? 'a' 'b'}} // => true
{{-le? 'a' 'a'}} // => true
{{-le? 'b' 'a'}} // => false
{{-ne? 1 2}} // => true
{{-ne? 2 2}} // => false
{{-ne? 'a' 'b'}} // => true
{{-ne? 'a' 'a'}} // => false
equals strictly(===
)
{{-equal? objec0 objec0}} // => true
{{-equal? objec0 objec01}} // => false
{{-deep-equal? objec1 objec1}} // => true
{{-deep-equal? objec1 objec11}} // => true
{{-equal? objec1 objec11}} // => false
{{-in? 'objec1' this}} // => true
{{-in? 'not' this}} // => false
'Object.hasOwnProperty()'
{{-has? 'objec1' this}} // => true
{{-has? 'not' this} // => false
{{-empty? false}} // => true
{{-empty? null}} // => true
{{-empty? undefined}} // => true
{{-empty? 0}} // => true
{{-empty? ''}} // => true
{{-empty? true}} // => true
{{-empty? 'undefined'}} // => false
{{-empty? 1}} // => true
{{-empty? false0}} // => true
{{-empty? strin0}} // => true
{{-empty? numbe0}} // => true
{{-empty? truee0}} // => true
{{-empty? funct0}} // => true
{{-empty? funct1}} // => true
{{-empty? objec0}} // => true
{{-empty? objec1}} // => false
{{-empty? array0}} // => true
{{-empty? array1}} // => false
{{-empty? strin1}} // => false
-not-empty?
equals {{-not? (-empty something)}}
.
{{-not-empty? false}} // => false
{{-string? false}} // => false
{{-string? 'true'}} // => true
{{-string? ''}} // => true
{{-array? false}} // => false
{{-array? array0}} // => true
{{-array? array1}} // => true
{{-map 'n-even?' (-range 0 5)}} // => [true,false,true,false,true]
{{-sort (-range 0 5) '-lt?'}} // => [4,3,2,1,0]
{{-take 3 (-range 0 5)}} // => [0,1,2]
{{-drop 3 (-range 0 5)}} // => [3,4]
{{-filter 'n-even?' (-range 0 5)}} // => [0,2,4]
{{-take-while 'n-even?' (-range 0 5)}} // => [0]
{{-drop-while 'n-even?' (-range 0 5)}} // => [1,2,3,4]
{{-slice (-range 0 5) 0 3}} // => false
//[[[0,1,2], [0,1,2]],[0,1,2]]
{{-flatten (-array (-array (-range 0 3) (-range 0 3)) (-range 0 3))}} // => [[0,1,2],[0,1,2],0,1,2]
//[[[0,1,2], [0,1,2]],[0,1,2]]
{{-json (-deep-flatten (-array (-array (-range 0 3) (-range 0 3)) (-range 0 3)))}} // => [0,1,2,0,1,2,0,1,2]
{{-array (-array (-range 0 3) (-range 0 3)) (-range 0 3)}} // => [[[0,1,2], [0,1,2]],[0,1,2]]
{{-range 0 3}} // => [0,1,2]
{{{-json (-object '{"key":"value"}')}}} // => {"key":"value"}
{{-size (-range 0 3)}} // => 3
{{-find 'n-odd?' (-range 2 5)}} // => 3
{{-reduce 'n-add' 0 (-range 0 10)}} // => 45
{{-first (-range 3 10)}} // => 3
{{-last (-range 3 10)}} // => 9
{{-join (-range 0 5) '-'}} // => 0-1-2-3-4
{{-sum (-range 0 5)}} // => 10
{{-product (-range 1 5)}} // => 24
{{-min (-range 0 5)}} // => 0
{{-max (-range 0 5)}} // => 4
{{{-json (-group-by 'n-even?' (-range 0 5))}}} // => {"true":[0,2,4],"false":[1,3]}
{{{-json (-grouped 2 (-range 0 5))}}} // => [[0,1],[2,3],[4]]
{{-every? 'n-even?' (-range 0 5)}} // => false
{{-every? 'n-even?' (-array 0 2 4 6)}} // => true
{{-every? 'n-even?' (-array 1 3 5)}} // => false
{{-every? 'n-even?' (-array)}} // => true
{{-some? 'n-even?' (-range 0 5)}} // => true
{{-some? 'n-even?' (-array 0 2 4 6)}} // => true
{{-some? 'n-even?' (-array 1 3 5)}} // => false
{{-some? 'n-even?' (-array)}} // => false
{{-none? 'n-even?' (-range 0 5)}} // => false
{{-none? 'n-even?' (-array 0 2 4 6)}} // => false
{{-none? 'n-even?' (-array 1 3 5)}} // => true
{{-none? 'n-even?' (-array)}} // => false
{{-contain? 0 (-range 1 5)}} // => false
{{-contain? 1 (-range 1 5)}} // => true
{{-union (-range 0 5) (-range 0 5)}} // => [0,1,2,3,4,0,1,2,3,4]
{{-difference (-range 0 5) (-range 3 8)}} // => [0,1,2]
{{-intersection (-range 0 5) (-range 3 8)}} // => [3,4]
{{-distinct (-array 0 0 1 1 2 2 3 3 4 4 5 5)}} // => [0,1,2,3,4,5]
sample data:
objec2 = {
"key":"value",
"key2":"value2"
}
{{-get 'key' objec2}} // => "value"
{{-keys objec2}} // => ["key", "key2"]
{{-values objec2}} // => ["value","value2"]
{{{-json (-range 0 5)}}} // => [1,2,3,4,5]
{{{-json objec2}}} // => {"key":"value","key2":"value2"}
{{{-as-is (-range 0 5)}}} // => [1,2,3,4,5]
{{{-map '-as-is' (-range 0 5)}}} // => // => [1,2,3,4,5]
{{{-group-by '-as-is' (-range 0 5)}}} // => // => {"0":[0],"1":[1],"2":[2],"3":[3],"4":[4]}
A function returns a partially applied function. You can chain other functions.
{{-filter (-partial '-gt?' 3) (-range 0 5)}} // => [0,1,2]
-call
calls a function at once.
{{{-call (-partial '-gt?' 3) 2}}} // => true
You can define data in current context of Template Data. It is simple concept, this[name] = value
.
It returns an empty string(''
).
{{{-let 'name' true}}} // => ''
You can log on console. It is simple concept, console.log(...)
.
It returns an empty string(''
).
{{{-log 'my log'}}} // => ''
{{{n-even? 0}}} // => true
{{{n-even? 1}}} // => false
{{{n-odd? 1}}} // => true
{{{n-odd? 2}}} // => false
{{{n-add 10 5}}} // => 15
{{{n-subtract 10 5}}} // => 5
{{{n-multiply 2 5}}} // => 10
{{{n-divide 10 2}}} // => 5
{{{n-modulo 10 3}}} // => 1
{{{n-bls 1 2}}} // => 4
Zero fill right shift.
{{{n-brs 2 1}}} // => 1
{{{n-brs -1 2}}} // => 0x3FFFFFFF
Sign propagating right shift.
{{{n-brs 2 1}}} // => 1
{{{n-brs -1 2}}} // => 1
{{{n-band 5 3}}} // => 1
{{{n-bor 5 3}}} // => 7
{{{n-bxor 5 3}}} // => 6
{{{s-size 'string'}}} // => 6
{{{s-trim ' string '}}} // => 'string'
{{{s-take 2 'string'}}} // => 'st'
{{{s-drop 2 'string'}}} // => 'ring'
{{{s-repeat 2 'string'}}} // => 'stringstring'
{{{s-concat 'st' 'ri' 'ng'}}} // => 'string'
{{{s-split ',' 's,t,r,i,n,g'}}} // => ['s','t','r','i','n','g']
{{{s-slice 'string' 1 4}}} // => 'tri'
{{{s-slice 'string' 1}}} // => 'tring'
{{{s-slice 'string'}}} // => 'string'
{{{s-slice 'string' 0 -1}}} // => 'strin'
{{{s-reverse 'string'}}} // => 'gnirts'
{{{s-replace 'str' 'int' 'string string STRING'}}} // => "inting string STRING"
{{{s-replace 'str' 'int' 'string string STRING' 'g'}}} // => "inting inting STRING"
{{{s-replace 'str' 'int' 'string string STRING' 'gi'}}} // => "inting inting intING"
{{{s-match 's.+?i' 'string string STRING'}}} // => ["stri"]
{{{s-match 's.+?i' 'string string STRING' 'g'}}} // => ["stri","stri"]
{{{s-match 's.+?i' 'string string STRING' 'gi'}}} // => ["stri","stri","STRI"]
{{{s-match 'si' 'string string STRING' 'gi')}}} // => []
{{{s-lowercase 'STRing'}}} // => 'string'
{{{s-uppercase 'STRing'}}} // => 'STRING'
{{{s-lowercase? 'STRING'}}} // => false
{{{s-lowercase? 'STRing'}}} // => false
{{{s-lowercase? 'string'}}} // => true
{{{s-uppercase? 'STRING'}}} // => true
{{{s-uppercase? 'STRing'}}} // => false
{{{s-uppercase? 'string'}}} // => false
{{{s-match? '.*r' 'string'}}} // => true
{{{s-match? 'g.*r' 'string'}}} // => false
{{{s-contain? 'str' 'string'}}} // => true
{{{s-contain? 'gnitrs' 'string'}}} // => false
{{{s-start-with? 'str' 'string'}}} // => true
{{{s-start-with? 'tri' 'string'}}} // => false
{{{s-end-with? 'ing' 'string'}}} // => true
{{{s-end-with? 'rin' 'string'}}} // => false
Start with:
-
: Collections functions and basic utilityn-
: Numeric functionsd-
: Date functionss-
: String functions
- Predicate should return real boolean(not falsey/truey values)
- The name of predicate should end with question mark('?')
- Mandatory arguments should be before target object, but Optional arguments should be after target object. Let's see s-replace:
{{s-replace old new s regOpts}}
old
andnew
are mandatory arguments.s
is main argumentregOpts
is optional.
MIT © Changwoo Park