ko-typed provides observable extensions for restricting and converting observable values based on type. Supports validation.
Converters between all common types are provided where conversion is common and unambiguous.
The converters API can be accessed from ko.typed
var converted = converter(value[, options]);
-
value
The input value to be converted -
options
(optional) Options used to control the conversion. May be a value specifying the default configurable option, or an object literal. Values override the default options.
options
The default options used when calling the function. May be overridden / replaced.
The .addConverter
method adds a converter to the internal list of standard converters.
This method is chainable.
ko.typed.addConverter(fromTypeName, toTypeName, converter[, defaultOptions[, defaultOption]]);
-
fromTypeName
TypeName of the sourcevalue
. Standard TypeName's are provided by the is-an library. -
toTypeName
TypeName of the result. -
converter
A function with the signatureconverter(value[, options])
. ThrowsTypeError
if the conversion cannot be performed with the given value/options. This function is not expected to validate the type of incoming arguments. The resulting type is expected to be oftoTypeName
. When called,options
will be an object literal containing the default options and overridden options. -
defaultOptions
(optional) An object literal containing the default options which will be provided toconverter
. -
defaultOption
(optional) The name of a key withindefaultOptions
. Whenconverter
is called with an options argument which is not anObject
, the specified option will be overridden.
function Converter(value, options) {
// See https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
return JSON.parse(value, options.reviver);
}
ko.typed.addConverter('String', 'Object.Literal', Converter, { reviver: undefined }, 'reviver');
var converter = ko.typed.getConverter('String', 'Object.Literal');
var converted = converter('{ "a": 1 }');
The .getConverter
method returns a specific converter, or undefined
if none is found.
Note that the returned converter is actually a wrapper around the original converter provided to .addConverter
.
ko.typed.getConverter(fromTypeName, toTypeName);
-
fromTypeName
TypeName of the sourcevalue
. Standard TypeName's are provided by the is-an library. -
toTypeName
TypeName of the result.
var converter = ko.typed.getConverter('Undefined', 'String');
The .removeConverter
method removes the specific converter.
No error is raised if the converter does not exist.
This method is chainable.
ko.typed.removeConverter(fromTypeName, toTypeName);
-
fromTypeName
TypeName of the sourcevalue
. Standard TypeName's are provided by the is-an library. -
toTypeName
TypeName of the result.
ko.typed.removeConverter('Undefined', 'String');
From | To |
---|---|
Undefined |
String ,Data ,Moment |
Boolean |
Number.Integer ,Number ,String |
Number.Integer |
Boolean ,Number ,String |
Number |
Boolean ,Number.Integer ,String |
String |
Undefined ,Boolean ,Number.Integer ,Number ,Date ,Moment |
Results in the empty string ''
.
Input | Output |
---|---|
undefined |
'' |
Results in the invalid date new Date(NaN)
.
Input | Output |
---|---|
undefined |
new Date(NaN) |
Results in the invalid moment moment.invalid()
.
Input | Output |
---|---|
undefined |
moment.invalid() |
options: value
->options.truthy: value
options.truthy: value
Default1
. The value to return when the input value is truthy.options.falsey: value
Default0
. The value to return when the input value is falsey.
Input | Output |
---|---|
false |
0 |
true |
1 |
options: value
->options.truthy: value
options.truthy: value
Default1
. The value to return when the input value is truthy.options.falsey: value
Default0
. The value to return when the input value is falsey.
Input | Output |
---|---|
false |
0 |
true |
1 |
options: value
->options.upperCase: value
options.truthy: value
Defaulttrue
. The value to return when the input value is truthy.options.falsey: value
Defaultfalse
. The value to return when the input value is falsey.options.upperCase: Boolean
Defaultfalse
. If true, the resulting value will be converted to uppercase.
Input | Output |
---|---|
false |
'false' |
true |
'true' |
options.truthy: value
Defaultundefined
. Ifvalue
is null or undefined and the input value does not matchoptions.falsey
, then the result istrue
. Ifvalue
matches input value, then the result istrue
.options.falsey: value
Defaultundefined
. Ifvalue
is null or undefined and the input value does not matchoptions.falsey
, then the result istrue
. Ifvalue
matches input value, then the result istrue
.
Throws TypeError if not match is found.
Input | Output |
---|---|
0 |
false |
123 |
true |
Results in the input value.
Input | Output |
---|---|
42 |
42 |
options: value
->options.base: value
options.base: Integer
Default10
. Convert the input value to a string with the specified base.options.upperCase: Boolean
Defaultfalse
. If true, the result will be converted to upper case.
Input | Output |
---|---|
42 |
'42' |
options.truthy: value
Defaultundefined
. Ifvalue
is null or undefined and the input value does not matchoptions.falsey
, then the result istrue
. Ifvalue
matches input value, then the result istrue
.options.falsey: value
Defaultundefined
. Ifvalue
is null or undefined and the input value does not matchoptions.falsey
, then the result istrue
. Ifvalue
matches input value, then the result istrue
.
Throws TypeError if not match is found.
Input | Output |
---|---|
0 |
false |
0.5 |
true |
options: value
->options.mode: value
options.mode: Undefined|String|Function
Defaultundefined
.Undefined
Input value must be an exact integer or the conversion will throw a TypeError.String
The name of a function from the Math standard unit. Normally this would be one of round/floor/ceil/round10/floor10/ceil10.Function
A function which takes a number and returns an integer. Should throw TypeError if the conversion cannot be performed due to the input value.
Throws TypeError if the conversion cannot be performed.
Input | Output |
---|---|
0.5 |
TypeError |
0 |
0 |
42 |
42 |
options: value
->options.decimals: value
options.decimals: Undefined|Integer
Defaultundefined
.Undefined
Use the default system conversionInteger
Ensure there is a fixed number of decimal places in the resulting string
Input | Output |
---|---|
0.5 |
'0.5' |
0 |
'0' |
42 |
'42' |
If string is empty returns undefined, otherwise throws TypeError.
options.trim: Boolean
Defaultfalse
. If true, trims leading and trailing whitespace from the input value before attempting conversion.
Input | Output |
---|---|
'value' |
TypeError |
'' |
undefined |
options: value
->options.strict: value
options.strict: Boolean
If true, only the first option is considered from each of the truthy/falsey listsoptions.trim
Defaultfalse
. If true, trims leading and trailing whitespace from the input value before attempting conversion.options.ignoreCase
Defaulttrue
If true, the input value is converted to lower case before conversion.options.truthy: Array
Default['true', 't', '1', '-1', 'yes', 'y']
. If the input value matches one of these, the result istrue
.options.falsey: Array
Default['false', 'f', '0', 'no', 'n']
. If the input value matches one of these, the result isfalse
.
Throws TypeError if no match is found.
Input | Output |
---|---|
'true' |
true |
't' |
true |
'1' |
true |
'-1' |
true |
'yes' |
true |
'y' |
true |
'false' |
false |
'f' |
false |
'0' |
false |
'no' |
false |
'n' |
false |
'value' |
TypeError |
options: value
->options.base: value
options.base: Integer
Default10
. The number base to use during conversion.options.strict: Boolean
Defaultfalse
. If false andbase
is10
, numbers with decimals are rounded up to the nearest integer.options.trim: Boolean
Defaultfalse
If true, trims leading and trailing whitespace from the input value before attempting conversion.
Throws TypeError if the conversion fails.
Input | Output |
---|---|
'0' |
0 |
'0.5' |
1 |
'42' |
42 |
'value' |
TypeError |
options: value
->options.decimals
options.decimals: Undefined|Integer
Defaultundefined
Integer
Resulting value is rounded to the specified number of decimal places.
options.trim: Boolean
Defaultfalse
If true, trims leading and trailing whitespace from the input value before attempting conversion.
Throws TypeError if the conversion fails.
Input | Output |
---|---|
'0' |
0 |
'0.5' |
0.5 |
'42' |
42 |
'value' |
TypeError |
options.trim: Boolean
Defaultfalse
If true, trims leading and trailing whitespace from the input value before attempting conversion.options.strict: Boolean
Defaulttrue
If true, input value must match the regular expression provided byoptions.format
options.format: RegExp
Default ISO. Provides a regexp for parsing aString
into date/time/timezone componentsoptions.formatDict: Object
Provides a name mapping for capture groups withinoptions.format
options.utc: Boolean
Defaultfalse
. If true, dates will be parsed as UTC time
Throws TypeError if the resulting Date
is invalid.
options: value
->options.format: value
options.format: String
Default'L'
. Used by themoment
constructoroptions.language: String
Default'en'
. Used by themoment
constructoroptions.strict: Boolean
Defaultfalse
. Used by themoment
constructoroptions.trim: Boolean
Defaultfalse
If true, trims leading and trailing whitespace from the input value before attempting conversion.
Throws TypeError if the resulting Moment
is invalid.
Returns undefined
if the input value is an invalid Date, otherwise throws TypeError.
If the input value is an invalid date, returns an empty string.
options: value
->options.format: value
options.format: String
Defaultdefault
. Format method used to convert date to a string.options.formats: Object
Mapping of supported formats and their respective method names. Default supported formats are: default, date, iso, json, localeDate, localeTime, locale, time, utc.options.params: Array
Default[]
. Array of arguments to pass to the selected formatting method.
Returns an equivalent moment.
Returns undefined
if the input value is an invalid Moment, otherwise throws TypeError.
If the input value is an invalid moment, returns an empty string.
options: value
->options.format: value
options.format: String
Default'L'
. Used bymoment.format
.options.locale: String
Default'en'
. Used bymoment.locale