Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature-request] use String.prototype.normalize() when compiled with dart2js #2

Open
jonasfj opened this issue Mar 25, 2019 · 1 comment
Labels
contribution welcome enhancement New feature or request help wanted Extra attention is needed

Comments

@jonasfj
Copy link

jonasfj commented Mar 25, 2019

Dart supports conditional imports, when running in a browser environment it would be possible to simply use:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize

This would not only be faster, but also reduce the size of compiled javascript code :)

@yshrsmz yshrsmz added enhancement New feature or request contribution welcome and removed enhancement New feature or request labels Mar 26, 2019
@yshrsmz yshrsmz added the help wanted Extra attention is needed label Aug 2, 2020
@olof-dev
Copy link

olof-dev commented Aug 20, 2022

Here's what I came up with for accessing this, using the official js-interop package. I don't know if it's the best way.

import 'package:js/js.dart';

@JS('String')
@staticInterop
class JsString {
  external JsString(String s);
}

extension JsStringExtension on JsString {
  external String normalize(String form);
}

extension StringExtensionHtml on String {
  String unicodeNormalize(String form) {
    // For now, this throws in debug mode, but it's fine in (web-)production
    final value = JsString(this);
    return value.normalize(form);
  }
}

If this is placed into a file normalizer_web.dart, one can do a conditional import from another file using

import 'normalizer_regular.dart' if (dart.library.html) 'normalizer_web.dart';

This way you can use the browser's normalize function (named unicodeNormalize above) on the web, and use something else on non-web.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution welcome enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants