Skip to content

Commit 62797f9

Browse files
Move conversion utils to production code, for reusability
1 parent 4400f26 commit 62797f9

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

splitio_web/lib/src/js_interop.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'dart:js_interop';
22

3+
// JS SDK types
4+
35
@JS()
46
extension type JS_IBrowserSDK._(JSObject _) implements JSObject {
57
}
@@ -13,3 +15,39 @@ extension type JS_BrowserSDKPackage._(JSObject _) implements JSObject {
1315
external JSFunction? WarnLogger;
1416
external JSFunction? ErrorLogger;
1517
}
18+
19+
// Conversion utils: JS to Dart types
20+
21+
@JS('Object.keys')
22+
external JSArray<JSString> _objectKeys(JSObject obj);
23+
24+
@JS('Reflect.get')
25+
external JSAny? _reflectGet(JSObject target, JSAny propertyKey);
26+
27+
List<dynamic> jsArrayToList(JSArray obj) {
28+
return obj.toDart.map(jsAnyToDart).toList();
29+
}
30+
31+
Map<String, dynamic> jsObjectToMap(JSObject obj) {
32+
return {
33+
for (final jsKey in _objectKeys(obj).toDart)
34+
// @TODO _reflectGet (js_interop) vs obj.getProperty (js_interop_unsafe)
35+
jsKey.toDart: jsAnyToDart(_reflectGet(obj, jsKey)),
36+
};
37+
}
38+
39+
dynamic jsAnyToDart(JSAny? value) {
40+
if (value is JSArray) {
41+
return jsArrayToList(value);
42+
} else if (value is JSObject) {
43+
return jsObjectToMap(value);
44+
} else if (value is JSString) {
45+
return value.toDart;
46+
} else if (value is JSNumber) {
47+
return value.toDartDouble;
48+
} else if (value is JSBoolean) {
49+
return value.toDart;
50+
} else {
51+
return value; // JS null and undefined are null in Dart
52+
}
53+
}

splitio_web/test/splitio_web_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:splitio_web/src/js_interop.dart';
77
import 'package:splitio_platform_interface/split_configuration.dart';
88
import 'package:splitio_platform_interface/split_sync_config.dart';
99
import 'package:splitio_platform_interface/split_rollout_cache_configuration.dart';
10-
import 'utils/js_interop_test_utils.dart';
1110

1211
extension on web.Window {
1312
@JS()

splitio_web/test/utils/js_interop_test_utils.dart

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)