Skip to content

Commit 470ffd6

Browse files
committed
Add the "plugin" prototype
1 parent c37138d commit 470ffd6

2 files changed

Lines changed: 160 additions & 0 deletions

File tree

prototype/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# plugin for JavaScript based objects #
2+
3+
This plugin adds on primitive Object (like string, number, array ...) additionnals methods
4+
5+
Nota bene: this plugin is AMD ready, and its name is "prototype"
6+
7+
List of additionnals methods
8+
------------
9+
10+
* string:
11+
- endWith(needle)
12+
- startWith(needle)
13+
- repeat(num)
14+
15+
* number:
16+
- toPaddedString(length, radix)
17+
18+
* array:
19+
- slice (to work on every browsers)
20+
- forEach(function)

prototype/prototype.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/* jshint define: false */
2+
3+
/**
4+
* @file This plugin adds on primitive Object (like string, number, array ...) additionnals methods
5+
* @version 1.0
6+
* @author Julien Roche
7+
* @copyright MIT
8+
*/
9+
10+
(function(){
11+
"use strict";
12+
13+
function definition($){
14+
/* String part */
15+
String.prototype.endWith = function (needle) {
16+
return this && this.match(needle + "$") == needle;
17+
};
18+
19+
String.prototype.repeat = function (num) {
20+
// return new Array(num + 1).join(this);
21+
var arr = [];
22+
arr.length = num + 1;
23+
return arr.join(this);
24+
};
25+
26+
String.prototype.startWith = function (needle) {
27+
return this && this.match("^" + needle) == needle;
28+
};
29+
30+
/* Number part */
31+
Number.prototype.toPaddedString = function (length, radix) {
32+
var string = this.toString(radix || 10), slength = string.length;
33+
for (var i = 0; i < (length - slength); i++) {
34+
string = "0" + string;
35+
}
36+
return string;
37+
};
38+
39+
/* Array part */
40+
41+
// See http://www.to-string.com/2012/05/29/fixing-splice-in-older-versions-of-internet-explorer-8-and-olders/
42+
if (document.documentMode && document.documentMode < 9) {
43+
// save original function of splice
44+
var originalSplice = Array.prototype.splice;
45+
46+
// provide a new implementation
47+
Array.prototype.splice = function() {
48+
49+
// since we can't modify 'arguments' array,
50+
// let's create a new one and copy all elements of 'arguments' into it
51+
var arr = [],
52+
i = 0,
53+
max = arguments.length;
54+
55+
for (; i < max; i++){
56+
arr.push(arguments[i]);
57+
}
58+
59+
// if this function had only one argument
60+
// compute 'deleteCount' and push it into arr
61+
if (arr.length==1) {
62+
arr.push(this.length - arr[0]);
63+
}
64+
65+
// invoke original splice() with our new arguments array
66+
return originalSplice.apply(this, arr);
67+
};
68+
}
69+
70+
// See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach
71+
if(!Array.prototype.forEach) {
72+
Array.prototype.forEach = function forEach(callback, thisArg) {
73+
var T, k;
74+
75+
if(this == null) {
76+
throw new TypeError("this is null or not defined");
77+
}
78+
79+
// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
80+
var O = Object(this);
81+
82+
// 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
83+
// 3. Let len be ToUint32(lenValue).
84+
var len = O.length >>> 0;
85+
// Hack to convert O.length to a UInt32
86+
87+
// 4. If IsCallable(callback) is false, throw a TypeError exception.
88+
// See: http://es5.github.com/#x9.11
89+
if( {}.toString.call(callback) !== "[object Function]") {
90+
throw new TypeError(callback + " is not a function");
91+
}
92+
93+
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
94+
if(thisArg) {
95+
T = thisArg;
96+
}
97+
98+
// 6. Let k be 0
99+
k = 0;
100+
101+
// 7. Repeat, while k < len
102+
while(k < len) {
103+
104+
var kValue;
105+
106+
// a. Let Pk be ToString(k).
107+
// This is implicit for LHS operands of the in operator
108+
// b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
109+
// This step can be combined with c
110+
// c. If kPresent is true, then
111+
if(Object.prototype.hasOwnProperty.call(O, k)) {
112+
113+
// i. Let kValue be the result of calling the Get internal method of O with argument Pk.
114+
kValue = O[k];
115+
116+
// ii. Call the Call internal method of callback with T as the this value and
117+
// argument list containing kValue, k, and O.
118+
callback.call(T, kValue, k, O);
119+
}
120+
// d. Increase k by 1.
121+
k++;
122+
}
123+
// 8. return undefined
124+
};
125+
}
126+
}
127+
128+
if (typeof module === "object" && typeof module.exports === "object") {
129+
// Node approach
130+
definition();
131+
132+
} else if (typeof define === "function" && define.amd) {
133+
// AMD approach
134+
define("prototype", [], definition);
135+
136+
} else if (window.jQuery) {
137+
// Classical way
138+
definition();
139+
}
140+
}());

0 commit comments

Comments
 (0)