Skip to content

Commit bc88e5b

Browse files
committed
Introduce reps for grips
1 parent e76b6f4 commit bc88e5b

16 files changed

+1902
-0
lines changed

data/core/string.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* See license.txt for terms of usage */
2+
3+
"use strict";
4+
5+
define(function(require, exports, module) {
6+
7+
var Str = {};
8+
9+
Str.cropString = function(text, limit, alternativeText)
10+
{
11+
if (!alternativeText) {
12+
alternativeText = "...";
13+
}
14+
15+
// Make sure it's a string.
16+
text = String(text);
17+
18+
// Use default limit if necessary.
19+
if (!limit) {
20+
limit = 50;//prefs["stringCropLength"];
21+
}
22+
23+
// Crop the string only if a limit is actually specified.
24+
if (limit <= 0) {
25+
return text;
26+
}
27+
28+
// Set the limit at least to the length of the alternative text
29+
// plus one character of the original text.
30+
if (limit <= alternativeText.length) {
31+
limit = alternativeText.length + 1;
32+
}
33+
34+
var halfLimit = (limit - alternativeText.length) / 2;
35+
36+
if (text.length > limit) {
37+
return text.substr(0, Math.ceil(halfLimit)) + alternativeText +
38+
text.substr(text.length - Math.floor(halfLimit));
39+
}
40+
41+
return text;
42+
};
43+
44+
Str.escapeNewLines = function(value) {
45+
return value.replace(/\r/gm, "\\r").replace(/\n/gm, "\\n");
46+
};
47+
48+
Str.cropMultipleLines = function(text, limit) {
49+
return this.escapeNewLines(this.cropString(text, limit));
50+
};
51+
52+
exports.Str = Str;
53+
});

0 commit comments

Comments
 (0)