Skip to content

Commit

Permalink
feat(Pipes): truncate pipe is available
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #1249
  • Loading branch information
shani-terminus authored and benjamincharity committed Mar 6, 2019
1 parent 6594bd7 commit 0b68fdd
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 25 deletions.
69 changes: 45 additions & 24 deletions demo/app/components/pipes/pipes.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ <h2>Date</h2>

<ts-card tsVerticalSpacing>

<h2>TimeAgo</h2>
<h2>RoundNumber</h2>

<pre tsVerticalSpacing>
Date:
{{ date }}

Old date:
{{ oldDate }}
Value:
3456.3456
</pre>

<div tsVerticalSpacing>
{{ date | tsTimeAgo:oldDate }} since I did any work
<div>
Round to 2: <b>{{ 3456.3456 | tsRoundNumber:2 }}</b>
<br>
Round to 0: <b>{{ 3456.3456 | tsRoundNumber }}</b>
<br>
Round to -2: <b>{{ 3456.3456 | tsRoundNumber:-2 }}</b>
</div>

</ts-card>
Expand All @@ -78,54 +79,74 @@ <h2>SentenceCase</h2>

<ts-card tsVerticalSpacing>

<h2>Usage in Class</h2>
<h2>TimeAgo</h2>

(See the associated .ts file)
<pre tsVerticalSpacing>
Date:
{{ date }}

Old date:
{{ oldDate }}
</pre>

<div tsVerticalSpacing>
{{ date | tsTimeAgo:oldDate }} since I did any work
</div>

</ts-card>


<ts-card tsVerticalSpacing>

<h2>TitleCase</h2>

<pre tsVerticalSpacing>
String: {{ myString }}
String 1: Careful man, there’s a beverage here!
String 2: THAT RUG REALLY TIED THE ROOM TOGETHER.
</pre>

<div>
{{ myStringTransformed }}
{{ 'Careful man, there’s a beverage here!' | tsTitleCase }}
<br>
{{ 'THAT RUG REALLY TIED THE ROOM TOGETHER.' | tsTitleCase }}
</div>

</ts-card>


<ts-card tsVerticalSpacing>

<h2>RoundNumber</h2>
<h2>TruncateAt</h2>

<pre tsVerticalSpacing>
Value:
3456.3456
'Here is my string' | tsTruncateAt:7
'Here is my string' | tsTruncateAt:8:'middle'
'Here is my string' | tsTruncateAt:6:'start'
</pre>

<div>
Round to 2: <b>{{ 3456.3456 | tsRoundNumber:2 }}</b>
{{ 'Here is my string' | tsTruncateAt:7 }}
<br>
Round to 0: <b>{{ 3456.3456 | tsRoundNumber }}</b>
{{ 'Here is my string' | tsTruncateAt:8:'middle' }}
<br>
Round to -2: <b>{{ 3456.3456 | tsRoundNumber:-2 }}</b>
{{ 'Here is my string' | tsTruncateAt:6:'start' }}
</div>

</ts-card>


<ts-card tsVerticalSpacing>

<h2>TitleCase</h2>
<h2>Usage in Class</h2>

(See the associated .ts file)

<pre tsVerticalSpacing>
String 1: Careful man, there’s a beverage here!
String 2: THAT RUG REALLY TIED THE ROOM TOGETHER.
String: {{ myString }}
</pre>

<div>
{{ 'Careful man, there’s a beverage here!' | tsTitleCase }}
<br>
{{ 'THAT RUG REALLY TIED THE ROOM TOGETHER.' | tsTitleCase }}
{{ myStringTransformed }}
</div>

</ts-card>
5 changes: 5 additions & 0 deletions terminus-ui/pipes/src/pipes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { TsRoundNumberPipe } from './round-number/round-number.pipe';
import { TsSentenceCasePipe } from './sentence-case/sentence-case.pipe';
import { TsTimeAgoPipe } from './time-ago/time-ago.pipe';
import { TsTitleCasePipe } from './title-case/title-case.pipe';
import { TsTruncateAtPipe } from './truncate/truncate.pipe';

export * from './date/date.pipe';
export * from './round-number/round-number.pipe';
export * from './sentence-case/sentence-case.pipe';
export * from './time-ago/time-ago.pipe';
export * from './title-case/title-case.pipe';
export * from './truncate/truncate.pipe';


@NgModule({
Expand All @@ -24,20 +26,23 @@ export * from './title-case/title-case.pipe';
TsSentenceCasePipe,
TsTimeAgoPipe,
TsTitleCasePipe,
TsTruncateAtPipe,
],
providers: [
TsDatePipe,
TsRoundNumberPipe,
TsSentenceCasePipe,
TsTimeAgoPipe,
TsTitleCasePipe,
TsTruncateAtPipe,
],
declarations: [
TsDatePipe,
TsRoundNumberPipe,
TsSentenceCasePipe,
TsTimeAgoPipe,
TsTitleCasePipe,
TsTruncateAtPipe,
],
})
export class TsPipesModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe(`TsSentenceCasePipe`, function() {
});


