Browse Source

HUE-5433 [core] Add startsWith, endsWith and includes string polyfills to hue.utils

Enrico Berti 9 years ago
parent
commit
4a637d95bd

+ 43 - 4
desktop/core/src/desktop/static/desktop/js/hue.utils.js

@@ -14,7 +14,9 @@
 // See the License for the specific language governing permissions and
 // See the License for the specific language governing permissions and
 // limitations under the License.
 // limitations under the License.
 
 
-// Array polyfills for older browsers
+/*
+ * Array polyfills
+*/
 if (!('clean' in Array.prototype)) {
 if (!('clean' in Array.prototype)) {
   Array.prototype.clean = function (deleteValue) {
   Array.prototype.clean = function (deleteValue) {
     for (var i = 0; i < this.length; i++) {
     for (var i = 0; i < this.length; i++) {
@@ -26,8 +28,8 @@ if (!('clean' in Array.prototype)) {
     return this;
     return this;
   };
   };
 }
 }
-if (!('move' in Array.prototype)) {
 
 
+if (!('move' in Array.prototype)) {
   Array.prototype.move = function (old_index, new_index) {
   Array.prototype.move = function (old_index, new_index) {
     if (new_index >= this.length) {
     if (new_index >= this.length) {
       var k = new_index - this.length;
       var k = new_index - this.length;
@@ -39,8 +41,8 @@ if (!('move' in Array.prototype)) {
     return this;
     return this;
   };
   };
 }
 }
-if (!('indexOf' in Array.prototype)) {
 
 
+if (!('indexOf' in Array.prototype)) {
   Array.prototype.indexOf = function (needle) {
   Array.prototype.indexOf = function (needle) {
     for (var i = 0; i < this.length; i++) {
     for (var i = 0; i < this.length; i++) {
       if (this[i] === needle) {
       if (this[i] === needle) {
@@ -50,7 +52,6 @@ if (!('indexOf' in Array.prototype)) {
     return -1;
     return -1;
   };
   };
 }
 }
-// adding missing .filter for IE8
 
 
 if (!('filter' in Array.prototype)) {
 if (!('filter' in Array.prototype)) {
   Array.prototype.filter = function (filter, that /*opt*/) {
   Array.prototype.filter = function (filter, that /*opt*/) {
@@ -69,6 +70,44 @@ Array.prototype.diff = function (a) {
     return a.indexOf(i) < 0;
     return a.indexOf(i) < 0;
   });
   });
 };
 };
+
+/*
+ * String polyfills
+*/
+if (!String.prototype.startsWith) {
+  String.prototype.startsWith = function (searchString, position) {
+    position = position || 0;
+    return this.substr(position, searchString.length) === searchString;
+  };
+}
+
+if (!String.prototype.endsWith) {
+  String.prototype.endsWith = function (searchString, position) {
+    var subjectString = this.toString();
+    if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
+      position = subjectString.length;
+    }
+    position -= searchString.length;
+    var lastIndex = subjectString.lastIndexOf(searchString, position);
+    return lastIndex !== -1 && lastIndex === position;
+  };
+}
+
+if (!String.prototype.includes) {
+  String.prototype.includes = function (search, start) {
+    'use strict';
+    if (typeof start !== 'number') {
+      start = 0;
+    }
+
+    if (start + search.length > this.length) {
+      return false;
+    } else {
+      return this.indexOf(search, start) !== -1;
+    }
+  };
+}
+
 /*
 /*
  * Add utility methods to the HUE object
  * Add utility methods to the HUE object
 */
 */

+ 12 - 0
desktop/core/src/desktop/static/desktop/spec/hueUtilsSpec.js

@@ -23,5 +23,17 @@
     it("should show the milliseconds in number format if the time is less than 60 seconds", function() {
     it("should show the milliseconds in number format if the time is less than 60 seconds", function() {
       expect(Number(10123).toHHMMSS()).toEqual('10.123s');
       expect(Number(10123).toHHMMSS()).toEqual('10.123s');
     });
     });
+
+    it("should have the String.startsWith polyfill", function() {
+      expect('banana'.startsWith('ba')).toBeTruthy();
+    });
+
+    it("should have the String.endsWith polyfill", function() {
+      expect('banana'.endsWith('na')).toBeTruthy();
+    });
+
+    it("should have the String.includes polyfill", function() {
+      expect('banana'.includes('anan')).toBeTruthy();
+    });
   });
   });
 })();
 })();