Skip to content

Commit

Permalink
Merge pull request #37 from nglrx/0.3.1
Browse files Browse the repository at this point in the history
0.3.1
  • Loading branch information
kneurgao authored Jul 26, 2020
2 parents 2a8e273 + cde9188 commit b119dd9
Show file tree
Hide file tree
Showing 35 changed files with 164 additions and 72 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A collection of pipes for Angular apps.

## Installation

Use [npm](https://www.npmjs.com/) to install @nglrx/pipes.
Use [npm](https://www.npmjs.com/) to install @nglrx/pipes.

```bash
npm i @nglrx/pipes
Expand Down Expand Up @@ -157,7 +157,7 @@ Usage: `string | lowerCase`
### padEnd

Pads the given string with a fill string so that the resulting string reaches the specified max length. The fill string is appended to the given string.\
Default fill string is space ' '.
Default fill string is space `' '`.

Usage: `string | padEnd:maxLength[:fillString]`

Expand All @@ -170,7 +170,7 @@ Usage: `string | padEnd:maxLength[:fillString]`
### padStart

Pads the given string with a fill string so that the resulting string reaches the specified max length. The fill string is prepended to the given string.\
Default fill string is space ' '.
Default fill string is space `' '`.

Usage: `string | padStart:maxLength[:fillString]`

Expand Down Expand Up @@ -221,7 +221,7 @@ Usage: `string | slugify[:separator]`
### split

Splits a given string into an array of sub-strings using an optional delimiter.\
Default delimiter is space ' '.\
Default delimiter is space `' '`.\
Optionally, you may also specify a limit (integer) on the number of splits.

Usage: `string | split[:delimiter][:limit]`
Expand Down Expand Up @@ -363,9 +363,11 @@ A collection of pipes exported by `NglrxGenericPipesModule`.

### length

Returns the length of a given string or array.
Returns the length of a given value of any supported type.\
Supported data types are string, array, number, boolean, or any data type which has own property 'length'.\
For an array the number of elements is returned. For others the number of characters in value is returned.

Usage: `string-OR-array | length`
Usage: `value | length`

```html
{{ 'This is a test string!' | length }}
Expand All @@ -378,9 +380,11 @@ Usage: `string-OR-array | length`

### reverse

Reverses a given string or array (of any type).
Reverses a given value of any supported type.\
Supported data types are string, array, number, boolean.\
For an array the sequence of elements is reversed. For others the sequence of characters in value is reversed.

Usage: `string-OR-array | reverse`
Usage: `value | reverse`

```html
{{ 'This is a test string!' | reverse }}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nglrx/pipes",
"version": "0.3.0",
"version": "0.3.1",
"author": "Kapil Neurgaonkar",
"description": "A collection of pipes for Angular apps.",
"license": "MIT",
Expand Down
20 changes: 12 additions & 8 deletions packages/pipes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A collection of pipes for Angular apps.

## Installation

Use [npm](https://www.npmjs.com/) to install @nglrx/pipes.
Use [npm](https://www.npmjs.com/) to install @nglrx/pipes.

```bash
npm i @nglrx/pipes
Expand Down Expand Up @@ -157,7 +157,7 @@ Usage: `string | lowerCase`
### padEnd

Pads the given string with a fill string so that the resulting string reaches the specified max length. The fill string is appended to the given string.\
Default fill string is space ' '.
Default fill string is space `' '`.

Usage: `string | padEnd:maxLength[:fillString]`

Expand All @@ -170,7 +170,7 @@ Usage: `string | padEnd:maxLength[:fillString]`
### padStart

Pads the given string with a fill string so that the resulting string reaches the specified max length. The fill string is prepended to the given string.\
Default fill string is space ' '.
Default fill string is space `' '`.

Usage: `string | padStart:maxLength[:fillString]`

Expand Down Expand Up @@ -221,7 +221,7 @@ Usage: `string | slugify[:separator]`
### split

Splits a given string into an array of sub-strings using an optional delimiter.\
Default delimiter is space ' '.\
Default delimiter is space `' '`.\
Optionally, you may also specify a limit (integer) on the number of splits.

Usage: `string | split[:delimiter][:limit]`
Expand Down Expand Up @@ -363,9 +363,11 @@ A collection of pipes exported by `NglrxGenericPipesModule`.

### length

Returns the length of a given string or array.
Returns the length of a given value of any supported type.\
Supported data types are string, array, number, boolean, or any data type which has own property 'length'.\
For an array the number of elements is returned. For others the number of characters in value is returned.

Usage: `string-OR-array | length`
Usage: `value | length`

```html
{{ 'This is a test string!' | length }}
Expand All @@ -378,9 +380,11 @@ Usage: `string-OR-array | length`

### reverse

Reverses a given string or array (of any type).
Reverses a given value of any supported type.\
Supported data types are string, array, number, boolean.\
For an array the sequence of elements is reversed. For others the sequence of characters in value is reversed.

Usage: `string-OR-array | reverse`
Usage: `value | reverse`

```html
{{ 'This is a test string!' | reverse }}
Expand Down
2 changes: 1 addition & 1 deletion packages/pipes/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nglrx/pipes",
"version": "0.3.0",
"version": "0.3.1",
"author": "Kapil Neurgaonkar",
"description": "A collection of pipes for Angular apps.",
"license": "MIT",
Expand Down
12 changes: 8 additions & 4 deletions packages/pipes/src/lib/generic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ A collection of pipes exported by `NglrxGenericPipesModule`.

### length

Returns the length of a given string or array.
Returns the length of a given value of any supported type.\
Supported data types are string, array, number, boolean, or any data type which has own property 'length'.\
For an array the number of elements is returned. For others the number of characters in value is returned.

Usage: `string-OR-array | length`
Usage: `value | length`

```html
{{ 'This is a test string!' | length }}
Expand All @@ -23,9 +25,11 @@ Usage: `string-OR-array | length`

### reverse

Reverses a given string or array (of any type).
Reverses a given value of any supported type.\
Supported data types are string, array, number, boolean.\
For an array the sequence of elements is reversed. For others the sequence of characters in value is reversed.

Usage: `string-OR-array | reverse`
Usage: `value | reverse`

```html
{{ 'This is a test string!' | reverse }}
Expand Down
6 changes: 3 additions & 3 deletions packages/pipes/src/lib/generic/length/length.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ describe('LengthPipe', () => {
});

it(`should return null for unsupported types Object, Date`, () => {
expect(pipe.transform(obj)).toEqual(null);
expect(pipe.transform(date)).toEqual(null);
expect(pipe.transform(obj)).toBeNull();
expect(pipe.transform(date)).toBeNull();
});

it(`should be null safe`, () => {
expect(pipe.transform(null)).toEqual(null);
expect(pipe.transform(null)).toBeNull();
});

});
4 changes: 3 additions & 1 deletion packages/pipes/src/lib/generic/length/length.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Pipe, PipeTransform } from '@angular/core';

