Skip to content

Commit

Permalink
left pad and right pad string pipes (danrevah#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah authored Dec 29, 2016
1 parent ecdf9e6 commit 9907eee
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
- [underscore](#underscore)
- [test](#test)
- [match](#match)
- [lpad](#lpad)
- [rpad](#rpad)
- [Array](#Array)
- [diff](#diff)
- [flatten](#flatten)
Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -305,6 +307,30 @@ API: `string | match: {RegExp}: {Flags}`
<p>{{'FOO' | match: '^foo': 'i' }}</p> <!-- Output: 'FOO' -->
```

### 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
<p>{{'foo' | lpad: 5}}</p> <!-- Output: " foo" -->
<!-- Cast a number to string in order to left pad it with zeros -->
<p>{{String(3) | lpad: 5: '0'}}</p> <!-- Output: "00003" -->
```

### 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
<p>{{'Foo' | rpad: 5: '#'}}</p> <!-- Output: "Foo##" -->
```

## Array

### diff
Expand Down
30 changes: 30 additions & 0 deletions src/app/pipes/string/lpad.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});

});
16 changes: 16 additions & 0 deletions src/app/pipes/string/lpad.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
30 changes: 30 additions & 0 deletions src/app/pipes/string/rpad.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});

});
16 changes: 16 additions & 0 deletions src/app/pipes/string/rpad.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 9907eee

Please sign in to comment.