Skip to content

Commit efe6ffe

Browse files
committed
Adds truncate filter.
1 parent ea8ea4a commit efe6ffe

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

demo.html

+13-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ <h3>reverse</h3>
2424
<h3>replace('World','Peeps')</h3>
2525
{{'Hello World' | replace('World','Peeps')}}
2626

27+
<h3>truncate(8, true)</h3>
28+
{{'Lorem Catsum Itsum' | truncate(8, true) }}
29+
2730
<h3>startsWith(T)</h3>
2831
<template repeat="{{friend in friends | startsWith('T')}}">
2932
{{ friend.name }}
@@ -83,15 +86,20 @@ <h3>trim</h3>
8386
template.colors = ['red','green','blue','yellow'];
8487
template.greeting = 'Hello World';
8588
template.friends = [{
86-
name: 'Matt'
89+
name: 'Matt',
90+
dinner: 'pizza'
8791
}, {
88-
name: 'Paul'
92+
name: 'Paul',
93+
dinner: 'sushi'
8994
}, {
90-
name: 'Eric'
95+
name: 'Eric',
96+
dinner: 'hummus'
9197
}, {
92-
name: 'Rob'
98+
name: 'Rob',
99+
dinner: 'burger'
93100
}, {
94-
name: 'Timothy'
101+
name: 'Timothy',
102+
dinner: 'pasta'
95103
}];
96104
</script>
97105
</body>

filter-truncate.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Return a truncated version of a string
3+
* @param {string} input
4+
* @param {integer} length
5+
* @param {boolean} killwords
6+
* @param {string} end
7+
* @return {string}
8+
*/
9+
PolymerExpressions.prototype.truncate = function (input, length, killwords, end) {
10+
var orig = input;
11+
length = length || 255;
12+
13+
if (input.length <= length)
14+
return input;
15+
16+
if (killwords) {
17+
input = input.substring(0, length);
18+
} else {
19+
var idx = input.lastIndexOf(' ', length);
20+
if (idx === -1) {
21+
idx = length;
22+
}
23+
24+
input = input.substring(0, idx);
25+
}
26+
27+
input += (end !== undefined && end !== null) ? end : '...';
28+
return input;
29+
};

polymer-filters.html

+16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<script src="filter-wordcount.js"></script>
1212
<script src="filter-random.js"></script>
1313
<script src="filter-round.js"></script>
14+
<script src="filter-truncate.js"></script>
1415
<script src="filter-trim.js"></script>
1516

1617
<!--
@@ -48,6 +49,20 @@
4849
4950
> I was a string with leading and trailing whitespace
5051
52+
####Truncate
53+
54+
{{'Lorem Catsum Itsum' | truncate(8) }}
55+
56+
> Lorem...
57+
58+
{{'Lorem Catsum Itsum' | truncate(8, true) }}
59+
60+
> Lorem Ca...
61+
62+
{{'Lorem Catsum Itsum' | truncate(8, true, ' [READ MORE]') }}
63+
64+
> Lorem Ca [READ MORE]
65+
5166
####Date
5267
5368
{{1288323623006 | date('yyyy-MM-dd HH:mm:ss Z')}}
@@ -115,5 +130,6 @@
115130
@element polymer-filters
116131
@blurb A collection of Polymer filters for formatting values of expressions for display to users
117132
@homepage http://addyosmani.github.io/polymer-filters
133+
@inspiration http://jinja.pocoo.org/docs/templates/
118134
-->
119135

0 commit comments

Comments
 (0)