sass / dart-sass

The reference implementation of Sass, written in Dart.
https://sass-lang.com/dart-sass
MIT License
3.97k stars 357 forks source link

Deep object-to-map conversion in objectToMap() #1766

Closed borutjures closed 2 years ago

borutjures commented 2 years ago

I used dart-sass to learn and implement a Dart library used in Node.js. Thanks Dart Sass team!

I used objectToMap() (lib/src/node/utils.dart) to convert JavaScript JSON objects but it only converts the first level. My JSON data uses more than one level of Map<String, dynamic>. (I use standard Dart fromJson() which expects the entire object to be converted into a map on every level)

I changed the following line in objectToMap() to make a deep conversion to Map:

Map<String, Object?> objectToMap(Object object) {
  var map = <String, Object?>{};
---  jsForEach(object, (key, value) => map[key] = value);
+++  jsForEach(object, (key, value) => map[key] = value == null || value is String || value is num || value is bool ? value : objectToMap(value));
  return map;
}

Your implementation obviously works for how you use it. I just thought it might be useful to support any JSON.

Thank you for the best example for JavaScript code to call Dart code! (I'm still struggling with how to call Dart static methods from JavaScript)

nex3 commented 2 years ago

Thanks for the thought, but we can already do what you're suggesting with jsutil.jsify(). objectToMap() exists explicitly to do this only at the top level.

borutjures commented 2 years ago

Thank you for the clarification about the intent of objectToMap(). However it cannot be replaced with jsify - see table below. I’ll remove the change and create a dartify().

jsify: Dart => JS dartify: JS => Dart objectToMap: JS => Dart