* 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)
| 140 | * @private |
| 141 | */ |
| 142 | function _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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…