Sha256: 45eb99f577242e0e12f8d2b946d71ac8aed479edf53a05c2f9c66862d88bd0db

Contents?: true

Size: 1.95 KB

Versions: 13

Compression:

Stored size: 1.95 KB

Contents

'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;
  }
});

Version data entries

13 entries across 13 versions & 4 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/core-js/modules/esnext.string.replace-all.js
tang-0.2.1 spec/tang_app/node_modules/core-js/modules/esnext.string.replace-all.js
tang-0.2.0 spec/tang_app/node_modules/core-js/modules/esnext.string.replace-all.js
tang-0.1.0 spec/tang_app/node_modules/core-js/modules/esnext.string.replace-all.js
tang-0.0.9 spec/tang_app/node_modules/core-js/modules/esnext.string.replace-all.js
enju_library-0.3.8 spec/dummy/node_modules/core-js/modules/esnext.string.replace-all.js
condenser-0.3 lib/condenser/processors/node_modules/core-js-pure/modules/esnext.string.replace-all.js
condenser-0.2 lib/condenser/processors/node_modules/core-js-pure/modules/esnext.string.replace-all.js
condenser-0.1 lib/condenser/processors/node_modules/core-js-pure/modules/esnext.string.replace-all.js
condenser-0.0.12 lib/condenser/processors/node_modules/core-js-pure/modules/esnext.string.replace-all.js
condenser-0.0.11 lib/condenser/processors/node_modules/core-js-pure/modules/esnext.string.replace-all.js
condenser-0.0.10 lib/condenser/processors/node_modules/core-js-pure/modules/esnext.string.replace-all.js
condenser-0.0.9 lib/condenser/processors/node_modules/core-js-pure/modules/esnext.string.replace-all.js