dartify<T> function
T
dartify<T>(
- dynamic jsObject
Returns Dart representation from JS Object.
Basic types (num
, bool
, String
) are returned as-is. JS arrays
are converted into List
instances. JS objects are converted into
Map
instances. Both arrays and objects are traversed recursively
converting nested values.
Converting JS objects always results in a Map<String, dynamic>
meaning
even if original object had an integer key set, it will be converted into
a String
. This is different from JS semantics where you are allowed to
access a key by passing its int value, e.g. obj[1]
would work in JS,
but fail in Dart.
See also:
Implementation
T dartify<T>(dynamic jsObject) {
if (_isBasicType(jsObject)) {
return jsObject as T;
}
if (jsObject is List) {
return jsObject.map(dartify).toList() as T;
}
var keys = objectKeys(jsObject);
var result = <String, dynamic>{};
for (var key in keys) {
result[key] = dartify(js_util.getProperty(jsObject, key));
}
return result as T;
}