Skip to content

Instantly share code, notes, and snippets.

RegExp.prototype.matches = function* (str) {
let moreThanOnce = this.global || this.sticky;
let myLastIndex = 0;
do {
// preserve lastIndex of another .exec() calls on same regexp
let savedLastIndex = this.lastIndex;
// use own state for lastIndex to match our str
this.lastIndex = myLastIndex;
let match = this.exec(str);
@abozhilov
abozhilov / gist:3895019
Created October 15, 2012 20:08
Partial application Value range
function valueRange(start, end) {
return function (value) {
return Math.min(Math.max(value, start), end);
};
}
//Get new value range [0, 20]
var f = valueRange(0, 20);
console.log(f(10)); //10
@abozhilov
abozhilov / gist:1333507
Created November 2, 2011 12:32
Arguments default value
function func(a, f) {
return function (args) {
args.__proto__ = a;
f.call(this, args);
};
};
var f = func({foo : 10, bar : 20}, function (args) {
print(args.foo, args.bar);
@abozhilov
abozhilov / gist:1170259
Created August 25, 2011 08:49
Array.prototype.toString - ECMAScript5
// In ECMA-262 5th edition all methods of Array.prototype are intentionally generic. It means if you pass different
// native object than array for the this value, the particular method will work in same way as for array instances.
// ECMA-262 does not define how those methods will work if you pass a host object for the this value. That behavior // is implementation dependent.
// For example:
var arr = [];
Array.prototype.push.call(arr, 1, 2);
arr[0]; //1
arr[1]; //2