just.defaults(value, defaultValueopt, opts) → {value}
Defaults to defaultValue if value looks like
defaultValue.
Parameters:
Name
Type
Description
value
Any
Any value.
defaultValue
[optional]
Any
A value with a desired type for value.
If an object literal is given, all the keys of value will default
to his corresponding key in this object.
opts
object
Some options.
Properties
Name
Type
Default
Description
ignoreDefaultKeys
[optional]
boolean
false
If `false` and defaultValue
is an object literal, the default keys will be added to value
or checked against this function for each repeated key.
checkLooks
[optional]
boolean
true
If `true`:
`[]` will match ONLY with another Array.
`{}` will match ONLY with another object literal.
If `false`
`[]` and `{}` will match with any other object.
checkDeepLooks
[optional]
boolean
true
Same as checkLooks but it works with the inner values
of the objects.
ignoreNull
[optional]
boolean
false
If `true`, defaultValues with null as a value won't be checked
and any value (except `undefined`) will be allowed.
Returns:
value if it looks like defaultValue or defaultValue otherwise.
-
Type
-
value
Examples
Name | Type | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
value
|
Any | Any value. | ||||||||||||||||||||
defaultValue
[optional] |
Any | A value with a desired type for value. If an object literal is given, all the keys of value will default to his corresponding key in this object. | ||||||||||||||||||||
opts
|
object | Some options.
Properties
|
defaults([1, 2], {a: 1}); // {a: 1}
defaults({}, null); // null: null is not an object literal.
defaults([], null, {'checkLooks': false}); // []: null is an object.
defaults(null, {}); // {}: null is not an object literal.
defaults(null, []); // []: null is not an Array.
defaults(1, NaN); // 1 (NaN is an instance of a Number)
defaults({'a': 1, 'b': 2}, {'a': 'some string'}, {'ignoreDefaultKeys': false}); // {'a': 'some string', 'b': 2}
defaults({'a': 1}, {'b': 2}, {'ignoreDefaultKeys': false}); // {'a': 1, 'b': 2}
defaults({'a': 1}, {'b': 2}, {'ignoreDefaultKeys': true}); // {'a': 1}
defaults(1, null, {'ignoreNull': false}) // null (1 is not an object)
defaults(1, null, {'ignoreNull': true}) // 1
defaults(undefined, null, {'ignoreNull': true}) // null
defaults({a: 1}, {a: null}, {'ignoreNull': true}) // {a: 1}