Namespace: from

just.from(value, defaultValueopt, opts) → {value}

Defaults to defaultValue if value looks like defaultValue.
Source:
just.js, line 670

Parameters:

NameTypeDescription
value
AnyAny value.
defaultValue
[optional]
AnyA 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
objectSome options.

Properties

NameTypeDefaultDescription
ignoreDefaultKeys
[optional]
booleanfalseIf `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]
booleantrueIf `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]
booleantrueSame as checkLooks but it works with the inner values of the objects.
ignoreNull
[optional]
booleanfalseIf `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

Example 1.
just.defaults([1, 2], {a: 1}); // {a: 1}
Example 2.
just.defaults({}, null); // null: null is not an object literal.
just.defaults([], null, {'checkLooks': false}); // []: null is an object.
just.defaults(null, {}); // {}: null is not an object literal.
just.defaults(null, []); // []: null is not an Array.
Example 3.
just.defaults(1, NaN); // 1 (NaN is an instance of a Number)
Example 4.
just.defaults({'a': 1, 'b': 2}, {'a': 'some string'}, {'ignoreDefaultKeys': false}); // {'a': 'some string', 'b': 2}
Example 5.
just.defaults({'a': 1}, {'b': 2}, {'ignoreDefaultKeys': false}); // {'a': 1, 'b': 2}
just.defaults({'a': 1}, {'b': 2}, {'ignoreDefaultKeys': true}); // {'a': 1}
Example 6.
just.defaults(1, null, {'ignoreNull': false}) // null (1 is not an object)
just.defaults(1, null, {'ignoreNull': true}) // 1
just.defaults(undefined, null, {'ignoreNull': true}) // null
just.defaults({a: 1}, {a: null}, {'ignoreNull': true}) // {a: 1}