import { TypeUtils } from '../../utils/type-utils';

@Pipe({
Expand All @@ -7,7 +8,8 @@ import { TypeUtils } from '../../utils/type-utils';
export class LengthPipe implements PipeTransform {

transform(value: any): number {
if (TypeUtils.isString(value) || TypeUtils.isArray(value) || (value && value.hasOwnProperty('length'))) {
if (TypeUtils.isString(value) || TypeUtils.isArray(value)
|| (value && value.hasOwnProperty('length'))) {
return value.length;
} else if (TypeUtils.isNumber(value) || TypeUtils.isBoolean(value)) {
return value.toString().length;
Expand Down
6 changes: 3 additions & 3 deletions packages/pipes/src/lib/generic/reverse/reverse.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ describe('ReversePipe', () => {
});

it(`should return input for unsupported types Object, Date`, () => {
expect(pipe.transform(obj)).toEqual(obj);
expect(pipe.transform(date)).toEqual(date);
expect(pipe.transform(obj)).toBe(obj);
expect(pipe.transform(date)).toBe(date);
});

it(`should be null safe`, () => {
expect(pipe.transform(null)).toEqual(null);
expect(pipe.transform(null)).toBeNull();
});

});
1 change: 1 addition & 0 deletions packages/pipes/src/lib/generic/reverse/reverse.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Pipe, PipeTransform } from '@angular/core';

import { TypeUtils } from '../../utils/type-utils';

@Pipe({
Expand Down
15 changes: 12 additions & 3 deletions packages/pipes/src/lib/number/abs/abs.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ describe('AbsPipe', () => {
const decimalNum = -459.1386;
const absOfDecimalNum = 459.1386;
const zero = 0;
const absOfZero = 0;
const infinity = Infinity;
const nan = NaN;

beforeEach(() => {
pipe = new AbsPipe();
Expand All @@ -33,11 +34,19 @@ describe('AbsPipe', () => {
});

it(`should return zero for absolute value of zero`, () => {
expect(pipe.transform(zero)).toEqual(absOfZero);
expect(pipe.transform(zero)).toEqual(zero);
});

it('should return NaN for absolute value of NaN', () => {
expect(pipe.transform(nan)).toEqual(nan);
});

it('should return Infinity for absolute value of Infinity', () => {
expect(pipe.transform(infinity)).toEqual(infinity);
});

it(`should be null safe`, () => {
expect(pipe.transform(null)).toEqual(0);
expect(pipe.transform(null)).toEqual(zero);
});

});
11 changes: 9 additions & 2 deletions packages/pipes/src/lib/number/avg/avg.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ describe('AvgPipe', () => {
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();
Expand All @@ -31,8 +34,12 @@ describe('AvgPipe', () => {
expect(pipe.transform(singleValueArr)).toEqual(avgOfSingleValueArr);
});

it(`should return null for an empty array`, () => {
expect(pipe.transform(emptyArr)).toBeNull();
it(`should return 0 for an empty array`, () => {
expect(pipe.transform(emptyArr)).toEqual(avgOfEmptyArr);
});

it('should return NaN if given array contains Infinity', () => {
expect(pipe.transform(nanArray)).toEqual(avgOfNanArray);
});

it(`should be null safe`, () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/pipes/src/lib/number/avg/avg.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { Pipe, PipeTransform } from '@angular/core';

import { NumberUtils } from '../../utils/number-utils';
import { TypeUtils } from '../../utils/type-utils';

@Pipe({
name: 'avg'
})
export class AvgPipe implements PipeTransform {

transform(values: number[]): number {
if (TypeUtils.isNullOrUndefined(values)) {
return null;
} else if (TypeUtils.isEmpty(values)) {
return 0;
} else if (!TypeUtils.areFinite(values)) {
return NaN;
}
const sum = NumberUtils.sum(values);
return sum ? sum / values.length : null;
return sum / values.length;
}

}
6 changes: 6 additions & 0 deletions packages/pipes/src/lib/number/max/max.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe('MaxPipe', () => {
const singleValueArr = [2];
const maxOfSingleValueArr = 2
const emptyArr = [];
const nanArray = [783, Infinity, NaN, 0, -391];
const maxOfNanArray = NaN;

beforeEach(() => {
pipe = new MaxPipe();
Expand All @@ -35,6 +37,10 @@ describe('MaxPipe', () => {
expect(pipe.transform(emptyArr)).toBeNull();
});

it('should return NaN if given array contains Infinity', () => {
expect(pipe.transform(nanArray)).toEqual(maxOfNanArray);
});

it(`should be null safe`, () => {
expect(pipe.transform(null)).toBeNull();
});
Expand Down
4 changes: 3 additions & 1 deletion packages/pipes/src/lib/number/max/max.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Pipe, PipeTransform } from '@angular/core';

import { TypeUtils } from '../../utils/type-utils';

@Pipe({
name: 'max'
})
export class MaxPipe implements PipeTransform {

transform(values: number[]): number {
return values && values.length > 0 ? Math.max(...values) : null;
return TypeUtils.isEmpty(values) ? null : Math.max(...values);
}

}
6 changes: 6 additions & 0 deletions packages/pipes/src/lib/number/min/min.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe('MinPipe', () => {
const singleValueArr = [2];
const minOfSingleValueArr = 2
const emptyArr = [];
const nanArray = [783, Infinity, NaN, 0, -391];
const minOfNanArray = NaN;

beforeEach(() => {
pipe = new MinPipe();
Expand All @@ -35,6 +37,10 @@ describe('MinPipe', () => {
expect(pipe.transform(emptyArr)).toBeNull();
});

it('should return NaN if given array contains Infinity', () => {
expect(pipe.transform(nanArray)).toEqual(minOfNanArray);
});

it(`should be null safe`, () => {
expect(pipe.transform(null)).toBeNull();
});
Expand Down
4 changes: 3 additions & 1 deletion packages/pipes/src/lib/number/min/min.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Pipe, PipeTransform } from '@angular/core';

import { TypeUtils } from '../../utils/type-utils';

@Pipe({
name: 'min'
})
export class MinPipe implements PipeTransform {

transform(values: number[]): number {
return values && values.length > 0 ? Math.min(...values) : null;
return TypeUtils.isEmpty(values) ? null : Math.min(...values);
}

}
Loading

0 comments on commit b119dd9

Please sign in to comment.