Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next #12

Merged
merged 2 commits into from
Dec 29, 2016
Merged

Next #12

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}