From 06eebd7e61a730745d4641446cbd056c15727233 Mon Sep 17 00:00:00 2001 From: SlyTed Date: Mon, 7 Oct 2024 20:43:59 +0200 Subject: [PATCH] feat(math): enhance typing --- CHANGELOG.md | 6 + package-lock.json | 4 +- package.json | 2 +- src/array/enumerate/enumerate.pipe.spec.ts | 2 +- src/helpers/cast/cast.helper.spec.ts | 10 +- .../type-guards/type-guards.helper.spec.ts | 4 +- src/math/convert/convert.pipe.spec.ts | 145 ++++++++++++++---- src/math/convert/convert.pipe.ts | 122 ++++----------- src/math/max-by/max-by.pipe.spec.ts | 15 +- src/math/max-by/max-by.pipe.ts | 6 + src/math/mean-by/mean-by.pipe.spec.ts | 15 +- src/math/mean-by/mean-by.pipe.ts | 6 + src/math/min-by/min-by.pipe.spec.ts | 15 +- src/math/min-by/min-by.pipe.ts | 6 + src/math/sum-by/sum-by.pipe.spec.ts | 13 +- src/math/sum-by/sum-by.pipe.ts | 6 + 16 files changed, 213 insertions(+), 164 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac427ea..d6f1b99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.2.2 - 2024-10-07 + +### Changed + +Enhance typing of 'transform()' methods + ## 0.2.1 - 2024-07-22 ### Changed diff --git a/package-lock.json b/package-lock.json index bfdce8c..d852024 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ngx-pipes-toolkit", - "version": "0.1.2", + "version": "0.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ngx-pipes-toolkit", - "version": "0.1.2", + "version": "0.2.1", "license": "MIT", "devDependencies": { "@analogjs/vite-plugin-angular": "^1.6.2", diff --git a/package.json b/package.json index 40e51b9..d6ae55f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "ngx-pipes-toolkit", "description": "Complementary pipes library for Angular", "author": "Perros Ted", - "version": "0.2.1", + "version": "0.2.2", "license": "MIT", "keywords": ["ng", "ngx", "angular", "pipes"], "repository": { diff --git a/src/array/enumerate/enumerate.pipe.spec.ts b/src/array/enumerate/enumerate.pipe.spec.ts index 8d303f4..b5efcb9 100644 --- a/src/array/enumerate/enumerate.pipe.spec.ts +++ b/src/array/enumerate/enumerate.pipe.spec.ts @@ -1,6 +1,6 @@ import { EnumeratePipe } from './enumerate.pipe'; -describe('ARRAY - Enumerate', () => { +suite('ARRAY - Enumerate', () => { let pipe: EnumeratePipe; beforeEach(() => { diff --git a/src/helpers/cast/cast.helper.spec.ts b/src/helpers/cast/cast.helper.spec.ts index 459d0cb..eb1d1bf 100644 --- a/src/helpers/cast/cast.helper.spec.ts +++ b/src/helpers/cast/cast.helper.spec.ts @@ -1,12 +1,10 @@ import { castArrayProperty, castToNumber } from './cast.helper'; -describe('HELPERS - Cast', () => { - describe('castArrayProperty()', () => { +suite('HELPERS - Cast', () => { + suite('castArrayProperty()', () => { test('Should return empty array if empty array provided', () => { const data: TestModel[] = []; - expect(castArrayProperty([] as TestModel[], 'property')).toEqual( - [], - ); + expect(castArrayProperty(data, 'property')).toEqual([]); }); test('Should return empty array if NaN numbers', () => { @@ -50,7 +48,7 @@ describe('HELPERS - Cast', () => { }); }); - describe('castToNumber()', () => { + suite('castToNumber()', () => { test('Should return NaN if NaN provided', () => { expect(castToNumber(Number.NaN)).toEqual(Number.NaN); }); diff --git a/src/helpers/type-guards/type-guards.helper.spec.ts b/src/helpers/type-guards/type-guards.helper.spec.ts index f622564..f331369 100644 --- a/src/helpers/type-guards/type-guards.helper.spec.ts +++ b/src/helpers/type-guards/type-guards.helper.spec.ts @@ -1,7 +1,7 @@ import { isNumberOrStringOrDate } from './type-guards.helper'; -describe('HELPERS - Type guards', () => { - describe('isNumberOrStringOrDate()', () => { +suite('HELPERS - Type guards', () => { + suite('isNumberOrStringOrDate()', () => { test('Should return false if nil', () => { expect(isNumberOrStringOrDate(undefined)).toEqual(false); expect(isNumberOrStringOrDate(null)).toEqual(false); diff --git a/src/math/convert/convert.pipe.spec.ts b/src/math/convert/convert.pipe.spec.ts index aa5585a..3b3f39b 100644 --- a/src/math/convert/convert.pipe.spec.ts +++ b/src/math/convert/convert.pipe.spec.ts @@ -1,127 +1,206 @@ import { ConvertPipe } from './convert.pipe'; -describe('MATH - Convert', () => { +suite('MATH - Convert', () => { let pipe: ConvertPipe; beforeEach(() => { pipe = new ConvertPipe(); }); - test('Should return null if nil value', () => { - expect(pipe.transform(undefined, 'A', 'A')).toBeNull(); - expect(pipe.transform(null, 'A', 'A')).toBeNull(); + test('Should return null angle if nil value', () => { + expect(pipe.transform(undefined, 'deg', 'deg')).toEqual(null); + expect(pipe.transform(null, 'deg', 'rad')).toEqual(null); }); - - test('Should return zero if zero value', () => { - expect(pipe.transform(0, 'deg', 'deg')).toEqual(0); - }); - - test('Should return correct angle if valid values', () => { + test('Should return correct angle if non-nil value', () => { expect(pipe.transform(10, 'deg', 'deg')).toEqual(10); expect(pipe.transform(10, 'deg', 'rad')).toBeCloseTo(0.174533); }); - test('Should return correct apparent power if valid values', () => { + test('Should return null apparent power if nil value', () => { + expect(pipe.transform(undefined, 'kVA', 'kVA')).toEqual(null); + expect(pipe.transform(null, 'kVA', 'VA')).toEqual(null); + }); + test('Should return correct apparent power if non-nil value', () => { expect(pipe.transform(10, 'kVA', 'kVA')).toEqual(10); expect(pipe.transform(10, 'kVA', 'VA')).toBeCloseTo(10_000); }); - test('Should return correct apparent power if valid values', () => { - expect(pipe.transform(10, 'km2', 'km2')).toEqual(10); - expect(pipe.transform(10, 'km2', 'm2')).toBeCloseTo(1e7); + test('Should return null area if nil value', () => { + expect(pipe.transform(undefined, 'm2', 'cm2')).toEqual(null); + expect(pipe.transform(null, 'm2', 'ha')).toEqual(null); + }); + test('Should return correct area if non-nil value', () => { + expect(pipe.transform(10, 'm2', 'cm2')).toEqual(100_000); + expect(pipe.transform(10, 'm2', 'ha')).toBeCloseTo(0.001); }); - test('Should return correct current if valid values', () => { + test('Should return null current if nil value', () => { + expect(pipe.transform(undefined, 'A', 'A')).toEqual(null); + expect(pipe.transform(null, 'kA', 'A')).toEqual(null); + }); + test('Should return correct current if non-nil value', () => { expect(pipe.transform(10, 'A', 'A')).toEqual(10); expect(pipe.transform(10, 'kA', 'A')).toBeCloseTo(10_000); }); - test('Should return correct distance if valid values', () => { + test('Should return null distance if nil value', () => { + expect(pipe.transform(undefined, 'km', 'km')).toEqual(null); + expect(pipe.transform(null, 'km', 'mi')).toEqual(null); + }); + test('Should return correct distance if non-nil value', () => { expect(pipe.transform(10, 'km', 'km')).toEqual(10); expect(pipe.transform(10, 'km', 'mi')).toBeCloseTo(6.21371); }); - test('Should return correct ditgital if valid values', () => { + test('Should return null ditgital if nil value', () => { + expect(pipe.transform(undefined, 'b', 'b')).toEqual(null); + expect(pipe.transform(null, 'b', 'B')).toEqual(null); + }); + test('Should return correct ditgital if non-nil value', () => { expect(pipe.transform(10, 'b', 'b')).toEqual(10); expect(pipe.transform(10, 'b', 'B')).toBeCloseTo(1.25); }); - test('Should return correct energy if valid values', () => { + test('Should return null energy if nil value', () => { + expect(pipe.transform(undefined, 'Wh', 'Wh')).toEqual(null); + expect(pipe.transform(null, 'Wh', 'J')).toEqual(null); + }); + test('Should return correct energy if non-nil value', () => { expect(pipe.transform(10, 'Wh', 'Wh')).toEqual(10); expect(pipe.transform(10, 'Wh', 'J')).toBeCloseTo(36_000); }); - test('Should return correct frequency if valid values', () => { + test('Should return null frequency if nil value', () => { + expect(pipe.transform(undefined, 'kHz', 'kHz')).toEqual(null); + expect(pipe.transform(null, 'kHz', 'Hz')).toEqual(null); + }); + test('Should return correct frequency if non-nil value', () => { expect(pipe.transform(10, 'kHz', 'kHz')).toEqual(10); expect(pipe.transform(10, 'kHz', 'Hz')).toBeCloseTo(10_000); }); - test('Should return correct illuminance if valid values', () => { + test('Should return null illuminance if nil value', () => { + expect(pipe.transform(null, 'ft-cd', 'ft-cd')).toEqual(null); + expect(pipe.transform(undefined, 'ft-cd', 'lx')).toEqual(null); + }); + test('Should return correct illuminance if non-nil value', () => { expect(pipe.transform(10, 'ft-cd', 'ft-cd')).toEqual(10); expect(pipe.transform(10, 'ft-cd', 'lx')).toBeCloseTo(107.639); }); - test('Should return correct mass if valid values', () => { + test('Should return null mass if nil value', () => { + expect(pipe.transform(undefined, 'kg', 'kg')).toEqual(null); + expect(pipe.transform(null, 'kg', 'lb')).toEqual(null); + }); + test('Should return correct mass if non-nil value', () => { expect(pipe.transform(10, 'kg', 'kg')).toEqual(10); expect(pipe.transform(10, 'kg', 'lb')).toBeCloseTo(22.0462); }); - test('Should return correct pace if valid values', () => { + test('Should return null pace if nil value', () => { + expect(pipe.transform(undefined, 's/m', 's/m')).toEqual(null); + expect(pipe.transform(null, 's/m', 'min/km')).toEqual(null); + }); + test('Should return correct pace if non-nil value', () => { expect(pipe.transform(10, 's/m', 's/m')).toEqual(10); expect(pipe.transform(10, 's/m', 'min/km')).toBeCloseTo(166.667); }); - test('Should return correct parts per if valid values', () => { + test('Should return null parts per if nil value', () => { + expect(pipe.transform(undefined, 'ppb', 'ppb')).toEqual(null); + expect(pipe.transform(null, 'ppb', 'ppt')).toEqual(null); + }); + test('Should return correct parts per if non-nil value', () => { expect(pipe.transform(10, 'ppb', 'ppb')).toEqual(10); expect(pipe.transform(10, 'ppb', 'ppt')).toBeCloseTo(10_000); }); - test('Should return correct power if valid values', () => { + test('Should return null power if nil value', () => { + expect(pipe.transform(undefined, 'kW', 'kW')).toEqual(null); + expect(pipe.transform(null, 'kW', 'W')).toEqual(null); + }); + test('Should return correct power if non-nil value', () => { expect(pipe.transform(10, 'kW', 'kW')).toEqual(10); expect(pipe.transform(10, 'kW', 'W')).toBeCloseTo(10_000); }); - test('Should return correct pressure if valid values', () => { + test('Should return null pressure if nil value', () => { + expect(pipe.transform(undefined, 'bar', 'bar')).toEqual(null); + expect(pipe.transform(null, 'bar', 'Pa')).toEqual(null); + }); + test('Should return correct pressure if non-nil value', () => { expect(pipe.transform(10, 'bar', 'bar')).toEqual(10); expect(pipe.transform(10, 'bar', 'Pa')).toBeCloseTo(1_000_000); }); - test('Should return correct reactive energy if valid values', () => { + test('Should return null reactive energy if nil value', () => { + expect(pipe.transform(undefined, 'kVARh', 'kVARh')).toEqual(null); + expect(pipe.transform(null, 'kVARh', 'VARh')).toEqual(null); + }); + test('Should return correct reactive energy if non-nil value', () => { expect(pipe.transform(10, 'kVARh', 'kVARh')).toEqual(10); expect(pipe.transform(10, 'kVARh', 'VARh')).toBeCloseTo(10_000); }); - test('Should return correct reactive power if valid values', () => { + test('Should return null reactive power if nil value', () => { + expect(pipe.transform(undefined, 'kVAR', 'kVAR')).toEqual(null); + expect(pipe.transform(null, 'kVAR', 'VAR')).toEqual(null); + }); + test('Should return correct reactive power if non-nil value', () => { expect(pipe.transform(10, 'kVAR', 'kVAR')).toEqual(10); expect(pipe.transform(10, 'kVAR', 'VAR')).toBeCloseTo(10_000); }); - test('Should return correct speed if valid values', () => { + test('Should return null speed if nil value', () => { + expect(pipe.transform(undefined, 'km/h', 'km/h')).toEqual(null); + expect(pipe.transform(null, 'km/h', 'ft/s')).toEqual(null); + }); + test('Should return correct speed if non-nil value', () => { expect(pipe.transform(10, 'km/h', 'km/h')).toEqual(10); expect(pipe.transform(10, 'km/h', 'ft/s')).toBeCloseTo(9.11344); }); - test('Should return correct temperature if valid values', () => { + test('Should return null temperature if nil value', () => { + expect(pipe.transform(undefined, 'C', 'C')).toEqual(null); + expect(pipe.transform(null, 'C', 'F')).toEqual(null); + }); + test('Should return correct temperature if non-nil value', () => { expect(pipe.transform(10, 'C', 'C')).toEqual(10); expect(pipe.transform(10, 'C', 'F')).toBeCloseTo(50); }); - test('Should return correct time if valid values', () => { + test('Should return null time if nil value', () => { + expect(pipe.transform(undefined, 'min', 'min')).toEqual(null); + expect(pipe.transform(null, 'min', 'h')).toEqual(null); + }); + test('Should return correct time if non-nil value', () => { expect(pipe.transform(10, 'min', 'min')).toEqual(10); expect(pipe.transform(10, 'min', 'h')).toBeCloseTo(0.1667); }); - test('Should return correct voltage if valid values', () => { + test('Should return null voltage if nil value', () => { + expect(pipe.transform(undefined, 'kV', 'kV')).toEqual(null); + expect(pipe.transform(null, 'kV', 'V')).toEqual(null); + }); + test('Should return correct voltage if non-nil value', () => { expect(pipe.transform(10, 'kV', 'kV')).toEqual(10); expect(pipe.transform(10, 'kV', 'V')).toBeCloseTo(10_000); }); - test('Should return correct volume if valid values', () => { + test('Should return null volume if nil value', () => { + expect(pipe.transform(undefined, 'l', 'l')).toEqual(null); + expect(pipe.transform(null, 'l', 'gal')).toEqual(null); + }); + test('Should return correct volume if non-nil value', () => { expect(pipe.transform(10, 'l', 'l')).toEqual(10); expect(pipe.transform(10, 'l', 'gal')).toBeCloseTo(2.64172); }); - test('Should return correct volume flow rate if valid values', () => { + test('Should return null volume flow rate if nil value', () => { + expect(pipe.transform(undefined, 'l/min', 'l/min')).toEqual(null); + expect(pipe.transform(null, 'l/min', 'gal/min')).toEqual(null); + }); + test('Should return correct volume flow rate if non-nil value', () => { expect(pipe.transform(10, 'l/min', 'l/min')).toEqual(10); expect(pipe.transform(10, 'l/min', 'gal/min')).toBeCloseTo(2.64172); }); diff --git a/src/math/convert/convert.pipe.ts b/src/math/convert/convert.pipe.ts index bf1ebdb..7ba4998 100644 --- a/src/math/convert/convert.pipe.ts +++ b/src/math/convert/convert.pipe.ts @@ -38,6 +38,8 @@ import convert, { }) @Injectable() export class ConvertPipe implements PipeTransform { + transform(value: null | undefined, currentUnit: Unit, finalUnit: Unit): null; + /** * Converts an angle value. * @@ -47,11 +49,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Angle, - finalUnit: Angle, - ): number | null; + transform(value: number, currentUnit: Angle, finalUnit: Angle): number; /** * Converts an apparent power value. @@ -63,10 +61,10 @@ export class ConvertPipe implements PipeTransform { * The result of the conversion, or `null` if the provided value is `nil`. */ transform( - value: number | null | undefined, + value: number, currentUnit: ApparentPower, finalUnit: ApparentPower, - ): number | null; + ): number; /** * Converts an area value. @@ -77,11 +75,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Area, - finalUnit: Area, - ): number | null; + transform(value: number, currentUnit: Area, finalUnit: Area): number; /** * Converts a current value. @@ -92,11 +86,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Current, - finalUnit: Current, - ): number | null; + transform(value: number, currentUnit: Current, finalUnit: Current): number; /** * Converts a distance value. @@ -107,11 +97,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Distance, - finalUnit: Distance, - ): number | null; + transform(value: number, currentUnit: Distance, finalUnit: Distance): number; /** * Converts a digital value. @@ -122,11 +108,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Ditgital, - finalUnit: Ditgital, - ): number | null; + transform(value: number, currentUnit: Ditgital, finalUnit: Ditgital): number; /** * Converts an energy value. @@ -137,11 +119,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Energy, - finalUnit: Energy, - ): number | null; + transform(value: number, currentUnit: Energy, finalUnit: Energy): number; /** * Converts a frequency value. @@ -153,10 +131,10 @@ export class ConvertPipe implements PipeTransform { * The result of the conversion, or `null` if the provided value is `nil`. */ transform( - value: number | null | undefined, + value: number, currentUnit: Frequency, finalUnit: Frequency, - ): number | null; + ): number; /** * Converts an illuminance value. @@ -168,10 +146,10 @@ export class ConvertPipe implements PipeTransform { * The result of the conversion, or `null` if the provided value is `nil`. */ transform( - value: number | null | undefined, + value: number, currentUnit: Illuminance, finalUnit: Illuminance, - ): number | null; + ): number; /** * Converts a mass value. @@ -182,11 +160,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Mass, - finalUnit: Mass, - ): number | null; + transform(value: number, currentUnit: Mass, finalUnit: Mass): number; /** * Converts a pace value. @@ -197,14 +171,10 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Pace, - finalUnit: Pace, - ): number | null; + transform(value: number, currentUnit: Pace, finalUnit: Pace): number; /** - * Converts a parts value. + * Converts a parts par value. * * @param value - The parts per value to convert. * @param currentUnit - The current parts per unit of the value. @@ -212,11 +182,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: PartsPer, - finalUnit: PartsPer, - ): number | null; + transform(value: number, currentUnit: PartsPer, finalUnit: PartsPer): number; /** * Converts a power value. @@ -227,11 +193,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Power, - finalUnit: Power, - ): number | null; + transform(value: number, currentUnit: Power, finalUnit: Power): number; /** * Converts a pressure value. @@ -242,11 +204,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Pressure, - finalUnit: Pressure, - ): number | null; + transform(value: number, currentUnit: Pressure, finalUnit: Pressure): number; /** * Converts a reactive energy value. @@ -258,10 +216,10 @@ export class ConvertPipe implements PipeTransform { * The result of the conversion, or `null` if the provided value is `nil`. */ transform( - value: number | null | undefined, + value: number, currentUnit: ReactiveEnergy, finalUnit: ReactiveEnergy, - ): number | null; + ): number; /** * Converts a reactive power value. @@ -273,10 +231,10 @@ export class ConvertPipe implements PipeTransform { * The result of the conversion, or `null` if the provided value is `nil`. */ transform( - value: number | null | undefined, + value: number, currentUnit: ReactivePower, finalUnit: ReactivePower, - ): number | null; + ): number; /** * Converts a speed value. @@ -287,11 +245,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Speed, - finalUnit: Speed, - ): number | null; + transform(value: number, currentUnit: Speed, finalUnit: Speed): number; /** * Converts a temperature value. @@ -303,10 +257,10 @@ export class ConvertPipe implements PipeTransform { * The result of the conversion, or `null` if the provided value is `nil`. */ transform( - value: number | null | undefined, + value: number, currentUnit: Temperature, finalUnit: Temperature, - ): number | null; + ): number; /** * Converts a time value. @@ -317,11 +271,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Time, - finalUnit: Time, - ): number | null; + transform(value: number, currentUnit: Time, finalUnit: Time): number; /** * Converts a voltage value. @@ -332,11 +282,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Voltage, - finalUnit: Voltage, - ): number | null; + transform(value: number, currentUnit: Voltage, finalUnit: Voltage): number; /** * Converts a volume value. @@ -347,11 +293,7 @@ export class ConvertPipe implements PipeTransform { * @returns * The result of the conversion, or `null` if the provided value is `nil`. */ - transform( - value: number | null | undefined, - currentUnit: Volume, - finalUnit: Volume, - ): number | null; + transform(value: number, currentUnit: Volume, finalUnit: Volume): number; /** * Converts a volume flow value. @@ -363,10 +305,10 @@ export class ConvertPipe implements PipeTransform { * The result of the conversion, or `null` if the provided value is `nil`. */ transform( - value: number | null | undefined, + value: number, currentUnit: VolumeFlowRate, finalUnit: VolumeFlowRate, - ): number | null; + ): number; /** * Converts a value with its current unit to The final unit. diff --git a/src/math/max-by/max-by.pipe.spec.ts b/src/math/max-by/max-by.pipe.spec.ts index a13ce29..8fa212c 100644 --- a/src/math/max-by/max-by.pipe.spec.ts +++ b/src/math/max-by/max-by.pipe.spec.ts @@ -3,7 +3,7 @@ import { MaxByPipe } from './max-by.pipe'; import { TEST_DATA } from 'test/test.data'; import type { TestModel } from 'test/test.model'; -describe('MATH - Max by', () => { +suite('MATH - Max by', () => { let pipe: MaxByPipe = new MaxByPipe(); beforeEach(() => { @@ -11,16 +11,15 @@ describe('MATH - Max by', () => { }); test('Should return null if nil value', () => { - expect( - pipe.transform(undefined as TestModel[] | undefined, 'numberOnly'), - ).toEqual(null); - expect(pipe.transform(null as TestModel[] | null, 'numberOnly')).toEqual( - null, - ); + expect(pipe.transform(undefined, 'numberOnly')).toEqual(null); + expect(pipe.transform(null, 'numberOnly')).toEqual(null); }); test('Should return null if empty array', () => { - expect(pipe.transform([] as TestModel[], 'numberOnly')).toEqual(null); + expect(pipe.transform([], 'numberOnly')).toEqual(null); + expect( + pipe.transform([] satisfies TestModel[] | null, 'numberOnly'), + ).toEqual(null); }); test('Should return null if non-empty array and non-number property', () => { diff --git a/src/math/max-by/max-by.pipe.ts b/src/math/max-by/max-by.pipe.ts index 92f16c8..42c8eed 100644 --- a/src/math/max-by/max-by.pipe.ts +++ b/src/math/max-by/max-by.pipe.ts @@ -15,6 +15,12 @@ import { castArrayProperty, castToNumber } from '../../helpers'; }) @Injectable() export class MaxByPipe implements PipeTransform { + transform(value: null | undefined, property: unknown): null; + transform( + value: T[], + property: K, + ): T; + /** * Returns the item with the largest value of an array of objects * based on one of their `number`, parsable `string` or `Date` property. diff --git a/src/math/mean-by/mean-by.pipe.spec.ts b/src/math/mean-by/mean-by.pipe.spec.ts index 92944f2..8a8e364 100644 --- a/src/math/mean-by/mean-by.pipe.spec.ts +++ b/src/math/mean-by/mean-by.pipe.spec.ts @@ -3,7 +3,7 @@ import { MeanByPipe } from './mean-by.pipe'; import { TEST_DATA } from 'test/test.data'; import type { TestModel } from 'test/test.model'; -describe('MATH - Mean by', () => { +suite('MATH - Mean by', () => { let pipe: MeanByPipe; beforeEach(() => { @@ -11,16 +11,15 @@ describe('MATH - Mean by', () => { }); test('Should return null if nil value', () => { - expect( - pipe.transform(undefined as TestModel[] | undefined, 'numberOnly'), - ).toEqual(null); - expect(pipe.transform(null as TestModel[] | null, 'numberOnly')).toEqual( - null, - ); + expect(pipe.transform(undefined, 'numberOnly')).toEqual(null); + expect(pipe.transform(null, 'numberOnly')).toEqual(null); }); test('Should return null if empty array', () => { - expect(pipe.transform([] as TestModel[], 'numberOnly')).toEqual(null); + expect(pipe.transform([], 'numberOnly')).toEqual(null); + expect( + pipe.transform([] satisfies TestModel[] | null, 'numberOnly'), + ).toEqual(null); }); test('Should return null if non-empty array and non-number property', () => { diff --git a/src/math/mean-by/mean-by.pipe.ts b/src/math/mean-by/mean-by.pipe.ts index 701d81c..d5158bf 100644 --- a/src/math/mean-by/mean-by.pipe.ts +++ b/src/math/mean-by/mean-by.pipe.ts @@ -16,6 +16,12 @@ import { castArrayProperty } from '../../helpers'; }) @Injectable() export class MeanByPipe implements PipeTransform { + transform(value: null | undefined, property: unknown): null; + transform( + value: T[], + property: K, + ): T; + /** * Calculates the mean of an array of objects * based on one of their `number`, parsable `string` or `Date` property. diff --git a/src/math/min-by/min-by.pipe.spec.ts b/src/math/min-by/min-by.pipe.spec.ts index 8fea41a..ebb51a0 100644 --- a/src/math/min-by/min-by.pipe.spec.ts +++ b/src/math/min-by/min-by.pipe.spec.ts @@ -3,7 +3,7 @@ import { MinByPipe } from './min-by.pipe'; import { TEST_DATA } from 'test/test.data'; import type { TestModel } from 'test/test.model'; -describe('MATH - Min by', () => { +suite('MATH - Min by', () => { let pipe: MinByPipe; beforeEach(() => { @@ -11,16 +11,15 @@ describe('MATH - Min by', () => { }); test('Should return null if nil value', () => { - expect( - pipe.transform(undefined as TestModel[] | undefined, 'numberOnly'), - ).toEqual(null); - expect(pipe.transform(null as TestModel[] | null, 'numberOnly')).toEqual( - null, - ); + expect(pipe.transform(undefined, 'numberOnly')).toEqual(null); + expect(pipe.transform(null, 'numberOnly')).toEqual(null); }); test('Should return null if empty array', () => { - expect(pipe.transform([] as TestModel[], 'numberOnly')).toEqual(null); + expect(pipe.transform([], 'numberOnly')).toEqual(null); + expect( + pipe.transform([] satisfies TestModel[] | null, 'numberOnly'), + ).toEqual(null); }); test('Should return null if non-empty array and non-number property', () => { diff --git a/src/math/min-by/min-by.pipe.ts b/src/math/min-by/min-by.pipe.ts index 53c205e..19aac4b 100644 --- a/src/math/min-by/min-by.pipe.ts +++ b/src/math/min-by/min-by.pipe.ts @@ -15,6 +15,12 @@ import { castArrayProperty, castToNumber } from '../../helpers'; }) @Injectable() export class MinByPipe implements PipeTransform { + transform(value: null | undefined, property: unknown): null; + transform( + value: T[], + property: K, + ): T; + /** * Returns the item with the smallest value of an array of objects * based on one of their `number`, parsable `string` or `Date` property. diff --git a/src/math/sum-by/sum-by.pipe.spec.ts b/src/math/sum-by/sum-by.pipe.spec.ts index 6eb6d9a..16cb7ea 100644 --- a/src/math/sum-by/sum-by.pipe.spec.ts +++ b/src/math/sum-by/sum-by.pipe.spec.ts @@ -3,7 +3,7 @@ import { SumByPipe } from './sum-by.pipe'; import { TEST_DATA } from 'test/test.data'; import type { TestModel } from 'test/test.model'; -describe('MATH - Sum by', () => { +suite('MATH - Sum by', () => { let pipe: SumByPipe; beforeEach(() => { @@ -11,12 +11,15 @@ describe('MATH - Sum by', () => { }); test('Should return null if nil value', () => { + expect(pipe.transform(undefined, 'numberOnly')).toEqual(null); + expect(pipe.transform(null, 'numberOnly')).toEqual(null); + }); + + test('Should return null if empty array', () => { + expect(pipe.transform([], 'numberOnly')).toEqual(null); expect( - pipe.transform(undefined as TestModel[] | undefined, 'numberOnly'), + pipe.transform([] satisfies TestModel[] | null, 'numberOnly'), ).toEqual(null); - expect(pipe.transform(null as TestModel[] | null, 'numberOnly')).toEqual( - null, - ); }); test('Should return null if empty array', () => { diff --git a/src/math/sum-by/sum-by.pipe.ts b/src/math/sum-by/sum-by.pipe.ts index 2dcfe37..044da6b 100644 --- a/src/math/sum-by/sum-by.pipe.ts +++ b/src/math/sum-by/sum-by.pipe.ts @@ -16,6 +16,12 @@ import { castArrayProperty } from '../../helpers'; }) @Injectable() export class SumByPipe implements PipeTransform { + transform(value: null | undefined, property: unknown): null; + transform( + value: T[], + property: K, + ): T; + /** * Calculates the sum of an array of objects * based on one of their `number`, parsable `string` or `Date` property.