Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

es.string.replace.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
  3. var fails = require('../internals/fails');
  4. var anObject = require('../internals/an-object');
  5. var toLength = require('../internals/to-length');
  6. var toInteger = require('../internals/to-integer');
  7. var requireObjectCoercible = require('../internals/require-object-coercible');
  8. var advanceStringIndex = require('../internals/advance-string-index');
  9. var getSubstitution = require('../internals/get-substitution');
  10. var regExpExec = require('../internals/regexp-exec-abstract');
  11. var wellKnownSymbol = require('../internals/well-known-symbol');
  12. var REPLACE = wellKnownSymbol('replace');
  13. var max = Math.max;
  14. var min = Math.min;
  15. var maybeToString = function (it) {
  16. return it === undefined ? it : String(it);
  17. };
  18. // IE <= 11 replaces $0 with the whole match, as if it was $&
  19. // https://stackoverflow.com/questions/6024666/getting-ie-to-replace-a-regex-with-the-literal-string-0
  20. var REPLACE_KEEPS_$0 = (function () {
  21. // eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing
  22. return 'a'.replace(/./, '$0') === '$0';
  23. })();
  24. // Safari <= 13.0.3(?) substitutes nth capture where n>m with an empty string
  25. var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = (function () {
  26. if (/./[REPLACE]) {
  27. return /./[REPLACE]('a', '$0') === '';
  28. }
  29. return false;
  30. })();
  31. var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
  32. var re = /./;
  33. re.exec = function () {
  34. var result = [];
  35. result.groups = { a: '7' };
  36. return result;
  37. };
  38. return ''.replace(re, '$<a>') !== '7';
  39. });
  40. // @@replace logic
  41. fixRegExpWellKnownSymbolLogic('replace', function (_, nativeReplace, maybeCallNative) {
  42. var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';
  43. return [
  44. // `String.prototype.replace` method
  45. // https://tc39.es/ecma262/#sec-string.prototype.replace
  46. function replace(searchValue, replaceValue) {
  47. var O = requireObjectCoercible(this);
  48. var replacer = searchValue == undefined ? undefined : searchValue[REPLACE];
  49. return replacer !== undefined
  50. ? replacer.call(searchValue, O, replaceValue)
  51. : nativeReplace.call(String(O), searchValue, replaceValue);
  52. },
  53. // `RegExp.prototype[@@replace]` method
  54. // https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
  55. function (string, replaceValue) {
  56. if (
  57. typeof replaceValue === 'string' &&
  58. replaceValue.indexOf(UNSAFE_SUBSTITUTE) === -1 &&
  59. replaceValue.indexOf('$<') === -1
  60. ) {
  61. var res = maybeCallNative(nativeReplace, this, string, replaceValue);
  62. if (res.done) return res.value;
  63. }
  64. var rx = anObject(this);
  65. var S = String(string);
  66. var functionalReplace = typeof replaceValue === 'function';
  67. if (!functionalReplace) replaceValue = String(replaceValue);
  68. var global = rx.global;
  69. if (global) {
  70. var fullUnicode = rx.unicode;
  71. rx.lastIndex = 0;
  72. }
  73. var results = [];
  74. while (true) {
  75. var result = regExpExec(rx, S);
  76. if (result === null) break;
  77. results.push(result);
  78. if (!global) break;
  79. var matchStr = String(result[0]);
  80. if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode);
  81. }
  82. var accumulatedResult = '';
  83. var nextSourcePosition = 0;
  84. for (var i = 0; i < results.length; i++) {
  85. result = results[i];
  86. var matched = String(result[0]);
  87. var position = max(min(toInteger(result.index), S.length), 0);
  88. var captures = [];
  89. // NOTE: This is equivalent to
  90. // captures = result.slice(1).map(maybeToString)
  91. // but for some reason `nativeSlice.call(result, 1, result.length)` (called in
  92. // the slice polyfill when slicing native arrays) "doesn't work" in safari 9 and
  93. // causes a crash (https://pastebin.com/N21QzeQA) when trying to debug it.
  94. for (var j = 1; j < result.length; j++) captures.push(maybeToString(result[j]));
  95. var namedCaptures = result.groups;
  96. if (functionalReplace) {
  97. var replacerArgs = [matched].concat(captures, position, S);
  98. if (namedCaptures !== undefined) replacerArgs.push(namedCaptures);
  99. var replacement = String(replaceValue.apply(undefined, replacerArgs));
  100. } else {
  101. replacement = getSubstitution(matched, S, position, captures, namedCaptures, replaceValue);
  102. }
  103. if (position >= nextSourcePosition) {
  104. accumulatedResult += S.slice(nextSourcePosition, position) + replacement;
  105. nextSourcePosition = position + matched.length;
  106. }
  107. }
  108. return accumulatedResult + S.slice(nextSourcePosition);
  109. }
  110. ];
  111. }, !REPLACE_SUPPORTS_NAMED_GROUPS || !REPLACE_KEEPS_$0 || REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE);