Skip to content

Commit

Permalink
feat(math): enhance typing
Browse files Browse the repository at this point in the history
  • Loading branch information
SlyTed committed Oct 7, 2024
1 parent f8afd6d commit 06eebd7
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 164 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.2.2 - 2024-10-07

### Changed

Enhance typing of 'transform()' methods

## 0.2.1 - 2024-07-22

### Changed
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/array/enumerate/enumerate.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EnumeratePipe } from './enumerate.pipe';

describe('ARRAY - Enumerate', () => {
suite('ARRAY - Enumerate', () => {
let pipe: EnumeratePipe;

beforeEach(() => {
Expand Down
10 changes: 4 additions & 6 deletions src/helpers/cast/cast.helper.spec.ts
Original file line number Diff line number Diff line change
@@ -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<number>[] = [];
expect(castArrayProperty([] as TestModel<number>[], 'property')).toEqual(
[],
);
expect(castArrayProperty(data, 'property')).toEqual([]);
});

test('Should return empty array if NaN numbers', () => {
Expand Down Expand Up @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/type-guards/type-guards.helper.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
145 changes: 112 additions & 33 deletions src/math/convert/convert.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
Expand Down
Loading

0 comments on commit 06eebd7

Please sign in to comment.