Skip to content

Commit ee50293

Browse files
refactor: simplify fallback treatment sanitization and type handling
1 parent 26fe9fb commit ee50293

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

src/evaluator/fallbackTreatmentsCalculator/__tests__/fallback-sanitizer.spec.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ describe('FallbacksSanitizer', () => {
1111
};
1212

1313
beforeEach(() => {
14-
jest.spyOn(console, 'error').mockImplementation(() => {});
15-
});
16-
17-
afterEach(() => {
18-
(loggerMock.error as jest.Mock).mockRestore();
14+
loggerMock.mockClear();
1915
});
2016

2117
describe('isValidFlagName', () => {
@@ -113,24 +109,25 @@ describe('FallbacksSanitizer', () => {
113109
expect(loggerMock.error).not.toHaveBeenCalled();
114110
});
115111
});
112+
116113
describe('sanitizeFallbacks', () => {
117-
test('returns undefined and logs error if fallbacks is not an object', () => {
114+
test('returns undefined and logs error if fallbacks is not an object', () => { // @ts-expect-error
118115
const result = sanitizeFallbacks(loggerMock, 'invalid_fallbacks');
119116
expect(result).toBeUndefined();
120117
expect(loggerMock.error).toHaveBeenCalledWith(
121118
'Fallback treatments - Discarded configuration: it must be an object with optional `global` and `byFlag` properties'
122119
);
123120
});
124121

125-
test('returns undefined and logs error if fallbacks is not an object', () => {
122+
test('returns undefined and logs error if fallbacks is not an object', () => { // @ts-expect-error
126123
const result = sanitizeFallbacks(loggerMock, true);
127124
expect(result).toBeUndefined();
128125
expect(loggerMock.error).toHaveBeenCalledWith(
129126
'Fallback treatments - Discarded configuration: it must be an object with optional `global` and `byFlag` properties'
130127
);
131128
});
132129

133-
test('sanitizes both global and byFlag fallbacks for empty object', () => {
130+
test('sanitizes both global and byFlag fallbacks for empty object', () => { // @ts-expect-error
134131
const result = sanitizeFallbacks(loggerMock, { global: {} });
135132
expect(result).toEqual({ global: undefined, byFlag: {} });
136133
});

src/evaluator/fallbackTreatmentsCalculator/fallbackSanitizer/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ function sanitizeGlobal(logger: ILogger, treatment?: Treatment | TreatmentWithCo
3333

3434
function sanitizeByFlag(
3535
logger: ILogger,
36-
byFlagFallbacks: Record<string, Treatment | TreatmentWithConfig> = {}
36+
byFlagFallbacks?: Record<string, Treatment | TreatmentWithConfig>
3737
): Record<string, Treatment | TreatmentWithConfig> {
3838
const sanitizedByFlag: Record<string, Treatment | TreatmentWithConfig> = {};
3939

40-
const entries = Object.keys(byFlagFallbacks);
41-
entries.forEach((flag) => {
42-
const t = byFlagFallbacks[flag];
43-
if (!t) return;
40+
if (!isObject(byFlagFallbacks)) return sanitizedByFlag;
41+
42+
Object.keys(byFlagFallbacks!).forEach((flag) => {
43+
const t = byFlagFallbacks![flag];
44+
4445
if (!isValidFlagName(flag)) {
4546
logger.error(`Fallback treatments - Discarded flag '${flag}': ${FallbackDiscardReason.FlagName}`);
4647
return;
@@ -57,14 +58,14 @@ function sanitizeByFlag(
5758
return sanitizedByFlag;
5859
}
5960

60-
export function sanitizeFallbacks(logger: ILogger, fallbacks: unknown): FallbackTreatmentConfiguration | undefined {
61+
export function sanitizeFallbacks(logger: ILogger, fallbacks: FallbackTreatmentConfiguration): FallbackTreatmentConfiguration | undefined {
6162
if (!isObject(fallbacks)) {
6263
logger.error('Fallback treatments - Discarded configuration: it must be an object with optional `global` and `byFlag` properties');
6364
return;
6465
}
6566

6667
return {
67-
global: sanitizeGlobal(logger, (fallbacks as FallbackTreatmentConfiguration).global),
68-
byFlag: sanitizeByFlag(logger, (fallbacks as FallbackTreatmentConfiguration).byFlag)
68+
global: sanitizeGlobal(logger, fallbacks.global),
69+
byFlag: sanitizeByFlag(logger, fallbacks.byFlag)
6970
};
7071
}

src/evaluator/fallbackTreatmentsCalculator/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ export type IFallbackTreatmentsCalculator = {
99
export const FALLBACK_PREFIX = 'fallback - ';
1010

1111
export class FallbackTreatmentsCalculator implements IFallbackTreatmentsCalculator {
12-
constructor(private readonly fallbacks: FallbackTreatmentConfiguration = {}) {}
12+
private readonly fallbacks: FallbackTreatmentConfiguration;
13+
14+
constructor(fallbacks: FallbackTreatmentConfiguration = {}) {
15+
this.fallbacks = fallbacks;
16+
}
1317

1418
resolve(flagName: string, label: string): TreatmentWithConfig & { label: string } {
1519
const treatment = this.fallbacks.byFlag?.[flagName];

0 commit comments

Comments
 (0)