| OLD | NEW |
| (Empty) |
| 1 @JS() | |
| 2 library js_util.base; | |
| 3 | |
| 4 import 'package:js/js.dart'; | |
| 5 import 'package:quiver_iterables/iterables.dart'; | |
| 6 | |
| 7 // js_util library uses the technique described in | |
| 8 // https://github.com/dart-lang/sdk/issues/25053 | |
| 9 // because dart2js compiler does not support [] operator. | |
| 10 | |
| 11 @JS() | |
| 12 @anonymous | |
| 13 class PropertyDescription { | |
| 14 external factory PropertyDescription( | |
| 15 {bool configurable, bool enumerable, value, bool writable}); | |
| 16 } | |
| 17 | |
| 18 /// A wrapper for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere
nce/Global_Objects/Object/defineProperty | |
| 19 @JS('Object.defineProperty') | |
| 20 external void defineProperty(o, String prop, PropertyDescription description); | |
| 21 | |
| 22 /// Returns `o[prop]`. | |
| 23 @JS('JsUtil.getValue') | |
| 24 external getValue(o, String prop); | |
| 25 | |
| 26 /// Creates a new JavaScript object. | |
| 27 @JS('JsUtil.newObject') | |
| 28 external newObject(); | |
| 29 | |
| 30 /// Performs `o[key] = value`. | |
| 31 void setValue(o, String key, value) { | |
| 32 defineProperty(o, key, new PropertyDescription(value: value)); | |
| 33 } | |
| 34 | |
| 35 /// Converts a Dart object to a JavaScript object. | |
| 36 /// | |
| 37 /// For example, you can convert a Dart [Map] to a JavaScript object. | |
| 38 /// | |
| 39 /// final jsObj = toJS({ | |
| 40 /// 'people': [ | |
| 41 /// {'firstName': 'Kwang Yul', 'lastName': 'Seo'}, | |
| 42 /// {'firstName': 'DoHyung', 'lastName': 'Kim'}, | |
| 43 /// {'firstName': 'Kyusun', 'lastName': 'Kim'} | |
| 44 /// ] | |
| 45 /// }); | |
| 46 /// | |
| 47 toJS(o) { | |
| 48 if (o is Map) { | |
| 49 final newObj = newObject(); | |
| 50 for (final keyValuePair in zip([o.keys, o.values])) { | |
| 51 setValue(newObj, keyValuePair[0], toJS(keyValuePair[1])); | |
| 52 } | |
| 53 return newObj; | |
| 54 } else if (o is List) { | |
| 55 return o.map((e) => toJS(e)).toList(); | |
| 56 } else { | |
| 57 return o; | |
| 58 } | |
| 59 } | |
| OLD | NEW |