Skip to content

Instantly share code, notes, and snippets.

@rikuba
Last active December 14, 2015 22:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikuba/5158969 to your computer and use it in GitHub Desktop.
Save rikuba/5158969 to your computer and use it in GitHub Desktop.
define generic methods
(function /*defineArrayGenericMethods*/(methods) {
var defineProperty = Object.defineProperty,
name;
if (defineProperty) {
for (name in methods) if (methods.hasOwnProperty(name)) {
if (!Array.hasOwnProperty(name)) {
defineProperty(Array, name, {
value: methods[name],
configurable: true,
enumerable: false,
writable: true
});
}
}
} else {
for (name in methods) if (methods.hasOwnProperty(name)) {
if (!Array.hasOwnProperty(name)) {
Array[name] = methods[name];
}
}
}
})((function () {
var proto = Array.prototype,
call = Function.prototype.call,
_array = [],
_concat = proto.concat,
_join = proto.join,
_pop = proto.pop,
_push = proto.push,
_reverse = proto.reverse,
_shift = proto.shift,
_slice = proto.slice,
_sort = proto.sort,
_splice = proto.splice,
_unshift = proto.unshift,
_indexOf = proto.indexOf,
_lastIndexOf = proto.lastIndexOf,
_every = proto.every,
_some = proto.some,
_forEach = proto.forEach,
_map = proto.map,
_filter = proto.filter,
_reduce = proto.reduce,
_reduceRight = proto.reduceRight;
return {
concat: function concat(array, item1) {
// http://d.hatena.ne.jp/rikuba/20130509/1368065880
return _concat.apply(_array, arguments);
},
join: function join(array, separator) {
return _join.call(array, separator);
},
pop: function pop(array) {
return _pop.call(array);
},
push: function push(array, item1) {
return call.apply(_push, arguments);
},
reverse: function reverse(array) {
return _reverse.call(array);
},
shift: function shift(array) {
return _shift.call(array);
},
slice: function slice(array, start, end) {
return _slice.call(array, start, end);
},
sort: function sort(array, comparefn) {
return _sort.call(array, comparefn);
},
splice: function splice(array, start, deleteCount) {
return call.apply(_splice, arguments);
},
unshift: function unshift(array, item1) {
return call.apply(_unshift, arguments);
},
indexOf: function indexOf(array, searchElement/*, fromIndex*/) {
return _indexOf.call(array, searchElement, arguments[2]);
},
lastIndexOf: function lastIndexOf(array, searchElement/*, fromIndex*/) {
return _lastIndexOf.call(array, searchElement, arguments[2]);
},
every: function every(array, callbackfn/*, thisArg*/) {
return _every.call(array, callbackfn, arguments[2]);
},
some: function some(array, callbackfn/*, thisArg*/) {
return _some.call(array, callbackfn, arguments[2]);
},
forEach: function forEach(array, callbackfn/*, thisArg*/) {
_forEach.call(array, callbackfn, arguments[2]);
},
map: function map(array, callbackfn/*, thisArg*/) {
return _map.call(array, callbackfn, arguments[2]);
},
filter: function filter(array, callbackfn/*, thisArg*/) {
return _filter.call(array, callbackfn, arguments[2]);
},
reduce: function reduce(array, callbackfn/*, initialValue*/) {
return _reduce.call(array, callbackfn, arguments[2]);
},
reduceRight: function reduceRight(array, callbackfn/*, initialValue*/) {
return _reduceRight.call(array, callbackfn, arguments[2]);
}
};
})());
(function /*defineStringGenericMethods*/(methods) {
var defineProperty = Object.defineProperty,
name;
if (defineProperty) {
for (name in methods) if (methods.hasOwnProperty(name)) {
if (!String.hasOwnProperty(name)) {
defineProperty(String, name, {
value: methods[name],
configurable: true,
enumerable: false,
writable: true
});
}
}
} else {
for (name in methods) if (methods.hasOwnProperty(name)) {
if (!String.hasOwnProperty(name)) {
String[name] = methods[name];
}
}
}
})((function () {
var proto = String.prototype,
call = Function.prototype.call,
_charAt = proto.charAt,
_charCodeAt = proto.charCodeAt,
_concat = proto.concat,
_indexOf = proto.indexOf,
_lastIndexOf = proto.lastIndexOf,
_localeCompare = proto.localeCompare,
_match = proto.match,
_replace = proto.replace,
_search = proto.search,
_slice = proto.slice,
_split = proto.split,
_substring = proto.substring,
_toLowerCase = proto.toLowerCase,
_toLocaleLowerCase = proto.toLocaleLowerCase,
_toUpperCase = proto.toUpperCase,
_toLocaleUpperCase = proto.toLocaleUpperCase,
_trim = proto.trim,
_substr = proto.substr;
return {
charAt: function charAt(string, pos) {
return _charAt.call(string, pos);
},
charCodeAt: function charCodeAt(string, pos) {
return _charCodeAt.call(string, pos);
},
concat: function concat(string, string1) {
return call.apply(_concat, arguments);
},
indexOf: function indexOf(string, searchString/*, position*/) {
return _indexOf.call(string, searchString, arguments[2]);
},
lastIndexOf: function lastIndexOf(string, searchString/*, position*/) {
return _lastIndexOf.call(string, searchString, arguments[2]);
},
localeCompare: function localeCompare(string, that) {
return _localeCompare.call(string, that);
},
match: function match(string, regexp) {
return _match.call(string, regexp);
},
replace: function replace(string, searchValue, replaceValue) {
return _replace.call(string, searchValue, replaceValue);
},
search: function search(string, regexp) {
return _search.call(string, regexp);
},
slice: function slice(string, start, end) {
return _slice.call(string, start, end);
},
split: function split(string, separator, limit) {
return _split.call(string, separator, limit);
},
substring: function substring(string, start, end) {
return _substring.call(string, start, end);
},
toLowerCase: function toLowerCase(string) {
return _toLowerCase.call(string);
},
toLocaleLowerCase: function toLocaleLowerCase(string) {
return _toLocaleLowerCase.call(string);
},
toUpperCase: function toUpperCase(string) {
return _toUpperCase.call(string);
},
toLocaleUpperCase: function toLocaleUpperCase(string) {
return _toLocaleUpperCase.call(string);
},
trim: function trim(string) {
return _trim.call(string);
},
substr: function substr(string, start, length) {
return _substr.call(string, start, length);
}
};
})());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment