Skip to content

Commit

Permalink
chore: linter
Browse files Browse the repository at this point in the history
  • Loading branch information
pnodet committed Nov 25, 2024
1 parent 8148715 commit 88d5207
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/arrays/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const remove = <T>(array: T[], element: T): T[] => {
const array_ = [...array];
const idx = array_.indexOf(element);

if (idx < 0) {
if (idx === -1) {
return [];
}

Expand Down
22 changes: 15 additions & 7 deletions src/currency/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,54 @@
import { describe, expect, test } from 'bun:test';
import { formatPrice, type Currency } from './index';
import { formatPrice } from './index';
import type { Currency } from './index';

describe('formatPrice', () => {
test('should format price with default locale and cents enabled', () => {
const money = { amount: '1234.56', currencyCode: 'EUR' } satisfies Currency;
const result = formatPrice(money);

expect(result).toBe('1 234,56 €');
});

test('should format price with custom locale and cents enabled', () => {
const money = { amount: '1234.56', currencyCode: 'USD' }satisfies Currency;
const money = { amount: '1234.56', currencyCode: 'USD' } satisfies Currency;
const result = formatPrice(money, 'en-US');

expect(result).toBe('$1,234.56');
});

test('should format price with quantity multiplier', () => {
const money = { amount: '1234.56', currencyCode: 'USD' }satisfies Currency;
const money = { amount: '1234.56', currencyCode: 'USD' } satisfies Currency;
const result = formatPrice(money, 'en-US', 2);

expect(result).toBe('$2,469.12');
});

test('should format price with cents disabled when applicable', () => {
const money = { amount: '1234.00', currencyCode: 'USD' }satisfies Currency;
const money = { amount: '1234.00', currencyCode: 'USD' } satisfies Currency;
const result = formatPrice(money, 'en-US', 1, true);

expect(result).toBe('$1,234');
});

test('should still include cents if the amount has fractional part even if disableCents is true', () => {
const money = { amount: '1234.56', currencyCode: 'USD' }satisfies Currency;
const money = { amount: '1234.56', currencyCode: 'USD' } satisfies Currency;
const result = formatPrice(money, 'en-US', 1, true);

expect(result).toBe('$1,234.56');
});

test('should format price correctly with a different currency code', () => {
const money = { amount: '1234.56', currencyCode: 'GBP' }satisfies Currency;
const money = { amount: '1234.56', currencyCode: 'GBP' } satisfies Currency;
const result = formatPrice(money, 'en-GB');

expect(result).toBe('£1,234.56');
});

test('should handle large quantities correctly', () => {
const money = { amount: '1000.00', currencyCode: 'EUR' }satisfies Currency;
const money = { amount: '1000.00', currencyCode: 'EUR' } satisfies Currency;
const result = formatPrice(money, 'fr', 100);

expect(result).toBe('100 000,00 €');
});
});
12 changes: 6 additions & 6 deletions src/exceptions/create-custom.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-classes-per-file */
import { generateId } from '../generate';
import { SafeJson } from '../safe-json';
import { httpStatus } from '../http-status';
Expand Down Expand Up @@ -71,11 +72,10 @@ export class Exception extends Error {
this.traceId = options?.traceId || generateId(8);
this.readableMessage = options?.readableMessage ?? undefined;
this.timestamp = options?.timestamp ?? Date.now();
this.meta = Object.assign(
{},
options?.meta,
(options?.cause as Exception | undefined)?.meta,
);
this.meta = {
...options?.meta,
...(options?.cause as Exception | undefined)?.meta,
};
this.logLevel = options?.logLevel ?? 'error';

if (options?.stack) {
Expand Down Expand Up @@ -153,7 +153,7 @@ export class Exception extends Error {
}

addMeta(meta: { [key: string]: unknown }): this {
(this as Mutable<Exception>).meta = Object.assign({}, this.meta, meta);
(this as Mutable<Exception>).meta = { ...this.meta, ...meta };

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/is/is-equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { union } from '../arrays/union';
* isEqual(new Date('2020-01-01'), new Date('2020-01-01')); // true
* isEqual([1, 2, 3], [1, 2, 3]); // true
*/
// eslint-disable-next-line id-length

export function isEqual(a: unknown, b: unknown): boolean {
if (Object.is(a, b)) {
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/objects/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export function clone<T>(obj: T): T {
}

if (typeof obj === 'object') {
return Object.assign({}, obj) as T;
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
return { ...obj } as T;
}

return obj;
Expand Down
1 change: 0 additions & 1 deletion src/regexp/email.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const RE_EMAIL =
// eslint-disable-next-line unicorn/better-regex
/^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}\])|(([\da-z-]+\.)+[a-z]{2,}))$/i;
12 changes: 12 additions & 0 deletions src/strings/escape.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@ describe('htmlEscape', () => {
test('should escape all relevant characters', () => {
const input = `Hello & "World" <'test'>`;
const expected = 'Hello &amp; &quot;World&quot; &lt;&#39;test&#39;&gt;';

expect(htmlEscape(input)).toBe(expected);
});

test('should escape ampersands correctly', () => {
const input = 'Fish & Chips';
const expected = 'Fish &amp; Chips';

expect(htmlEscape(input)).toBe(expected);
});

test('should escape double quotes correctly', () => {
const input = 'She said "Hello"';
const expected = 'She said &quot;Hello&quot;';

expect(htmlEscape(input)).toBe(expected);
});

test('should escape single quotes correctly', () => {
const input = "It's a test";
const expected = 'It&#39;s a test';

expect(htmlEscape(input)).toBe(expected);
});

test('should escape less than and greater than signs correctly', () => {
const input = '<div>Hello</div>';
const expected = '&lt;div&gt;Hello&lt;/div&gt;';

expect(htmlEscape(input)).toBe(expected);
});
});
Expand All @@ -37,42 +42,49 @@ describe('htmlUnescape', () => {
test('should unescape all relevant characters', () => {
const input = 'Hello &amp; &quot;World&quot; &lt;&#39;test&#39;&gt;';
const expected = `Hello & "World" <'test'>`;

expect(htmlUnescape(input)).toBe(expected);
});

test('should unescape ampersands correctly', () => {
const input = 'Fish &amp; Chips';
const expected = 'Fish & Chips';

expect(htmlUnescape(input)).toBe(expected);
});

test('should unescape double quotes correctly', () => {
const input = 'She said &quot;Hello&quot;';
const expected = 'She said "Hello"';

expect(htmlUnescape(input)).toBe(expected);
});

test('should unescape single quotes correctly', () => {
const input = 'It&#39;s a test';
const expected = "It's a test";

expect(htmlUnescape(input)).toBe(expected);
});

test('should unescape less than and greater than signs correctly', () => {
const input = '&lt;div&gt;Hello&lt;/div&gt;';
const expected = '<div>Hello</div>';

expect(htmlUnescape(input)).toBe(expected);
});

test('should handle mixed numeric and named entities correctly', () => {
const input = 'It&#39;s &quot;complicated&quot; &amp; messy';
const expected = `It's "complicated" & messy`;

expect(htmlUnescape(input)).toBe(expected);
});

test('should handle multiple consecutive escaped characters correctly', () => {
const input = '&lt;&lt;&gt;&gt;&quot;&quot;&amp;&amp;';
const expected = '<<>>""&&';

expect(htmlUnescape(input)).toBe(expected);
});
});

0 comments on commit 88d5207

Please sign in to comment.