Skip to content

Commit

Permalink
fix: Replace humanize string with own implementation. (#250)
Browse files Browse the repository at this point in the history
* chore: Add a more precise test for the error message formatting.

* fix: Replace humanize-string with own implementation.

* chore: Add new contributors.
  • Loading branch information
Hannes Leutloff authored Jan 20, 2021
1 parent 8749102 commit da09278
Show file tree
Hide file tree
Showing 6 changed files with 932 additions and 440 deletions.
4 changes: 2 additions & 2 deletions lib/defekt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CustomError } from './CustomError';
import { ErrorConstructors } from './ErrorConstructors';
import humanizeString from 'humanize-string';
import { formatErrorMessage } from './formatErrorMessage';

const defekt = function <TErrorDefinition extends Record<string, { code?: string }>>
(errorDefinitions: TErrorDefinition): ErrorConstructors<TErrorDefinition> {
Expand All @@ -26,7 +26,7 @@ const defekt = function <TErrorDefinition extends Record<string, { code?: string
public static code: string = code;

/* eslint-disable default-param-last */
public constructor (message = `${humanizeString(errorName)}.`, {
public constructor (message = formatErrorMessage({ errorName }), {
cause,
data
}: {
Expand Down
24 changes: 24 additions & 0 deletions lib/formatErrorMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const formatErrorMessage = function ({ errorName }: {
errorName: string;
}): string {
const almostFormattedErrorMessage = errorName.
split('').
map((character: string, index: number): string => {
if (index === 0) {
return character.toUpperCase();
}

if (character.toUpperCase() === character) {
return ` ${character.toLowerCase()}`;
}

return character;
}).
join('');

return `${almostFormattedErrorMessage}.`;
};

export {
formatErrorMessage
};
Loading

0 comments on commit da09278

Please sign in to comment.