test(`should format a date`, () => {
test(`should format a string`, () => {
const strings: string[] = [
'HELLO THERE',
'hi there friend',
Expand Down
71 changes: 71 additions & 0 deletions terminus-ui/pipes/src/truncate/truncate.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { TsTruncateAtPipe } from './truncate.pipe';

describe(`TsTruncateAtPipe`, function() {
let pipeClass: TsTruncateAtPipe;
let pipe: Function;
const inputString = 'This is my test string';
const ellipses = '\u2026';

beforeEach(() => {
pipeClass = new TsTruncateAtPipe();
pipe = pipeClass.transform;
});

test(`should return undefined if no value is passed in`, () => {
expect(pipe(null)).toEqual(undefined);
expect(pipe('')).toEqual(undefined);
});

test(`should return the string if no parameters are passed`, () => {
expect(pipe(inputString)).toEqual('This is my test string');
});

test(`should return the string if string length is shorter than character count`, () => {
expect(pipe(inputString, 24)).toEqual('This is my test string');
});

test(`should return warning if string is passed instead of number`, () => {
window.console.warn = jest.fn();
pipe(inputString, 'start');

expect(window.console.warn).toHaveBeenCalled();
});

test(`should default to end if no position is specified`, () => {
expect(pipe(inputString, 10, 'undefined as any')).toEqual(`This is m${ellipses}`);
});


describe(`end`, () => {
test(`should be the format if no position is specified`, () => {
expect(pipe(inputString, 10)).toEqual(`This is m${ellipses}`);
expect(pipe(inputString, 10)).toHaveLength(10);
});

test(`should remove last part of the string`, () => {
expect(pipe(inputString, 10, 'end')).toEqual(`This is m${ellipses}`);
expect(pipe(inputString, 10, 'end')).toHaveLength(10);
});
});


describe(`start`, () => {
test(`should remove first part of the string`, () => {
expect(pipe(inputString, 10, 'start')).toEqual(`${ellipses}st string`);
expect(pipe(inputString, 10, 'start')).toHaveLength(10);
});
});


describe(`middle`, () => {
test(`should have equal number of characters on each side of ellipses if character count is odd`, () => {
expect(pipe(inputString, 11, 'middle')).toEqual(`This ${ellipses}tring`);
expect(pipe(inputString, 11, 'middle')).toHaveLength(11);
});

test(`should have one more characters in front of ellipses if character count is even`, () => {
expect(pipe(inputString, 10, 'middle')).toEqual(`This ${ellipses}ring`);
expect(pipe(inputString, 10, 'middle')).toHaveLength(10);
});
});
});
87 changes: 87 additions & 0 deletions terminus-ui/pipes/src/truncate/truncate.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {
Pipe,
PipeTransform,
} from '@angular/core';
import { isNumber } from '@terminus/ngx-tools';



/**
* Define the accepted string values for the {@link TsTruncateAtPipe} position
*/
export type TsTruncatePositionType
= 'start'
| 'middle'
| 'end'
;

/**
* Define the allowed truncation position types Used by {@link TsTruncateAtPipe} position
*/
export const allowedTruncationTypes: TsTruncatePositionType[] = [
'start',
'middle',
'end',
];


/**
* The truncate at pipe
*
* @example
* {{ 'Here is my string' | tsTruncateAt:7 }} // Outputs: `Here i…`
* {{ 'Here is my string' | tsTruncateAt:8:'middle' }} // Outputs: `Here…ing`
*
* <example-url>https://getterminus.github.io/ui-demos-master/components/pipes</example-url>
*/
@Pipe({
name: 'tsTruncateAt',
})
export class TsTruncateAtPipe implements PipeTransform {
transform(value: string, charCount: number = 0, position: TsTruncatePositionType = 'end'): string | undefined {
// Check for null values to avoid issues during data-binding
if (!value) {
return;
}

// Insure the correct type
if (!isNumber(charCount)) {
console.warn(`${charCount} is not a number.`);
}

if ((value.length < charCount) || (charCount < 1)) {
return value;
}

let newString = value;
const ellipses = '\u2026';

switch (position) {
case('start'):
newString = ellipses + value.slice(-(charCount - 1));
break;
case('middle'):
let charCountStart: number;
let charCountEnd: number;
// determine how many characters are on each side of the split
// if there are an odd number of characters, the beginning of the string is longer
if ((charCount - 1) % 2 === 1) {
charCountEnd = (charCount - 1) / 2;
charCountStart = charCountEnd + 1;
} else {
charCountEnd = (charCount - 1) / 2;
charCountStart = charCountEnd;
}
newString = value.slice(0, charCountStart) + ellipses + value.slice(-(charCountEnd));
break;
case('end'):
newString = value.slice(0, charCount - 1) + ellipses;
break;
default:
newString = value.slice(0, charCount - 1) + ellipses;
break;
}

return newString;
}
}

0 comments on commit 0b68fdd

Please sign in to comment.