forked from appfigures/string-replace-to-array
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-replace-to-array.js
More file actions
117 lines (93 loc) · 2.75 KB
/
Copy pathstring-replace-to-array.js
File metadata and controls
117 lines (93 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Inspiration: https://github.com/facebook/react/issues/3386
var invariant = require('invariant'),
isString = require('lodash.isstring'),
flatten = require('lodash.flatten')
function replace (string, regexpOrSubstr, newValueOrFn) {
invariant(typeof string === 'string', 'First param must be a string')
//invariant(typeof regexpOrSubstr === 'string' || regexpOrSubstr instanceof RegExp, 'Second param must be a string pattern or a regular expression')
var fn = (typeof regexpOrSubstr === 'string') ? replaceUsingString : replaceUsingRegexp
return fn(string, regexpOrSubstr, newValueOrFn)
}
function replaceUsingString (string, patternString, newValueOrFn) {
var index = string.indexOf(patternString)
if (index >= 0) {
var arr = []
var endIndex = index + patternString.length
if (index > 0) {
arr.push(string.substring(0, index))
}
arr.push(
(typeof newValueOrFn === 'function') ?
newValueOrFn(
string.substring(index, endIndex),
index,
string
) :
newValueOrFn
)
if (endIndex < string.length) {
arr.push(string.substring(endIndex))
}
return arr
} else {
return [string]
}
}
function replaceUsingRegexp (string, regexp, newValueOrFn) {
var output = []
var replacerIsFn = (typeof newValueOrFn === 'function')
var storedLastIndex = regexp.lastIndex
regexp.lastIndex = 0
var result
var lastIndex = 0
while (result = regexp.exec(string)) {
var index = result.index
if (result[0] === '') {
// When the regexp is an empty string
// we still want to advance our cursor to the next item.
// This is the behavior of String.replace.
regexp.lastIndex++
}
if (index !== lastIndex) {
output.push(string.substring(lastIndex, index))
}
var match = result[0]
lastIndex = index + match.length
var out = replacerIsFn ?
newValueOrFn.apply(this, result.concat(index, result.input)) :
newValueOrFn
output.push(out)
if (!regexp.global) {
break
}
}
if (lastIndex < string.length) {
output.push(string.substring(lastIndex))
}
regexp.lastIndex = storedLastIndex
return output
}
/**
*
* @typedef {function(string): any} MatchFunction
*/
/**
*
* @param {string|string[]} string
* @param {RegExp|string} regexpOrSubstr
* @param {string|MatchFunction} newSubStrOrFn
* @returns {Array}
*/
function stringReplaceToArray (string, regexpOrSubstr, newSubStrOrFn) {
if (isString(string)) {
return replace(string, regexpOrSubstr, newSubStrOrFn)
} else if (!Array.isArray(string) || !string[0]) {
throw new TypeError('First argument must be an array or non-empty string');
} else {
return flatten(string.map(function (string) {
if (!isString(string)) return string
return replace(string, regexpOrSubstr, newSubStrOrFn)
}))
}
}
module.exports = stringReplaceToArray;