-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #647 from zloirock/map-upsert
Update `Map#updateOrInsert` -> `Map#upsert`
- Loading branch information
Showing
26 changed files
with
266 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require('../../modules/es.map'); | ||
require('../../modules/esnext.map.upsert'); | ||
var entryUnbind = require('../../internals/entry-unbind'); | ||
|
||
module.exports = entryUnbind('Map', 'upsert'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require('../../modules/es.weak-map'); | ||
require('../../modules/esnext.weak-map.upsert'); | ||
var entryUnbind = require('../../internals/entry-unbind'); | ||
|
||
module.exports = entryUnbind('WeakMap', 'upsert'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
var anObject = require('../internals/an-object'); | ||
|
||
// `Map.prototype.upsert` method | ||
// https://github.com/thumbsupep/proposal-upsert | ||
module.exports = function upsert(key, updateFn, insertFn) { | ||
var map = anObject(this); | ||
if (typeof updateFn != 'function' && typeof insertFn != 'function') { | ||
throw TypeError('At least one callback required'); | ||
} | ||
var value; | ||
if (map.has(key)) { | ||
value = map.get(key); | ||
if (typeof updateFn == 'function') { | ||
value = updateFn(value); | ||
map.set(key, value); | ||
} | ||
} else if (typeof insertFn == 'function') { | ||
value = insertFn(); | ||
map.set(key, value); | ||
} return value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,11 @@ | ||
'use strict'; | ||
// TODO: remove from `core-js@4` | ||
var $ = require('../internals/export'); | ||
var IS_PURE = require('../internals/is-pure'); | ||
var anObject = require('../internals/an-object'); | ||
var aFunction = require('../internals/a-function'); | ||
var $upsert = require('../internals/map-upsert'); | ||
|
||
// `Set.prototype.updateOrInsert` method | ||
// https://docs.google.com/presentation/d/1_xtrGSoN1-l2Q74eCXPHBbbrBHsVyqArWN0ebnW-pVQ/ | ||
// `Map.prototype.updateOrInsert` method (replaced by `Map.prototype.upsert`) | ||
// https://github.com/thumbsupep/proposal-upsert | ||
$({ target: 'Map', proto: true, real: true, forced: IS_PURE }, { | ||
updateOrInsert: function updateOrInsert(key, onUpdate, onInsert) { | ||
var map = anObject(this); | ||
aFunction(onUpdate); | ||
aFunction(onInsert); | ||
var value = map.has(key) ? onUpdate(map.get(key)) : onInsert(); | ||
map.set(key, value); | ||
return value; | ||
} | ||
updateOrInsert: $upsert | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var IS_PURE = require('../internals/is-pure'); | ||
var $upsert = require('../internals/map-upsert'); | ||
|
||
// `Map.prototype.upsert` method | ||
// https://github.com/thumbsupep/proposal-upsert | ||
$({ target: 'Map', proto: true, real: true, forced: IS_PURE }, { | ||
upsert: $upsert | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var IS_PURE = require('../internals/is-pure'); | ||
var $upsert = require('../internals/map-upsert'); | ||
|
||
// `WeakMap.prototype.upsert` method | ||
// https://github.com/thumbsupep/proposal-upsert | ||
$({ target: 'WeakMap', proto: true, real: true, forced: IS_PURE }, { | ||
upsert: $upsert | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
require('../modules/esnext.map.update-or-insert'); | ||
// TODO: remove from `core-js@4` | ||
require('./map-upsert'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// https://github.com/thumbsupep/proposal-upsert | ||
// TODO: remove from `core-js@4` | ||
require('../modules/esnext.map.update-or-insert'); | ||
require('../modules/esnext.map.upsert'); | ||
require('../modules/esnext.weak-map.upsert'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Map from 'core-js-pure/features/map'; | ||
|
||
QUnit.test('Map#upsert', assert => { | ||
const { upsert } = Map.prototype; | ||
assert.isFunction(upsert); | ||
assert.arity(upsert, 3); | ||
assert.name(upsert, 'upsert'); | ||
assert.nonEnumerable(Map.prototype, 'upsert'); | ||
|
||
const map = new Map([['a', 2]]); | ||
assert.same(map.upsert('a', function (value) { | ||
assert.same(arguments.length, 1, 'correct number of callback arguments'); | ||
assert.same(value, 2, 'correct value in callback'); | ||
return value ** 2; | ||
}, () => { | ||
assert.ok(false, 'should not be called'); | ||
return 3; | ||
}), 4, 'returns a correct value'); | ||
assert.same(map.upsert('b', value => { | ||
assert.ok(false, 'should not be called'); | ||
return value ** 2; | ||
}, function () { | ||
assert.same(arguments.length, 0, 'correct number of callback arguments'); | ||
return 3; | ||
}), 3, 'returns a correct value'); | ||
assert.same(map.size, 2, 'correct size'); | ||
assert.same(map.get('a'), 4, 'correct result #1'); | ||
assert.same(map.get('b'), 3, 'correct result #2'); | ||
|
||
assert.same(new Map([['a', 2]]).upsert('b', null, () => 3), 3); | ||
assert.same(new Map([['a', 2]]).upsert('a', value => value ** 2), 4); | ||
|
||
assert.throws(() => new Map().upsert('a'), TypeError); | ||
assert.throws(() => upsert.call({}, 'a', () => { /* empty */ }, () => { /* empty */ }), TypeError); | ||
assert.throws(() => upsert.call([], 'a', () => { /* empty */ }, () => { /* empty */ }), TypeError); | ||
assert.throws(() => upsert.call(undefined, 'a', () => { /* empty */ }, () => { /* empty */ }), TypeError); | ||
assert.throws(() => upsert.call(null, 'a', () => { /* empty */ }, () => { /* empty */ }), TypeError); | ||
}); |
Oops, something went wrong.