From b452bf7caae65d6147b43d5037de8436ac536bbb Mon Sep 17 00:00:00 2001 From: ssube Date: Sat, 1 Aug 2020 10:11:29 -0500 Subject: [PATCH] feat(map): normalize helper handles numeric values --- src/Map.ts | 6 +++--- test/utils/TestMap.ts | 29 ++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/Map.ts b/src/Map.ts index 36d3cb67..6714315b 100644 --- a/src/Map.ts +++ b/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'; @@ -209,7 +207,9 @@ export function normalizeMap(map: MapLike): Dict> { 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()]; } } diff --git a/test/utils/TestMap.ts b/test/utils/TestMap.ts index 9da7758f..366bf080 100644 --- a/test/utils/TestMap.ts +++ b/test/utils/TestMap.ts @@ -8,12 +8,12 @@ import { getOrDefault, makeDict, makeMap, + mergeMap, mustGet, + normalizeMap, pairsToMap, - setOrPush, - mergeMap, pushMergeMap, - normalizeMap, + setOrPush, } from '../../src/Map'; const DEFAULT_VALUE = 'default'; @@ -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]); + }); }); });