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

Unified Diff: packages/js_util/src/js_util_base.dart

Issue 2104113003: Added js and js_util to observatory deps (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 4 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « packages/js_util/js_util.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packages/js_util/src/js_util_base.dart
diff --git a/packages/js_util/src/js_util_base.dart b/packages/js_util/src/js_util_base.dart
new file mode 100644
index 0000000000000000000000000000000000000000..7f35991c98c4c8d500bf9c9aedb2fdb764b9b082
--- /dev/null
+++ b/packages/js_util/src/js_util_base.dart
@@ -0,0 +1,59 @@
+@JS()
+library js_util.base;
+
+import 'package:js/js.dart';
+import 'package:quiver_iterables/iterables.dart';
+
+// js_util library uses the technique described in
+// https://github.com/dart-lang/sdk/issues/25053
+// because dart2js compiler does not support [] operator.
+
+@JS()
+@anonymous
+class PropertyDescription {
+ external factory PropertyDescription(
+ {bool configurable, bool enumerable, value, bool writable});
+}
+
+/// A wrapper for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
+@JS('Object.defineProperty')
+external void defineProperty(o, String prop, PropertyDescription description);
+
+/// Returns `o[prop]`.
+@JS('JsUtil.getValue')
+external getValue(o, String prop);
+
+/// Creates a new JavaScript object.
+@JS('JsUtil.newObject')
+external newObject();
+
+/// Performs `o[key] = value`.
+void setValue(o, String key, value) {
+ defineProperty(o, key, new PropertyDescription(value: value));
+}
+
+/// Converts a Dart object to a JavaScript object.
+///
+/// For example, you can convert a Dart [Map] to a JavaScript object.
+///
+/// final jsObj = toJS({
+/// 'people': [
+/// {'firstName': 'Kwang Yul', 'lastName': 'Seo'},
+/// {'firstName': 'DoHyung', 'lastName': 'Kim'},
+/// {'firstName': 'Kyusun', 'lastName': 'Kim'}
+/// ]
+/// });
+///
+toJS(o) {
+ if (o is Map) {
+ final newObj = newObject();
+ for (final keyValuePair in zip([o.keys, o.values])) {
+ setValue(newObj, keyValuePair[0], toJS(keyValuePair[1]));
+ }
+ return newObj;
+ } else if (o is List) {
+ return o.map((e) => toJS(e)).toList();
+ } else {
+ return o;
+ }
+}
« no previous file with comments | « packages/js_util/js_util.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698