Skip to content

Commit 18d7c42

Browse files
committed
Create a null split registry
1 parent 9adfcba commit 18d7c42

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Assignment from './assignment';
22
import { type Split } from './split';
3-
import { createSplitRegistry, type SplitRegistry } from './splitRegistry';
3+
import { createSplitRegistry, NULL_SPLIT_REGISTRY, type SplitRegistry } from './splitRegistry';
44

55
const DEFAULT_VISITOR_COOKIE_NAME = 'tt_visitor_id';
66

@@ -35,7 +35,7 @@ export type Config = {
3535

3636
function parseSplitRegistry(rawSplits: RawConfig['splits']): SplitRegistry {
3737
if (!rawSplits) {
38-
return createSplitRegistry(null);
38+
return NULL_SPLIT_REGISTRY;
3939
}
4040

4141
const splits = Object.entries(rawSplits).map<Split>(([name, values]) => ({

src/splitRegistry.test.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createSplitRegistry } from './splitRegistry';
1+
import { createSplitRegistry, NULL_SPLIT_REGISTRY } from './splitRegistry';
22

33
function setupSplitRegistry() {
44
return createSplitRegistry([
@@ -15,18 +15,14 @@ describe('SplitRegistry', () => {
1515
expect(split!.name).toEqual('split1');
1616
expect(split!.isFeatureGate).toEqual(true);
1717
expect(split!.weighting).toEqual({ foo: 50, bar: 50, baz: 0 });
18-
1918
expect(splitRegistry.getSplit('unknown split')).toEqual(undefined);
2019
});
2120
});
2221

2322
describe('.isLoaded', () => {
24-
it('is not loaded if null is passed in', () => {
23+
it('is loaded when splits are provided', () => {
2524
const splitRegistry = setupSplitRegistry();
26-
expect(splitRegistry.isLoaded).toEqual(true);
27-
28-
const notLoadedRegistry = createSplitRegistry(null);
29-
expect(notLoadedRegistry.isLoaded).toEqual(false);
25+
expect(splitRegistry.isLoaded).toBe(true);
3026
});
3127
});
3228

@@ -37,3 +33,11 @@ describe('SplitRegistry', () => {
3733
});
3834
});
3935
});
36+
37+
describe('NULL_SPLIT_REGISTRY', () => {
38+
it('provides a null implementation', () => {
39+
expect(NULL_SPLIT_REGISTRY.isLoaded).toBe(false);
40+
expect(NULL_SPLIT_REGISTRY.getSplit('foo')).toBeUndefined();
41+
expect(NULL_SPLIT_REGISTRY.asV1Hash()).toBeUndefined();
42+
});
43+
});

src/splitRegistry.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ export type SplitRegistry = {
1010
asV1Hash: () => V1Hash;
1111
};
1212

13-
export function createSplitRegistry(input: Split[] | null): SplitRegistry {
14-
const isLoaded = input !== null;
15-
const splits = Object.fromEntries((input || []).map(split => [split.name, split]));
13+
export const NULL_SPLIT_REGISTRY: SplitRegistry = {
14+
isLoaded: false,
15+
getSplit: () => undefined,
16+
asV1Hash: () => ({})
17+
};
1618

17-
return {
18-
isLoaded,
19-
getSplit: splitName => splits[splitName],
20-
asV1Hash: () => Object.fromEntries(Object.entries(splits).map(([splitName, split]) => [splitName, split.weighting]))
21-
};
22-
}
19+
export const createSplitRegistry = (splits: Split[]): SplitRegistry => ({
20+
isLoaded: true,
21+
getSplit: splitName => splits.find(split => split.name === splitName),
22+
asV1Hash: () => Object.fromEntries(splits.map(split => [split.name, split.weighting]))
23+
});

0 commit comments

Comments
 (0)