Cyril Auburtin (2018-03-15T14:52:19.000Z)
cyril.auburtin at gmail.com (2018-03-15T14:58:18.487Z)
I totally agree that Map (and Set) should have `.map`, `.filter` and a few more (`.slice`, ..) methods, similarly to immutableJS. Since Maps and Sets are ordered, it would make sense example polyfill: ```js class SortableMap { constructor(data) { this.m = new Map(Array.isArray(data) ? data : Object.entries(data)); } set(k, v) { this.m.set(k, v); return this; } get(k) { return this.m.get(k); } forEach(fn) { this.m.forEach(fn); } map(fn) { return Array.from(this.m, fn); } sort(fn) { const a = [...this.m].sort((x, y) => fn(x[1], y[1])); // sort with values this.m = new SortableMap(a); return this; } *[Symbol.iterator]() { yield* this.m; } } const m = new SortableMap([["lol", "far"], ["ok", "boo"]]); m.sort((a, b) => a.localeCompare(b)); console.log(1, m); // 1 SortableMap { m: SortableMap { m: Map { 'ok' => 'boo', 'lol' => 'far' m.set("bip", "cri"); console.log(2, m.map()); // 2 [ [ 'ok', 'boo' ], [ 'lol', 'far' ], [ 'bip', 'cri' ] ] m.sort((a, b) => a.localeCompare(b)); console.log(3, m.map(([k, v]) => [k, v + "!"])); // 3 [ [ 'ok', 'boo!' ], [ 'bip', 'cri!' ], [ 'lol', 'far!' ] ] ``` 2018-01-20 1:38 GMT+01:00 Michał Wadas <michalwadas at gmail.com>:
cyril.auburtin at gmail.com (2018-03-15T14:54:44.174Z)
I totally agree that Map (and Set) should have `.map`, `.filter` and a few more (`.slice`, ..) methods, similarly to immutableJS. Since Maps and Sets are ordered, it would make sense example polyfill: ```js class SortableMap { constructor(data) { this.m = new Map(Array.isArray(data) ? data : Object.entries(data)); } set(k, v) { this.m.set(k, v); return this; } get(k) { return this.m.get(k); } forEach(fn) { this.m.forEach(fn); } map(fn) { return Array.from(this.m, fn); } sort(fn) { const a = [...this.m].sort((x, y) => fn(x[1], y[1])); // sort with values this.m = new SortableMap(a); return this; } *[Symbol.iterator]() { yield* this.m; } } const m = new SortableMap([['lol', 'far'], ['ok', 'boo']]); m.sort((a, b) => a.localeCompare(b)); console.log(1, m); // 1 SortableMap { m: SortableMap { m: Map { 'ok' => 'boo', 'lol' => 'far' } } } m.set('bip', 'cri'); console.log(2, m.map()); // 2 [ [ 'ok', 'boo' ], [ 'lol', 'far' ], [ 'bip', 'cri' ] ] m.sort((a, b) => a.localeCompare(b)); console.log(3, m.map()); // 3 [ [ 'ok', 'boo' ], [ 'bip', 'cri' ], [ 'lol', 'far' ] ] ``` 2018-01-20 1:38 GMT+01:00 Michał Wadas <michalwadas at gmail.com>: