Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Implement ChainedMap.getOrCompute
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Jun 6, 2018
1 parent a2b1524 commit dc39b7c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 35 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ delete(key)
get(key)
```

```js
// Fetch the value from a Map located at the corresponding key.
// If the key is missing, the key is set to the result of function fun.
// key: *
// fun: Function () -> value
// returns: value
getOrCompute(key, fun)
```

```js
// Set a value on the Map stored at the `key` location.
// key: *
Expand Down
7 changes: 7 additions & 0 deletions src/ChainedMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ module.exports = class extends Chainable {
return this.store.get(key);
}

getOrCompute(key, fun) {
if (!this.has(key)) {
this.set(key, fun());
}
return this.get(key);
}

has(key) {
return this.store.has(key);
}
Expand Down
12 changes: 2 additions & 10 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,11 @@ module.exports = class extends ChainedMap {
}

entry(name) {
if (!this.entryPoints.has(name)) {
this.entryPoints.set(name, new ChainedSet(this));
}

return this.entryPoints.get(name);
return this.entryPoints.getOrCompute(name, () => new ChainedSet(this));
}

plugin(name) {
if (!this.plugins.has(name)) {
this.plugins.set(name, new Plugin(this, name));
}

return this.plugins.get(name);
return this.plugins.getOrCompute(name, () => new Plugin(this, name));
}

toConfig() {
Expand Down
12 changes: 2 additions & 10 deletions src/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@ module.exports = class extends ChainedMap {
}

defaultRule(name) {
if (!this.defaultRules.has(name)) {
this.defaultRules.set(name, new Rule(this, name));
}

return this.defaultRules.get(name);
return this.defaultRules.getOrCompute(name, () => new Rule(this, name));
}

rule(name) {
if (!this.rules.has(name)) {
this.rules.set(name, new Rule(this, name));
}

return this.rules.get(name);
return this.rules.getOrCompute(name, () => new Rule(this, name));
}

toConfig() {
Expand Down
6 changes: 1 addition & 5 deletions src/Resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ module.exports = class extends ChainedMap {
}

plugin(name) {
if (!this.plugins.has(name)) {
this.plugins.set(name, new Plugin(this, name));
}

return this.plugins.get(name);
return this.plugins.getOrCompute(name, () => new Plugin(this, name));
}

toConfig() {
Expand Down
12 changes: 2 additions & 10 deletions src/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,11 @@ module.exports = class Rule extends ChainedMap {
}

use(name) {
if (!this.uses.has(name)) {
this.uses.set(name, new Use(this, name));
}

return this.uses.get(name);
return this.uses.getOrCompute(name, () => new Use(this, name));
}

oneOf(name) {
if (!this.oneOfs.has(name)) {
this.oneOfs.set(name, new Rule(this, name));
}

return this.oneOfs.get(name);
return this.oneOfs.getOrCompute(name, () => new Rule(this, name));
}

pre() {
Expand Down
8 changes: 8 additions & 0 deletions test/ChainedMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ test('get', t => {
t.is(map.get('a'), 'alpha');
});

test('getOrCompute', t => {
const map = new ChainedMap();

t.is(map.get('a'), undefined);
t.is(map.getOrCompute('a', () => 'alpha'), 'alpha');
t.is(map.get('a'), 'alpha');
});

test('clear', t => {
const map = new ChainedMap();

Expand Down

0 comments on commit dc39b7c

Please sign in to comment.