-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #524 from zloirock/string-replace-all-stage-2
Move `String#replaceAll` to stage 2, update behavior by the spec draft
- Loading branch information
Showing
19 changed files
with
123 additions
and
24 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 |
---|---|---|
|
@@ -3,5 +3,6 @@ module.exports = { | |
3.1: [ | ||
'es.string.match-all', | ||
'es.symbol.match-all', | ||
'esnext.symbol.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require('../../modules/esnext.symbol.replace-all'); | ||
|
||
module.exports = require('../../internals/wrapped-well-known-symbol').f('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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,50 @@ | ||
'use strict'; | ||
var hide = require('../internals/hide'); | ||
var requireObjectCoercible = require('../internals/require-object-coercible'); | ||
var anObject = require('../internals/an-object'); | ||
var isRegExp = require('../internals/is-regexp'); | ||
var getRegExpFlags = require('../internals/regexp-flags'); | ||
var speciesConstructor = require('../internals/species-constructor'); | ||
var REPLACE_ALL = require('../internals/well-known-symbol')('replaceAll'); | ||
var IS_PURE = require('../internals/is-pure'); | ||
var RegExpPrototype = RegExp.prototype; | ||
|
||
var $replaceAll = function (string, replaceValue) { | ||
var rx = anObject(this); | ||
var flags = String('flags' in RegExpPrototype ? rx.flags : getRegExpFlags.call(rx)); | ||
if (!~flags.indexOf('g')) { | ||
rx = new (speciesConstructor(rx, RegExp))(rx.source, flags + 'g'); | ||
} | ||
return String(string).replace(rx, replaceValue); | ||
}; | ||
|
||
// `String.prototype.replaceAll` method | ||
// https://github.com/tc39/proposal-string-replace-all | ||
require('../internals/export')({ target: 'String', proto: true }, { | ||
replaceAll: function replaceAll(searchValue, replaceValue) { | ||
var O = requireObjectCoercible(this); | ||
var search, flags; | ||
if (isRegExp(searchValue)) { | ||
flags = getRegExpFlags.call(searchValue); | ||
if (!~flags.indexOf('g')) { | ||
search = new (speciesConstructor(searchValue, RegExp))(searchValue.source, flags + 'g'); | ||
} else search = searchValue; | ||
return String(O).replace(search, replaceValue); | ||
var replacer, string, searchString, template, result, i; | ||
if (searchValue != null) { | ||
replacer = searchValue[REPLACE_ALL]; | ||
if (replacer !== undefined) { | ||
return replacer.call(searchValue, O, replaceValue); | ||
} else if (IS_PURE && isRegExp(searchValue)) { | ||
return $replaceAll.call(searchValue, O, replaceValue); | ||
} | ||
} | ||
string = String(O); | ||
searchString = String(searchValue); | ||
template = string.split(searchString); | ||
if (typeof replaceValue !== 'function') { | ||
return template.join(String(replaceValue)); | ||
} | ||
return String(O).split(searchValue).join(replaceValue); | ||
result = template[0]; | ||
for (i = 1; i < template.length; i++) { | ||
result += String(replaceValue(searchString, i - 1, string)); | ||
result += template[i]; | ||
} | ||
return result; | ||
} | ||
}); | ||
|
||
IS_PURE || REPLACE_ALL in RegExpPrototype || hide(RegExpPrototype, REPLACE_ALL, $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// `Symbol.replaceAll` well-known symbol | ||
// https://tc39.github.io/proposal-string-replaceall/ | ||
require('../internals/define-well-known-symbol')('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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
require('../modules/esnext.string.replace-all'); | ||
require('../modules/esnext.symbol.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require('../proposals/set-methods'); | ||
require('../proposals/string-replace-all'); | ||
|
||
module.exports = require('./3'); |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Symbol from 'core-js-pure/features/symbol'; | ||
|
||
QUnit.test('Symbol.replaceAll', assert => { | ||
assert.ok('replaceAll' in Symbol, 'Symbol.replaceAll is available'); | ||
assert.ok(Object(Symbol.replaceAll) instanceof Symbol, 'Symbol.replaceAll is symbol'); | ||
}); |
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,13 @@ | ||
import { DESCRIPTORS } from '../helpers/constants'; | ||
|
||
QUnit.test('Symbol.replaceAll', assert => { | ||
assert.ok('replaceAll' in Symbol, 'Symbol.replaceAll is available'); | ||
assert.nonEnumerable(Symbol, 'replaceAll'); | ||
assert.ok(Object(Symbol.replaceAll) instanceof Symbol, 'Symbol.replaceAll is symbol'); | ||
if (DESCRIPTORS) { | ||
const descriptor = Object.getOwnPropertyDescriptor(Symbol, 'replaceAll'); | ||
assert.ok(!descriptor.enumerble, 'non-enumerable'); | ||
assert.ok(!descriptor.writable, 'non-writable'); | ||
assert.ok(!descriptor.configurable, 'non-configurable'); | ||
} | ||
}); |
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