Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move
String#replaceAll
to stable ES (#842)
per Jun TC39 meeting
- Loading branch information
1 parent
389307d
commit 8b121e3
Showing
19 changed files
with
89 additions
and
68 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,5 +55,6 @@ module.exports = { | |
3.6: [ | ||
'es.regexp.sticky', | ||
'es.regexp.test', | ||
'es.string.replace-all', | ||
], | ||
}; |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require('../../modules/es.string.replace-all'); | ||
var entryUnbind = require('../../internals/entry-unbind'); | ||
|
||
module.exports = entryUnbind('String', 'replaceAll'); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
require('../../modules/esnext.string.replace-all'); | ||
var entryUnbind = require('../../internals/entry-unbind'); | ||
|
||
module.exports = entryUnbind('String', 'replaceAll'); | ||
module.exports = require('../../es/string/replace-all'); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var requireObjectCoercible = require('../internals/require-object-coercible'); | ||
var isRegExp = require('../internals/is-regexp'); | ||
var getRegExpFlags = require('../internals/regexp-flags'); | ||
var wellKnownSymbol = require('../internals/well-known-symbol'); | ||
var IS_PURE = require('../internals/is-pure'); | ||
|
||
var REPLACE = wellKnownSymbol('replace'); | ||
var RegExpPrototype = RegExp.prototype; | ||
|
||
// `String.prototype.replaceAll` method | ||
// https://github.com/tc39/proposal-string-replace-all | ||
$({ target: 'String', proto: true }, { | ||
replaceAll: function replaceAll(searchValue, replaceValue) { | ||
var O = requireObjectCoercible(this); | ||
var IS_REG_EXP, flags, replacer, string, searchString, template, result, position, index; | ||
if (searchValue != null) { | ||
IS_REG_EXP = isRegExp(searchValue); | ||
if (IS_REG_EXP) { | ||
flags = String(requireObjectCoercible('flags' in RegExpPrototype | ||
? searchValue.flags | ||
: getRegExpFlags.call(searchValue) | ||
)); | ||
if (!~flags.indexOf('g')) throw TypeError('`.replaceAll` does not allow non-global regexes'); | ||
} | ||
replacer = searchValue[REPLACE]; | ||
if (replacer !== undefined) { | ||
return replacer.call(searchValue, O, replaceValue); | ||
} else if (IS_PURE && IS_REG_EXP) { | ||
return String(O).replace(searchValue, replaceValue); | ||
} | ||
} | ||
string = String(O); | ||
searchString = String(searchValue); | ||
if (searchString === '') return replaceAll.call(string, /(?:)/g, replaceValue); | ||
template = string.split(searchString); | ||
if (typeof replaceValue !== 'function') { | ||
return template.join(String(replaceValue)); | ||
} | ||
result = template[0]; | ||
position = result.length; | ||
for (index = 1; index < template.length; index++) { | ||
result += String(replaceValue(searchString, position, string)); | ||
position += searchString.length + template[index].length; | ||
result += template[index]; | ||
} | ||
return result; | ||
} | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,2 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var requireObjectCoercible = require('../internals/require-object-coercible'); | ||
var isRegExp = require('../internals/is-regexp'); | ||
var getRegExpFlags = require('../internals/regexp-flags'); | ||
var wellKnownSymbol = require('../internals/well-known-symbol'); | ||
var IS_PURE = require('../internals/is-pure'); | ||
|
||
var REPLACE = wellKnownSymbol('replace'); | ||
var RegExpPrototype = RegExp.prototype; | ||
|
||
// `String.prototype.replaceAll` method | ||
// https://github.com/tc39/proposal-string-replace-all | ||
$({ target: 'String', proto: true }, { | ||
replaceAll: function replaceAll(searchValue, replaceValue) { | ||
var O = requireObjectCoercible(this); | ||
var IS_REG_EXP, flags, replacer, string, searchString, template, result, position, index; | ||
if (searchValue != null) { | ||
IS_REG_EXP = isRegExp(searchValue); | ||
if (IS_REG_EXP) { | ||
flags = String(requireObjectCoercible('flags' in RegExpPrototype | ||
? searchValue.flags | ||
: getRegExpFlags.call(searchValue) | ||
)); | ||
if (!~flags.indexOf('g')) throw TypeError('`.replaceAll` does not allow non-global regexes'); | ||
} | ||
replacer = searchValue[REPLACE]; | ||
if (replacer !== undefined) { | ||
return replacer.call(searchValue, O, replaceValue); | ||
} else if (IS_PURE && IS_REG_EXP) { | ||
return String(O).replace(searchValue, replaceValue); | ||
} | ||
} | ||
string = String(O); | ||
searchString = String(searchValue); | ||
if (searchString === '') return replaceAll.call(string, /(?:)/g, replaceValue); | ||
template = string.split(searchString); | ||
if (typeof replaceValue !== 'function') { | ||
return template.join(String(replaceValue)); | ||
} | ||
result = template[0]; | ||
position = result.length; | ||
for (index = 1; index < template.length; index++) { | ||
result += String(replaceValue(searchString, position, string)); | ||
position += searchString.length + template[index].length; | ||
result += template[index]; | ||
} | ||
return result; | ||
} | ||
}); | ||
// TODO: Remove from `core-js@4` | ||
require('./es.string.replace-all'); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var parent = require('../../es/string/replace-all'); | ||
|
||
module.exports = parent; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require('../proposals/promise-any'); | ||
require('../proposals/string-replace-all'); | ||
var parent = require('./4'); | ||
|
||
module.exports = parent; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
require('../proposals/global-this'); | ||
require('../proposals/promise-all-settled'); | ||
require('../proposals/string-match-all'); | ||
require('../proposals/string-replace-all'); | ||
var path = require('../internals/path'); | ||
|
||
module.exports = path; |
This file contains 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
This file contains 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
File renamed without changes.
File renamed without changes.
This file contains 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