From 9907eee9bc468136f40aad92a6e283b93e18b0a6 Mon Sep 17 00:00:00 2001 From: Dan Revah Date: Thu, 29 Dec 2016 11:26:30 +0200 Subject: [PATCH 01/10] left pad and right pad string pipes (#12) --- README.md | 28 +++++++++++++++++++++++++++- src/app/pipes/string/lpad.spec.ts | 30 ++++++++++++++++++++++++++++++ src/app/pipes/string/lpad.ts | 16 ++++++++++++++++ src/app/pipes/string/rpad.spec.ts | 30 ++++++++++++++++++++++++++++++ src/app/pipes/string/rpad.ts | 16 ++++++++++++++++ 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 src/app/pipes/string/lpad.spec.ts create mode 100644 src/app/pipes/string/lpad.ts create mode 100644 src/app/pipes/string/rpad.spec.ts create mode 100644 src/app/pipes/string/rpad.ts diff --git a/README.md b/README.md index 3cf6ea4b..0558457e 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ - [underscore](#underscore) - [test](#test) - [match](#match) + - [lpad](#lpad) + - [rpad](#rpad) - [Array](#Array) - [diff](#diff) - [flatten](#flatten) @@ -262,7 +264,7 @@ API: `string | latinise` ### lines -Removes accents from Latin characters. +Converts a string with new lines into an array of each line. API: `string | lines` @@ -305,6 +307,30 @@ API: `string | match: {RegExp}: {Flags}`

{{'FOO' | match: '^foo': 'i' }}

``` +### lpad + +Left pad a string to a given length using a given pad character (default is a space) + + +API: `string | lpad: length: [padCharacter:string|optional]` + +```html +

{{'foo' | lpad: 5}}

+ +

{{String(3) | lpad: 5: '0'}}

+``` + +### rpad + +Right pad a string to a given length using a given pad character (default is a space) + + +API: `string | rpad: length: [padCharacter:string|optional]` + +```html +

{{'Foo' | rpad: 5: '#'}}

+``` + ## Array ### diff diff --git a/src/app/pipes/string/lpad.spec.ts b/src/app/pipes/string/lpad.spec.ts new file mode 100644 index 00000000..50f3d0a9 --- /dev/null +++ b/src/app/pipes/string/lpad.spec.ts @@ -0,0 +1,30 @@ +import {LeftPadPipe} from "./lpad"; + +describe('LeftPadPipe Tests', () => { + let pipe:LeftPadPipe; + + beforeEach(() => { + pipe = new LeftPadPipe(); + }); + + it('Should left pad with 2 blanks', () => { + let result = pipe.transform('foo', 5); + expect(result).toEqual(' foo'); + }); + + it('Should left pad a number casted to string with 5 zeros', () => { + let result = pipe.transform(String(2), 6, '0'); + expect(result).toEqual('000002'); + }); + + it('Should not add padding', () => { + let result = pipe.transform('foo', 3); + expect(result).toEqual('foo'); + }); + + it('Should not add padding', () => { + let result = pipe.transform('foofoo', 3); + expect(result).toEqual('foofoo'); + }); + +}); diff --git a/src/app/pipes/string/lpad.ts b/src/app/pipes/string/lpad.ts new file mode 100644 index 00000000..67dc9922 --- /dev/null +++ b/src/app/pipes/string/lpad.ts @@ -0,0 +1,16 @@ +import { PipeTransform, Pipe } from '@angular/core'; +import GeneralHelper from '../helpers/helpers'; + +@Pipe({ name: 'lpad' }) +export class LeftPadPipe implements PipeTransform { + + transform(str: string, length: number, padCharacter: string = ' '): string { + if (!GeneralHelper.isString(str) || str.length >= length) { + return str; + } + while (str.length < length) { + str = padCharacter + str; + } + return str; + } +} diff --git a/src/app/pipes/string/rpad.spec.ts b/src/app/pipes/string/rpad.spec.ts new file mode 100644 index 00000000..499014ec --- /dev/null +++ b/src/app/pipes/string/rpad.spec.ts @@ -0,0 +1,30 @@ +import {RightPadPipe} from "./rpad"; + +describe('RightPadPipe Tests', () => { + let pipe: RightPadPipe; + + beforeEach(() => { + pipe = new RightPadPipe(); + }); + + it('Should right pad with 2 blanks', () => { + let result = pipe.transform('foo', 5); + expect(result).toEqual('foo '); + }); + + it('Should right pad a number casted to string with 5 zeros', () => { + let result = pipe.transform(String(2), 6, '0'); + expect(result).toEqual('200000'); + }); + + it('Should not add padding if sting length is the same as length', () => { + let result = pipe.transform('foo', 3); + expect(result).toEqual('foo'); + }); + + it('Should not add padding if sting length is greater than length', () => { + let result = pipe.transform('foofoo', 3); + expect(result).toEqual('foofoo'); + }); + +}); diff --git a/src/app/pipes/string/rpad.ts b/src/app/pipes/string/rpad.ts new file mode 100644 index 00000000..ba7e308d --- /dev/null +++ b/src/app/pipes/string/rpad.ts @@ -0,0 +1,16 @@ +import { PipeTransform, Pipe } from '@angular/core'; +import GeneralHelper from '../helpers/helpers'; + +@Pipe({ name: 'rpad' }) +export class RightPadPipe implements PipeTransform { + + transform(str: string, length: number = 1, padCharacter: string = ' '): string { + if (!GeneralHelper.isString(str) || str.length >= length) { + return str; + } + while (str.length < length) { + str = str + padCharacter; + } + return str; + } +} From 4d290f8d1c07427cb99895154b0c3195c97d7b95 Mon Sep 17 00:00:00 2001 From: danrevah Date: Thu, 29 Dec 2016 11:33:18 +0200 Subject: [PATCH 02/10] Add LeftPad and RightPad pipes to Module --- package.json | 2 +- src/app/pipes/string/index.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2117d6bb..33a58043 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ng2-pipes", - "version": "1.0.0", + "version": "1.1.0", "author": "Dan Revah", "description": "Useful angular2 pipes", "license": "MIT", diff --git a/src/app/pipes/string/index.ts b/src/app/pipes/string/index.ts index eceb523a..88ad9a8d 100644 --- a/src/app/pipes/string/index.ts +++ b/src/app/pipes/string/index.ts @@ -15,12 +15,14 @@ import {LinesPipe} from './lines'; import {UnderscorePipe} from './underscore'; import {MatchPipe} from './match'; import {TestPipe} from './test'; +import {LeftPadPipe} from './lpad'; +import {RightPadPipe} from './rpad'; export const STRING_PIPES = [ LeftTrimPipe, RepeatPipe, RightTrimPipe, ScanPipe, ShortenPipe, StripTagsPipe, TrimPipe, UcFirstPipe, UcWordsPipe, SlugifyPipe, CamelizePipe, LatinisePipe, LinesPipe, UnderscorePipe, MatchPipe, - TestPipe + TestPipe, LeftPadPipe, RightPadPipe ]; @NgModule({ @@ -46,3 +48,5 @@ export * from './lines'; export * from './underscore'; export * from './match'; export * from './test'; +export * from './lpad'; +export * from './rpad'; From b968aacbd6536ff9a379a638db3d0964ee6f7144 Mon Sep 17 00:00:00 2001 From: danrevah Date: Thu, 29 Dec 2016 11:36:16 +0200 Subject: [PATCH 03/10] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 33a58043..d07b3c86 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ng2-pipes", - "version": "1.1.0", + "version": "1.1.1", "author": "Dan Revah", "description": "Useful angular2 pipes", "license": "MIT", From 805a382d4cdd7f904a08ec4603b7a9975bf035e3 Mon Sep 17 00:00:00 2001 From: danrevah Date: Wed, 4 Jan 2017 21:23:37 +0200 Subject: [PATCH 04/10] Change package name --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d07b3c86..37eb474d 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "ng2-pipes", + "name": "ngx-pipes", "version": "1.1.1", "author": "Dan Revah", "description": "Useful angular2 pipes", From a9ce1d15a857a55fa1760d3bf12bcad963ba967a Mon Sep 17 00:00:00 2001 From: danrevah Date: Wed, 4 Jan 2017 21:57:57 +0200 Subject: [PATCH 05/10] Change package name --- README.md | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0558457e..beca5edf 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Angular Pipes -[![npm](https://img.shields.io/npm/v/ng2-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ng2-pipes) [![Travis](https://img.shields.io/travis/danrevah/ng-pipes.svg?style=flat-square)](https://travis-ci.org/danrevah/ng-pipes) [![Coveralls](https://img.shields.io/coveralls/danrevah/ng-pipes.svg?style=flat-square)](https://coveralls.io/github/danrevah/ng-pipes?branch=master) [![npm](https://img.shields.io/npm/dt/ng2-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ng2-pipes) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square)](https://github.com/danrevah/ng-pipes/blob/master/LICENSE.md) +[![npm](https://img.shields.io/npm/v/ngx-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ngx-pipes) [![Travis](https://img.shields.io/travis/danrevah/ngx-pipes.svg?style=flat-square)](https://travis-ci.org/danrevah/ngx-pipes) [![Coveralls](https://img.shields.io/coveralls/danrevah/ngx-pipes.svg?style=flat-square)](https://coveralls.io/github/danrevah/ngx-pipes?branch=master) [![npm](https://img.shields.io/npm/dt/ng2-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ng2-pipes) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square)](https://github.com/danrevah/ng-pipes/blob/master/LICENSE.md) > Useful pipes for Angular 2+ with no external dependencies! @@ -88,7 +88,7 @@ 1. Use npm to install the package ```terminal - $ npm install ng2-pipes --save + $ npm install ngx-pipes --save ``` 2. You could either add into your module `imports` the `NgPipesModule` in order to add all of the pipes, Or add a specific module such as `NgArrayPipesModule`, `NgObjectPipesModule`, `NgStringPipesModule`, `NgMathPipesModule` or `NgBooleanPipesModule`. diff --git a/package.json b/package.json index 37eb474d..f028633f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ngx-pipes", - "version": "1.1.1", + "version": "1.1.2", "author": "Dan Revah", "description": "Useful angular2 pipes", "license": "MIT", From b5c7e77ae414967ef322b3cdfad47a84e285b4b2 Mon Sep 17 00:00:00 2001 From: danrevah Date: Wed, 4 Jan 2017 22:13:17 +0200 Subject: [PATCH 06/10] Update package name --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index beca5edf..babde810 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Angular Pipes +# ngx-pipes [![npm](https://img.shields.io/npm/v/ngx-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ngx-pipes) [![Travis](https://img.shields.io/travis/danrevah/ngx-pipes.svg?style=flat-square)](https://travis-ci.org/danrevah/ngx-pipes) [![Coveralls](https://img.shields.io/coveralls/danrevah/ngx-pipes.svg?style=flat-square)](https://coveralls.io/github/danrevah/ngx-pipes?branch=master) [![npm](https://img.shields.io/npm/dt/ng2-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ng2-pipes) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square)](https://github.com/danrevah/ng-pipes/blob/master/LICENSE.md) > Useful pipes for Angular 2+ with no external dependencies! From c3297b83d003611d38e7f641cad3c303add2a76a Mon Sep 17 00:00:00 2001 From: danrevah Date: Wed, 4 Jan 2017 22:20:12 +0200 Subject: [PATCH 07/10] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index babde810..bd7a57fc 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # ngx-pipes [![npm](https://img.shields.io/npm/v/ngx-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ngx-pipes) [![Travis](https://img.shields.io/travis/danrevah/ngx-pipes.svg?style=flat-square)](https://travis-ci.org/danrevah/ngx-pipes) [![Coveralls](https://img.shields.io/coveralls/danrevah/ngx-pipes.svg?style=flat-square)](https://coveralls.io/github/danrevah/ngx-pipes?branch=master) [![npm](https://img.shields.io/npm/dt/ng2-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ng2-pipes) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square)](https://github.com/danrevah/ng-pipes/blob/master/LICENSE.md) -> Useful pipes for Angular 2+ with no external dependencies! +> Useful pipes for Angular 2 and beyond with no external dependencies! ## Table of contents @@ -94,7 +94,7 @@ 2. You could either add into your module `imports` the `NgPipesModule` in order to add all of the pipes, Or add a specific module such as `NgArrayPipesModule`, `NgObjectPipesModule`, `NgStringPipesModule`, `NgMathPipesModule` or `NgBooleanPipesModule`. ```typescript - import {NgPipesModule} from 'ng2-pipes'; + import {NgPipesModule} from 'ngx-pipes'; @NgModule({ // ... @@ -108,7 +108,7 @@ 3. Pipes are also injectable and can be used in Components / Services / etc.. ```typescript - import {ReversePipe} from 'ng2-pipes/src/app/pipes/array/reverse'; + import {ReversePipe} from 'ngx-pipes/src/app/pipes/array/reverse'; @Component({ // .. From 30a49771a87f69f5cabd6d842c284cc4af2f4154 Mon Sep 17 00:00:00 2001 From: danrevah Date: Thu, 5 Jan 2017 00:35:17 +0200 Subject: [PATCH 08/10] Update package name --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f028633f..82e4450c 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ ], "repository": { "type": "git", - "url": "https://github.com/danrevah/ng-pipes.git" + "url": "https://github.com/danrevah/ngx-pipes.git" }, "devDependencies": { "@angular/common": "^2.1.0", From 1107a06bc6b8beeb0d72b23d80a7171e00eb4805 Mon Sep 17 00:00:00 2001 From: danrevah Date: Sat, 7 Jan 2017 17:58:01 +0200 Subject: [PATCH 09/10] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 82e4450c..94d2669e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ngx-pipes", - "version": "1.1.2", + "version": "1.1.3", "author": "Dan Revah", "description": "Useful angular2 pipes", "license": "MIT", From 54261bee228276140061e2aae8b6ca0d24e6d2a1 Mon Sep 17 00:00:00 2001 From: Patrick Kleckner Date: Thu, 12 Jan 2017 03:31:17 -0500 Subject: [PATCH 10/10] Added "Sum" to the Table of Contents (#14) A link to the "Sum" example was missing from the Table of Contents. I have added it in. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bd7a57fc..1120d775 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ - [Math](#math) - [min](#min) - [max](#max) + - [sum](#sum) - [percentage](#percentage) - [ceil](#ceil) - [floor](#floor)