forked from shmuelie/Humanizer.Js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
truncateExtensions.ts
61 lines (57 loc) · 1.63 KB
/
truncateExtensions.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
interface String
{
truncate(length: number): string;
truncate(length: number, truncator: Humanizer.ITruncator, from?: Humanizer.TruncateFrom): string;
truncate(length: number, truncationString: string, from?: Humanizer.TruncateFrom): string;
truncate(length: number, truncationString: string, truncator: Humanizer.ITruncator, from?: Humanizer.TruncateFrom): string;
}
module Humanizer
{
"use strict";
/**
* Truncation location for humanizer
* @enum
* @readonly
*/
export enum TruncateFrom
{
/** Truncate letters from the left (start) of the string */
Left,
/** Truncate letters from the right (end) of the string */
Right
}
String.prototype.truncate = function (): string
{
var length: number = arguments[0];
var truncationString: string = "…";
var from: TruncateFrom = TruncateFrom.Right;
var truncator: ITruncator = Truncator.FixedLength;
if (arguments.length > 1)
{
if (typeof arguments[1] === "string")
{
truncationString = arguments[1];
}
else
{
truncator = arguments[1];
}
}
if (arguments.length > 2)
{
if (typeof arguments[2] === "number")
{
from = arguments[2];
}
else
{
truncator = arguments[2];
}
}
if (arguments.length > 3)
{
from = arguments[3];
}
return truncator.truncate(this, length, truncationString, from);
};
}