From 5276a6fec24cf9f3fe6cbc1aee8d0084b36be96a Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Sun, 26 Jul 2020 23:48:13 +0530 Subject: [PATCH 01/12] Changes in npm config --- package.json | 4 +++- packages/pipes/package.json | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 5f22e0b..8a47d73 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nglrx/pipes", - "version": "0.3.1", + "version": "0.4.0", "author": "Kapil Neurgaonkar", "description": "A collection of pipes for Angular apps.", "license": "MIT", @@ -12,10 +12,12 @@ "bugs": "https://github.com/nglrx/pipes/issues", "keywords": [ "ng", + "ngx", "angular", "pipes", "angular pipes", "library", + "angular library", "filters", "angular filters" ], diff --git a/packages/pipes/package.json b/packages/pipes/package.json index 402d973..0e83bac 100644 --- a/packages/pipes/package.json +++ b/packages/pipes/package.json @@ -1,6 +1,6 @@ { "name": "@nglrx/pipes", - "version": "0.3.1", + "version": "0.4.0", "author": "Kapil Neurgaonkar", "description": "A collection of pipes for Angular apps.", "license": "MIT", @@ -12,10 +12,12 @@ "bugs": "https://github.com/nglrx/pipes/issues", "keywords": [ "ng", + "ngx", "angular", "pipes", "angular pipes", "library", + "angular library", "filters", "angular filters" ], From ca90ef6a388d68911c70b64fea6918487826a0eb Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Sun, 26 Jul 2020 23:48:32 +0530 Subject: [PATCH 02/12] Corrections in Readme --- README.md | 2 +- packages/pipes/README.md | 2 +- packages/pipes/src/lib/string/README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7007f00..77e2e17 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ Range of position is from 0 (default) to n-1, where n is length of the string. ### concat -Concatenates one or more string(s) to current string at the end.\ +Concatenates one or more string(s) to current string at the end. Usage: `string | concat:string1[:string2]...` diff --git a/packages/pipes/README.md b/packages/pipes/README.md index 7007f00..77e2e17 100644 --- a/packages/pipes/README.md +++ b/packages/pipes/README.md @@ -132,7 +132,7 @@ Range of position is from 0 (default) to n-1, where n is length of the string. ### concat -Concatenates one or more string(s) to current string at the end.\ +Concatenates one or more string(s) to current string at the end. Usage: `string | concat:string1[:string2]...` diff --git a/packages/pipes/src/lib/string/README.md b/packages/pipes/src/lib/string/README.md index 9b15085..b319d67 100644 --- a/packages/pipes/src/lib/string/README.md +++ b/packages/pipes/src/lib/string/README.md @@ -47,7 +47,7 @@ Range of position is from 0 (default) to n-1, where n is length of the string. ### concat -Concatenates one or more string(s) to current string at the end.\ +Concatenates one or more string(s) to current string at the end. Usage: `string | concat:string1[:string2]...` From da457604e85f84464f48ee34dad82942e89515b3 Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Mon, 27 Jul 2020 00:43:52 +0530 Subject: [PATCH 03/12] Refactored code --- packages/pipes/src/lib/number/avg/avg.pipe.ts | 2 +- packages/pipes/src/lib/number/sum/sum.pipe.ts | 2 +- packages/pipes/src/lib/utils/number-utils.ts | 10 ++++++++++ packages/pipes/src/lib/utils/type-utils.ts | 10 ---------- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/pipes/src/lib/number/avg/avg.pipe.ts b/packages/pipes/src/lib/number/avg/avg.pipe.ts index 6e6976c..dfcfb7a 100644 --- a/packages/pipes/src/lib/number/avg/avg.pipe.ts +++ b/packages/pipes/src/lib/number/avg/avg.pipe.ts @@ -13,7 +13,7 @@ export class AvgPipe implements PipeTransform { return null; } else if (TypeUtils.isEmpty(values)) { return 0; - } else if (!TypeUtils.areFinite(values)) { + } else if (!NumberUtils.areFinite(values)) { return NaN; } const sum = NumberUtils.sum(values); diff --git a/packages/pipes/src/lib/number/sum/sum.pipe.ts b/packages/pipes/src/lib/number/sum/sum.pipe.ts index 456ec2a..f16c938 100644 --- a/packages/pipes/src/lib/number/sum/sum.pipe.ts +++ b/packages/pipes/src/lib/number/sum/sum.pipe.ts @@ -13,7 +13,7 @@ export class SumPipe implements PipeTransform { return null; } else if (TypeUtils.isEmpty(values)) { return 0; - } else if (!TypeUtils.areFinite(values)) { + } else if (!NumberUtils.areFinite(values)) { return NaN; } return NumberUtils.sum(values); diff --git a/packages/pipes/src/lib/utils/number-utils.ts b/packages/pipes/src/lib/utils/number-utils.ts index 22f19d9..5070b01 100644 --- a/packages/pipes/src/lib/utils/number-utils.ts +++ b/packages/pipes/src/lib/utils/number-utils.ts @@ -10,4 +10,14 @@ export class NumberUtils { return sum; } + /** + * Checks if all numbers in a given array are finite + * @param values array of numbers + * @returns true if all numbers are finite, else false + */ + static areFinite(values: number[]) { + const areFinite = values.every(value => isFinite(value)); + return areFinite; + } + } diff --git a/packages/pipes/src/lib/utils/type-utils.ts b/packages/pipes/src/lib/utils/type-utils.ts index 956e9d9..3b58721 100644 --- a/packages/pipes/src/lib/utils/type-utils.ts +++ b/packages/pipes/src/lib/utils/type-utils.ts @@ -33,14 +33,4 @@ export class TypeUtils { return typeof value === 'undefined' || value === null; } - /** - * Checks if all numbers in a given array are finite - * @param values array of numbers - * @returns true if all numbers are finite, else false - */ - static areFinite(values: number[]) { - const areFinite = values.every(value => isFinite(value)); - return areFinite; - } - } From 8b7aafe9ce70f71de5c0c31039fc4bfa0728a7e1 Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Wed, 29 Jul 2020 00:16:41 +0530 Subject: [PATCH 04/12] enhancement Created type-of pipe --- README.md | 17 +++++ packages/pipes/README.md | 17 +++++ packages/pipes/src/lib/generic/README.md | 17 +++++ .../lib/generic/nglrx-generic-pipes.module.ts | 4 +- .../lib/generic/type-of/type-of.pipe.spec.ts | 69 +++++++++++++++++++ .../src/lib/generic/type-of/type-of.pipe.ts | 12 ++++ packages/pipes/src/lib/index.ts | 1 + 7 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 packages/pipes/src/lib/generic/type-of/type-of.pipe.spec.ts create mode 100644 packages/pipes/src/lib/generic/type-of/type-of.pipe.ts diff --git a/README.md b/README.md index 77e2e17..45c7115 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ export class YourComponent { - [Generic Pipes](#generic-pipes) - [length](#length) - [reverse](#reverse) + - [typeOf](#typeof) ## String Pipes @@ -394,5 +395,21 @@ Usage: `value | reverse` ``` + +### typeOf + +Returns the type of given value.\ +Returns the name of the type in string. All types are supported. + +Usage: `value | typeOf` + +```html +{{ 'This is a test string!' | typeOf }} + + +{{ { foo: 'bar' } | typeOf }} + +``` + \ For more information on pipes, refer to [Angular - pipes](https://angular.io/guide/pipes) documentation. diff --git a/packages/pipes/README.md b/packages/pipes/README.md index 77e2e17..45c7115 100644 --- a/packages/pipes/README.md +++ b/packages/pipes/README.md @@ -97,6 +97,7 @@ export class YourComponent { - [Generic Pipes](#generic-pipes) - [length](#length) - [reverse](#reverse) + - [typeOf](#typeof) ## String Pipes @@ -394,5 +395,21 @@ Usage: `value | reverse` ``` + +### typeOf + +Returns the type of given value.\ +Returns the name of the type in string. All types are supported. + +Usage: `value | typeOf` + +```html +{{ 'This is a test string!' | typeOf }} + + +{{ { foo: 'bar' } | typeOf }} + +``` + \ For more information on pipes, refer to [Angular - pipes](https://angular.io/guide/pipes) documentation. diff --git a/packages/pipes/src/lib/generic/README.md b/packages/pipes/src/lib/generic/README.md index a863b87..ccfdc40 100644 --- a/packages/pipes/src/lib/generic/README.md +++ b/packages/pipes/src/lib/generic/README.md @@ -4,6 +4,7 @@ A collection of pipes exported by `NglrxGenericPipesModule`. - [length](#length) - [reverse](#reverse) + - [typeOf](#typeof) ### length @@ -38,3 +39,19 @@ Usage: `value | reverse` {{ ['a', 'b', 'c', 'd', 'e'] | reverse }} ``` + + +### typeOf + +Returns the type of given value.\ +Returns the name of the type in string. All types are supported. + +Usage: `value | typeOf` + +```html +{{ 'This is a test string!' | typeOf }} + + +{{ { foo: 'bar' } | typeOf }} + +``` diff --git a/packages/pipes/src/lib/generic/nglrx-generic-pipes.module.ts b/packages/pipes/src/lib/generic/nglrx-generic-pipes.module.ts index d8ee804..73bbedb 100644 --- a/packages/pipes/src/lib/generic/nglrx-generic-pipes.module.ts +++ b/packages/pipes/src/lib/generic/nglrx-generic-pipes.module.ts @@ -2,10 +2,12 @@ import { NgModule } from '@angular/core'; import { ReversePipe } from './reverse/reverse.pipe'; import { LengthPipe } from './length/length.pipe'; +import { TypeOfPipe } from './type-of/type-of.pipe'; const GENERIC_PIPES = [ ReversePipe, - LengthPipe + LengthPipe, + TypeOfPipe ]; @NgModule({ diff --git a/packages/pipes/src/lib/generic/type-of/type-of.pipe.spec.ts b/packages/pipes/src/lib/generic/type-of/type-of.pipe.spec.ts new file mode 100644 index 0000000..ce7444d --- /dev/null +++ b/packages/pipes/src/lib/generic/type-of/type-of.pipe.spec.ts @@ -0,0 +1,69 @@ +import { TypeOfPipe } from './type-of.pipe'; + +describe('TypeOfPipe', () => { + let pipe: TypeOfPipe; + + const str = 'This is a test string!'; + const typeOfStr = 'string'; + const num = 22; + const nan = NaN; + const infinity = Infinity; + const typeOfNum = 'number'; + const bool = true; + const typeOfBool = 'boolean'; + const obj = { foo: 'bar' }; + const arr = ['a', 'b', 'c', 'd', 'e']; + const typeOfObj = 'object'; + const date = new Date(); + function func() {} + const typeOfFunction = 'function'; + const typeOfUndefined = 'undefined'; + + beforeEach(() => { + pipe = new TypeOfPipe(); + }); + + it('should create an instance', () => { + expect(pipe).toBeTruthy(); + }); + + it(`should find type of a string`, () => { + expect(pipe.transform(str)).toEqual(typeOfStr); + }); + + it(`should find type of a number`, () => { + expect(pipe.transform(num)).toEqual(typeOfNum); + expect(pipe.transform(nan)).toEqual(typeOfNum); + expect(pipe.transform(infinity)).toEqual(typeOfNum); + }); + + it(`should find type of a boolean`, () => { + expect(pipe.transform(bool)).toEqual(typeOfBool); + }); + + it(`should find type of a arr`, () => { + expect(pipe.transform(arr)).toEqual(typeOfObj); + }); + + it(`should find type of an object`, () => { + expect(pipe.transform(obj)).toEqual(typeOfObj); + }); + + it(`should find type of a Date`, () => { + expect(pipe.transform(date)).toEqual(typeOfObj); + }); + + it(`should find type of undefined`, () => { + expect(pipe.transform(null)).toEqual(typeOfObj); + expect(pipe.transform(undefined)).toEqual('undefined'); + }); + + it(`should find type of function`, () => { + expect(pipe.transform(func)).toEqual(typeOfFunction); + }); + + it(`should find type of undefined`, () => { + expect(pipe.transform(undefined)).toEqual(typeOfUndefined); + }); + +}); diff --git a/packages/pipes/src/lib/generic/type-of/type-of.pipe.ts b/packages/pipes/src/lib/generic/type-of/type-of.pipe.ts new file mode 100644 index 0000000..ec7687c --- /dev/null +++ b/packages/pipes/src/lib/generic/type-of/type-of.pipe.ts @@ -0,0 +1,12 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'typeOf' +}) +export class TypeOfPipe implements PipeTransform { + + transform(value: any): string { + return typeof value; + } + +} diff --git a/packages/pipes/src/lib/index.ts b/packages/pipes/src/lib/index.ts index 8cf11d0..367a042 100644 --- a/packages/pipes/src/lib/index.ts +++ b/packages/pipes/src/lib/index.ts @@ -6,6 +6,7 @@ export { NglrxPipesModule } from './nglrx-pipes.module'; export { NglrxGenericPipesModule } from './generic/nglrx-generic-pipes.module'; export { LengthPipe } from './generic/length/length.pipe'; export { ReversePipe } from './generic/reverse/reverse.pipe'; +export { TypeOfPipe } from './generic/type-of/type-of.pipe'; export { NglrxStringPipesModule } from './string/nglrx-string-pipes.module'; export { CamelCasePipe } from './string/camel-case/camel-case.pipe'; From c42b3feb09bcfb1be8a00ace3bdc2bacc2ef2d61 Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Wed, 29 Jul 2020 01:12:20 +0530 Subject: [PATCH 05/12] enhancement Created pipe for square root --- README.md | 13 +++++ packages/pipes/README.md | 13 +++++ packages/pipes/src/lib/index.ts | 1 + packages/pipes/src/lib/number/README.md | 13 +++++ .../lib/number/nglrx-number-pipes.module.ts | 2 + .../src/lib/number/sqrt/sqrt.pipe.spec.ts | 51 +++++++++++++++++++ .../pipes/src/lib/number/sqrt/sqrt.pipe.ts | 12 +++++ 7 files changed, 105 insertions(+) create mode 100644 packages/pipes/src/lib/number/sqrt/sqrt.pipe.spec.ts create mode 100644 packages/pipes/src/lib/number/sqrt/sqrt.pipe.ts diff --git a/README.md b/README.md index 45c7115..c113b40 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ export class YourComponent { - [avg](#avg) - [max](#max) - [min](#min) + - [sqrt](#sqrt) - [sum](#sum) - [Generic Pipes](#generic-pipes) - [length](#length) @@ -346,6 +347,18 @@ Usage: `array | min` ``` +### sqrt + +Returns the square root of given number. + +Usage: `number | sqrt` + +```html +{{ 625 | abs }} + +``` + + ### sum Returns the sum of all numbers in a given array. diff --git a/packages/pipes/README.md b/packages/pipes/README.md index 45c7115..c113b40 100644 --- a/packages/pipes/README.md +++ b/packages/pipes/README.md @@ -93,6 +93,7 @@ export class YourComponent { - [avg](#avg) - [max](#max) - [min](#min) + - [sqrt](#sqrt) - [sum](#sum) - [Generic Pipes](#generic-pipes) - [length](#length) @@ -346,6 +347,18 @@ Usage: `array | min` ``` +### sqrt + +Returns the square root of given number. + +Usage: `number | sqrt` + +```html +{{ 625 | abs }} + +``` + + ### sum Returns the sum of all numbers in a given array. diff --git a/packages/pipes/src/lib/index.ts b/packages/pipes/src/lib/index.ts index 367a042..0c940bb 100644 --- a/packages/pipes/src/lib/index.ts +++ b/packages/pipes/src/lib/index.ts @@ -30,4 +30,5 @@ export { AbsPipe } from './number/abs/abs.pipe'; export { AvgPipe } from './number/avg/avg.pipe'; export { MaxPipe } from './number/max/max.pipe'; export { MinPipe } from './number/min/min.pipe'; +export { SqrtPipe } from './number/sqrt/sqrt.pipe'; export { SumPipe } from './number/sum/sum.pipe'; diff --git a/packages/pipes/src/lib/number/README.md b/packages/pipes/src/lib/number/README.md index e429915..ccef4f4 100644 --- a/packages/pipes/src/lib/number/README.md +++ b/packages/pipes/src/lib/number/README.md @@ -6,6 +6,7 @@ A collection of pipes exported by `NglrxNumberPipesModule`. - [avg](#avg) - [max](#max) - [min](#min) + - [sqrt](#sqrt) - [sum](#sum) @@ -57,6 +58,18 @@ Usage: `array | min` ``` +### sqrt + +Returns the square root of given number. + +Usage: `number | sqrt` + +```html +{{ 625 | abs }} + +``` + + ### sum Returns the sum of all numbers in a given array. diff --git a/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts b/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts index 2e9763b..980530b 100644 --- a/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts +++ b/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts @@ -4,6 +4,7 @@ import { AbsPipe } from './abs/abs.pipe'; import { AvgPipe } from './avg/avg.pipe'; import { MaxPipe } from './max/max.pipe'; import { MinPipe } from './min/min.pipe'; +import { SqrtPipe } from './sqrt/sqrt.pipe'; import { SumPipe } from './sum/sum.pipe'; const NUMBER_PIPES = [ @@ -11,6 +12,7 @@ const NUMBER_PIPES = [ AvgPipe, MaxPipe, MinPipe, + SqrtPipe, SumPipe, ]; diff --git a/packages/pipes/src/lib/number/sqrt/sqrt.pipe.spec.ts b/packages/pipes/src/lib/number/sqrt/sqrt.pipe.spec.ts new file mode 100644 index 0000000..bf37e43 --- /dev/null +++ b/packages/pipes/src/lib/number/sqrt/sqrt.pipe.spec.ts @@ -0,0 +1,51 @@ +import { SqrtPipe } from './sqrt.pipe'; + +describe('SqrtPipe', () => { + let pipe: SqrtPipe; + + const positiveNum = 625; + const sqrtOfPositiveNum = 25; + const negativeNum = -25; + const decimalNum = 243.36; + const sqrtOfDecimalNum = 15.6; + const zero = 0; + const infinity = Infinity; + const nan = NaN; + + beforeEach(() => { + pipe = new SqrtPipe(); + }); + + it('should create an instance', () => { + expect(pipe).toBeTruthy(); + }); + + it(`should find square root of positive number`, () => { + expect(pipe.transform(positiveNum)).toEqual(sqrtOfPositiveNum); + }); + + it(`should return NaN for square root of negative number`, () => { + expect(pipe.transform(negativeNum)).toEqual(nan); + }); + + it(`should find square root of decimal number`, () => { + expect(pipe.transform(decimalNum)).toEqual(sqrtOfDecimalNum); + }); + + it(`should return zero for square root value of zero`, () => { + expect(pipe.transform(zero)).toEqual(zero); + }); + + it('should return NaN for square root value of NaN', () => { + expect(pipe.transform(nan)).toEqual(nan); + }); + + it('should return Infinity for square root value of Infinity', () => { + expect(pipe.transform(infinity)).toEqual(infinity); + }); + + it(`should be null safe`, () => { + expect(pipe.transform(null)).toEqual(zero); + }); + +}); diff --git a/packages/pipes/src/lib/number/sqrt/sqrt.pipe.ts b/packages/pipes/src/lib/number/sqrt/sqrt.pipe.ts new file mode 100644 index 0000000..26caa1d --- /dev/null +++ b/packages/pipes/src/lib/number/sqrt/sqrt.pipe.ts @@ -0,0 +1,12 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'sqrt' +}) +export class SqrtPipe implements PipeTransform { + + transform(value: number): number { + return Math.sqrt(value); + } + +} From 6a92e53a71dbd6b27246daca313db5cf5104029c Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Wed, 29 Jul 2020 01:33:42 +0530 Subject: [PATCH 06/12] Upgraded npm dependencies --- package-lock.json | 76 +++++++++++------------------------------------ package.json | 6 ++-- 2 files changed, 20 insertions(+), 62 deletions(-) diff --git a/package-lock.json b/package-lock.json index 139b023..a5fc347 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@nglrx/pipes", - "version": "0.3.0", + "version": "0.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1709,16 +1709,6 @@ "regenerator-runtime": "^0.13.4" } }, - "@babel/runtime-corejs3": { - "version": "7.10.4", - "resolved": "https://registry.npmjs.org/@babel/runtime-corejs3/-/runtime-corejs3-7.10.4.tgz", - "integrity": "sha512-BFlgP2SoLO9HJX9WBwN67gHWMBhDX/eDz64Jajd6mR/UAUzqrNMm99d4qHnVaKscAElZoFiPv+JpR/Siud5lXw==", - "dev": true, - "requires": { - "core-js-pure": "^3.0.0", - "regenerator-runtime": "^0.13.4" - } - }, "@babel/template": { "version": "7.8.6", "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.8.6.tgz", @@ -2106,9 +2096,9 @@ "dev": true }, "@types/node": { - "version": "14.0.24", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.24.tgz", - "integrity": "sha512-btt/oNOiDWcSuI721MdL8VQGnjsKjlTMdrKyTcLCKeQp/n4AAMFJ961wMbp+09y8WuGPClDEv07RIItdXKIXAA==", + "version": "14.0.26", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.26.tgz", + "integrity": "sha512-W+fpe5s91FBGE0pEa0lnqGLL4USgpLgs4nokw16SrBBco/gQxuua7KnArSEOd5iaMqbbSHV10vUDkJYJJqpXKA==", "dev": true }, "@types/normalize-package-data": { @@ -4211,12 +4201,6 @@ } } }, - "core-js-pure": { - "version": "3.6.5", - "resolved": "https://registry.npmjs.org/core-js-pure/-/core-js-pure-3.6.5.tgz", - "integrity": "sha512-lacdXOimsiD0QyNf9BC/mxivNJ/ybBGJXQFKzRekp1WTHoVUWsUHEn+2T8GJAzzIhyOuXA+gOxCVN3l+5PLPUA==", - "dev": true - }, "core-util-is": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", @@ -5169,9 +5153,9 @@ "dev": true }, "ws": { - "version": "7.3.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.0.tgz", - "integrity": "sha512-iFtXzngZVXPGgpTlP1rBqsUK82p9tKqsWRPg5L56egiljujJT3vGAYnHANvFxBieXrTFavhzhxW52jnaWV+w2w==", + "version": "7.3.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.1.tgz", + "integrity": "sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==", "dev": true } } @@ -7471,9 +7455,9 @@ } }, "jasmine-core": { - "version": "3.5.0", - "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-3.5.0.tgz", - "integrity": "sha512-nCeAiw37MIMA9w9IXso7bRaLl+c/ef3wnxsoSAlYrzS+Ot0zTG6nU8G/cIfGkqpkjX2wNaIW9RFG0TwIFnG6bA==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-3.6.0.tgz", + "integrity": "sha512-8uQYa7zJN8hq9z+g8z1bqCfdC8eoDAeVnM5sfqs7KHv9/ifoJ500m018fpFc7RDaO6SWCLCXwo/wPSNcdYTgcw==", "dev": true }, "jasmine-spec-reporter": { @@ -7631,9 +7615,9 @@ } }, "karma": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/karma/-/karma-5.1.0.tgz", - "integrity": "sha512-I3aPbkuIbwuBo6wSog97P5WnnhCgUTsWTu/bEw1vZVQFbXmKO3PK+cfFhZioOgVtJAuQxoyauGNjnwXNHMCxbw==", + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/karma/-/karma-5.1.1.tgz", + "integrity": "sha512-xAlOr5PMqUbiKXSv5PCniHWV3aiwj6wIZ0gUVcwpTCPVQm/qH2WAMFWxtnpM6KJqhkRWrIpovR4Rb0rn8GtJzQ==", "dev": true, "requires": { "body-parser": "^1.19.0", @@ -7704,15 +7688,6 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, - "decamelize": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-3.2.0.tgz", - "integrity": "sha512-4TgkVUsmmu7oCSyGBm5FvfMoACuoh9EOidm7V5/J2X2djAwwt57qb3F2KMP2ITqODTCSwb+YRV+0Zqrv18k/hw==", - "dev": true, - "requires": { - "xregexp": "^4.2.4" - } - }, "find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", @@ -7806,13 +7781,13 @@ } }, "yargs": { - "version": "15.4.0", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.0.tgz", - "integrity": "sha512-D3fRFnZwLWp8jVAAhPZBsmeIHY8tTsb8ItV9KaAaopmC6wde2u6Yw29JBIZHXw14kgkRnYmDgmQU4FVMDlIsWw==", + "version": "15.4.1", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.1.tgz", + "integrity": "sha512-aePbxDmcYW++PaqBsJ+HYUFwCdv4LVvdnhBy78E57PIor8/OVvhMrADFFEDh8DHDFRv/O9i3lPhsENjO7QX0+A==", "dev": true, "requires": { "cliui": "^6.0.0", - "decamelize": "^3.2.0", + "decamelize": "^1.2.0", "find-up": "^4.1.0", "get-caller-file": "^2.0.1", "require-directory": "^2.1.1", @@ -7832,14 +7807,6 @@ "requires": { "camelcase": "^5.0.0", "decamelize": "^1.2.0" - }, - "dependencies": { - "decamelize": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", - "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=", - "dev": true - } } } } @@ -14978,15 +14945,6 @@ "integrity": "sha1-wodrBhaKrcQOV9l+gRkayPQ5iz4=", "dev": true }, - "xregexp": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/xregexp/-/xregexp-4.3.0.tgz", - "integrity": "sha512-7jXDIFXh5yJ/orPn4SXjuVrWWoi4Cr8jfV1eHv9CixKSbU+jY4mxfrBwAuDvupPNKpMUY+FeIqsVw/JLT9+B8g==", - "dev": true, - "requires": { - "@babel/runtime-corejs3": "^7.8.3" - } - }, "xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", diff --git a/package.json b/package.json index 8a47d73..a0456ec 100644 --- a/package.json +++ b/package.json @@ -53,12 +53,12 @@ "@angular/language-service": "^10.0.5", "@types/jasmine": "^3.5.11", "@types/jasminewd2": "~2.0.8", - "@types/node": "^14.0.24", + "@types/node": "^14.0.26", "codecov": "^3.7.2", "codelyzer": "^6.0.0", - "jasmine-core": "~3.5.0", + "jasmine-core": "~3.6.0", "jasmine-spec-reporter": "~5.0.2", - "karma": "~5.1.0", + "karma": "~5.1.1", "karma-chrome-launcher": "~3.1.0", "karma-coverage-istanbul-reporter": "~3.0.3", "karma-jasmine": "~3.3.1", From f81e4da8777698a41253c6ab9c9f4c81ff71f925 Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Wed, 29 Jul 2020 02:35:29 +0530 Subject: [PATCH 07/12] Corrected Readme for sqrt pipe --- README.md | 2 +- packages/pipes/README.md | 2 +- packages/pipes/src/lib/number/README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c113b40..153d779 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,7 @@ Returns the square root of given number. Usage: `number | sqrt` ```html -{{ 625 | abs }} +{{ 625 | sqrt }} ``` diff --git a/packages/pipes/README.md b/packages/pipes/README.md index c113b40..153d779 100644 --- a/packages/pipes/README.md +++ b/packages/pipes/README.md @@ -354,7 +354,7 @@ Returns the square root of given number. Usage: `number | sqrt` ```html -{{ 625 | abs }} +{{ 625 | sqrt }} ``` diff --git a/packages/pipes/src/lib/number/README.md b/packages/pipes/src/lib/number/README.md index ccef4f4..c880289 100644 --- a/packages/pipes/src/lib/number/README.md +++ b/packages/pipes/src/lib/number/README.md @@ -65,7 +65,7 @@ Returns the square root of given number. Usage: `number | sqrt` ```html -{{ 625 | abs }} +{{ 625 | sqrt }} ``` From 06a43ec986934798f801989851d806cb67439ddd Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Wed, 29 Jul 2020 02:40:53 +0530 Subject: [PATCH 08/12] enhancement: Created pipe for calculating power --- README.md | 14 ++++++++ packages/pipes/README.md | 14 ++++++++ packages/pipes/src/lib/index.ts | 1 + packages/pipes/src/lib/number/README.md | 14 ++++++++ .../lib/number/nglrx-number-pipes.module.ts | 2 ++ .../pipes/src/lib/number/pow/pow.pipe.spec.ts | 34 +++++++++++++++++++ packages/pipes/src/lib/number/pow/pow.pipe.ts | 14 ++++++++ 7 files changed, 93 insertions(+) create mode 100644 packages/pipes/src/lib/number/pow/pow.pipe.spec.ts create mode 100644 packages/pipes/src/lib/number/pow/pow.pipe.ts diff --git a/README.md b/README.md index 153d779..bdc877f 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ export class YourComponent { - [avg](#avg) - [max](#max) - [min](#min) + - [pow](#pow) - [sqrt](#sqrt) - [sum](#sum) - [Generic Pipes](#generic-pipes) @@ -347,6 +348,19 @@ Usage: `array | min` ``` +### pow + +Returns the value of the base raised to a specified power.\ +Default value of exponent is 0. + +Usage: `base | pow [:exponent]` + +```html +{{ 4 | pow: 3 }} + +``` + + ### sqrt Returns the square root of given number. diff --git a/packages/pipes/README.md b/packages/pipes/README.md index 153d779..bdc877f 100644 --- a/packages/pipes/README.md +++ b/packages/pipes/README.md @@ -93,6 +93,7 @@ export class YourComponent { - [avg](#avg) - [max](#max) - [min](#min) + - [pow](#pow) - [sqrt](#sqrt) - [sum](#sum) - [Generic Pipes](#generic-pipes) @@ -347,6 +348,19 @@ Usage: `array | min` ``` +### pow + +Returns the value of the base raised to a specified power.\ +Default value of exponent is 0. + +Usage: `base | pow [:exponent]` + +```html +{{ 4 | pow: 3 }} + +``` + + ### sqrt Returns the square root of given number. diff --git a/packages/pipes/src/lib/index.ts b/packages/pipes/src/lib/index.ts index 0c940bb..d085630 100644 --- a/packages/pipes/src/lib/index.ts +++ b/packages/pipes/src/lib/index.ts @@ -30,5 +30,6 @@ export { AbsPipe } from './number/abs/abs.pipe'; export { AvgPipe } from './number/avg/avg.pipe'; export { MaxPipe } from './number/max/max.pipe'; export { MinPipe } from './number/min/min.pipe'; +export { PowPipe } from './number/pow/pow.pipe'; export { SqrtPipe } from './number/sqrt/sqrt.pipe'; export { SumPipe } from './number/sum/sum.pipe'; diff --git a/packages/pipes/src/lib/number/README.md b/packages/pipes/src/lib/number/README.md index c880289..7babdf6 100644 --- a/packages/pipes/src/lib/number/README.md +++ b/packages/pipes/src/lib/number/README.md @@ -6,6 +6,7 @@ A collection of pipes exported by `NglrxNumberPipesModule`. - [avg](#avg) - [max](#max) - [min](#min) + - [pow](#pow) - [sqrt](#sqrt) - [sum](#sum) @@ -58,6 +59,19 @@ Usage: `array | min` ``` +### pow + +Returns the value of the base raised to a specified power.\ +Default value of exponent is 0. + +Usage: `base | pow [:exponent]` + +```html +{{ 4 | pow: 3 }} + +``` + + ### sqrt Returns the square root of given number. diff --git a/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts b/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts index 980530b..ad0bfd7 100644 --- a/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts +++ b/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts @@ -4,6 +4,7 @@ import { AbsPipe } from './abs/abs.pipe'; import { AvgPipe } from './avg/avg.pipe'; import { MaxPipe } from './max/max.pipe'; import { MinPipe } from './min/min.pipe'; +import { PowPipe } from './pow/pow.pipe'; import { SqrtPipe } from './sqrt/sqrt.pipe'; import { SumPipe } from './sum/sum.pipe'; @@ -12,6 +13,7 @@ const NUMBER_PIPES = [ AvgPipe, MaxPipe, MinPipe, + PowPipe, SqrtPipe, SumPipe, ]; diff --git a/packages/pipes/src/lib/number/pow/pow.pipe.spec.ts b/packages/pipes/src/lib/number/pow/pow.pipe.spec.ts new file mode 100644 index 0000000..2bb156c --- /dev/null +++ b/packages/pipes/src/lib/number/pow/pow.pipe.spec.ts @@ -0,0 +1,34 @@ +import { PowPipe } from './pow.pipe'; + +describe('PowPipe', () => { + let pipe: PowPipe; + + beforeEach(() => { + pipe = new PowPipe(); + }); + + it('should create an instance', () => { + expect(pipe).toBeTruthy(); + }); + + it(`should find value of base raised to exponent`, () => { + expect(pipe.transform(4, 3)).toEqual(64); + expect(pipe.transform(-5, 2)).toEqual(25); + expect(pipe.transform(6.5, 2.1)).toEqual(50.946933504748095); + }); + + it(`should find value of base raised to default exponent`, () => { + expect(pipe.transform(10)).toEqual(1); + expect(pipe.transform(Infinity)).toEqual(1); + }); + + it(`should find value of base raised to Infinity or NaN`, () => { + expect(pipe.transform(293, Infinity)).toEqual(Infinity); + expect(pipe.transform(462, NaN)).toEqual(NaN); + }); + + it(`should be null safe`, () => { + expect(pipe.transform(null)).toEqual(1); + }); + +}); diff --git a/packages/pipes/src/lib/number/pow/pow.pipe.ts b/packages/pipes/src/lib/number/pow/pow.pipe.ts new file mode 100644 index 0000000..b8706b7 --- /dev/null +++ b/packages/pipes/src/lib/number/pow/pow.pipe.ts @@ -0,0 +1,14 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'pow' +}) +export class PowPipe implements PipeTransform { + + private static DEFAULT_EXPONENT = 0; + + transform(base: number, exponent: number = PowPipe.DEFAULT_EXPONENT): number { + return Math.pow(base, exponent); + } + +} From 91dbf7599109da8da9f2b52ffff267a2cb03378b Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Wed, 29 Jul 2020 02:53:39 +0530 Subject: [PATCH 09/12] Fixed typo in word slugified --- .../src/lib/string/slugify/slugify.pipe.spec.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/pipes/src/lib/string/slugify/slugify.pipe.spec.ts b/packages/pipes/src/lib/string/slugify/slugify.pipe.spec.ts index 1eba6b8..56d28a2 100644 --- a/packages/pipes/src/lib/string/slugify/slugify.pipe.spec.ts +++ b/packages/pipes/src/lib/string/slugify/slugify.pipe.spec.ts @@ -4,11 +4,11 @@ describe('SlugifyPipe', () => { let pipe: SlugifyPipe; const str = 'this_-is__a__ test - string!'; - const sluggifiedStr = 'this-is-a-test-string'; + const slugifiedStr = 'this-is-a-test-string'; const separator = '_'; - const sluggifiedStrWithSeparator = 'this_is_a_test_string'; + const slugifiedStrWithSeparator = 'this_is_a_test_string'; const strWithSplChars = '^this:string~is!separated@with#many$special%characters%'; - const sluggifiedStrWithSplChars = 'this_string_is_separated_with_many_special_characters'; + const slugifiedStrWithSplChars = 'this_string_is_separated_with_many_special_characters'; const emptyStr = ''; const whitespaceStr = '\t\n '; @@ -21,15 +21,15 @@ describe('SlugifyPipe', () => { }); it(`should slugify a string with default separator`, () => { - expect(pipe.transform(str)).toEqual(sluggifiedStr); + expect(pipe.transform(str)).toEqual(slugifiedStr); }); it(`should slugify a string with specified separator`, () => { - expect(pipe.transform(str, separator)).toEqual(sluggifiedStrWithSeparator); + expect(pipe.transform(str, separator)).toEqual(slugifiedStrWithSeparator); }); it(`should slugify a string with special characters`, () => { - expect(pipe.transform(strWithSplChars, separator)).toEqual(sluggifiedStrWithSplChars); + expect(pipe.transform(strWithSplChars, separator)).toEqual(slugifiedStrWithSplChars); }); it(`should return empty string on slugifying an empty string`, () => { From a827121343a15a47e067713045905530e5bd1ec9 Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Thu, 30 Jul 2020 23:43:27 +0530 Subject: [PATCH 10/12] Removed constants used in test specs and used inline values --- .../lib/generic/length/length.pipe.spec.ts | 31 ++++--------- .../lib/generic/reverse/reverse.pipe.spec.ts | 27 ++++-------- .../lib/generic/type-of/type-of.pipe.spec.ts | 44 ++++++------------- .../pipes/src/lib/number/abs/abs.pipe.spec.ts | 24 +++------- .../pipes/src/lib/number/avg/avg.pipe.spec.ts | 21 +++------ .../pipes/src/lib/number/max/max.pipe.spec.ts | 20 +++------ .../pipes/src/lib/number/min/min.pipe.spec.ts | 20 +++------ .../src/lib/number/sqrt/sqrt.pipe.spec.ts | 23 +++------- .../pipes/src/lib/number/sum/sum.pipe.spec.ts | 21 +++------ .../string/camel-case/camel-case.pipe.spec.ts | 23 +++------- .../lib/string/char-at/char-at.pipe.spec.ts | 23 +++------- .../src/lib/string/concat/concat.pipe.spec.ts | 19 +++----- .../string/lower-case/lower-case.pipe.spec.ts | 15 +++---- .../lib/string/pad-end/pad-end.pipe.spec.ts | 20 +++------ .../string/pad-start/pad-start.pipe.spec.ts | 21 +++------ .../pascal-case/pascal-case.pipe.spec.ts | 23 +++------- .../sentence-case/sentence-case.pipe.spec.ts | 24 +++------- .../lib/string/slugify/slugify.pipe.spec.ts | 21 +++------ .../src/lib/string/split/split.pipe.spec.ts | 36 +++++---------- .../string/title-case/title-case.pipe.spec.ts | 38 +++++----------- .../string/trim-left/trim-left.pipe.spec.ts | 11 ++--- .../string/trim-right/trim-right.pipe.spec.ts | 11 ++--- .../src/lib/string/trim/trim.pipe.spec.ts | 11 ++--- .../string/upper-case/upper-case.pipe.spec.ts | 15 +++---- 24 files changed, 161 insertions(+), 381 deletions(-) diff --git a/packages/pipes/src/lib/generic/length/length.pipe.spec.ts b/packages/pipes/src/lib/generic/length/length.pipe.spec.ts index 68cab6c..32ae310 100644 --- a/packages/pipes/src/lib/generic/length/length.pipe.spec.ts +++ b/packages/pipes/src/lib/generic/length/length.pipe.spec.ts @@ -3,21 +3,6 @@ import { LengthPipe } from './length.pipe'; describe('LengthPipe', () => { let pipe: LengthPipe; - const str = 'This is a test string!'; - const strLength = 22; - const emptyStr = ''; - const emptyStrLength = 0; - const num = -12345.67890; - const numLength = 11; - const bool = true; - const boolLength = 4; - const arr = ['a', 'b', 'c', 'd', 'e']; - const arrLength = 5; - const emptyArr = []; - const emptyArrLength = 0; - const obj = { foo: 'bar' }; - const date = new Date(); - beforeEach(() => { pipe = new LengthPipe(); }); @@ -27,32 +12,32 @@ describe('LengthPipe', () => { }); it(`should find length of a string`, () => { - expect(pipe.transform(str)).toEqual(strLength); + expect(pipe.transform('This is a test string!')).toEqual(22); }); it(`should find length of an empty string`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStrLength); + expect(pipe.transform('')).toEqual(0); }); it(`should find length of a number`, () => { - expect(pipe.transform(num)).toEqual(numLength); + expect(pipe.transform(-12345.67890)).toEqual(11); }); it(`should find length of a boolean`, () => { - expect(pipe.transform(bool)).toEqual(boolLength); + expect(pipe.transform(true)).toEqual(4); }); it(`should find length of an array`, () => { - expect(pipe.transform(arr)).toEqual(arrLength); + expect(pipe.transform(['a', 'b', 'c', 'd', 'e'])).toEqual(5); }); it(`should find length of an empty array`, () => { - expect(pipe.transform(emptyArr)).toEqual(emptyArrLength); + expect(pipe.transform([])).toEqual(0); }); it(`should return null for unsupported types Object, Date`, () => { - expect(pipe.transform(obj)).toBeNull(); - expect(pipe.transform(date)).toBeNull(); + expect(pipe.transform({ foo: 'bar' })).toBeNull(); + expect(pipe.transform(new Date())).toBeNull(); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/generic/reverse/reverse.pipe.spec.ts b/packages/pipes/src/lib/generic/reverse/reverse.pipe.spec.ts index b6e79ee..d0f6d07 100644 --- a/packages/pipes/src/lib/generic/reverse/reverse.pipe.spec.ts +++ b/packages/pipes/src/lib/generic/reverse/reverse.pipe.spec.ts @@ -3,19 +3,6 @@ import { ReversePipe } from './reverse.pipe'; describe('ReversePipe', () => { let pipe: ReversePipe; - const str = 'This is a test string!'; - const reverseStr = '!gnirts tset a si sihT' - const emptyStr = ''; - const num = -12345.67890; - const reverseNumStr = '9876.54321-'; - const bool = false; - const boolReverse = 'eslaf' - const arr = ['a', 'b', 'c', 'd', 'e']; - const arrReverse = ['e', 'd', 'c', 'b', 'a']; - const emptyArr = []; - const obj = { foo: 'bar' }; - const date = new Date(); - beforeEach(() => { pipe = new ReversePipe(); }); @@ -25,31 +12,33 @@ describe('ReversePipe', () => { }); it(`should reverse a string`, () => { - expect(pipe.transform(str)).toEqual(reverseStr); + expect(pipe.transform('This is a test string!')).toEqual('!gnirts tset a si sihT'); }); it(`should reverse an empty string`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStr); + expect(pipe.transform('')).toEqual(''); }); it(`should reverse a number`, () => { - expect(pipe.transform(num)).toEqual(reverseNumStr); + expect(pipe.transform(-12345.67890)).toEqual('9876.54321-'); }); it(`should reverse a boolean`, () => { - expect(pipe.transform(bool)).toEqual(boolReverse); + expect(pipe.transform(false)).toEqual('eslaf'); }); it(`should reverse an array`, () => { - expect(pipe.transform(arr)).toEqual(arrReverse); + expect(pipe.transform(['a', 'b', 'c', 'd', 'e'])).toEqual(['e', 'd', 'c', 'b', 'a']); }); it(`should reverse an empty array`, () => { - expect(pipe.transform(emptyArr)).toEqual(emptyArr); + expect(pipe.transform([])).toEqual([]); }); it(`should return input for unsupported types Object, Date`, () => { + const obj = { foo: 'bar' }; expect(pipe.transform(obj)).toBe(obj); + const date = new Date(); expect(pipe.transform(date)).toBe(date); }); diff --git a/packages/pipes/src/lib/generic/type-of/type-of.pipe.spec.ts b/packages/pipes/src/lib/generic/type-of/type-of.pipe.spec.ts index ce7444d..66add49 100644 --- a/packages/pipes/src/lib/generic/type-of/type-of.pipe.spec.ts +++ b/packages/pipes/src/lib/generic/type-of/type-of.pipe.spec.ts @@ -3,22 +3,6 @@ import { TypeOfPipe } from './type-of.pipe'; describe('TypeOfPipe', () => { let pipe: TypeOfPipe; - const str = 'This is a test string!'; - const typeOfStr = 'string'; - const num = 22; - const nan = NaN; - const infinity = Infinity; - const typeOfNum = 'number'; - const bool = true; - const typeOfBool = 'boolean'; - const obj = { foo: 'bar' }; - const arr = ['a', 'b', 'c', 'd', 'e']; - const typeOfObj = 'object'; - const date = new Date(); - function func() {} - const typeOfFunction = 'function'; - const typeOfUndefined = 'undefined'; - beforeEach(() => { pipe = new TypeOfPipe(); }); @@ -28,42 +12,42 @@ describe('TypeOfPipe', () => { }); it(`should find type of a string`, () => { - expect(pipe.transform(str)).toEqual(typeOfStr); + expect(pipe.transform('This is a test string!')).toEqual('string'); }); it(`should find type of a number`, () => { - expect(pipe.transform(num)).toEqual(typeOfNum); - expect(pipe.transform(nan)).toEqual(typeOfNum); - expect(pipe.transform(infinity)).toEqual(typeOfNum); + const typeOfNum = 'number'; + expect(pipe.transform(22)).toEqual(typeOfNum); + expect(pipe.transform(NaN)).toEqual(typeOfNum); + expect(pipe.transform(Infinity)).toEqual(typeOfNum); }); it(`should find type of a boolean`, () => { - expect(pipe.transform(bool)).toEqual(typeOfBool); + expect(pipe.transform(true)).toEqual('boolean'); }); - it(`should find type of a arr`, () => { - expect(pipe.transform(arr)).toEqual(typeOfObj); + it(`should find type of a array`, () => { + expect(pipe.transform(['a', 'b', 'c', 'd', 'e'])).toEqual('object'); }); it(`should find type of an object`, () => { - expect(pipe.transform(obj)).toEqual(typeOfObj); + expect(pipe.transform({ foo: 'bar' })).toEqual('object'); }); it(`should find type of a Date`, () => { - expect(pipe.transform(date)).toEqual(typeOfObj); + expect(pipe.transform(new Date())).toEqual('object'); }); - it(`should find type of undefined`, () => { - expect(pipe.transform(null)).toEqual(typeOfObj); - expect(pipe.transform(undefined)).toEqual('undefined'); + it(`should find type of null`, () => { + expect(pipe.transform(null)).toEqual('object'); }); it(`should find type of function`, () => { - expect(pipe.transform(func)).toEqual(typeOfFunction); + expect(pipe.transform(() => {})).toEqual('function'); }); it(`should find type of undefined`, () => { - expect(pipe.transform(undefined)).toEqual(typeOfUndefined); + expect(pipe.transform(undefined)).toEqual('undefined'); }); }); diff --git a/packages/pipes/src/lib/number/abs/abs.pipe.spec.ts b/packages/pipes/src/lib/number/abs/abs.pipe.spec.ts index 26af4a1..3ed85d5 100644 --- a/packages/pipes/src/lib/number/abs/abs.pipe.spec.ts +++ b/packages/pipes/src/lib/number/abs/abs.pipe.spec.ts @@ -3,16 +3,6 @@ import { AbsPipe } from './abs.pipe'; describe('AbsPipe', () => { let pipe: AbsPipe; - const positiveNum = 394; - const absOfPositiveNum = 394; - const negativeNum = -244; - const absOfNegativeNum = 244; - const decimalNum = -459.1386; - const absOfDecimalNum = 459.1386; - const zero = 0; - const infinity = Infinity; - const nan = NaN; - beforeEach(() => { pipe = new AbsPipe(); }); @@ -22,31 +12,31 @@ describe('AbsPipe', () => { }); it(`should find absolute of positive number`, () => { - expect(pipe.transform(positiveNum)).toEqual(absOfPositiveNum); + expect(pipe.transform(394)).toEqual(394); }); it(`should find absolute of negative number`, () => { - expect(pipe.transform(negativeNum)).toEqual(absOfNegativeNum); + expect(pipe.transform(-247)).toEqual(247); }); it(`should find absolute of decimal number`, () => { - expect(pipe.transform(decimalNum)).toEqual(absOfDecimalNum); + expect(pipe.transform(-459.1386)).toEqual(459.1386); }); it(`should return zero for absolute value of zero`, () => { - expect(pipe.transform(zero)).toEqual(zero); + expect(pipe.transform(0)).toEqual(0); }); it('should return NaN for absolute value of NaN', () => { - expect(pipe.transform(nan)).toEqual(nan); + expect(pipe.transform(NaN)).toEqual(NaN); }); it('should return Infinity for absolute value of Infinity', () => { - expect(pipe.transform(infinity)).toEqual(infinity); + expect(pipe.transform(Infinity)).toEqual(Infinity); }); it(`should be null safe`, () => { - expect(pipe.transform(null)).toEqual(zero); + expect(pipe.transform(null)).toEqual(0); }); }); diff --git a/packages/pipes/src/lib/number/avg/avg.pipe.spec.ts b/packages/pipes/src/lib/number/avg/avg.pipe.spec.ts index df25c27..2c34d18 100644 --- a/packages/pipes/src/lib/number/avg/avg.pipe.spec.ts +++ b/packages/pipes/src/lib/number/avg/avg.pipe.spec.ts @@ -3,17 +3,6 @@ import { AvgPipe } from './avg.pipe'; describe('AvgPipe', () => { let pipe: AvgPipe; - const arr = [10, 45, 200, 5, 92]; - const avgOfArr = 70.4; - const mixedArr = [30, 0, -99, -52, -9]; - const avgOfMixedArr = -26; - const singleValueArr = [2]; - const avgOfSingleValueArr = 2 - const emptyArr = []; - const avgOfEmptyArr = 0; - const nanArray = [783, Infinity, NaN, 0, -391]; - const avgOfNanArray = NaN; - beforeEach(() => { pipe = new AvgPipe(); }); @@ -23,23 +12,23 @@ describe('AvgPipe', () => { }); it(`should find avg from an array of numbers`, () => { - expect(pipe.transform(arr)).toEqual(avgOfArr); + expect(pipe.transform([10, 45, 200, 5, 92])).toEqual(70.4); }); it(`should find avg from an array of both positive and negative numbers`, () => { - expect(pipe.transform(mixedArr)).toEqual(avgOfMixedArr); + expect(pipe.transform([30, 0, -99, -52, -9])).toEqual(-26); }); it(`should find avg from an array with single value`, () => { - expect(pipe.transform(singleValueArr)).toEqual(avgOfSingleValueArr); + expect(pipe.transform([7])).toEqual(7); }); it(`should return 0 for an empty array`, () => { - expect(pipe.transform(emptyArr)).toEqual(avgOfEmptyArr); + expect(pipe.transform([])).toEqual(0); }); it('should return NaN if given array contains Infinity', () => { - expect(pipe.transform(nanArray)).toEqual(avgOfNanArray); + expect(pipe.transform([872, Infinity, NaN, 0, -693])).toEqual(NaN); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/number/max/max.pipe.spec.ts b/packages/pipes/src/lib/number/max/max.pipe.spec.ts index 91084f2..363303e 100644 --- a/packages/pipes/src/lib/number/max/max.pipe.spec.ts +++ b/packages/pipes/src/lib/number/max/max.pipe.spec.ts @@ -3,16 +3,6 @@ import { MaxPipe } from './max.pipe'; describe('MaxPipe', () => { let pipe: MaxPipe; - const arr = [10, 45, 200, 5, 92]; - const maxOfArr = 200; - const mixedArr = [30, 0, -99, -52, -9]; - const maxOfMixedArr = 30; - const singleValueArr = [2]; - const maxOfSingleValueArr = 2 - const emptyArr = []; - const nanArray = [783, Infinity, NaN, 0, -391]; - const maxOfNanArray = NaN; - beforeEach(() => { pipe = new MaxPipe(); }); @@ -22,23 +12,23 @@ describe('MaxPipe', () => { }); it(`should find maximum from an array of numbers`, () => { - expect(pipe.transform(arr)).toEqual(maxOfArr); + expect(pipe.transform([10, 45, 200, 5, 92])).toEqual(200); }); it(`should find maximum from an array of both positive and negative numbers`, () => { - expect(pipe.transform(mixedArr)).toEqual(maxOfMixedArr); + expect(pipe.transform([30, 0, -99, -52, -9])).toEqual(30); }); it(`should find maximum from an array with single value`, () => { - expect(pipe.transform(singleValueArr)).toEqual(maxOfSingleValueArr); + expect(pipe.transform([2])).toEqual(2); }); it(`should return null for an empty array`, () => { - expect(pipe.transform(emptyArr)).toBeNull(); + expect(pipe.transform([])).toBeNull(); }); it('should return NaN if given array contains Infinity', () => { - expect(pipe.transform(nanArray)).toEqual(maxOfNanArray); + expect(pipe.transform([783, Infinity, NaN, 0, -391])).toEqual(NaN); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/number/min/min.pipe.spec.ts b/packages/pipes/src/lib/number/min/min.pipe.spec.ts index 011b2fe..0e79e97 100644 --- a/packages/pipes/src/lib/number/min/min.pipe.spec.ts +++ b/packages/pipes/src/lib/number/min/min.pipe.spec.ts @@ -3,16 +3,6 @@ import { MinPipe } from './min.pipe'; describe('MinPipe', () => { let pipe: MinPipe; - const arr = [10, 45, 200, 5, 92]; - const minOfArr = 5; - const mixedArr = [30, 0, -99, -52, -9]; - const minOfMixedArr = -99; - const singleValueArr = [2]; - const minOfSingleValueArr = 2 - const emptyArr = []; - const nanArray = [783, Infinity, NaN, 0, -391]; - const minOfNanArray = NaN; - beforeEach(() => { pipe = new MinPipe(); }); @@ -22,23 +12,23 @@ describe('MinPipe', () => { }); it(`should find minimum from an array of numbers`, () => { - expect(pipe.transform(arr)).toEqual(minOfArr); + expect(pipe.transform([10, 45, 200, 5, 92])).toEqual(5); }); it(`should find minimum from an array of both positive and negative numbers`, () => { - expect(pipe.transform(mixedArr)).toEqual(minOfMixedArr); + expect(pipe.transform([30, 0, -99, -52, -9])).toEqual(-99); }); it(`should find minimum from an array with single value`, () => { - expect(pipe.transform(singleValueArr)).toEqual(minOfSingleValueArr); + expect(pipe.transform([5])).toEqual(5); }); it(`should return null for an empty array`, () => { - expect(pipe.transform(emptyArr)).toBeNull(); + expect(pipe.transform([])).toBeNull(); }); it('should return NaN if given array contains Infinity', () => { - expect(pipe.transform(nanArray)).toEqual(minOfNanArray); + expect(pipe.transform([793, Infinity, NaN, 0, -391])).toEqual(NaN); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/number/sqrt/sqrt.pipe.spec.ts b/packages/pipes/src/lib/number/sqrt/sqrt.pipe.spec.ts index bf37e43..1a6785f 100644 --- a/packages/pipes/src/lib/number/sqrt/sqrt.pipe.spec.ts +++ b/packages/pipes/src/lib/number/sqrt/sqrt.pipe.spec.ts @@ -3,15 +3,6 @@ import { SqrtPipe } from './sqrt.pipe'; describe('SqrtPipe', () => { let pipe: SqrtPipe; - const positiveNum = 625; - const sqrtOfPositiveNum = 25; - const negativeNum = -25; - const decimalNum = 243.36; - const sqrtOfDecimalNum = 15.6; - const zero = 0; - const infinity = Infinity; - const nan = NaN; - beforeEach(() => { pipe = new SqrtPipe(); }); @@ -21,31 +12,31 @@ describe('SqrtPipe', () => { }); it(`should find square root of positive number`, () => { - expect(pipe.transform(positiveNum)).toEqual(sqrtOfPositiveNum); + expect(pipe.transform(625)).toEqual(25); }); it(`should return NaN for square root of negative number`, () => { - expect(pipe.transform(negativeNum)).toEqual(nan); + expect(pipe.transform(-25)).toEqual(NaN); }); it(`should find square root of decimal number`, () => { - expect(pipe.transform(decimalNum)).toEqual(sqrtOfDecimalNum); + expect(pipe.transform(243.36)).toEqual(15.6); }); it(`should return zero for square root value of zero`, () => { - expect(pipe.transform(zero)).toEqual(zero); + expect(pipe.transform(0)).toEqual(0); }); it('should return NaN for square root value of NaN', () => { - expect(pipe.transform(nan)).toEqual(nan); + expect(pipe.transform(NaN)).toEqual(NaN); }); it('should return Infinity for square root value of Infinity', () => { - expect(pipe.transform(infinity)).toEqual(infinity); + expect(pipe.transform(Infinity)).toEqual(Infinity); }); it(`should be null safe`, () => { - expect(pipe.transform(null)).toEqual(zero); + expect(pipe.transform(null)).toEqual(0); }); }); diff --git a/packages/pipes/src/lib/number/sum/sum.pipe.spec.ts b/packages/pipes/src/lib/number/sum/sum.pipe.spec.ts index 512f01a..b49a836 100644 --- a/packages/pipes/src/lib/number/sum/sum.pipe.spec.ts +++ b/packages/pipes/src/lib/number/sum/sum.pipe.spec.ts @@ -3,17 +3,6 @@ import { SumPipe } from './sum.pipe'; describe('SumPipe', () => { let pipe: SumPipe; - const arr = [10, 45, 200, 5, 92]; - const sumOfArr = 352; - const mixedArr = [30, 0, -99, -52, -9]; - const sumOfMixedArr = -130; - const singleValueArr = [2]; - const sumOfSingleValueArr = 2 - const emptyArr = []; - const sumOfEmptyArr = 0; - const nanArray = [783, Infinity, NaN, 0, -391]; - const sumOfNanArray = NaN; - beforeEach(() => { pipe = new SumPipe(); }); @@ -23,23 +12,23 @@ describe('SumPipe', () => { }); it(`should find sum from an array of numbers`, () => { - expect(pipe.transform(arr)).toEqual(sumOfArr); + expect(pipe.transform([10, 45, 200, 5, 92])).toEqual(352); }); it(`should find sum from an array of both positive and negative numbers`, () => { - expect(pipe.transform(mixedArr)).toEqual(sumOfMixedArr); + expect(pipe.transform([30, 0, -99, -52, -9])).toEqual(-130); }); it(`should find sum from an array with single value`, () => { - expect(pipe.transform(singleValueArr)).toEqual(sumOfSingleValueArr); + expect(pipe.transform([8])).toEqual(8); }); it(`should return 0 for an empty array`, () => { - expect(pipe.transform(emptyArr)).toEqual(sumOfEmptyArr); + expect(pipe.transform([])).toEqual(0); }); it('should return NaN if given array contains Infinity', () => { - expect(pipe.transform(nanArray)).toEqual(sumOfNanArray); + expect(pipe.transform([783, Infinity, NaN, 0, -391])).toEqual(NaN); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/camel-case/camel-case.pipe.spec.ts b/packages/pipes/src/lib/string/camel-case/camel-case.pipe.spec.ts index 6fde478..d57e835 100644 --- a/packages/pipes/src/lib/string/camel-case/camel-case.pipe.spec.ts +++ b/packages/pipes/src/lib/string/camel-case/camel-case.pipe.spec.ts @@ -3,17 +3,6 @@ import { CamelCasePipe } from './camel-case.pipe'; describe('CamelCasePipe', () => { let pipe: CamelCasePipe; - const str = 'This iS A TEST string'; - const strCamelCase = 'thisIsATestString'; - const sluggishStr = '-This IS another-tEst-_strinG_'; - const sluggishStrCamelCase = 'thisIsAnotherTestString'; - const singleCharacterStr = 'A'; - const singleCharacterStrCamelCase = 'a'; - const singleWordStr = 'SiNGle'; - const singleWordStrCamelCase = 'single'; - const emptyStr = ''; - const whitespaceStr = '\t\n '; - beforeEach(() => { pipe = new CamelCasePipe(); }); @@ -23,27 +12,27 @@ describe('CamelCasePipe', () => { }); it(`should convert a string to camel case`, () => { - expect(pipe.transform(str)).toEqual(strCamelCase); + expect(pipe.transform('This iS A TEST string')).toEqual('thisIsATestString'); }); it(`should convert a sluggish string to camel case`, () => { - expect(pipe.transform(sluggishStr)).toEqual(sluggishStrCamelCase); + expect(pipe.transform('-This IS another-tEst-_strinG_')).toEqual('thisIsAnotherTestString'); }); it(`should convert a string with single character to camel case`, () => { - expect(pipe.transform(singleCharacterStr)).toEqual(singleCharacterStrCamelCase); + expect(pipe.transform('A')).toEqual('a'); }); it(`should convert a string with single word to camel case`, () => { - expect(pipe.transform(singleWordStr)).toEqual(singleWordStrCamelCase); + expect(pipe.transform('SiNGle')).toEqual('single'); }); it(`should return empty string on converting an empty string to camel case`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStr); + expect(pipe.transform('')).toEqual(''); }); it(`should return empty string on converting a white space string to camel case`, () => { - expect(pipe.transform(whitespaceStr)).toEqual(emptyStr); + expect(pipe.transform('\t\n ')).toEqual(''); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/char-at/char-at.pipe.spec.ts b/packages/pipes/src/lib/string/char-at/char-at.pipe.spec.ts index f593c8e..cc076e1 100644 --- a/packages/pipes/src/lib/string/char-at/char-at.pipe.spec.ts +++ b/packages/pipes/src/lib/string/char-at/char-at.pipe.spec.ts @@ -3,13 +3,6 @@ import { CharAtPipe } from './char-at.pipe'; describe('CharAtPipe', () => { let pipe: CharAtPipe; - const str = 'This is a test string!'; - const position = 5; - const charAtPosition = 'i'; - const charAtDefaultPosition = 'T'; - const invalidPosition = 50; - const emptyStr = ''; - beforeEach(() => { pipe = new CharAtPipe(); }); @@ -19,23 +12,21 @@ describe('CharAtPipe', () => { }); it(`should return the character at a position in string`, () => { - expect(pipe.transform(str, position)).toEqual(charAtPosition); + expect(pipe.transform('This is a test string!', 5)).toEqual('i'); }); it(`should return the character at default position i.e. 0 in string`, () => { - expect(pipe.transform(str)).toEqual(charAtDefaultPosition); + expect(pipe.transform('This is a test string!')).toEqual('T'); }); it(`should return empty for invalid position in string`, () => { - expect(pipe.transform(str, invalidPosition)).toEqual(emptyStr); - }); - - it(`should return empty for a position in an empty string`, () => { - expect(pipe.transform(emptyStr, position)).toEqual(emptyStr); + expect(pipe.transform('This is a test string!', -50)).toEqual(''); + expect(pipe.transform('This is a test string!', 50)).toEqual(''); }); - it(`should return empty for invalid position in an empty string`, () => { - expect(pipe.transform(emptyStr, invalidPosition)).toEqual(emptyStr); + it(`should return empty for any position in an empty string`, () => { + expect(pipe.transform('', 0)).toEqual(''); + expect(pipe.transform('', -10)).toEqual(''); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/concat/concat.pipe.spec.ts b/packages/pipes/src/lib/string/concat/concat.pipe.spec.ts index 57907f0..4060927 100644 --- a/packages/pipes/src/lib/string/concat/concat.pipe.spec.ts +++ b/packages/pipes/src/lib/string/concat/concat.pipe.spec.ts @@ -3,14 +3,6 @@ import { ConcatPipe } from './concat.pipe'; describe('ConcatPipe', () => { let pipe: ConcatPipe; - const str = 'This is '; - const anotherStr = 'a string!'; - const concatenatedStr = 'This is a string!'; - const tabString = '\t'; - const concatenatedStrWithTab = 'This is \t'; - const emptyStr = ''; - const multipleConcatenatedStrings = 'This is \ta string!'; - beforeEach(() => { pipe = new ConcatPipe(); }); @@ -20,19 +12,20 @@ describe('ConcatPipe', () => { }); it(`should concatenate a given string to current string`, () => { - expect(pipe.transform(str, anotherStr)).toEqual(concatenatedStr); + expect(pipe.transform('This is ', 'a string!')).toEqual('This is a string!'); }); - it(`should concatenate a given string to string with non-printable characters`, () => { - expect(pipe.transform(str, tabString)).toEqual(concatenatedStrWithTab); + it(`should concatenate a given string with non-printable characters`, () => { + expect(pipe.transform('This is ', '\t')).toEqual('This is \t'); }); it(`should return same string on concatenating with an empty string`, () => { - expect(pipe.transform(str, emptyStr)).toEqual(str); + expect(pipe.transform('This is ', '')).toEqual('This is '); }); it(`should concatenate multiple strings to a string`, () => { - expect(pipe.transform(str, tabString, anotherStr, emptyStr)).toEqual(multipleConcatenatedStrings); + expect(pipe.transform('This is ', '\t', 'a string!', '')) + .toEqual('This is \ta string!'); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/lower-case/lower-case.pipe.spec.ts b/packages/pipes/src/lib/string/lower-case/lower-case.pipe.spec.ts index 06f96f6..8ada8f7 100644 --- a/packages/pipes/src/lib/string/lower-case/lower-case.pipe.spec.ts +++ b/packages/pipes/src/lib/string/lower-case/lower-case.pipe.spec.ts @@ -3,11 +3,6 @@ import { LowerCasePipe } from './lower-case.pipe'; describe('LowerCasePipe', () => { let pipe: LowerCasePipe; - const str = 'This IS a Test string!'; - const lowercaseStr = 'this is a test string!' - const emptyStr = ''; - const whitespaceStr = '\t\n '; - beforeEach(() => { pipe = new LowerCasePipe(); }); @@ -17,15 +12,19 @@ describe('LowerCasePipe', () => { }); it(`should convert a string to lower case`, () => { - expect(pipe.transform(str)).toEqual(lowercaseStr); + expect(pipe.transform('This IS a Test string!')).toEqual('this is a test string!'); }); it(`should return empty string on converting an empty string to lower case`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStr); + expect(pipe.transform('')).toEqual(''); + }); + + it(`should return same string while converting special chars or numbers to lower case`, () => { + expect(pipe.transform('~1@3$5^7*9)-')).toEqual('~1@3$5^7*9)-'); }); it(`should return same string on converting a white space string to lower case`, () => { - expect(pipe.transform(whitespaceStr)).toEqual(whitespaceStr); + expect(pipe.transform('\t\n ')).toEqual('\t\n '); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/pad-end/pad-end.pipe.spec.ts b/packages/pipes/src/lib/string/pad-end/pad-end.pipe.spec.ts index 114d1bd..6824c85 100644 --- a/packages/pipes/src/lib/string/pad-end/pad-end.pipe.spec.ts +++ b/packages/pipes/src/lib/string/pad-end/pad-end.pipe.spec.ts @@ -3,15 +3,6 @@ import { PadEndPipe } from './pad-end.pipe'; describe('PadEndPipe', () => { let pipe: PadEndPipe; - const str = 'This is a test string!'; - const maxLengthOfStr = 24; - const leftPaddedString = 'This is a test string! '; - const maxLengthOfStrWithFillString = 29; - const fillString = '---' - const leftPaddedStringWithFillString = 'This is a test string!-------'; - const emptyStr = ''; - const fillStringSlashes = '//' - beforeEach(() => { pipe = new PadEndPipe(); }); @@ -21,20 +12,21 @@ describe('PadEndPipe', () => { }); it(`should pad a string from right with default fill string`, () => { - expect(pipe.transform(str, maxLengthOfStr)).toEqual(leftPaddedString); + expect(pipe.transform('This is a test string!', 24)).toEqual('This is a test string! '); }); it(`should pad a string from right with specified fill string`, () => { - expect(pipe.transform(str, maxLengthOfStrWithFillString, fillString)) - .toEqual(leftPaddedStringWithFillString); + expect(pipe.transform('This is a test string!', 29, '---')) + .toEqual('This is a test string!-------'); }); it(`should return empty string on trimming an empty string from right`, () => { - expect(pipe.transform(emptyStr, 0, fillStringSlashes)).toEqual(emptyStr); + expect(pipe.transform('', 0, '//')).toEqual(''); }); it(`should return fill string on padding empty string from right with fill string`, () => { - expect(pipe.transform(fillStringSlashes, fillStringSlashes.length+1, emptyStr)) + const fillStringSlashes = '//'; + expect(pipe.transform(fillStringSlashes, fillStringSlashes.length + 1, '')) .toEqual(fillStringSlashes); }); diff --git a/packages/pipes/src/lib/string/pad-start/pad-start.pipe.spec.ts b/packages/pipes/src/lib/string/pad-start/pad-start.pipe.spec.ts index 012b430..df0d9b7 100644 --- a/packages/pipes/src/lib/string/pad-start/pad-start.pipe.spec.ts +++ b/packages/pipes/src/lib/string/pad-start/pad-start.pipe.spec.ts @@ -3,15 +3,6 @@ import { PadStartPipe } from './pad-start.pipe'; describe('PadStartPipe', () => { let pipe: PadStartPipe; - const str = 'This is a test string!'; - const maxLengthOfStr = 25; - const leftPaddedString = ' This is a test string!'; - const maxLengthOfStrWithFillString = 27; - const fillString = '--' - const leftPaddedStringWithFillString = '-----This is a test string!'; - const emptyStr = ''; - const fillStringSlashes = '//' - beforeEach(() => { pipe = new PadStartPipe(); }); @@ -21,20 +12,22 @@ describe('PadStartPipe', () => { }); it(`should pad a string from left with default fill string`, () => { - expect(pipe.transform(str, maxLengthOfStr)).toEqual(leftPaddedString); + expect(pipe.transform('This is a test string!', 25)) + .toEqual(' This is a test string!'); }); it(`should pad a string from left with specified fill string`, () => { - expect(pipe.transform(str, maxLengthOfStrWithFillString, fillString)) - .toEqual(leftPaddedStringWithFillString); + expect(pipe.transform('This is a test string!', 27, '--')) + .toEqual('-----This is a test string!'); }); it(`should return empty string on trimming an empty string from left`, () => { - expect(pipe.transform(emptyStr, 0, fillStringSlashes)).toEqual(emptyStr); + expect(pipe.transform('', 0, '//')).toEqual(''); }); it(`should return fill string on padding empty string from left with fill string`, () => { - expect(pipe.transform(fillStringSlashes, fillStringSlashes.length+1, emptyStr)) + const fillStringSlashes = '//'; + expect(pipe.transform(fillStringSlashes, fillStringSlashes.length+1, '')) .toEqual(fillStringSlashes); }); diff --git a/packages/pipes/src/lib/string/pascal-case/pascal-case.pipe.spec.ts b/packages/pipes/src/lib/string/pascal-case/pascal-case.pipe.spec.ts index acea363..9f932cf 100644 --- a/packages/pipes/src/lib/string/pascal-case/pascal-case.pipe.spec.ts +++ b/packages/pipes/src/lib/string/pascal-case/pascal-case.pipe.spec.ts @@ -3,17 +3,6 @@ import { PascalCasePipe } from './pascal-case.pipe'; describe('PascalCasePipe', () => { let pipe: PascalCasePipe; - const str = 'This iS A TEST string'; - const strPascalCase = 'ThisIsATestString'; - const sluggishStr = '-This IS another-tEst-_strinG_'; - const sluggishStrPascalCase = 'ThisIsAnotherTestString'; - const singleCharacterStr = 'a'; - const singleCharacterStrPascalCase = 'A'; - const singleWordStr = 'siNGle'; - const singleWordStrPascalCase = 'Single'; - const emptyStr = ''; - const whitespaceStr = '\t\n '; - beforeEach(() => { pipe = new PascalCasePipe(); }); @@ -23,27 +12,27 @@ describe('PascalCasePipe', () => { }); it(`should convert a string to pascal case`, () => { - expect(pipe.transform(str)).toEqual(strPascalCase); + expect(pipe.transform('This iS A TEST string')).toEqual('ThisIsATestString'); }); it(`should convert a sluggish string to pascal case`, () => { - expect(pipe.transform(sluggishStr)).toEqual(sluggishStrPascalCase); + expect(pipe.transform('-This IS another-tEst-_strinG_')).toEqual('ThisIsAnotherTestString'); }); it(`should convert a string with single character to pascal case`, () => { - expect(pipe.transform(singleCharacterStr)).toEqual(singleCharacterStrPascalCase); + expect(pipe.transform('a')).toEqual('A'); }); it(`should convert a string with single word to pascal case`, () => { - expect(pipe.transform(singleWordStr)).toEqual(singleWordStrPascalCase); + expect(pipe.transform('siNGle')).toEqual('Single'); }); it(`should return empty string on converting an empty string to pascal case`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStr); + expect(pipe.transform('')).toEqual(''); }); it(`should return empty string on converting a white space string to pascal case`, () => { - expect(pipe.transform(whitespaceStr)).toEqual(emptyStr); + expect(pipe.transform('\t\n ')).toEqual(''); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/sentence-case/sentence-case.pipe.spec.ts b/packages/pipes/src/lib/string/sentence-case/sentence-case.pipe.spec.ts index 1c6e5d2..27f6764 100644 --- a/packages/pipes/src/lib/string/sentence-case/sentence-case.pipe.spec.ts +++ b/packages/pipes/src/lib/string/sentence-case/sentence-case.pipe.spec.ts @@ -3,16 +3,6 @@ import { SentenceCasePipe } from './sentence-case.pipe'; describe('SentenceCasePipe', () => { let pipe: SentenceCasePipe; - const str = 'This IS a Test string!'; - const sentenceCaseStr = 'This is a test string!' - const singleCharStr = 'a'; - const sentenceCaseSingleCharStr = 'A' - const singleWordStr = 'single'; - const sentenceCaseSingleWordStr = 'Single'; - const specialCharStr = '*** is not converted.'; - const emptyStr = ''; - const whitespaceStr = '\t\n '; - beforeEach(() => { pipe = new SentenceCasePipe(); }); @@ -22,27 +12,27 @@ describe('SentenceCasePipe', () => { }); it(`should convert a string to sentence case`, () => { - expect(pipe.transform(str)).toEqual(sentenceCaseStr); + expect(pipe.transform('This IS a Test string!')).toEqual('This is a test string!'); }); it(`should convert a string with single character to sentence case`, () => { - expect(pipe.transform(singleCharStr)).toEqual(sentenceCaseSingleCharStr); + expect(pipe.transform('a')).toEqual('A'); }); it(`should convert a string with single word to sentence case`, () => { - expect(pipe.transform(singleWordStr)).toEqual(sentenceCaseSingleWordStr); + expect(pipe.transform('single')).toEqual('Single'); }); - it(`should return same string on converting a string starting with special character to sentence case`, () => { - expect(pipe.transform(specialCharStr)).toEqual(specialCharStr); + it(`should return same string for string starting with special char`, () => { + expect(pipe.transform('$$$ is not converted.')).toEqual('$$$ is not converted.'); }); it(`should return empty string on converting an empty string to sentence case`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStr); + expect(pipe.transform('')).toEqual(''); }); it(`should return same string on converting a white space string to sentence case`, () => { - expect(pipe.transform(whitespaceStr)).toEqual(whitespaceStr); + expect(pipe.transform('\t\n ')).toEqual('\t\n '); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/slugify/slugify.pipe.spec.ts b/packages/pipes/src/lib/string/slugify/slugify.pipe.spec.ts index 56d28a2..dec1801 100644 --- a/packages/pipes/src/lib/string/slugify/slugify.pipe.spec.ts +++ b/packages/pipes/src/lib/string/slugify/slugify.pipe.spec.ts @@ -3,15 +3,6 @@ import { SlugifyPipe } from './slugify.pipe'; describe('SlugifyPipe', () => { let pipe: SlugifyPipe; - const str = 'this_-is__a__ test - string!'; - const slugifiedStr = 'this-is-a-test-string'; - const separator = '_'; - const slugifiedStrWithSeparator = 'this_is_a_test_string'; - const strWithSplChars = '^this:string~is!separated@with#many$special%characters%'; - const slugifiedStrWithSplChars = 'this_string_is_separated_with_many_special_characters'; - const emptyStr = ''; - const whitespaceStr = '\t\n '; - beforeEach(() => { pipe = new SlugifyPipe(); }); @@ -21,23 +12,25 @@ describe('SlugifyPipe', () => { }); it(`should slugify a string with default separator`, () => { - expect(pipe.transform(str)).toEqual(slugifiedStr); + expect(pipe.transform('this_-is__a__ test - string!')).toEqual('this-is-a-test-string'); }); it(`should slugify a string with specified separator`, () => { - expect(pipe.transform(str, separator)).toEqual(slugifiedStrWithSeparator); + expect(pipe.transform('this_-is__a__ test - string!', '_')) + .toEqual('this_is_a_test_string'); }); it(`should slugify a string with special characters`, () => { - expect(pipe.transform(strWithSplChars, separator)).toEqual(slugifiedStrWithSplChars); + expect(pipe.transform('^this:string~is!separated@with#many$special%characters%', '_')) + .toEqual('this_string_is_separated_with_many_special_characters'); }); it(`should return empty string on slugifying an empty string`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStr); + expect(pipe.transform('')).toEqual(''); }); it(`should return same string on slugifying a whitespace string`, () => { - expect(pipe.transform(whitespaceStr)).toEqual(emptyStr); + expect(pipe.transform('\t\n ')).toEqual(''); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/split/split.pipe.spec.ts b/packages/pipes/src/lib/string/split/split.pipe.spec.ts index 887d32d..f962d53 100644 --- a/packages/pipes/src/lib/string/split/split.pipe.spec.ts +++ b/packages/pipes/src/lib/string/split/split.pipe.spec.ts @@ -3,23 +3,6 @@ import { SplitPipe } from './split.pipe'; describe('SplitPipe', () => { let pipe: SplitPipe; - const strWithDefaultDelimiter = 'This is a string!'; - const splitStrWithDefaultDelimiter = ['This', 'is', 'a', 'string!']; - const limit = 3; - const splitStrWithDefaultDelimiterAndLimit = ['This', 'is', 'a']; - const strWithDelimiter = 'This-is-another string'; - const delimiter = '-'; - const splitStrWithDelimiter = ['This', 'is', 'another string']; - const anotherStrWithDelimiter = '_Yet_another_string_'; - const anotherDelimiter = '_'; - const anotherLimit = 4; - const splitAnotherStrWithDelimiter = ['', 'Yet', 'another', 'string']; - const stringWithWhitespaceDelimiter = 'String delimited\nwith\twhitespace \t\ncharacters' - const whitespaceRegExp = new RegExp('\\s+'); - const splitStrWithRegExpDelimiter = ['String', 'delimited', 'with', 'whitespace', 'characters']; - const emptyStr = ''; - const splitEmptyStr = ['']; - beforeEach(() => { pipe = new SplitPipe(); }); @@ -29,30 +12,31 @@ describe('SplitPipe', () => { }); it(`should split a string using default delimiter`, () => { - expect(pipe.transform(strWithDefaultDelimiter)).toEqual(splitStrWithDefaultDelimiter); + expect(pipe.transform('This is a string!')).toEqual(['This', 'is', 'a', 'string!']); }); it(`should split a string using default delimiter and limit`, () => { - expect(pipe.transform(strWithDefaultDelimiter, undefined, limit)) - .toEqual(splitStrWithDefaultDelimiterAndLimit); + expect(pipe.transform('This is a string!', undefined, 3)) + .toEqual(['This', 'is', 'a']); }); it(`should split a string using specified delimiter`, () => { - expect(pipe.transform(strWithDelimiter, delimiter)).toEqual(splitStrWithDelimiter) + expect(pipe.transform('This-is-another string', '-')) + .toEqual(['This', 'is', 'another string']); }); it(`should split another string using specified delimiter and limit`, () => { - expect(pipe.transform(anotherStrWithDelimiter, anotherDelimiter, anotherLimit)) - .toEqual(splitAnotherStrWithDelimiter); + expect(pipe.transform('_Yet_another_string_', '_', 4)) + .toEqual(['', 'Yet', 'another', 'string']); }); it(`should split a string using whitespace RegExp as delimiter`, () => { - expect(pipe.transform(stringWithWhitespaceDelimiter, whitespaceRegExp)) - .toEqual(splitStrWithRegExpDelimiter); + expect(pipe.transform('String delimited\nwith\twhitespace \t\ncharacters', new RegExp('\\s+'))) + .toEqual(['String', 'delimited', 'with', 'whitespace', 'characters']); }); it(`should return empty on splitting an empty string`, () => { - expect(pipe.transform(emptyStr)).toEqual(splitEmptyStr); + expect(pipe.transform('')).toEqual(['']); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/title-case/title-case.pipe.spec.ts b/packages/pipes/src/lib/string/title-case/title-case.pipe.spec.ts index bc3e38c..305ea36 100644 --- a/packages/pipes/src/lib/string/title-case/title-case.pipe.spec.ts +++ b/packages/pipes/src/lib/string/title-case/title-case.pipe.spec.ts @@ -3,22 +3,6 @@ import { TitleCasePipe } from './title-case.pipe'; describe('TitleCasePipe', () => { let pipe: TitleCasePipe; - const str = 'this IS a tEST string!'; - const titleCaseStr = 'This Is A Test String!' - const sluggishStr = 'this-is-a-test-string'; - const separator = '\-'; - const titleCaseSluggishStr = 'This-Is-A-Test-String'; - const exclusions = [ 'is', 'a' ] - const titleCaseStrWithExclusions = 'This is a Test String!' - const titleCaseSluggishStrWithExclusions = 'This-is-a-Test-String'; - const singleCharStr = 'a'; - const titleCaseSingleCharStr = 'A' - const singleWordStr = 'single'; - const titleCaseSingleWordStr = 'Single'; - const specialCharStr = '*** |s ^ot @onverted.'; - const emptyStr = ''; - const whitespaceStr = '\t\n '; - beforeEach(() => { pipe = new TitleCasePipe(); }); @@ -28,39 +12,41 @@ describe('TitleCasePipe', () => { }); it(`should convert a string to title case`, () => { - expect(pipe.transform(str)).toEqual(titleCaseStr); + expect(pipe.transform('this IS a tEST string!')).toEqual('This Is A Test String!'); }); it(`should convert a string to title case with separator`, () => { - expect(pipe.transform(sluggishStr, separator)).toEqual(titleCaseSluggishStr); + expect(pipe.transform('this-is-a-test-string', '\-')).toEqual('This-Is-A-Test-String'); }); it(`should convert a string to title case with exclusions`, () => { - expect(pipe.transform(str, undefined, exclusions)).toEqual(titleCaseStrWithExclusions); + expect(pipe.transform('this IS a tEST string!', undefined, [ 'is', 'a' ])) + .toEqual('This is a Test String!'); }); it(`should convert a string to title case with separator and exclusions`, () => { - expect(pipe.transform(sluggishStr, separator, exclusions)).toEqual(titleCaseSluggishStrWithExclusions); + expect(pipe.transform('this-is-a-test-string', '\-', [ 'is', 'a' ])) + .toEqual('This-is-a-Test-String'); }); it(`should convert a string with single character to title case`, () => { - expect(pipe.transform(singleCharStr)).toEqual(titleCaseSingleCharStr); + expect(pipe.transform('a')).toEqual('A'); }); it(`should convert a string with single word to title case`, () => { - expect(pipe.transform(singleWordStr)).toEqual(titleCaseSingleWordStr); + expect(pipe.transform('single')).toEqual('Single'); }); - it(`should return same string on converting a string with all words starting with special character to title case`, () => { - expect(pipe.transform(specialCharStr)).toEqual(specialCharStr); + it(`should return same string for string with all words starting with special char`, () => { + expect(pipe.transform('$$$ |s ^ot @onverted.')).toEqual('$$$ |s ^ot @onverted.'); }); it(`should return empty string on converting an empty string to title case`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStr); + expect(pipe.transform('')).toEqual(''); }); it(`should return same string on converting a white space string to title case`, () => { - expect(pipe.transform(whitespaceStr)).toEqual(whitespaceStr); + expect(pipe.transform('\t\n ')).toEqual('\t\n '); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/trim-left/trim-left.pipe.spec.ts b/packages/pipes/src/lib/string/trim-left/trim-left.pipe.spec.ts index 2650703..2dcdc8d 100644 --- a/packages/pipes/src/lib/string/trim-left/trim-left.pipe.spec.ts +++ b/packages/pipes/src/lib/string/trim-left/trim-left.pipe.spec.ts @@ -3,11 +3,6 @@ import { TrimLeftPipe } from './trim-left.pipe'; describe('TrimLeftPipe', () => { let pipe: TrimLeftPipe; - const str = ' This is a test string! '; - const strTrimmedFromLeft = 'This is a test string! ' - const emptyStr = ''; - const whitespaceStr = ' \t \n '; - beforeEach(() => { pipe = new TrimLeftPipe(); }); @@ -17,15 +12,15 @@ describe('TrimLeftPipe', () => { }); it(`should trim a string from left`, () => { - expect(pipe.transform(str)).toEqual(strTrimmedFromLeft); + expect(pipe.transform(' This is a test string! ')).toEqual('This is a test string! '); }); it(`should return empty string on trimming an empty string from left`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStr); + expect(pipe.transform('')).toEqual(''); }); it(`should return empty string on trimming string with only whitespaces from left`, () => { - expect(pipe.transform(whitespaceStr)).toEqual(emptyStr); + expect(pipe.transform(' \t \n ')).toEqual(''); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/trim-right/trim-right.pipe.spec.ts b/packages/pipes/src/lib/string/trim-right/trim-right.pipe.spec.ts index 71bc1a8..6f17f9b 100644 --- a/packages/pipes/src/lib/string/trim-right/trim-right.pipe.spec.ts +++ b/packages/pipes/src/lib/string/trim-right/trim-right.pipe.spec.ts @@ -3,11 +3,6 @@ import { TrimRightPipe } from './trim-right.pipe'; describe('TrimRightPipe', () => { let pipe: TrimRightPipe; - const str = ' This is a test string! '; - const strTrimmedFromRight = ' This is a test string!' - const emptyStr = ''; - const whitespaceStr = ' \t \n '; - beforeEach(() => { pipe = new TrimRightPipe(); }); @@ -17,15 +12,15 @@ describe('TrimRightPipe', () => { }); it(`should trim a string from left`, () => { - expect(pipe.transform(str)).toEqual(strTrimmedFromRight); + expect(pipe.transform(' This is a test string! ')).toEqual(' This is a test string!'); }); it(`should return empty string on trimming an empty string from right`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStr); + expect(pipe.transform('')).toEqual(''); }); it(`should return empty string on trimming string with only whitespaces from right`, () => { - expect(pipe.transform(whitespaceStr)).toEqual(emptyStr); + expect(pipe.transform(' \t \n ')).toEqual(''); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/trim/trim.pipe.spec.ts b/packages/pipes/src/lib/string/trim/trim.pipe.spec.ts index 22063af..c1474d1 100644 --- a/packages/pipes/src/lib/string/trim/trim.pipe.spec.ts +++ b/packages/pipes/src/lib/string/trim/trim.pipe.spec.ts @@ -3,11 +3,6 @@ import { TrimPipe } from './trim.pipe'; describe('TrimPipe', () => { let pipe: TrimPipe; - const str = ' This is a test string! '; - const trimmedStr = 'This is a test string!' - const emptyStr = ''; - const whitespaceStr = ' \t \n '; - beforeEach(() => { pipe = new TrimPipe(); }); @@ -17,15 +12,15 @@ describe('TrimPipe', () => { }); it(`should trim a string`, () => { - expect(pipe.transform(str)).toEqual(trimmedStr); + expect(pipe.transform(' This is a test string! ')).toEqual('This is a test string!'); }); it(`should return empty string on trimming an empty string`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStr); + expect(pipe.transform('')).toEqual(''); }); it(`should return empty string on trimming a string with only whitespaces`, () => { - expect(pipe.transform(whitespaceStr)).toEqual(emptyStr); + expect(pipe.transform(' \t \n ')).toEqual(''); }); it(`should be null safe`, () => { diff --git a/packages/pipes/src/lib/string/upper-case/upper-case.pipe.spec.ts b/packages/pipes/src/lib/string/upper-case/upper-case.pipe.spec.ts index 76836c6..9ddc8ef 100644 --- a/packages/pipes/src/lib/string/upper-case/upper-case.pipe.spec.ts +++ b/packages/pipes/src/lib/string/upper-case/upper-case.pipe.spec.ts @@ -3,11 +3,6 @@ import { UpperCasePipe } from './upper-case.pipe'; describe('UpperCasePipe', () => { let pipe: UpperCasePipe; - const str = 'This is A Test string!'; - const uppercaseStr = 'THIS IS A TEST STRING!' - const emptyStr = ''; - const whitespaceStr = '\t\n '; - beforeEach(() => { pipe = new UpperCasePipe(); }); @@ -17,15 +12,19 @@ describe('UpperCasePipe', () => { }); it(`should convert a string to upper case`, () => { - expect(pipe.transform(str)).toEqual(uppercaseStr); + expect(pipe.transform('This is A Test string!')).toEqual('THIS IS A TEST STRING!'); }); it(`should return empty string on converting an empty string to upper case`, () => { - expect(pipe.transform(emptyStr)).toEqual(emptyStr); + expect(pipe.transform('')).toEqual(''); + }); + + it(`should return same string while converting special chars or numbers to upper case`, () => { + expect(pipe.transform('~1@3$5^7*9)-')).toEqual('~1@3$5^7*9)-'); }); it(`should return same string on converting a white space string to upper case`, () => { - expect(pipe.transform(whitespaceStr)).toEqual(whitespaceStr); + expect(pipe.transform('\t\n ')).toEqual('\t\n '); }); it(`should be null safe`, () => { From 9dec203247cbdee1ab35fe6a0dcbec5e38493fcb Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Fri, 31 Jul 2020 00:38:32 +0530 Subject: [PATCH 11/12] enhancement : Added percentage pipe --- README.md | 14 ++++++ packages/pipes/README.md | 14 ++++++ packages/pipes/src/lib/index.ts | 1 + packages/pipes/src/lib/number/README.md | 14 ++++++ .../lib/number/nglrx-number-pipes.module.ts | 2 + .../pipes/src/lib/number/pct/pct.pipe.spec.ts | 50 +++++++++++++++++++ packages/pipes/src/lib/number/pct/pct.pipe.ts | 16 ++++++ 7 files changed, 111 insertions(+) create mode 100644 packages/pipes/src/lib/number/pct/pct.pipe.spec.ts create mode 100644 packages/pipes/src/lib/number/pct/pct.pipe.ts diff --git a/README.md b/README.md index bdc877f..12e81d3 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ export class YourComponent { - [avg](#avg) - [max](#max) - [min](#min) + - [pct](#pct) - [pow](#pow) - [sqrt](#sqrt) - [sum](#sum) @@ -348,6 +349,19 @@ Usage: `array | min` ``` +### pct + +Returns how much percent is a number of the given total. If not specified default value is 100.\ +Optionally, number of decimal places (integer) may be specified to round-off the percentage. + +Usage: `number | pct [:total] [:decimalPlaces]` + +```html +{{ 25 | pct : 483 : 2 }} + +``` + + ### pow Returns the value of the base raised to a specified power.\ diff --git a/packages/pipes/README.md b/packages/pipes/README.md index bdc877f..12e81d3 100644 --- a/packages/pipes/README.md +++ b/packages/pipes/README.md @@ -93,6 +93,7 @@ export class YourComponent { - [avg](#avg) - [max](#max) - [min](#min) + - [pct](#pct) - [pow](#pow) - [sqrt](#sqrt) - [sum](#sum) @@ -348,6 +349,19 @@ Usage: `array | min` ``` +### pct + +Returns how much percent is a number of the given total. If not specified default value is 100.\ +Optionally, number of decimal places (integer) may be specified to round-off the percentage. + +Usage: `number | pct [:total] [:decimalPlaces]` + +```html +{{ 25 | pct : 483 : 2 }} + +``` + + ### pow Returns the value of the base raised to a specified power.\ diff --git a/packages/pipes/src/lib/index.ts b/packages/pipes/src/lib/index.ts index d085630..83ade31 100644 --- a/packages/pipes/src/lib/index.ts +++ b/packages/pipes/src/lib/index.ts @@ -30,6 +30,7 @@ export { AbsPipe } from './number/abs/abs.pipe'; export { AvgPipe } from './number/avg/avg.pipe'; export { MaxPipe } from './number/max/max.pipe'; export { MinPipe } from './number/min/min.pipe'; +export { PctPipe } from './number/pct/pct.pipe'; export { PowPipe } from './number/pow/pow.pipe'; export { SqrtPipe } from './number/sqrt/sqrt.pipe'; export { SumPipe } from './number/sum/sum.pipe'; diff --git a/packages/pipes/src/lib/number/README.md b/packages/pipes/src/lib/number/README.md index 7babdf6..f922cd1 100644 --- a/packages/pipes/src/lib/number/README.md +++ b/packages/pipes/src/lib/number/README.md @@ -6,6 +6,7 @@ A collection of pipes exported by `NglrxNumberPipesModule`. - [avg](#avg) - [max](#max) - [min](#min) + - [pct](#pct) - [pow](#pow) - [sqrt](#sqrt) - [sum](#sum) @@ -59,6 +60,19 @@ Usage: `array | min` ``` +### pct + +Returns how much percent is a number of the given total. If not specified default value is 100.\ +Optionally, number of decimal places (integer) may be specified to round-off the percentage. + +Usage: `number | pct [:total] [:decimalPlaces]` + +```html +{{ 25 | pct : 483 : 2 }} + +``` + + ### pow Returns the value of the base raised to a specified power.\ diff --git a/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts b/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts index ad0bfd7..abe6555 100644 --- a/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts +++ b/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts @@ -4,6 +4,7 @@ import { AbsPipe } from './abs/abs.pipe'; import { AvgPipe } from './avg/avg.pipe'; import { MaxPipe } from './max/max.pipe'; import { MinPipe } from './min/min.pipe'; +import { PctPipe } from './pct/pct.pipe'; import { PowPipe } from './pow/pow.pipe'; import { SqrtPipe } from './sqrt/sqrt.pipe'; import { SumPipe } from './sum/sum.pipe'; @@ -13,6 +14,7 @@ const NUMBER_PIPES = [ AvgPipe, MaxPipe, MinPipe, + PctPipe, PowPipe, SqrtPipe, SumPipe, diff --git a/packages/pipes/src/lib/number/pct/pct.pipe.spec.ts b/packages/pipes/src/lib/number/pct/pct.pipe.spec.ts new file mode 100644 index 0000000..7ad1584 --- /dev/null +++ b/packages/pipes/src/lib/number/pct/pct.pipe.spec.ts @@ -0,0 +1,50 @@ +import { PctPipe } from './pct.pipe'; + +describe('PctPipe', () => { + let pipe: PctPipe; + + beforeEach(() => { + pipe = new PctPipe(); + }); + + it('should create an instance', () => { + expect(pipe).toBeTruthy(); + }); + + it(`should find percentage of a number of given total`, () => { + expect(pipe.transform(4, 64)).toEqual(6.25); + }); + + it(`should find percentage of a number of default total`, () => { + expect(pipe.transform(60)).toEqual(60); + }); + + it(`should find percentage of a number of given total and decimal places`, () => { + expect(pipe.transform(25, 483, 2)).toEqual(5.18); + }); + + it(`should find percentage of a negative number of given total`, () => { + expect(pipe.transform(-4, 64)).toEqual(-6.25); + }); + + it(`should find percentage of a decimal number of given total`, () => { + expect(pipe.transform(8.4, 8400)).toEqual(0.1); + }); + + it(`should return zero for percentage of zero`, () => { + expect(pipe.transform(0)).toEqual(0); + }); + + it('should return NaN for percentage of NaN', () => { + expect(pipe.transform(NaN)).toEqual(NaN); + }); + + it('should return Infinity for percentage of Infinity', () => { + expect(pipe.transform(Infinity)).toEqual(Infinity); + }); + + it(`should be null safe`, () => { + expect(pipe.transform(null)).toEqual(0); + }); + +}); diff --git a/packages/pipes/src/lib/number/pct/pct.pipe.ts b/packages/pipes/src/lib/number/pct/pct.pipe.ts new file mode 100644 index 0000000..e3ed449 --- /dev/null +++ b/packages/pipes/src/lib/number/pct/pct.pipe.ts @@ -0,0 +1,16 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'pct' +}) +export class PctPipe implements PipeTransform { + + transform(value: number, total: number = 100, decimalPlaces?: number): number { + let pct = value / total * 100; + if (decimalPlaces >= 0) { + pct = +pct.toFixed(Math.round(decimalPlaces)) + } + return pct; + } + +} From 23ad7af4cc4fe715595b7bed6ab63d94f98b3885 Mon Sep 17 00:00:00 2001 From: Kapil Neurgaonkar Date: Sat, 1 Aug 2020 19:37:59 +0530 Subject: [PATCH 12/12] enhancement Created round pipe --- README.md | 26 ++++++++++ packages/pipes/README.md | 26 ++++++++++ packages/pipes/src/lib/index.ts | 1 + packages/pipes/src/lib/number/README.md | 26 ++++++++++ .../lib/number/nglrx-number-pipes.module.ts | 2 + .../src/lib/number/round/round.pipe.spec.ts | 52 +++++++++++++++++++ .../pipes/src/lib/number/round/round.pipe.ts | 28 ++++++++++ 7 files changed, 161 insertions(+) create mode 100644 packages/pipes/src/lib/number/round/round.pipe.spec.ts create mode 100644 packages/pipes/src/lib/number/round/round.pipe.ts diff --git a/README.md b/README.md index 12e81d3..defb717 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ export class YourComponent { - [min](#min) - [pct](#pct) - [pow](#pow) + - [round](#round) - [sqrt](#sqrt) - [sum](#sum) - [Generic Pipes](#generic-pipes) @@ -375,6 +376,31 @@ Usage: `base | pow [:exponent]` ``` +### round + +Returns the rounded value of given number. By default the value is rounded to the nearest integer. + +It also accepts an optional argument `RoundType` for rounding the value up or down.\ +`RoundType.Default` = Default rounding as in `Math.round()` +`RoundType.Floor` = Round down as in `Math.floor()` +`RoundType.Ceil` = Round up as in `Math.ceil()` + +Optionally, the number of decimal places to which the result should be rounded may also be specified. + +Usage: `number | round [:decimalPlaces][:roundType]` + +```html +{{ 1234.56789 | round }} + + +{{ 1234.56789 | round : 3 : RoundType.Floor }} + + +{{ 9876.54321 | round : 2 : RoundType.Ceil }} + +``` + + ### sqrt Returns the square root of given number. diff --git a/packages/pipes/README.md b/packages/pipes/README.md index 12e81d3..defb717 100644 --- a/packages/pipes/README.md +++ b/packages/pipes/README.md @@ -95,6 +95,7 @@ export class YourComponent { - [min](#min) - [pct](#pct) - [pow](#pow) + - [round](#round) - [sqrt](#sqrt) - [sum](#sum) - [Generic Pipes](#generic-pipes) @@ -375,6 +376,31 @@ Usage: `base | pow [:exponent]` ``` +### round + +Returns the rounded value of given number. By default the value is rounded to the nearest integer. + +It also accepts an optional argument `RoundType` for rounding the value up or down.\ +`RoundType.Default` = Default rounding as in `Math.round()` +`RoundType.Floor` = Round down as in `Math.floor()` +`RoundType.Ceil` = Round up as in `Math.ceil()` + +Optionally, the number of decimal places to which the result should be rounded may also be specified. + +Usage: `number | round [:decimalPlaces][:roundType]` + +```html +{{ 1234.56789 | round }} + + +{{ 1234.56789 | round : 3 : RoundType.Floor }} + + +{{ 9876.54321 | round : 2 : RoundType.Ceil }} + +``` + + ### sqrt Returns the square root of given number. diff --git a/packages/pipes/src/lib/index.ts b/packages/pipes/src/lib/index.ts index 83ade31..569f47e 100644 --- a/packages/pipes/src/lib/index.ts +++ b/packages/pipes/src/lib/index.ts @@ -32,5 +32,6 @@ export { MaxPipe } from './number/max/max.pipe'; export { MinPipe } from './number/min/min.pipe'; export { PctPipe } from './number/pct/pct.pipe'; export { PowPipe } from './number/pow/pow.pipe'; +export { RoundPipe, RoundType } from './number/round/round.pipe'; export { SqrtPipe } from './number/sqrt/sqrt.pipe'; export { SumPipe } from './number/sum/sum.pipe'; diff --git a/packages/pipes/src/lib/number/README.md b/packages/pipes/src/lib/number/README.md index f922cd1..1b254fa 100644 --- a/packages/pipes/src/lib/number/README.md +++ b/packages/pipes/src/lib/number/README.md @@ -8,6 +8,7 @@ A collection of pipes exported by `NglrxNumberPipesModule`. - [min](#min) - [pct](#pct) - [pow](#pow) + - [round](#round) - [sqrt](#sqrt) - [sum](#sum) @@ -86,6 +87,31 @@ Usage: `base | pow [:exponent]` ``` +### round + +Returns the rounded value of given number. By default the value is rounded to the nearest integer. + +It also accepts an optional argument `RoundType` for rounding the value up or down.\ +`RoundType.Default` = Default rounding as in `Math.round()` +`RoundType.Floor` = Round down as in `Math.floor()` +`RoundType.Ceil` = Round up as in `Math.ceil()` + +Optionally, the number of decimal places to which the result should be rounded may also be specified. + +Usage: `number | round [:decimalPlaces][:roundType]` + +```html +{{ 1234.56789 | round }} + + +{{ 1234.56789 | round : 3 : RoundType.Floor }} + + +{{ 9876.54321 | round : 2 : RoundType.Ceil }} + +``` + + ### sqrt Returns the square root of given number. diff --git a/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts b/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts index abe6555..42fdfad 100644 --- a/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts +++ b/packages/pipes/src/lib/number/nglrx-number-pipes.module.ts @@ -6,6 +6,7 @@ import { MaxPipe } from './max/max.pipe'; import { MinPipe } from './min/min.pipe'; import { PctPipe } from './pct/pct.pipe'; import { PowPipe } from './pow/pow.pipe'; +import { RoundPipe } from './round/round.pipe'; import { SqrtPipe } from './sqrt/sqrt.pipe'; import { SumPipe } from './sum/sum.pipe'; @@ -16,6 +17,7 @@ const NUMBER_PIPES = [ MinPipe, PctPipe, PowPipe, + RoundPipe, SqrtPipe, SumPipe, ]; diff --git a/packages/pipes/src/lib/number/round/round.pipe.spec.ts b/packages/pipes/src/lib/number/round/round.pipe.spec.ts new file mode 100644 index 0000000..60a4b8b --- /dev/null +++ b/packages/pipes/src/lib/number/round/round.pipe.spec.ts @@ -0,0 +1,52 @@ +import {RoundPipe, RoundType} from './round.pipe'; + +describe('RoundPipe', () => { + let pipe: RoundPipe; + + beforeEach(() => { + pipe = new RoundPipe(); + }); + + it('should create an instance', () => { + expect(pipe).toBeTruthy(); + }); + + it(`should round off positive number`, () => { + expect(pipe.transform(1234.56)).toEqual(1235); + expect(pipe.transform(1234.456, undefined, RoundType.Default)).toEqual(1234); + expect(pipe.transform(1234.56789, 3)).toEqual(1234.568); + expect(pipe.transform(9876.54321, 3)).toEqual(9876.543); + }); + + it(`should round off of negative number`, () => { + expect(pipe.transform(-1234.56)).toEqual(-1235); + expect(pipe.transform(-1234.456, undefined, RoundType.Default)).toEqual(-1234); + expect(pipe.transform(-1234.56789, 3)).toEqual(-1234.568); + expect(pipe.transform(-9876.54321, 3)).toEqual(-9876.543); + }); + + it(`should round down positive number`, () => { + expect(pipe.transform(1234.56, undefined, RoundType.Floor)).toEqual(1234); + expect(pipe.transform(1234.56789, 3, RoundType.Floor)).toEqual(1234.567); + expect(pipe.transform(9876.54321, 3, RoundType.Floor)).toEqual(9876.543); + }); + + it(`should round down negative number`, () => { + expect(pipe.transform(-1234.56, undefined, RoundType.Floor)).toEqual(-1235); + expect(pipe.transform(-1234.56789, 3, RoundType.Floor)).toEqual(-1234.568); + expect(pipe.transform(-9876.54321, 3, RoundType.Floor)).toEqual(-9876.544); + }); + + it(`should round up positive number`, () => { + expect(pipe.transform(1234.56, undefined, RoundType.Ceil)).toEqual(1235); + expect(pipe.transform(1234.56789, 3, RoundType.Ceil)).toEqual(1234.568); + expect(pipe.transform(9876.54321, 3, RoundType.Ceil)).toEqual(9876.544); + }); + + it(`should round up negative number`, () => { + expect(pipe.transform(-1234.56, undefined, RoundType.Ceil)).toEqual(-1234); + expect(pipe.transform(-1234.56789, 3, RoundType.Ceil)).toEqual(-1234.567); + expect(pipe.transform(-9876.54321, 3, RoundType.Ceil)).toEqual(-9876.543); + }); + +}); diff --git a/packages/pipes/src/lib/number/round/round.pipe.ts b/packages/pipes/src/lib/number/round/round.pipe.ts new file mode 100644 index 0000000..e88b5d0 --- /dev/null +++ b/packages/pipes/src/lib/number/round/round.pipe.ts @@ -0,0 +1,28 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +export enum RoundType { + Default, + Floor, + Ceil +} + +@Pipe({ + name: 'round' +}) +export class RoundPipe implements PipeTransform { + + transform(value: number, decimalPlaces: number = 0, roundType?: RoundType): number { + const factor = Math.pow(10, decimalPlaces); + + switch (roundType) { + case RoundType.Floor: + return Math.floor(value * factor) / factor; + case RoundType.Ceil: + return Math.ceil(value * factor) / factor; + case RoundType.Default: + default: + return Math.round(value * factor) / factor; + } + } + +}