adding option parameter for setTruncateAtWordEnd

This commit is contained in:
goelvivek 2013-09-30 14:26:19 +05:30 коммит произвёл Andrew Naylor
Родитель 3ea665c55c
Коммит fa82c62c09
2 изменённых файлов: 29 добавлений и 1 удалений

Просмотреть файл

@ -235,6 +235,10 @@ Setting either of these properties to true will specify "content-available" in t
The value to specify for the `mdm` field where applicable.
### notification.truncateAtWordEnd
When this parameter is set and `notification#trim()` is called it will attempt to truncate the string at the nearest space.
### notification.setAlertText(alert)
Set the `aps.alert` text body. This will use the most space-efficient means.

Просмотреть файл

@ -26,6 +26,8 @@ function Notification (payload) {
this.mdm = undefined;
this.compiled = false;
this.truncateAtWordEnd = false;
}
/**
@ -51,6 +53,7 @@ Notification.prototype.clone = function (device) {
notification.newsstandAvailable = this.newsstandAvailable;
notification.contentAvailable = this.contentAvailable;
notification.mdm = this.mdm;
notification.truncateAtWordEnd = this.truncateAtWordEnd;
return notification;
};
@ -201,6 +204,16 @@ Notification.prototype.setMDM = function (mdm) {
return this;
};
/**
* Set the 'truncateAtWordEnd' flag for truncation logic
* @param {Boolean} [truncateAtWordEnd] Whether the truncateAtWordEnd flag should be set or not.
*/
Notification.prototype.setTruncateAtWordEnd = function (truncateAtWordEnd) {
this.truncateAtWordEnd = truncateAtWordEnd;
return this;
};
/**
* If an alert object doesn't already exist create it and transfer any existing message into the .body property
* @private
@ -276,11 +289,22 @@ Notification.prototype.compile = function () {
* @private
*/
Notification.prototype.truncateStringToLength = function (string, length) {
// Convert to a buffer and back to a string with the correct encoding to truncate the unicode series correctly.
var result = new Buffer(string, this.encoding || 'utf8').toString(this.encoding || 'utf8', 0, length);
if (this.truncateAtWordEnd === true) {
var lastSpaceIndexInResult = result.lastIndexOf(' ');
// Only truncate the string further if the remainder isn't a whole word
// which we can tell by checking the *next* character in the original string
if(lastSpaceIndexInResult != -1 && string.charAt(result.length + 1) != ' '){
result=result.substr(0,lastSpaceIndexInResult);
}
}
}
// since we might have chopped off the end of a multi-byte sequence, remove any
// invalid characters (represented as U+FFFD "REPLACEMENT CHARACTER") for UTF-8
// or orphaned lead surrogates for UTF-16 (UCS-2) - where only the tail surrogate
// or orphaned lead surrogates for UTF-16 (UCS-2) - where only the tail surrogate
// has been removed.
var done = false;
var encoding = this.encoding || 'utf8';