84ca5969f2cb5469d3019907193bddc2581bc489
[platform/framework/web/crosswalk-tizen.git] /
1 /*! http://mths.be/endswith v0.2.0 by @mathias */
2 if (!String.prototype.endsWith) {
3         (function() {
4                 'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
5                 var defineProperty = (function() {
6                         // IE 8 only supports `Object.defineProperty` on DOM elements
7                         try {
8                                 var object = {};
9                                 var $defineProperty = Object.defineProperty;
10                                 var result = $defineProperty(object, object, object) && $defineProperty;
11                         } catch(error) {}
12                         return result;
13                 }());
14                 var toString = {}.toString;
15                 var endsWith = function(search) {
16                         if (this == null) {
17                                 throw TypeError();
18                         }
19                         var string = String(this);
20                         if (search && toString.call(search) == '[object RegExp]') {
21                                 throw TypeError();
22                         }
23                         var stringLength = string.length;
24                         var searchString = String(search);
25                         var searchLength = searchString.length;
26                         var pos = stringLength;
27                         if (arguments.length > 1) {
28                                 var position = arguments[1];
29                                 if (position !== undefined) {
30                                         // `ToInteger`
31                                         pos = position ? Number(position) : 0;
32                                         if (pos != pos) { // better `isNaN`
33                                                 pos = 0;
34                                         }
35                                 }
36                         }
37                         var end = Math.min(Math.max(pos, 0), stringLength);
38                         var start = end - searchLength;
39                         if (start < 0) {
40                                 return false;
41                         }
42                         var index = -1;
43                         while (++index < searchLength) {
44                                 if (string.charCodeAt(start + index) != searchString.charCodeAt(index)) {
45                                         return false;
46                                 }
47                         }
48                         return true;
49                 };
50                 if (defineProperty) {
51                         defineProperty(String.prototype, 'endsWith', {
52                                 'value': endsWith,
53                                 'configurable': true,
54                                 'writable': true
55                         });
56                 } else {
57                         String.prototype.endsWith = endsWith;
58                 }
59         }());
60 }