From 86bc4ea4ac4dcc4737a588c84dc98a51d8362c18 Mon Sep 17 00:00:00 2001 From: Nikola Mihaylov Date: Tue, 30 Mar 2021 13:57:13 +0300 Subject: [PATCH] Remove redundant number tagging --- src/data-tagging.ts | 22 +++++----------------- src/tests/unit-tests/data-tagging.test.ts | 15 --------------- 2 files changed, 5 insertions(+), 32 deletions(-) diff --git a/src/data-tagging.ts b/src/data-tagging.ts index 81c6bb8..50087b1 100644 --- a/src/data-tagging.ts +++ b/src/data-tagging.ts @@ -7,19 +7,11 @@ export enum Tag { type PlainObject = Record; -type Tagged = { - [K in keyof T]: T[K] extends number ? string | number : Tagged -} - export function tagString(value: string, tag: Tag): string { return `[${tag}]${value}[/${tag}]`; } -export function tagNumber(value: number, tag: Tag): string { - return `[${tag}]${value}[/${tag}]`; -} - -export function tagObject(object: T, tagSettings: Array<[string, Tag]>): Tagged { +export function tagObject(object: T, tagSettings: Array<[string, Tag]>): T { if (isError(object)) { return tagError(object, tagSettings); } else if (isPlainObject(object)) { @@ -29,7 +21,7 @@ export function tagObject(object: T, tagSettings: throw new Error('Input must be plain object or a class inheriting from Error'); } -function tagPlainObject(plainObject: T, tagSettings: Array<[string, Tag]>): Tagged { +function tagPlainObject(plainObject: T, tagSettings: Array<[string, Tag]>): T { const clonedObj = clone(plainObject); const pathToTagMap = constructPathToTagMap(tagSettings); @@ -44,16 +36,12 @@ function tagPlainObject(plainObject: T, tagSettings: Arra if (typeof rawValue === 'string') { lodash.set(clonedObj, path, tagString(rawValue, tag)); } - - if (typeof rawValue === 'number') { - lodash.set(clonedObj, path, tagNumber(rawValue, tag)); - } } - return clonedObj as Tagged; + return clonedObj; } -function tagError(err: T, tagSettings: Array<[string, Tag]>): Tagged { +function tagError(err: T, tagSettings: Array<[string, Tag]>): T { const clonedErr = new Error(err.message); Object.setPrototypeOf(clonedErr, Object.getPrototypeOf(err)); @@ -64,7 +52,7 @@ function tagError(err: T, tagSettings: Array<[string, Tag]>): T Object.assign(clonedErr, tagPlainObject(dataToAssign, tagSettings)); - return clonedErr as Tagged; + return clonedErr as T; } function collectPaths(input: any, currentPath?: string) { diff --git a/src/tests/unit-tests/data-tagging.test.ts b/src/tests/unit-tests/data-tagging.test.ts index cf37f32..679b496 100644 --- a/src/tests/unit-tests/data-tagging.test.ts +++ b/src/tests/unit-tests/data-tagging.test.ts @@ -1,5 +1,4 @@ import { - tagNumber, tagString, tagObject, Tag, @@ -12,12 +11,6 @@ describe('Data Tagging', () => { }); }); - describe('tagNumber', () => { - it('should wrap the provided tag around the number that needs to be tagd', () => { - expect(tagNumber(42, Tag.PII)).toEqual('[PII]42[/PII]'); - }); - }); - describe('tagObject', () => { it('should wrap the provided tag around root-level elements in object', () => { const originalObject = { name: 'Pencho' }; @@ -31,14 +24,6 @@ describe('Data Tagging', () => { expect(tagObject(originalObject, [['data.name', Tag.PII], ['data.email', Tag.PII]])).toEqual(taggedObject); }); - it('should work with numbers', () => { - const originalObject = { id: 1 }; - - expect(tagObject(originalObject, [['id', Tag.PII]])).toEqual({ - id: '[PII]1[/PII]', - }); - }); - it('should NOT wrap the provided tag around elements that are not specified for taging in object', () => { const originalObject = { favouriteColor: 'red', nested: { field: 'value' } }; expect(tagObject(originalObject, [['name', Tag.PII]])).toEqual(originalObject);