js_interop_utils 1.0.3 copy "js_interop_utils: ^1.0.3" to clipboard
js_interop_utils: ^1.0.3 copied to clipboard

Platformweb

A Dart library for seamless interaction with JavaScript using `dart:js_interop`, supporting deep transformations of objects, maps, and arrays.

js_interop_utils #

pub package Null Safety Codecov Dart CI GitHub Tag New Commits Last Commits Pull Requests Code size License

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 Dart Map to a deeply nested JavaScript object.

Iterable Extensions #

  • toJS: same as toJS for List.
  • toJSDeep: Converts a Dart Iterable 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.

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! 🚀😄

License #

Apache License - Version 2.0

1
likes
150
points
192
downloads

Publisher

unverified uploader

Weekly Downloads

A Dart library for seamless interaction with JavaScript using `dart:js_interop`, supporting deep transformations of objects, maps, and arrays.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

More

Packages that depend on js_interop_utils