forked from danrevah/ngx-pipes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
left pad and right pad string pipes (danrevah#12)
- Loading branch information
Showing
5 changed files
with
119 additions
and
1 deletion.
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
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'); | ||
}); | ||
|
||
}); |
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,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; | ||
} | ||
} |
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,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'); | ||
}); | ||
|
||
}); |
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,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; | ||
} | ||
} |