Skip to content

Commit

Permalink
generalize setting / fixing regex / string well-known symbols, #83
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jul 7, 2015
1 parent 28a4715 commit 8799301
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 78 deletions.
16 changes: 16 additions & 0 deletions library/modules/$.fix-re-wks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = function(KEY, exec){
var SYMBOL = require('./$.wks')(KEY)
, methods = exec(SYMBOL, ''[KEY]);
if(function(){
try {
var O = {};
O[SYMBOL] = function(){ return 7; };
return ''[KEY](O) != 7;
} catch(e){
return true;
}
}()){
require('./$.redef')(String.prototype, KEY, methods[0]);
require('./$').hide(RegExp.prototype, SYMBOL, methods[1]);
}
};
10 changes: 0 additions & 10 deletions library/modules/$.need-fix-re-wks.js

This file was deleted.

16 changes: 16 additions & 0 deletions modules/$.fix-re-wks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = function(KEY, exec){
var SYMBOL = require('./$.wks')(KEY)
, methods = exec(SYMBOL, ''[KEY]);
if(function(){
try {
var O = {};
O[SYMBOL] = function(){ return 7; };
return ''[KEY](O) != 7;
} catch(e){
return true;
}
}()){
require('./$.redef')(String.prototype, KEY, methods[0]);
require('./$').hide(RegExp.prototype, SYMBOL, methods[1]);
}
};
10 changes: 0 additions & 10 deletions modules/$.need-fix-re-wks.js

This file was deleted.

29 changes: 15 additions & 14 deletions modules/es6.regexp.match.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
if(require('./$.need-fix-re-wks')('match')){
var $match = ''.match
, MATCH = require('./$.wks')('match');
// 21.1.3.11 String.prototype.match(regexp)
require('./$.redef')(String.prototype, 'match', function match(regexp){
var str = String(this)
, fn = regexp == undefined ? undefined : regexp[MATCH];
return fn !== undefined ? fn.call(regexp, str) : new RegExp(regexp)[MATCH](str);
});
// 21.2.5.6 RegExp.prototype[@@match](string)
require('./$').hide(RegExp.prototype, MATCH, function(string){
return $match.call(string, this);
});
}
// @@match logic
require('./$.fix-re-wks')('match', function(MATCH, $match){
return [
// 21.1.3.11 String.prototype.match(regexp)
function match(regexp){
var str = String(this)
, fn = regexp == undefined ? undefined : regexp[MATCH];
return fn !== undefined ? fn.call(regexp, str) : new RegExp(regexp)[MATCH](str);
},
// 21.2.5.6 RegExp.prototype[@@match](string)
function(string){
return $match.call(string, this);
}
];
});
33 changes: 17 additions & 16 deletions modules/es6.regexp.replace.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
if(require('./$.need-fix-re-wks')('replace')){
var $replace = ''.replace
, REPLACE = require('./$.wks')('replace');
// 21.1.3.14 String.prototype.replace(searchValue, replaceValue)
require('./$.redef')(String.prototype, 'replace', function replace(searchValue, replaceValue){
var str = String(this)
, fn = searchValue == undefined ? undefined : searchValue[REPLACE];
return fn !== undefined
? fn.call(searchValue, str, replaceValue)
: $replace.call(str, searchValue, replaceValue);
});
// 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue)
require('./$').hide(RegExp.prototype, REPLACE, function(string, replaceValue){
return $replace.call(string, this, replaceValue);
});
}
// @@replace logic
require('./$.fix-re-wks')('replace', function(REPLACE, $replace){
return [
// 21.1.3.14 String.prototype.replace(searchValue, replaceValue)
function replace(searchValue, replaceValue){
var str = String(this)
, fn = searchValue == undefined ? undefined : searchValue[REPLACE];
return fn !== undefined
? fn.call(searchValue, str, replaceValue)
: $replace.call(str, searchValue, replaceValue);
},
// 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue)
function(string, replaceValue){
return $replace.call(string, this, replaceValue);
}
];
});
29 changes: 15 additions & 14 deletions modules/es6.regexp.search.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
if(require('./$.need-fix-re-wks')('search')){
var $search = ''.search
, SEARCH = require('./$.wks')('search');
// 21.1.3.15 String.prototype.search(regexp)
require('./$.redef')(String.prototype, 'search', function search(regexp){
var str = String(this)
, fn = regexp == undefined ? undefined : regexp[SEARCH];
return fn !== undefined ? fn.call(regexp, str) : new RegExp(regexp)[SEARCH](str);
});
// 21.2.5.9 RegExp.prototype[@@search](string)
require('./$').hide(RegExp.prototype, SEARCH, function(string){
return $search.call(string, this);
});
}
// @@search logic
require('./$.fix-re-wks')('search', function(SEARCH, $search){
return [
// 21.1.3.15 String.prototype.search(regexp)
function search(regexp){
var str = String(this)
, fn = regexp == undefined ? undefined : regexp[SEARCH];
return fn !== undefined ? fn.call(regexp, str) : new RegExp(regexp)[SEARCH](str);
},
// 21.2.5.9 RegExp.prototype[@@search](string)
function(string){
return $search.call(string, this);
}
];
});
29 changes: 15 additions & 14 deletions modules/es6.regexp.split.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
if(require('./$.need-fix-re-wks')('split')){
var $split = ''.split
, SPLIT = require('./$.wks')('split');
// 21.1.3.17 String.prototype.split(separator, limit)
require('./$.redef')(String.prototype, 'split', function split(separator, limit){
var str = String(this)
, fn = separator == undefined ? undefined : separator[SPLIT];
return fn !== undefined ? fn.call(separator, str, limit) : $split.call(str, separator, limit);
});
// 21.2.5.11 RegExp.prototype[@@split](string, limit)
require('./$').hide(RegExp.prototype, SPLIT, function(string, limit){
return $split.call(string, this, limit);
});
}
// @@split logic
require('./$.fix-re-wks')('split', function(SPLIT, $split){
return [
// 21.1.3.17 String.prototype.split(separator, limit)
function split(separator, limit){
var str = String(this)
, fn = separator == undefined ? undefined : separator[SPLIT];
return fn !== undefined ? fn.call(separator, str, limit) : $split.call(str, separator, limit);
},
// 21.2.5.11 RegExp.prototype[@@split](string, limit)
function(string, limit){
return $split.call(string, this, limit);
}
];
});

0 comments on commit 8799301

Please sign in to comment.