Skip to content

feat: add wrap pipe to add a prefix and a suffix to a string #117

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

Merged
merged 3 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- [match](#match)
- [lpad](#lpad)
- [rpad](#rpad)
- [wrap](#wrap)
- [Array](#Array)
- [diff](#diff)
- [flatten](#flatten)
Expand Down Expand Up @@ -349,6 +350,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: '');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be ok to have your project formatted with Prettier?

You wouldn't have to bother reviewing styles like that :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that suppose to act as a linter? how's it different from tslint?

}
}