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

Implement ChainedMap.getOrCompute #63

Merged
merged 1 commit into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 fn.
// key: *
// fn: Function () -> value
// returns: value
getOrCompute(key, fn)
```

```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, fn) {
if (!this.has(key)) {
this.set(key, fn());
}
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 @@ -96,19 +96,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