From 9907eee9bc468136f40aad92a6e283b93e18b0a6 Mon Sep 17 00:00:00 2001 From: Dan Revah Date: Thu, 29 Dec 2016 11:26:30 +0200 Subject: [PATCH] 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; + } +}