Skip to content

Commit

Permalink
chore: Upgrade packages, add explicit prettier config
Browse files Browse the repository at this point in the history
  • Loading branch information
jamcry committed Nov 17, 2020
1 parent 1fccf57 commit 5fda357
Show file tree
Hide file tree
Showing 11 changed files with 2,129 additions and 3,239 deletions.
3 changes: 1 addition & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"parser": "@typescript-eslint/parser",
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
"prettier/@typescript-eslint"
],
"parserOptions": {
"ecmaVersion": 2018,
Expand Down
12 changes: 12 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
printWidth: 80,
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: false,
jsxSingleQuote: false,
trailingComma: "none",
bracketSpacing: false,
jsxBracketSameLine: true,
arrowParens: "always"
};
5,253 changes: 2,069 additions & 3,184 deletions package-lock.json

Large diffs are not rendered by default.

27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,25 @@
"homepage": "https://github.com/Hipo/hipo-exceptions-js#readme",
"lint-staged": {
"src/**/*.{ts,tsx}": [
"lint"
"eslint"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && npm run test && npm run type-check"
"pre-commit": "lint-staged && npm run test && npm run type-check"
}
},
"devDependencies": {
"@types/jest": "25.1.2",
"@typescript-eslint/eslint-plugin": "2.6.1",
"@typescript-eslint/parser": "2.6.1",
"eslint": "6.6.0",
"eslint-config-prettier": "6.5.0",
"eslint-plugin-prettier": "3.1.1",
"husky": "4.2.3",
"jest": "25.1.0",
"lint-staged": "10.0.8",
"prettier": "1.19.1",
"ts-jest": "25.2.0",
"typescript": "3.7.2"
"@types/jest": "26.0.15",
"@typescript-eslint/eslint-plugin": "4.6.0",
"@typescript-eslint/parser": "4.6.0",
"eslint": "7.12.1",
"eslint-config-prettier": "6.15.0",
"husky": "4.3.0",
"jest": "26.6.1",
"lint-staged": "10.5.0",
"prettier": "2.1.2",
"ts-jest": "26.4.3",
"typescript": "4.0.5"
}
}
2 changes: 1 addition & 1 deletion src/ExceptionTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createMapFromObject, isObjectEmpty } from "./utils/dataStructureUtils";
import {createMapFromObject, isObjectEmpty} from "./utils/dataStructureUtils";
import {
getErrorDetail,
getStringMessage,
Expand Down
4 changes: 2 additions & 2 deletions src/ExceptionTransformerModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type ExceptionMap = Map<string, object>;
type ExceptionMap = Map<string, unknown>;

interface ExceptionDetail {
[key: string]: ExceptionDetailValue;
Expand All @@ -21,7 +21,7 @@ type ErrorMessageGeneratorOptions = {
skipTypes?: string[];
shouldHideErrorKey?: boolean;
shouldCapitalizeErrorKey?: boolean;
fieldLabelMap?: { [key: string]: string };
fieldLabelMap?: {[key: string]: string};
};

type OnUnexpectedException = (details: {
Expand Down
17 changes: 6 additions & 11 deletions src/utils/dataStructureUtils.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
function createMapFromObject(obj: { [key: string]: any }) {
function createMapFromObject(obj: {[key: string]: any}) {
return new Map(Object.entries(obj));
}

function isArrayOfStrings(x: unknown): x is string[] {
return Array.isArray(x) && x.every(item => typeof item === "string");
return Array.isArray(x) && x.every((item) => typeof item === "string");
}

function isArrayOfObjects(x: unknown): x is Array<{ [key: string]: any }> {
function isArrayOfObjects(x: unknown): x is Array<{[key: string]: any}> {
return (
Array.isArray(x) &&
x.every(item => typeof item === "object" && !Array.isArray(item))
x.every((item) => typeof item === "object" && !Array.isArray(item))
);
}

function isObjectEmpty(obj: unknown): obj is {} {
function isObjectEmpty(obj: unknown): obj is unknown {
return typeof obj === "object" && !Array.isArray(obj) && obj
? Object.keys(obj).length === 0
: false;
}

export {
createMapFromObject,
isArrayOfStrings,
isArrayOfObjects,
isObjectEmpty
};
export {createMapFromObject, isArrayOfStrings, isArrayOfObjects, isObjectEmpty};
10 changes: 5 additions & 5 deletions src/utils/errorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isArrayOfObjects,
isObjectEmpty
} from "./dataStructureUtils";
import { convertSnakeCaseToTitleCase } from "./stringUtils";
import {convertSnakeCaseToTitleCase} from "./stringUtils";

function getErrorDetail(
errorInfo: Exception | null | undefined
Expand Down Expand Up @@ -53,7 +53,7 @@ interface StringMessageGeneratorKeyOptions {
customKey?: string;
shouldCapitalizeErrorKey?: boolean;
shouldHideErrorKey?: boolean;
fieldLabelMap?: { [key: string]: string };
fieldLabelMap?: {[key: string]: string};
}

function getStringMessage(
Expand All @@ -72,7 +72,7 @@ function getStringMessage(
} else if (isArrayOfObjects(errorDetailValue)) {
// errorDetailValue = [ {}, {}, {..} ]
const firstNonEmptyErrorObject = (errorDetailValue as ExceptionDetail[]).find(
x => !isObjectEmpty(x)
(x) => !isObjectEmpty(x)
);

if (firstNonEmptyErrorObject) {
Expand Down Expand Up @@ -135,7 +135,7 @@ function getErrorKeyForStringMessageGenerator(
}

function deleteProperty(exceptionDetail: ExceptionDetail, path: string) {
const filteredObj = { ...exceptionDetail };
const filteredObj = {...exceptionDetail};
const keys = path.split(".");

keys.reduce<undefined | ExceptionDetailValue>((value, key, index) => {
Expand Down Expand Up @@ -164,7 +164,7 @@ function removeKnownKeysFromErrorDetail(
}

function getValueFromPath(exceptionDetail: ExceptionDetail, path: string) {
const filteredObj = { ...exceptionDetail };
const filteredObj = {...exceptionDetail};
const keys = path.split(".");

return keys.reduce<undefined | ExceptionDetailValue>((acc, key) => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function convertSnakeCaseToTitleCase(string: string) {
return string
.split("_")
.map(word => word[0].toUpperCase() + word.slice(1))
.map((word) => word[0].toUpperCase() + word.slice(1))
.join(" ");
}

export { convertSnakeCaseToTitleCase };
export {convertSnakeCaseToTitleCase};
10 changes: 5 additions & 5 deletions tests/utils/dataStructureUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe("createMapFromObject", () => {
{},
{},
{},
{ phone_number: ["The phone number entered is not valid."] }
{phone_number: ["The phone number entered is not valid."]}
]
},
fallback_message: "This is a random fallback message"
Expand All @@ -80,7 +80,7 @@ describe("createMapFromObject", () => {
{},
{},
{},
{ phone_number: ["The phone number entered is not valid."] }
{phone_number: ["The phone number entered is not valid."]}
]
}
],
Expand Down Expand Up @@ -179,7 +179,7 @@ describe("isArrayOfObjects", () => {

it("should return true when given array of non empty object(s)", () => {
const objectArrayResult = isArrayOfObjects([
{ "key-string-1": "value-string-1" }
{"key-string-1": "value-string-1"}
]);
const longObjectArrayResult = isArrayOfObjects([
{
Expand Down Expand Up @@ -224,7 +224,7 @@ describe("isArrayOfObjects", () => {
const arrayOfArraysAndObjectsResult = isArrayOfObjects([
[],
["not", "empty", "array"],
{ "object-key": "object-value" }
{"object-key": "object-value"}
]);

expect(emptyArrayOfArrayResult).toBe(false);
Expand Down Expand Up @@ -259,6 +259,6 @@ describe("isObjectEmpty", () => {
});

it("should return false when object is not empty", () => {
expect(isObjectEmpty({ "any key": "any value" })).toBe(false);
expect(isObjectEmpty({"any key": "any value"})).toBe(false);
});
});
26 changes: 13 additions & 13 deletions tests/utils/errorUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ describe("getErrorDetail", () => {
type: "ValidationError",
detail: {
non_field_errors: [
{ address: ["The address entered is not valid."] },
{ phone_number: ["The phone number entered is not valid."] }
{address: ["The address entered is not valid."]},
{phone_number: ["The phone number entered is not valid."]}
]
},
fallback_message: "This is a random fallback message"
Expand All @@ -49,8 +49,8 @@ describe("getErrorDetail", () => {

expect(detail).toStrictEqual({
non_field_errors: [
{ address: ["The address entered is not valid."] },
{ phone_number: ["The phone number entered is not valid."] }
{address: ["The address entered is not valid."]},
{phone_number: ["The phone number entered is not valid."]}
]
});
});
Expand All @@ -62,8 +62,8 @@ describe("getErrorDetail", () => {
non_field_errors: [
{},
{},
{ address: ["The address entered is not valid."] },
{ phone_number: ["The phone number entered is not valid."] }
{address: ["The address entered is not valid."]},
{phone_number: ["The phone number entered is not valid."]}
]
},
fallback_message: "This is a random fallback message"
Expand All @@ -87,8 +87,8 @@ describe("getErrorDetail", () => {
non_field_errors: [
{},
{},
{ address: ["The address entered is not valid."] },
{ phone_number: ["The phone number entered is not valid."] }
{address: ["The address entered is not valid."]},
{phone_number: ["The phone number entered is not valid."]}
]
});
expect(detailWithOnlyEmptyObjects).toStrictEqual({
Expand Down Expand Up @@ -170,7 +170,7 @@ describe("generateMessageFromStringArray", () => {
describe("generateFieldErrorFromErrorDetail", () => {
const mockErrorDetail: ExceptionDetail = {
title: ["Title is missing"],
questions: [{}, {}, {}, { answer: ["required"] }, {}]
questions: [{}, {}, {}, {answer: ["required"]}, {}]
};

it("should return correct field error", () => {
Expand Down Expand Up @@ -238,7 +238,7 @@ describe("getStringMessage", () => {

it("should replace error key using fieldLabelMap", () => {
const message = getStringMessage(mockErrorDetail, {
fieldLabelMap: { phone_number: "Custom Title" }
fieldLabelMap: {phone_number: "Custom Title"}
});
expect(message).toBe(
"Custom Title: The phone number entered is not valid."
Expand All @@ -247,7 +247,7 @@ describe("getStringMessage", () => {

it("should not replace error key if no matching value in fieldLabelMap", () => {
const message = getStringMessage(mockErrorDetail, {
fieldLabelMap: { address: "Custom Title" }
fieldLabelMap: {address: "Custom Title"}
});
expect(message).toBe(
"phone_number: The phone number entered is not valid."
Expand All @@ -258,7 +258,7 @@ describe("getStringMessage", () => {
describe("when error detail is an object", () => {
const mockErrorDetail: ExceptionDetailValue = {
title: ["Title is missing"],
questions: [{}, {}, {}, { answer: ["required"] }, {}]
questions: [{}, {}, {}, {answer: ["required"]}, {}]
};

it("should return first field's error message", () => {
Expand Down Expand Up @@ -298,7 +298,7 @@ describe("getStringMessage", () => {

it("should add given key to the message and return", () => {
const key = "Title";
const message = getStringMessage(mockErrorDetail, { customKey: key });
const message = getStringMessage(mockErrorDetail, {customKey: key});
expect(message).toBe(`${key}: ${mockErrorDetail[0]}`);
});
});
Expand Down

0 comments on commit 5fda357

Please sign in to comment.