Skip to content

Commit

Permalink
Merge pull request #11 from spyr0s/pad-pipes
Browse files Browse the repository at this point in the history
Pad pipes
  • Loading branch information
danrevah authored Dec 29, 2016
2 parents 571970a + a645b9f commit 3c0515c
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ coverage/
npm-debug.log
testem.log
/typings
yarn-error.log

# e2e
/e2e/*.js
Expand Down
55 changes: 37 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Angular 2 - 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/ng2-pipes.svg?style=flat-square)](https://travis-ci.org/danrevah/ng2-pipes) [![Coveralls](https://img.shields.io/coveralls/danrevah/ng2-pipes.svg?style=flat-square)](https://coveralls.io/github/danrevah/ng2-pipes?branch=master)
# 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)

> Useful pipes for Angular2.
> Useful pipes for Angular 2+ with no external dependencies!
## Table of contents

Expand All @@ -24,6 +25,8 @@
- [underscore](#underscore)
- [test](#test)
- [match](#match)
- [lpad](#lpad)
- [rpad](#rpad)
- [Array](#Array)
- [diff](#diff)
- [flatten](#flatten)
Expand All @@ -40,7 +43,7 @@
- [every](#every)
- [some](#some)
- [sample](#sample)
- [groupBy](#groupBy)
- [groupBy](#groupby)
- [Object](#object)
- [keys](#keys)
- [values](#values)
Expand Down Expand Up @@ -261,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 @@ -304,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 Expand Up @@ -554,20 +581,12 @@ Returns object of grouped by items by discriminator

API: `array | groupBy: [string | Function]`

```javascript
import {GroupByPipe} from 'ng2-pipes/src/app/pipes/array/group-by';

@Component({
// ...
providers: [GroupByPipe]
})
export class AppComponent {
constructor(private groupByPipe: GroupByPipe) {
// ..
const arrayObject = [{elm: 'foo', value: 0}, {elm: 'bar', value: 1}, {elm: 'foo', value: 2}];
const groupedObject = groupByPipe.transform(arrayObject, 'elm'));
// `groupedObject` -> Contains: {foo: [{elm: 'foo', value: 0}, {elm: 'foo', value: 2}], bar: [{elm: 'bar', value: 1}]}
}
```typescript
this.arrayObject = [{elm: 'foo', value: 0}, {elm: 'bar', value: 1}, {elm: 'foo', value: 2}];
```

```html
<p>{{ arrayObject | groupBy: 'elm' }}</p> <!-- Output: "{foo: [{elm: 'foo', value: 0}, {elm: 'foo', value: 2}], bar: [{elm: 'bar', value: 1}]}" -->
```

## Object
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng2-pipes",
"version": "0.5.4",
"version": "1.0.0",
"author": "Dan Revah",
"description": "Useful angular2 pipes",
"license": "MIT",
Expand All @@ -27,7 +27,7 @@
],
"repository": {
"type": "git",
"url": "https://github.com/danrevah/ng2-pipes.git"
"url": "https://github.com/danrevah/ng-pipes.git"
},
"devDependencies": {
"@angular/common": "^2.1.0",
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 3c0515c

Please sign in to comment.