I'm providing a few helpers right now. There are few API docs and here's the GitHub repository.
Map
Currently EcmaScript supports Map
objects without property "accessors" for reasons:
- The key can conflict with Object constructor's properties, including:
toString()
hasOwnProperty()
constructor
- The key can be any type.
flash.utils.Dictionary
supports this.
flash.utils.Dictionary
does not resolve the conflict previously mentioned. So you may accidentally access the method hasOwnProperty()
or override it and thus lose access to it.
So, with a Map type, instead of mapObject.x
, mapObject[k]
, delete mapObject.x
and so on, you use methods like:
mapObject.get('x')
mapObject.set('x', 10)
mapObject.deleteKey('x')
Map can be constructed from a plain object, flash.utils.Dictionary
, an Array of key-value entries or as a duplication of another Map.
I provided both Map and Set objects, using insertion order. The iterators are simple for now. If you need weak keys, you probably want to use flash.utils.Dictionary
(since you'll probably use non-String keys, the property ambiguity issue doesn't arise).
var mapObject:Map = new Map({x: 10, y: 16});
// iterating key-value pairs
for each (var entry:Array in mapObject)
{
trace('key:', entry[0]);
trace('value:', entry[1]);
}
// since the Map above was constructed from a plain object (` { ... } `)
// the order of the keys is indeterministic.
// key: 'x'
// value: 10
// key: 'y'
// value: 16
String formatting
Languages support string formatting in different ways. What I provided here won't be very useful, but helps on simple cases. For internationalization, I'll soon probably implement FTL (Fluent Translation List) from projectfluent.org in ActionScript.
Here's what format
looks like:
assertEquals(format('$fooQux', { fooQux: 'Fq' }), 'Fq');
assertEquals(format('$<foo-qux>', {}), 'undefined');
assertEquals(format('$1', ['foo']), 'foo');
Promise
More people are familiar with the EcmaScript built-in Promise and usually don't use any other types other than Promise, so I converted this polyfill to ActionScript. Yeah, there's an existing Promise implementation on GitHub, but it looks bloated to me. Anyway, since ActionScript doesn't still officially support using reserved words after dot, instead of .catch
you type .promiseCatch
or .otherwise
, and instead of .finally
you type .promiseFinally
or .always
:
promiseObject
.then(function(value:*):* {
// fulfill procedure
})
.otherwise(function(error:*):* {
// reject procedure
})
.always(function():void {
// procedure
});
Assertion
There are few assertion functions at com.rialight.util.assertion
and the AssertionError subtype.