Skip to content

Commit

Permalink
fix: implementation of copying problem object
Browse files Browse the repository at this point in the history
  • Loading branch information
imabp committed Oct 22, 2022
1 parent fbf15b4 commit 1224cba
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
4 changes: 3 additions & 1 deletion __tests__/_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export default class ProblemContextHelper {
_instanceOptions: ProblemInterface = {
type: "null-or-falsey-document",
title: "The AsyncAPI document is null or a JS falsey value.",
detail: "The AsyncAPI document is null or a JS falsey value."
detail: "The AsyncAPI document is null or a JS falsey value.",
leaveThisWhenCopy:"This is used to test copy function: LEAVE PROPS. This will not be undefined in new copy. ",
skipThisWhenCopy:"This is used to test copy functionL SKIP PROPS. This should be undefined in new copy.",
}


Expand Down
8 changes: 5 additions & 3 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ describe("Class Methods Test Suite", () => {
test("Method: Copy, mode: LEAVE_PROPS", () => {
const _problemCopy = _testContext._problemInstance.copy(
COPY_MODE.LEAVE_PROPS,
[]
["leaveThisWhenCopy"]
);
expect(_problemCopy).toBeInstanceOf(Problem);
expect(_problemCopy.type).toBe(_testContext._problemInstance.type);
expect(_problemCopy.title).toBe(_testContext._problemInstance.title);
expect(_problemCopy.leaveThisWhenCopy).toBe(_testContext._problemInstance.leaveThisWhenCopy);
});

test("Method: Copy, mode: SKIP_PROPS", () => {
const _copiedProblem = _testContext._problemInstance.copy(
COPY_MODE.SKIP_PROPS,
["details"]
["skipThisWhenCopy"]
);
expect(_copiedProblem.details).toBeUndefined();
expect(_copiedProblem).toBeInstanceOf(Problem);
expect(_copiedProblem.skipThisWhenCopy).toBeUndefined();
});

test("Method: isOfType", () => {
Expand Down
4 changes: 0 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export const DEFAULT_KEYS = [
"http",
"type",
"title",
"detail",
"instance",
"stack",
];

export enum COPY_MODE {
Expand Down
30 changes: 20 additions & 10 deletions src/problem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "./types";

import { COPY_MODE } from "./constants";

import { objectToProblemMap } from "./util";
export class Problem extends Error implements ProblemInterface {
public type: string;
public title: string;
Expand All @@ -33,20 +33,30 @@ export class Problem extends Error implements ProblemInterface {

copy(mode: COPY_MODE = COPY_MODE.LEAVE_PROPS, props: string[] = []): Problem {
switch (mode) {
case COPY_MODE.LEAVE_PROPS:
const newProblem = new Problem(this.problem);
props.forEach((prop) => (newProblem[prop] = undefined));
// returns a new problem object with preserved keys passed as props
case COPY_MODE.LEAVE_PROPS:{
let newProblemKeyValuePairs:Record<string,any> = {
type: this.problem.type,
title:this.problem.title,
}
props.forEach((key)=>{
newProblemKeyValuePairs={...newProblemKeyValuePairs, [key]:this.problem[key]}
})
const newProblem = new Problem(objectToProblemMap(newProblemKeyValuePairs));
return newProblem;

}
// skip the copy of keys
case COPY_MODE.SKIP_PROPS:
default: {
const newProblem = new Problem(this.problem);
let keysToBeCopied: string[] = [];
for (let key in newProblem) {
// Default Keys cannot be skipped
let newProblemKeyValuePairs:Record<string,any>={};

// loop to copy only the required keys
for (let key in this.problem) {
// Skip only those keys, which are given in props and NOT a default key.
if (props.includes(key) && !DEFAULT_KEYS.includes(key)) continue;
keysToBeCopied.push(key);
newProblemKeyValuePairs[key] = this.problem[key];
}
const newProblem = new Problem(objectToProblemMap(newProblemKeyValuePairs))
return newProblem;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ProblemInterface } from "types";

export const objectToProblemMap = (obj:Record<string,any>) =>{
const type: string = obj.type;
const title: string = obj.title;
const problemObject:ProblemInterface = {
type, title, ...obj
}
return problemObject
}

0 comments on commit 1224cba

Please sign in to comment.