RE: Array.prototype.remove(item)

2018-10-11 Thread Man Hoang
### With `Array.prototype.remove(item)` Code is clean and concise. ``` js if (array.remove(item)) { // [item] was removed, do something here. } ``` ### Without `Array.prototype.remove(item)` Code is verbose or duplicated (everyone has their own version of `removeArrayItem`) => **WASTE**. ```

RE: Array.prototype.remove(item)

2018-10-10 Thread Man Hoang
The problem with `Set` is that its `add` method returns `this` instead of `boolean`. If `Set.prototype.add` returned `boolean`, I would have used `Set`. That’s why in the `select` method of my sample code, I use a custom defined method named `pushIfAbsent`. The actual type of `_values` is not `A

Proposal: Add Map.prototype.putIfAbsent

2018-10-10 Thread Man Hoang
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) {

Re: Re: (Map|Set|WeakMap)#set() returns `this` ?

2018-10-10 Thread Man Hoang
What JS needs is a [cascade operator](https://esdiscuss.org/topic/cascade-operator). With this operator, property accessors and methods can be effectively chained without requiring methods to return `this`. ___ es-discuss mailing list es-discuss@mozil

Re: Re: .. cascade operator

2018-10-09 Thread Man Hoang
This operator should have been added years ago. It would enable many APIs to return more appropriate results instead of `this`. For example, `Set.prototype.add(value)` could return a boolean indicating if an item was actually added instead of `this`.

RE: Re: Array.prototype.remove(item)

2018-10-09 Thread Man Hoang
The benefits are - efficient (memory & speed) - clear intent - concise There are always use cases where you want to mutate arrays. How would you rewrite the following `deselect` method using `filter`? ``` js export class Selector { private _values: T[] = []; get values(): ReadonlyArray

Re: Re: Array.prototype.remove(item)

2018-10-09 Thread Man Hoang
There are cases that you only want to remove the first occurrence of an item (because you know there is at most one occurrence of that item) and there are cases that you want to remove more than one item. Therefore, I propose adding the following methods (written in TypeScript here for static ty