Skip to content

Commit

Permalink
feat: add wrap pipe to add a prefix and a suffix to a string (#117)
Browse files Browse the repository at this point in the history
* feat: add `wrap` pipe to add a prefix and a suffix to a string

This closes #112

* Update wrap.ts

* Update wrap.ts

spacing
  • Loading branch information
maxime1992 authored and danrevah committed Jul 12, 2018
1 parent 395b684 commit e2f633e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- [match](#match)
- [lpad](#lpad)
- [rpad](#rpad)
- [wrap](#wrap)
- [Array](#Array)
- [diff](#diff)
- [flatten](#flatten)
Expand Down Expand Up @@ -361,6 +362,17 @@ Right pad a string to a given length using a given pad character (default is a
<p>{{'Foo' | rpad: 5: '#'}}</p> <!-- Output: "Foo##" -->
```

### wrap

Wrap a string between a prefix and a suffix


**Usage:** `string | wrap: prefix: suffix`

```html
<p>{{'Foo' | wrap: 'nice prefix ': ' and awesome suffix!'}}</p> <!-- Output: "nice prefix Foo and awesome suffix!" -->
```

## Array

### diff
Expand Down
1 change: 1 addition & 0 deletions src/pipes/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ export {MatchPipe} from './match';
export {TestPipe} from './test';
export {LeftPadPipe} from './lpad';
export {RightPadPipe} from './rpad';
export {WrapPipe} from './wrap';
37 changes: 37 additions & 0 deletions src/pipes/string/wrap.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { WrapPipe } from './wrap';

describe('WrapPipe Tests', () => {
let pipe: WrapPipe;

beforeEach(() => {
pipe = new WrapPipe();
});

it('Should not do anything if main text is not a string', () => {
expect(pipe.transform(null)).toEqual(null);
expect(pipe.transform(undefined)).toEqual(undefined);
expect(pipe.transform(42 as any)).toEqual(42 as any);
expect(pipe.transform({name: 'foo'} as any)).toEqual({name: 'foo'} as any);
});

it('Should skip the prefix if prefix text is not a string', () => {
expect(pipe.transform('main text', undefined)).toEqual('main text');
expect(pipe.transform('main text', undefined)).toEqual('main text');
expect(pipe.transform('main text', 42 as any)).toEqual('main text');
expect(pipe.transform('main text', {name: 'foo'} as any)).toEqual('main text');
});

it('Should skip the suffix if suffix text is not a string', () => {
expect(pipe.transform('main text', 'great prefix ', undefined)).toEqual('great prefix main text');
expect(pipe.transform('main text', 'great prefix ', undefined)).toEqual('great prefix main text');
expect(pipe.transform('main text', 'great prefix ', 42 as any)).toEqual('great prefix main text');
expect(pipe.transform('main text', 'great prefix ', {name: 'foo'} as any)).toEqual('great prefix main text');
});

it('Should wrap properly', () => {
expect(pipe.transform('main text')).toEqual('main text');
expect(pipe.transform('main text', 'great prefix ', ' awesome suffix')).toEqual('great prefix main text awesome suffix');
expect(pipe.transform('main text', 'only prefix ')).toEqual('only prefix main text');
expect(pipe.transform('main text', undefined, ' only suffix')).toEqual('main text only suffix');
});
});
15 changes: 15 additions & 0 deletions src/pipes/string/wrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Pipe, PipeTransform } from '@angular/core';
import { isString } from '../helpers/helpers';

@Pipe({name: 'wrap'})
export class WrapPipe implements PipeTransform {
transform(str: string, prefix: string = '', suffix: string = ''): string {
if (!isString(str)) {
return str;
}

return (!!prefix && isString(prefix) ? prefix : '') +
str +
(!!suffix && isString(suffix) ? suffix : '');
}
}

0 comments on commit e2f633e

Please sign in to comment.