js_interop_utils 1.0.3 js_interop_utils: ^1.0.3 copied to clipboard
A Dart library for seamless interaction with JavaScript using `dart:js_interop`, supporting deep transformations of objects, maps, and arrays.
js_interop_utils #
This library leverages dart:js_interop
to provide utilities for seamless interaction between Dart and JavaScript
objects, enabling deep and flexible transformations.
It includes extensions for Object
, Map
, Iterable
, JSObject
, JSArray
, and more.
Features #
- Object Extensions: Convert Dart objects to JavaScript objects using
.toJSDeep
. - Map Extensions: Convert Dart
Map
objects to JavaScript objects, supporting deep transformations. - Iterable Extensions: Handle Dart lists and collections, converting them to JavaScript arrays, including typed arrays.
- JSObject Utilities: Access JavaScript object keys, properties, and convert them back to Dart maps.
- JSArray Utilities: Work with JavaScript arrays, including pushing elements and converting them to Dart lists.
Usage #
Converting Dart Objects to JavaScript #
// import 'dart:js_interop'; // Already exported by `js_interop_utils`
import 'package:js_interop_utils/js_interop_utils.dart';
void main() {
var map = {'key': 'value', 'number': 42};
var jsObject = map.toJSDeep; // Convert to `JSObject`
}
Working with JavaScript Objects #
import 'package:js_interop_utils/js_interop_utils.dart';
void main() {
var jsObject = JSObject();
jsObject.put('a', 1);
jsObject.put('b', 2);
print(jsObject.get('a')); // Access property `a`
}
Handling Collections #
import 'package:js_interop_utils/js_interop_utils.dart';
void main() {
var list = [1, 2, 3];
var jsArray = list.toJSDeep; // Convert to `JSArray`
}
Accessing JavaScript Array Utilities #
import 'package:js_interop_utils/js_interop_utils.dart';
void main() {
var jsArray = JSArray();
jsArray.push('a');
jsArray.pushVarArgs('b', 3);
var list = jsArray.toList(); // Convert to Dart `List`
print(list); // ['a','b',3]
}
Extensions Overview #
Object Extensions #
toJSDeep
: Converts any Dart object to a deeply nested JavaScript object.
Map Extensions #
toJSDeep
: Converts a DartMap
to a deeply nested JavaScript object.
Iterable Extensions #
toJS
: same astoJS
forList
.toJSDeep
: Converts a DartIterable
to a deeply nested JavaScript array.
JSObject Extensions #
keys
: Returns all keys of a JavaScript object.get
: Access a property by key.toMap
: Converts a JavaScript object to a Dart map.
JSArray Extensions #
push
: Appends an element to a JavaScript array.toList
: Converts a JavaScript array to a Dart list.
Features and bugs #
Please file feature requests and bugs at the issue tracker.
Author #
Graciliano M. Passos: gmpassos@GitHub.
Sponsor #
Don't be shy, show some love, and become our GitHub Sponsor. Your support means the world to us, and it keeps the code caffeinated! ☕✨
Thanks a million! 🚀😄