Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(306)

Side by Side Diff: packages/js_util/src/js_util_base.dart

Issue 2119523002: Added full js & js_util packages (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/js_util/pubspec.yaml ('k') | packages/js_util/test/js_util_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « packages/js_util/pubspec.yaml ('k') | packages/js_util/test/js_util_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698