Last change
on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
721 bytes
|
Line | |
---|
1 | 'use strict';
|
---|
2 | var anObject = require('../internals/an-object');
|
---|
3 |
|
---|
4 | // `Map.prototype.upsert` method
|
---|
5 | // https://github.com/thumbsupep/proposal-upsert
|
---|
6 | module.exports = function upsert(key, updateFn /* , insertFn */) {
|
---|
7 | var map = anObject(this);
|
---|
8 | var insertFn = arguments.length > 2 ? arguments[2] : undefined;
|
---|
9 | var value;
|
---|
10 | if (typeof updateFn != 'function' && typeof insertFn != 'function') {
|
---|
11 | throw TypeError('At least one callback required');
|
---|
12 | }
|
---|
13 | if (map.has(key)) {
|
---|
14 | value = map.get(key);
|
---|
15 | if (typeof updateFn == 'function') {
|
---|
16 | value = updateFn(value);
|
---|
17 | map.set(key, value);
|
---|
18 | }
|
---|
19 | } else if (typeof insertFn == 'function') {
|
---|
20 | value = insertFn();
|
---|
21 | map.set(key, value);
|
---|
22 | } return value;
|
---|
23 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.