Skip to content

Commit

Permalink
Remove redundant number tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikola Mihaylov committed Mar 30, 2021
1 parent bc7e7ee commit 86bc4ea
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 32 deletions.
22 changes: 5 additions & 17 deletions src/data-tagging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,11 @@ export enum Tag {

type PlainObject = Record<string, unknown>;

type Tagged<T> = {
[K in keyof T]: T[K] extends number ? string | number : Tagged<T[K]>
}

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<T extends PlainObject | Error>(object: T, tagSettings: Array<[string, Tag]>): Tagged<T> {
export function tagObject<T extends PlainObject | Error>(object: T, tagSettings: Array<[string, Tag]>): T {
if (isError(object)) {
return tagError(object, tagSettings);
} else if (isPlainObject(object)) {
Expand All @@ -29,7 +21,7 @@ export function tagObject<T extends PlainObject | Error>(object: T, tagSettings:
throw new Error('Input must be plain object or a class inheriting from Error');
}

function tagPlainObject<T extends PlainObject>(plainObject: T, tagSettings: Array<[string, Tag]>): Tagged<T> {
function tagPlainObject<T extends PlainObject>(plainObject: T, tagSettings: Array<[string, Tag]>): T {
const clonedObj = clone(plainObject);
const pathToTagMap = constructPathToTagMap(tagSettings);

Expand All @@ -44,16 +36,12 @@ function tagPlainObject<T extends PlainObject>(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<T>;
return clonedObj;
}

function tagError<T extends Error>(err: T, tagSettings: Array<[string, Tag]>): Tagged<T> {
function tagError<T extends Error>(err: T, tagSettings: Array<[string, Tag]>): T {
const clonedErr = new Error(err.message);
Object.setPrototypeOf(clonedErr, Object.getPrototypeOf(err));

Expand All @@ -64,7 +52,7 @@ function tagError<T extends Error>(err: T, tagSettings: Array<[string, Tag]>): T

Object.assign(clonedErr, tagPlainObject(dataToAssign, tagSettings));

return clonedErr as Tagged<T>;
return clonedErr as T;
}

function collectPaths(input: any, currentPath?: string) {
Expand Down
15 changes: 0 additions & 15 deletions src/tests/unit-tests/data-tagging.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
tagNumber,
tagString,
tagObject,
Tag,
Expand All @@ -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' };
Expand All @@ -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);
Expand Down

0 comments on commit 86bc4ea

Please sign in to comment.