MCPcopy Index your code
hub / github.com/josdejong/mathjs / _getSubstring

Function _getSubstring

src/function/matrix/subset.js:142–173  ·  view source on GitHub ↗

* Retrieve a subset of a string * @param {string} str string from which to get a substring * @param {Index} index An index or list of indices (character positions) * @returns {string} substring * @private

(str, index)

Source from the content-addressed store, hash-verified

140 * @private
141 */
142function _getSubstring (str, index) {
143 if (!isIndex(index)) {
144 // TODO: better error message
145 throw new TypeError('Index expected')
146 }
147
148 if (isEmptyIndex(index)) { return '' }
149 validateIndexSourceSize(Array.from(str), index)
150
151 if (index.size().length !== 1) {
152 throw new DimensionError(index.size().length, 1)
153 }
154
155 // validate whether the range is out of range
156 const strLen = str.length
157 validateIndex(index.min()[0], strLen)
158 validateIndex(index.max()[0], strLen)
159
160 const range = index.dimension(0)
161
162 let substr = ''
163 function callback (v) {
164 substr += str.charAt(v)
165 }
166 if (Number.isInteger(range)) {
167 callback(range)
168 } else {
169 range.forEach(callback)
170 }
171
172 return substr
173}
174
175/**
176 * Replace a substring in a string

Callers

nothing calls this directly

Calls 10

isIndexFunction · 0.90
isEmptyIndexFunction · 0.90
validateIndexSourceSizeFunction · 0.90
validateIndexFunction · 0.90
callbackFunction · 0.70
sizeMethod · 0.65
minMethod · 0.65
maxMethod · 0.65
isIntegerMethod · 0.65
forEachMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…