Proposal: Add Map.prototype.putIfAbsent

Man Hoang jolleekin at outlook.com
Wed Oct 10 07:46:09 UTC 2018


Consider the following function
``` js
/**
* Parses the locale sensitive string [value] into a number.
*/
export function parseNumber(
    value: string,
    locale: string = navigator.language
): number {
    let decimalSeparator = decimalSeparators.get(locale);
    if (!decimalSeparator) {
        decimalSeparator = Intl.NumberFormat(locale).format(1.1)[1];
        decimalSeparators.set(locale, decimalSeparator);
    }

    let cleanRegExp = regExps.get(decimalSeparator);
    if (!cleanRegExp) {
        cleanRegExp = new RegExp(`[^-+0-9${decimalSeparator}]`, 'g');
        regExps.set(decimalSeparator, cleanRegExp);
    }

    value = value
        .replace(cleanRegExp, '')
        .replace(decimalSeparator, '.');

    return parseFloat(value);
}

const decimalSeparators = new Map<string, string>();
const regExps = new Map<string, RegExp>();
```

This function can be simplified quite a bit as follows
``` js
export function parseNumber(
    value: string,
    locale: string = navigator.language
): number {
    const decimalSeparator = decimalSeparators.putIfAbsent(
        locale, () => Intl.NumberFormat(locale).format(1.1)[1]);

    const cleanRegExp = regExps.putIfAbsent(
        decimalSeparator, () => new RegExp(`[^-+0-9${decimalSeparator}]`, 'g'));

    value = value
        .replace(cleanRegExp, '')
        .replace(decimalSeparator, '.');

    return parseFloat(value);
}
```
if `Map` has the following instance method
``` js
export class Map<K, V> {
    /**
     * Look up the value of [key], or add a new value if it isn't there.
     *
     * Returns the value associated to [key], if there is one.
     * Otherwise calls [ifAbsent] to get a new value, associates [key] to
     * that value, and then returns the new value.
     */
    putIfAbsent(key: K, ifAbsent: () => V): V {
        let v = this.get(key);
        if (v === undefined) {
            v = ifAbsent();
            this.set(key, v);
        }
        return v;
    }
}
```

Java's Map has a `putIfAbsent` method, which accepts a value rather than a function for the second parameter. This is not ideal as computing the value may be expensive. A function would allow the value to be computed lazily.

References
- [Dart] [Map.putIfAbsent](https://api.dartlang.org/stable/2.0.0/dart-core/Map/putIfAbsent.html)
- [Java] [Map.putIfAbsent](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#putIfAbsent-K-V-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181010/904ed3e7/attachment.html>


More information about the es-discuss mailing list