Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix Notification.trim() for alerts with multibyte chars.

  • Loading branch information...
commit ad3135bb710ba0c2fd4dd89907cd4ddae57c4337 1 parent 0ae733f
@mmaelzer mmaelzer authored committed
Showing with 15 additions and 2 deletions.
  1. +15 −2 lib/notification.js
View
17 lib/notification.js
@@ -159,7 +159,7 @@ Notification.prototype.trim = function() {
if (length < tooLong) {
return length - tooLong;
}
- this.alert = this.alert.substring(0, length - tooLong);
+ this.alert = this.truncateStringToBytes(this.alert, length - tooLong);
return tooLong;
}
else if(typeof this.alert == "object" && typeof this.alert.body == "string") {
@@ -167,7 +167,7 @@ Notification.prototype.trim = function() {
if (length < tooLong) {
return length - tooLong;
}
- this.alert.body = this.alert.body.substring(0, length - tooLong);
+ this.alert.body = this.truncateStringToBytes(this.alert.body, length - tooLong);
return tooLong;
}
return -tooLong;
@@ -189,6 +189,19 @@ Notification.prototype.compile = function () {
/**
* @private
+ */
+Notification.prototype.truncateStringToBytes = function(string, bytes) {
+ var truncated = string.substring(0, bytes);
+
+ while (Buffer.byteLength(truncated, this.encoding || 'utf8') > bytes) {
+ truncated = truncated.substring(0, truncated.length - 1);
+ }
+
+ return truncated;
+}
+
+/**
+ * @private
*/
Notification.prototype.toJSON = function () {
if (this.payload === undefined) {
Please sign in to comment.
Something went wrong with that request. Please try again.