Skip to content

Commit

Permalink
feat(map): normalize helper handles numeric values
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Aug 1, 2020
1 parent cae0432 commit b452bf7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Map.ts
@@ -1,5 +1,3 @@
import { isObject } from 'lodash';

import { NotFoundError } from './error/NotFoundError';
import { mergeList, toList } from './List';
import { doesExist, isNil, mustExist, Optional } from './Maybe';
Expand Down Expand Up @@ -209,7 +207,9 @@ export function normalizeMap(map: MapLike<unknown>): Dict<Array<string>> {
data[key] = value;
} else if (typeof value === 'string') {
data[key] = [value];
} else if (isObject(value)) {
} else if (typeof value === 'number') {
data[key] = [value.toString()];
} else if (typeof value === 'object' && doesExist(value)) {
data[key] = [value.toString()];
}
}
Expand Down
29 changes: 24 additions & 5 deletions test/utils/TestMap.ts
Expand Up @@ -8,12 +8,12 @@ import {
getOrDefault,
makeDict,
makeMap,
mergeMap,
mustGet,
normalizeMap,
pairsToMap,
setOrPush,
mergeMap,
pushMergeMap,
normalizeMap,
setOrPush,
} from '../../src/Map';

const DEFAULT_VALUE = 'default';
Expand Down Expand Up @@ -227,7 +227,26 @@ describe('map utils', async () => {
expect(normalized.toad).to.deep.equal(['too']);
});

/* ['foo', 1] */
xit('should convert numbers into string values');
it('should convert number arguments into string values', () => {
const INPUT_VALUE = 123;
const input = new Map([
['foo', INPUT_VALUE],
]);
const normalized = normalizeMap(input);

expect(normalized.foo).to.deep.equal([INPUT_VALUE.toString()]);
});

it('should convert object arguments into string values', () => {
const OUTPUT_VALUE = 'bar';
const input = new Map([
['foo', {
toString: () => OUTPUT_VALUE,
}],
]);
const normalized = normalizeMap(input);

expect(normalized.foo).to.deep.equal([OUTPUT_VALUE]);
});
});
});

0 comments on commit b452bf7

Please sign in to comment.