5 more things I don’t like in Haxe, and how to fix them

1. Non-consistent utf-8 support across platforms

There is excellent haxe.Utf8 class, which allows us to work with utf-8 in a cross-platform way.

But what about cross-platform toLowerCase() or toUpperCase()? What regular expression should I write to replace all non-letters (including unicode ones) with, say, spaces?

To solve this problems, I created zame-haxe-stringutils library.

  • Use Utf8Ext.toLoweCase() to convert utf-8 string to lover case, across all platforms.
  • Use Utf8Ext.toUpperCase() to convert utf-8 string to upper case, across all platforms.
  • Use Utf8Ext.getUnicodeRe() to get regular expression that matches all characters, including unicode (like /[\p{L}]/ in PCRE).
  • Use Utf8Ext.getUnicodeRe(“^”) to get regular expression that matches all non-characters (like /[\P{L}]/ in PCRE).
  • Utf8Ext.getUnicodeRe(“0-9”) will match numbers and characters, while Utf8Ext.getUnicodeRe(“^0-9”) will match anything, except numbers and characters.

This library also have Transliteration class, but currently it is only usable for Belarusian language.

 

2. Unable to use Lambda with an Iterator, for example with map.keys()

Haxe have excellent Lambda class, which accepts an Itarable only (an Iterable is a data structure which has an iterator() method). But sometimes I want to use it with an Iterator, like Lambda.array(map.keys()). Unfortunately this code will not compile.

To solve this I create LambdaExt class (it is in zame-haxe-miscutils library).

It can be used as drop-in replacement of Lambda when you need to work with an Iterator (you should still use original Lambda for an Iterable).

 

3. Must write lot of code to work with Dynamic (for example, when working with parsed json)

As you know, haxe.Json.parse() returns parses json as Dynamic, and you must use Reflect.xxx() methods to work with it in a safe manner.

Fortunately I found fantastic abstract over Dynamic somewhere in the internet, and put it as DynamicExt class to the miscutils library.

Just cast Dynamic to DynamicExt and work with it like Map. Also miscutils have DynamicTools class, which helps parsing response from the server in a easy way.

 

4. No easy way to get inner text from xml node

Let’s say you must use old good Xml class (and can’t use haxe.xml.Fast). To get inner text in a safe manner, you must do following:

var child = node.firstChild();

if (child != null && (child.nodeType == Xml.PCData || child.nodeType == Xml.CData)) {
    return child.nodeValue;
} else {
    return ""; // There is no inner text in this node, return empty string instead
}

Or you can use XmlExt.innerText() instead (it is in miscutils library).

 

5. haxe.zip.Reader fails to open some .zip files

haxe.zip.Reader is fantastic and very useful class, but it can’t open some .zip files, and throws “Unsupported flags” exception.

Fortunately, it can be fixed by commenting few lines in it – I put “corrected” version of it to ZipReader class (available in zame-haxe-fileutils library).

You can also find ZipReaderExt class, which simplify work with .zip files.

 

Links to libraries

  1. stringutils – https://github.com/restorer/zame-haxe-stringutils
  2. miscutils – https://github.com/restorer/zame-haxe-miscutils
  3. fileutils – https://github.com/restorer/zame-haxe-fileutils

To use them just open terminal and execute

haxelib git zame-stringutils https://github.com/restorer/zame-haxe-stringutils.git
haxelib git zame-miscutils https://github.com/restorer/zame-haxe-miscutils.git
haxelib git zame-fileutils https://github.com/restorer/zame-haxe-fileutils.git

Alternatively, you can just copy / paste required classes inside your project.

P. S. Libraries released under MIT license.

6 thoughts on “5 more things I don’t like in Haxe, and how to fix them

  1. Really nice gems of code you have here! Would you mind if i’ll borrow some of it into HaxeFlixel core?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.