Skip to content

Commit

Permalink
feat(index): implment .mapEntry()
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Weaver committed Dec 14, 2018
1 parent d84149d commit b9e72dc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
29 changes: 22 additions & 7 deletions src/ropex/RopexIndex.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { EntryKey, RopexState } from './types';
import { EntryKey, RopexState, RopexStateIndex } from './types';
import { RopexStore } from './RopexStore';

export class RopexIndex<Entry extends object, K extends EntryKey> {
constructor(private readonly ropexStore: RopexStore<Entry, K>) {}
constructor(
private readonly ropexStore: RopexStore<Entry, K>,
private readonly indexName: string,
private readonly indexState: RopexStateIndex<Entry, K>,
) {}

// Getters

Expand Down Expand Up @@ -94,10 +98,21 @@ export class RopexIndex<Entry extends object, K extends EntryKey> {
* @param key The key of the entry to apply the map function to
* @param map Function to map an entry to another entry
*/
public mapEntry(
key: EntryKey,
map: (entry: Entry) => Entry,
): RopexIndex<Entry, K> {
public mapEntry(key: K, map: (entry: Entry) => Entry): RopexIndex<Entry, K> {
if (!this.indexState.keys.includes(key)) {
if (process.env.NODE_ENV === 'development') {
console.warn(
`Trying to map entry with key "${key}" that's not in index "${
this.indexName
}"`,
);
}

return this;
}

this.ropexStore.mapEntry(key, map);

return this;
}

Expand Down Expand Up @@ -132,6 +147,6 @@ export class RopexIndex<Entry extends object, K extends EntryKey> {
* Remove this index and return the parent {@link RopexStore}
*/
public remove(): RopexStore<Entry, K> {
return this.ropexStore;
return this.ropexStore.remove(this.indexName);
}
}
5 changes: 4 additions & 1 deletion src/ropex/RopexStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export class RopexStore<Entry extends object, K extends EntryKey> {
* @param key The key of the index to lookup
*/
public index(key: string): RopexIndex<Entry, K> {
return new RopexIndex<Entry, K>(this);
const index = this.newState.indexes[key] || { meta: {}, keys: [] };
this.newState.indexes[key] = index;

return new RopexIndex<Entry, K>(this, key, index);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/ropex/__tests__/RopexIndex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ describe('RopexIndex', () => {
},
});
});

it("Shouldn't update the entry if it's not in the index", () => {
const state = {
...baseState,
drafts: { c: { id: 'c', data: 'test' } },
indexes: {
...baseState.indexes,
test: {
meta: {},
keys: ['c'],
},
},
};
expect(
ropex(state)
.index('index')
.mapEntry('c', entry => ({ ...entry, data: 'test' }))
.done(),
).toEqual(state);
});
});

describe('.mapEntries()', () => {
Expand Down

0 comments on commit b9e72dc

Please sign in to comment.