Projekt

Obecné

Profil

Stáhnout (4.31 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
var GetIntrinsic = require('../GetIntrinsic');
4

    
5
var $TypeError = GetIntrinsic('%TypeError%');
6

    
7
var callBound = require('../helpers/callBound');
8
var regexTester = require('../helpers/regexTester');
9
var every = require('../helpers/every');
10

    
11
var $charAt = callBound('String.prototype.charAt');
12
var $strSlice = callBound('String.prototype.slice');
13
var $indexOf = callBound('String.prototype.indexOf');
14
var $parseInt = parseInt;
15

    
16
var isDigit = regexTester(/^[0-9]$/);
17

    
18
var inspect = require('object-inspect');
19

    
20
var Get = require('./Get');
21
var IsArray = require('./IsArray');
22
var IsInteger = require('./IsInteger');
23
var ToObject = require('./ToObject');
24
var ToString = require('./ToString');
25
var Type = require('./Type');
26

    
27
var canDistinguishSparseFromUndefined = 0 in [undefined]; // IE 6 - 8 have a bug where this returns false
28

    
29
var isStringOrHole = function (capture, index, arr) {
30
	return Type(capture) === 'String' || (canDistinguishSparseFromUndefined ? !(index in arr) : Type(capture) === 'Undefined');
31
};
32

    
33
// http://www.ecma-international.org/ecma-262/9.0/#sec-getsubstitution
34

    
35
// eslint-disable-next-line max-statements, max-params, max-lines-per-function
36
module.exports = function GetSubstitution(matched, str, position, captures, namedCaptures, replacement) {
37
	if (Type(matched) !== 'String') {
38
		throw new $TypeError('Assertion failed: `matched` must be a String');
39
	}
40
	var matchLength = matched.length;
41

    
42
	if (Type(str) !== 'String') {
43
		throw new $TypeError('Assertion failed: `str` must be a String');
44
	}
45
	var stringLength = str.length;
46

    
47
	if (!IsInteger(position) || position < 0 || position > stringLength) {
48
		throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, and less than or equal to the length of `string`, got ' + inspect(position));
49
	}
50

    
51
	if (!IsArray(captures) || !every(captures, isStringOrHole)) {
52
		throw new $TypeError('Assertion failed: `captures` must be a List of Strings, got ' + inspect(captures));
53
	}
54

    
55
	if (Type(replacement) !== 'String') {
56
		throw new $TypeError('Assertion failed: `replacement` must be a String');
57
	}
58

    
59
	var tailPos = position + matchLength;
60
	var m = captures.length;
61
	if (Type(namedCaptures) !== 'Undefined') {
62
		namedCaptures = ToObject(namedCaptures); // eslint-disable-line no-param-reassign
63
	}
64

    
65
	var result = '';
66
	for (var i = 0; i < replacement.length; i += 1) {
67
		// if this is a $, and it's not the end of the replacement
68
		var current = $charAt(replacement, i);
69
		var isLast = (i + 1) >= replacement.length;
70
		var nextIsLast = (i + 2) >= replacement.length;
71
		if (current === '$' && !isLast) {
72
			var next = $charAt(replacement, i + 1);
73
			if (next === '$') {
74
				result += '$';
75
				i += 1;
76
			} else if (next === '&') {
77
				result += matched;
78
				i += 1;
79
			} else if (next === '`') {
80
				result += position === 0 ? '' : $strSlice(str, 0, position - 1);
81
				i += 1;
82
			} else if (next === "'") {
83
				result += tailPos >= stringLength ? '' : $strSlice(str, tailPos);
84
				i += 1;
85
			} else {
86
				var nextNext = nextIsLast ? null : $charAt(replacement, i + 2);
87
				if (isDigit(next) && next !== '0' && (nextIsLast || !isDigit(nextNext))) {
88
					// $1 through $9, and not followed by a digit
89
					var n = $parseInt(next, 10);
90
					// if (n > m, impl-defined)
91
					result += (n <= m && Type(captures[n - 1]) === 'Undefined') ? '' : captures[n - 1];
92
					i += 1;
93
				} else if (isDigit(next) && (nextIsLast || isDigit(nextNext))) {
94
					// $00 through $99
95
					var nn = next + nextNext;
96
					var nnI = $parseInt(nn, 10) - 1;
97
					// if nn === '00' or nn > m, impl-defined
98
					result += (nn <= m && Type(captures[nnI]) === 'Undefined') ? '' : captures[nnI];
99
					i += 2;
100
				} else if (next === '<') {
101
					// eslint-disable-next-line max-depth
102
					if (Type(namedCaptures) === 'Undefined') {
103
						result += '$<';
104
						i += 2;
105
					} else {
106
						var endIndex = $indexOf(replacement, '>', i);
107
						// eslint-disable-next-line max-depth
108
						if (endIndex > -1) {
109
							var groupName = $strSlice(replacement, i, endIndex);
110
							var capture = Get(namedCaptures, groupName);
111
							// eslint-disable-next-line max-depth
112
							if (Type(capture) !== 'Undefined') {
113
								result += ToString(capture);
114
							}
115
							i += '$<' + groupName + '>'.length;
116
						}
117
					}
118
				} else {
119
					result += '$';
120
				}
121
			}
122
		} else {
123
			// the final $, or else not a $
124
			result += $charAt(replacement, i);
125
		}
126
	}
127
	return result;
128
};
(29-29/115)