From 88d52071d17eff196d3ac9193f61a0e1e72cb4a9 Mon Sep 17 00:00:00 2001 From: pnodet Date: Mon, 25 Nov 2024 19:33:06 +0100 Subject: [PATCH] chore: linter --- src/arrays/remove.ts | 2 +- src/currency/index.spec.ts | 22 +++++++++++++++------- src/exceptions/create-custom.ts | 12 ++++++------ src/is/is-equal.ts | 2 +- src/objects/clone.ts | 3 ++- src/regexp/email.ts | 1 - src/strings/escape.spec.ts | 12 ++++++++++++ 7 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/arrays/remove.ts b/src/arrays/remove.ts index 29f1ecb..b5b08f9 100644 --- a/src/arrays/remove.ts +++ b/src/arrays/remove.ts @@ -9,7 +9,7 @@ export const remove = (array: T[], element: T): T[] => { const array_ = [...array]; const idx = array_.indexOf(element); - if (idx < 0) { + if (idx === -1) { return []; } diff --git a/src/currency/index.spec.ts b/src/currency/index.spec.ts index f7fc1a5..76f61be 100644 --- a/src/currency/index.spec.ts +++ b/src/currency/index.spec.ts @@ -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 €'); }); }); diff --git a/src/exceptions/create-custom.ts b/src/exceptions/create-custom.ts index fbe2032..f859202 100644 --- a/src/exceptions/create-custom.ts +++ b/src/exceptions/create-custom.ts @@ -1,3 +1,4 @@ +/* eslint-disable max-classes-per-file */ import { generateId } from '../generate'; import { SafeJson } from '../safe-json'; import { httpStatus } from '../http-status'; @@ -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) { @@ -153,7 +153,7 @@ export class Exception extends Error { } addMeta(meta: { [key: string]: unknown }): this { - (this as Mutable).meta = Object.assign({}, this.meta, meta); + (this as Mutable).meta = { ...this.meta, ...meta }; return this; } diff --git a/src/is/is-equal.ts b/src/is/is-equal.ts index ef3ff5d..bfeb12f 100644 --- a/src/is/is-equal.ts +++ b/src/is/is-equal.ts @@ -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; diff --git a/src/objects/clone.ts b/src/objects/clone.ts index 387ec47..5ac5f2a 100644 --- a/src/objects/clone.ts +++ b/src/objects/clone.ts @@ -62,7 +62,8 @@ export function clone(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; diff --git a/src/regexp/email.ts b/src/regexp/email.ts index 7fddc9d..ada0704 100644 --- a/src/regexp/email.ts +++ b/src/regexp/email.ts @@ -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; diff --git a/src/strings/escape.spec.ts b/src/strings/escape.spec.ts index b44ace0..8340206 100644 --- a/src/strings/escape.spec.ts +++ b/src/strings/escape.spec.ts @@ -5,30 +5,35 @@ describe('htmlEscape', () => { test('should escape all relevant characters', () => { const input = `Hello & "World" <'test'>`; const expected = 'Hello & "World" <'test'>'; + expect(htmlEscape(input)).toBe(expected); }); test('should escape ampersands correctly', () => { const input = 'Fish & Chips'; const expected = 'Fish & Chips'; + expect(htmlEscape(input)).toBe(expected); }); test('should escape double quotes correctly', () => { const input = 'She said "Hello"'; const expected = 'She said "Hello"'; + expect(htmlEscape(input)).toBe(expected); }); test('should escape single quotes correctly', () => { const input = "It's a test"; const expected = 'It's a test'; + expect(htmlEscape(input)).toBe(expected); }); test('should escape less than and greater than signs correctly', () => { const input = '
Hello
'; const expected = '<div>Hello</div>'; + expect(htmlEscape(input)).toBe(expected); }); }); @@ -37,42 +42,49 @@ describe('htmlUnescape', () => { test('should unescape all relevant characters', () => { const input = 'Hello & "World" <'test'>'; const expected = `Hello & "World" <'test'>`; + expect(htmlUnescape(input)).toBe(expected); }); test('should unescape ampersands correctly', () => { const input = 'Fish & Chips'; const expected = 'Fish & Chips'; + expect(htmlUnescape(input)).toBe(expected); }); test('should unescape double quotes correctly', () => { const input = 'She said "Hello"'; const expected = 'She said "Hello"'; + expect(htmlUnescape(input)).toBe(expected); }); test('should unescape single quotes correctly', () => { const input = 'It'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 = '<div>Hello</div>'; const expected = '
Hello
'; + expect(htmlUnescape(input)).toBe(expected); }); test('should handle mixed numeric and named entities correctly', () => { const input = 'It's "complicated" & messy'; const expected = `It's "complicated" & messy`; + expect(htmlUnescape(input)).toBe(expected); }); test('should handle multiple consecutive escaped characters correctly', () => { const input = '<<>>""&&'; const expected = '<<>>""&&'; + expect(htmlUnescape(input)).toBe(expected); }); });