forked from as3/as3-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtruncate2.as
More file actions
36 lines (26 loc) · 802 Bytes
/
truncate2.as
File metadata and controls
36 lines (26 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package utils.string {
/**
* Returns a String truncated to a specified length with optional suffix.
* @param value Input String
* @param length Length the String should be shortened to
* @param suffix (optional, default="...") String to append to the end of the truncated String
* @returns String String truncated to a specified length with optional suffix
*/
public function truncate2(value:String, length:uint, suffix:String = "..."):String {
var out:String = "";
var l:uint = length;
if(value) {
l -= suffix.length;
var trunc:String = value;
if(trunc.length > l) {
trunc = trunc.substr(0, l);
if(/[^\s]/.test(value.charAt(l))) {
trunc = rtrim(trunc.replace(/\w+$|\s+$/, ""));
}
trunc += suffix;
}
out = trunc;
}
return out;
}
}