Neither rivetsjs nor rivetsjs-stdlib is under develepment anymore |
A bunch of daily used formatters and bindings for rivetsjs.
Stdlib was built as a simple drop-in-place library for common problems while working with rivetsjs. It extends rivetsjs with severals formatters (as example default
and numberFormat
) and binders. If you find something missing, unclear written or you got any problems then feel free to send me a message or an email
Please dont miss to take a look at the test cases
The library has been built for rivetjs 0.7.0. And optional for jQuery 2.1.1 and Momentjs 2.8.3
jQuery is only needed for the template binder. Momentjs is only needed for the date formatters.
The library has the same browser support as rivetjs
Rivetsjs-stdlib is dual licensed with MIT and GPL
All settings can be found in rivets.stdlib. You can change the values according to your locale!
Value | Default | Description |
---|---|---|
defaultPrecision | 2 | numberFormat will return a number with defaultPrecision places after the comma |
defaultThousandSeparator | ' | the symbol used to separated every group of thousands |
defaultDecimalSeparator | . | the symbol used as the decimal point |
defaultDateFormat | YYYY-MM-DD | momentjs pattern for the formatter date |
defaultTimeFormat | HH:mm:ss | momentjs pattern for the formatter time |
defaultDatetimeFormat | YYYY-MM-DD HH:mm:ss | momentjs pattern for the formatter datetime |
The value on which the formatter will be applied is always called target
. In case that the formatter is marked as variadic then you might pass as many parameters as you wish
Shortcut | Description | ? |
---|---|---|
eq | isEqual | |
ne | isEqual && negate | |
lt | isLess | |
gt | isGreater | |
le | isLessEqual | |
lte | isLessEqual | |
ge | isGreaterEqual | |
gte | isGreaterEqual | |
neg | negate | |
prv | preventDefault | |
inject | stringFormat | |
format | dateFormat | |
len | length | |
def | default | |
stringify | prettyPrint | |
int | integer |
Returns target in a humanreadable string
- target: any
- return: string
Displays target in the browser console
- target: any
This formatter returns a default value for target
if it is empty (detected with the formatter isEmpty)
- target: any
- param val: any | if target is empty then this value will be returned
- return: any
Example:
<span rv-text="notexistingvalue | default 'property does not exist'"></span>
Result:
<span>property does not exist</span>
Uses the + operation between target
and the given parameter without doing any conversion (see sum). Therefor this
function can be used to concat strings as well
- target: any
- param val: any
- return: int, float, NaN, str
Example:
<span rv-text="12 | add 1"></span>
Result:
<span>13</span>
Uses the - operation between target
and the given parameter without doing any conversion (see substract)
- target: any
- param val: any
- return: int, float, NaN, str
Example:
<span rv-text="12 | sub 1"></span>
Result:
<span>11</span>
Calls a method on the given object. The first parameters defines the object and the second the methodname. Target will be passed as the first argument to the method.
- target: any
- param obj: object
- param property: string
- variadic: any
- return: any
Example:
<span rv-text="10 | map 'Math' 'sin'"></span>
Result:
<span>-0.5440211108893699</span>
Returns true if the target represents an empty state (empty array, empty string, false, etc)
- target: any
- return: boolean
Returns true if the given target is of the type boolean
- target: any
- return: bool
Returns true if the given target can be expressed as a numeric value. This covers integers, floats, strings and booleans
- target: any
- return: bool
Returns true if the given target can not be expressed as a numeric value. This covers objects, arrays and strings
- target: any
- return: bool
Returns true if the given target is an integer
- target: any
- return: bool
Returns true if the given target is a float
- target: any
- return: bool
Returns true if the given target is an integer or a float
- target: any
- return: bool
Returns true if the given target is an object
- target: any
- return: bool
Returns true if the given target is a function
- target: any
- return: bool
Returns true if the given target is an array
- target: any
- return: bool
Returns true if the given target is a string
- target: any
- return: bool
Returns true if the given target is infinity
- target: any
- return: bool
Returns the boolean representation of the given target. The conversion is similiar to the behaviour of if() {}
- target: any
- return: bool
Returns the integer representation of the given target.
- target: any
- return: integer
Returns the float representation of the given target
- target: any
- return: float
Returns the integer representation of the given target if the float representation is not more precise
- target: any
- return: integer|float
Returns the array representation of the given target. Objects will be flatten down by their values and single values will be wrapped in a array. Arrays will be returned unchanged
- target: any
- return: array
Returns the string representation of the given target. This actually calls the JS method toString()
- target: any
- return: string
Exactly the same as toInteger but implemented as two-way formatter
- two-way
- target: any
- return: string
Returns true if the target and the first parameter are equal
- target: any
- parameter val: any
- return: bool
Returns true if the target is smaller as first parameter. Both values will be converted to a numeric representation
- target: any
- parameter val: any
- return: bool
Returns true if the target is greater as first parameter. Both values will be converted to a numeric representation
- target: any
- parameter val: any
- return: bool
Returns true if the target is smaller or is equal to the first parameter. Both values will be converted to a numeric representation
- target: any
- parameter val: any
- return: bool
Returns true if the target is greater or is equal to the first parameter. Both values will be converted to a numeric representation
- target: any
- parameter val: any
- return: bool
Returns true if the target or one of parameters are true
- target: any
- variadic: any
- return: bool
Returns true if the target and all parameters are true
- target: any
- variadic: any
- return: bool
Returns the negated value of target
- target: any
- return: bool
Returns the first parameter if the target is true or returns the second parameter
- target: bool
- param trueCase: any | will be returned if target is true
- param falseCase: any | will be returned if target is false
- return: any
Returns the sum of the target and the first parameter. Both values will be converted to a numeric representation
- target: any
- parameter val: any
- return: integer|float
Returns the substraction of the target and the first parameter. Both values will be converted to a numeric representation
- target: any
- parameter val: any
- return: integer|float
Returns the multiplication of the target and the first parameter. Both values will be converted to a numeric representation
- target: any
- parameter val: any
- return: integer|float
Returns the division of the target and the first parameter. Both values will be converted to a numeric representation. If the denominator is 0 then Infinity is returned
- target: any
- parameter val: any
- return: integer|float|Infinity
Returns the smallest number from the passed parameters and the target
- target: integer|float
- variadic: integer|float
- return: integer|float|Infinity|NaN
Returns the biggest number from the passed parameters and the target
- target: integer|float
- variadic: integer|float
- return: integer|float|Infinity|NaN
Returns a formatted version of the target as string. The number will always be rounded after the DIN 1333 (1.55 => 1.6 and -1.55 => -1.6)
- target: integer|float
- parameter precision: integer default rivets.stdlib.defaultPrecision
- parameter decimalSeparator: string default rivets.stdlib.defaultDecimalSeparator
- parameter thousandSeparator: string default rivets.stdlib.defaultThousandSeparator
- return: string
Returns the target with all appearance of %s replaced by the according parameter
- target: string
- variadic: string
- return: string
Example:
<span rv-text="'some %s for %s' | stringFormat 'text' 'you'"></span>
Result:
<span>some text for you</span>
Splits the target into an array by a given seperator
- target: string
- param val: string
- return: array
Converts the target to lowercase
- target: string
- return: string
Converts the target to uppercase
- target: string
- return: string
Converts the first letter of every word to uppercase in target. Every substring separated by a space or a - will be detected as a word. The rest stays the it used to (URL will not be converted to Url)
- target: string
- return: string
Example:
<span rv-text="'string teXt-caSe uRl' | capitalize"></span>
Result:
<span>String TeXt-CaSe URl</span>
Returns true if the target contains the given substring or if target array holds the parameter
- target: string|array
- return: bool
Shortcut for the function ['array'] | contains 'value' | negate
- target: string|array
- return: bool
Returns the string length, the array length or the count of keys of an object
- target: any
- return: integer
Returns the date portion as string from target (JS Date). Default formatting is rivets.stdlib.defaultDateFormat
- target: Date
- return: string
Returns the time portion as string from target (JS Date). Default formatting is rivets.stdlib.defaultTimeFormat
- target: Date
- return: string
Returns a datetime as string from target (JS Date). Default formatting is rivets.stdlib.defaultDatetimeFormat
- target: Date
- return: string
Returns the unix timestamp from target (JS Date)
- target: Date
- return: integer
Returns the JS Date object representing the given unix timestamp
- target: integer
- return: Date
Returns the momentjs object representing of the given JS Date. (use this method afterwards with the formatter map
)
- target: Date
- return: momentjs
Returns a string formatted with momentjs.format. The first parameter specifies the format pattern
- target: JS Date
- param val: string | documented in momentjs
- return: string
Returns an object holding the object, key and the corresponding value. The returned object has therefor always three properties: object, property and value
- target: object
- return: object
Example:
<div rv-each-item="myobject | pairs">
Found key { item.property | stringify } with value { item.value | stringify }
</div>
Returns all keys of target
- target: object
- return: array
Returns all values of target
- target: object
- return: array
Returns the string length, the array length or the count of keys of an object
- target: any
- return: integer
Returns the string by joining the target with the given parameter
- target: array
- param val: string
- return: string
Returns true if the target contains the given substring or if target array holds the parameter
- target: string|array
- return: bool
Returns the string length, the array length or the count of keys of an object
- target: any
- return: integer
Returns a new function which will call target with the arguments given to wrap and with the arguments used in the event caller. The arguments passed to wrap can be accessed as the first arguments
- target: function
- variadic: any
- return: function
Example:
<div rv-each-item="collection">
<div rv-on-click="aClickHandler | wrap item"></div>
</div>
Result:
function aClickHandler(item, event) {
/* a function with those arguments will be called */
}
Returns a anonym functions which calls target with a delay
- target: function
- param ts: integer | delay in milliseconds
- return: function
Returns a anonym functions which calls preventDefault and afterwards target
- target: function
- return: function
Resets the css width value with the target
Resets the css height value with the target
The template binder is aquivalent to rv-html with the exceptions that the data bindings are accessible in the included html. You should not use that functions since rivetsjs provides a much better way (components). But for the most people this binder just will work fine and for non-complex problem its absolute legit to use it
Example:
<div id="mydiv" rv-template="myhtml"></div>
<script>
rivets.bind($('#mydiv'), {myhtml: '<span rv-text="val"></span>', val: 'nice'})
</script>
Results:
<div id="mydiv"><span>nice</span></div>