-
-
Save fredyang/1920723 to your computer and use it in GitHub Desktop.
ECMAScript5.js : ES5 準拠のメソッドを定義するJavaScriptライブラリ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ECMAScript5.js (ECMA-262 Edition 5) | |
* | |
* @version 1.0 | |
* @author think49 | |
*/ | |
// 15.4.3.2 Array.isArray ( arg ) | |
// | |
// 1. If Type(arg) is not Object, return false. | |
// 2. If the value of the [[Class]] internal property of arg is "Array", then return true. | |
// 3. Return false. | |
if (typeof Array.isArray !== 'function') { | |
Array.isArray = function (arg) { | |
if (typeof arg !== 'object') { | |
return false; | |
} | |
if (Object.prototype.toString.call(arg) === '[object Array]') { // Object.prototype.toString outputs [[Class]] internal property | |
return true; | |
} | |
return false; | |
}; | |
} | |
// Array.prototype | |
(function () { | |
// 15.4.4.18 Array.prototype.forEach ( callbackfn [, thisArg] ) | |
// | |
// 1. Let O be the result of calling ToObject passing the this value as the argument. | |
// 2. Let lenValue be the result of calling the [[Get]] internal method of O with the argument "length". | |
// 3. Let len be ToUint32(lenValue). | |
// 4. If IsCallable(callbackfn) is false, throw a TypeError exception. | |
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined. | |
// 6. Let ke be 0. | |
// 7. Repeat, white k < len | |
// a. Let Pk be ToString(k) | |
// b. Let kPresent be the result of calling the [[HasProperty]] internal method of O with argument Pk. | |
// c. Let kPresent is true, then | |
// i. Let kValue be the result of calling the [[Get]] internal method of O with argument Pk. | |
// ii. Call the [[Call]] internal method of callbackfn with T as the this value and argument list containing kValue, k, and O. | |
// d. Increase k by 1. | |
// 8. Return. | |
if (typeof this.forEach !== 'function') { | |
this.forEach = function (callbackfn /*, thisArg*/) { | |
var thisArg, O, k, len; | |
if ('function' !== typeof callbackfn) { | |
throw new TypeError(callbackfn + ' is not a function'); | |
} | |
O = Object(this); // ToObject(this) | |
len = Number(O.length); // ToUint32(lenValue) | |
thisArg = arguments[1]; | |
if (arguments.length < 2) { // for "strict mode" | |
thisArg = this; | |
} | |
for (k = 0, len = O.length; k < len; k++) { | |
if (k in O) { | |
callbackfn.call(thisArg, O[k], k, O); | |
} | |
} | |
}; | |
} | |
// 15.4.4.17 Array.prototype.some ( callbackfn [ , thisArg ] ) | |
// | |
// 1. Let O be the result of calling ToObject passing the this value as the argument. | |
// 2. Let lenValue be the result of calling the [[Get]] internal method of O with the argument "length". | |
// 3. Let len be ToUint32(lenValue). | |
// 4. If IsCallable(callbackfn) is false, throw a TypeError exception. | |
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined. | |
// 6. Let k be 0. | |
// 7. Repeat, while k < len | |
// a. Let Pk be ToString(k). | |
// b. Let kPresent be the result of calling the [[HasProperty]] internal method of O with argument Pk. | |
// c. If kPresent is true, then | |
// i. Let kValue be the result of calling the [[Get]] internal method of O with argument Pk. | |
// ii. Let testResult be the result of calling the [[Call]] internal method of callbackfn with T as the this value and argument list containing kValue, k, and O. | |
// iii. If ToBoolean(testResult) is true, return true. | |
// d. Increase k by 1. | |
// 8. Return false. | |
if (typeof this.some !== 'function') { | |
this.some = function (callbackfn, thisArg) { | |
var O, testResult, k, len; | |
if (typeof callbackfn !== 'function') { | |
throw new TypeError(callbackfn + ' is not a function'); | |
} | |
O = Object(this); // ToObject(this) | |
len = Number(O.length); // ToUint32(lenValue) | |
for (k = 0; k < len; k++) { | |
if (k in O) { | |
if (thisArg) { // for "strict mode" | |
testResult = callbackfn.call(thisArg, O[k], k, O); | |
} else { | |
testResult = callbackfn(O[k], k, O); | |
} | |
if (testResult) { | |
return true; | |
} | |
} | |
} | |
return false; | |
}; | |
} | |
}).call(Array.prototype); | |
// String.prototype | |
(function () { | |
// Unicode 5.2.0 Property-List ( http://www.unicode.org/Public/5.2.0/ucd/PropList.txt ) | |
/* | |
var White_Space = { | |
Cc: '\u0009-\u000D\u0085', | |
Zs: '\u0020\u00A0\u1680\u180E\u2000-\u200A\u202F\u205F\u3000', | |
Zl: '\u2028', | |
Zp: '\u2029' | |
}; | |
*/ | |
// 15.5.4.20 String.prototype.trim() | |
// | |
// 1. Call CheckObjectCoercible passing the this value as its argument. | |
// 2. Let S be the result of calling ToString, giving it the this value as its argument. | |
// 3. Let T be a String value that is a copy of S with both leading and trailing white space removed. The definition of white space is the union of WhiteSpace (*1) and LineTerminator(*2). | |
// 4. Return T. | |
// | |
// (*1) 7.2 White Space | |
// (*2) 7.3 Line Terminators | |
// | |
// | |
// Unicode 5.2.0 Property-List ( http://www.unicode.org/Public/5.2.0/ucd/PropList.txt ) | |
// | |
// 0009..000D ; White_Space # Cc [5] <control-0009>..<control-000D> | |
// 0020 ; White_Space # Zs SPACE | |
// 0085 ; White_Space # Cc <control-0085> | |
// 00A0 ; White_Space # Zs NO-BREAK SPACE | |
// 1680 ; White_Space # Zs OGHAM SPACE MARK | |
// 180E ; White_Space # Zs MONGOLIAN VOWEL SEPARATOR | |
// 2000..200A ; White_Space # Zs [11] EN QUAD..HAIR SPACE | |
// 2028 ; White_Space # Zl LINE SEPARATOR | |
// 2029 ; White_Space # Zp PARAGRAPH SEPARATOR | |
// 202F ; White_Space # Zs NARROW NO-BREAK SPACE | |
// 205F ; White_Space # Zs MEDIUM MATHEMATICAL SPACE | |
// 3000 ; White_Space # Zs IDEOGRAPHIC SPACE | |
if (typeof this.trim !== 'function') { | |
this.trim = function () { | |
var S = String(this), | |
WhiteSpace = '\u0009\u000B\u000C\u0020\u00A0\uFEFF', | |
Zs = '\u1680\u180E\u2000-\u200A\u202F\u205F\u3000', // Unicode 5.2.0 White_Space#Zs besides the WhiteSpace | |
LineTerminator = '\u000A\u000D\u2028\u2029', | |
whitespaces = '[' + WhiteSpace + Zs + LineTerminator + ']+'; | |
return S.replace(new RegExp('^' + whitespaces + '|' + whitespaces + '$', 'g'), ''); | |
}; | |
} | |
}).call(String.prototype); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment