-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
wrap
pipe to add a prefix and a suffix to a string (#117)
* 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
1 parent
395b684
commit e2f633e
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 : ''); | ||
} | ||
} |