All files main.js

100% Statements 363/363
100% Branches 14/14
100% Functions 1/1
100% Lines 363/363

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 3641x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 950x 950x 950x 950x 9x 9x 950x 10x 10x 950x 8x 8x 923x 923x 950x 771x 771x 950x 14x 14x 950x 24x 24x 114x 114x 950x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var base = require( '@stdlib/slice/base/seq2slice' );
var format = require( '@stdlib/string/format' );
 
 
// MAIN //
 
/**
* Converts a subsequence string to a Slice object.
*
* ## Notes
*
* -   A subsequence string has the following format:
*
*     ```text
*     <start>:<stop>:<increment>
*     ```
*
*     where
*
*     -   If an `increment` is not specified, the default increment is `1`. An increment of zero is **not** allowed.
*     -   The `start` index is **inclusive**.
*     -   The `stop` index is **exclusive**.
*     -   Both `start` and `stop` indices are _optional_. If not provided, `start` and `stop` default to index extremes. Which extremes correspond to which index depends on whether the `increment` is positive or negative.
*     -   Both `start` and `stop` can be negative; in which case, the corresponding index is resolved by subtracting the respective value from the provided length `len`.
*     -   Both `start` and `stop` can use the `end` keyword (e.g., `end-2::2`, `end-3:`, etc), which supports basic subtraction and division.
*     -   The `end` keyword resolves to the provided length `len`. Thus, `:-1` is equivalent to `:end-1`, `:-2` is equivalent to `:end-2`, and so on and so forth. The exception is when performing a division operation when the `increment` is less than zero; in which case, `end` is equal to `len-1` in order to preserve user expectations when `end/d` equals a whole number and slicing from right-to-left. The result from a division operation is **rounded down** to the nearest integer value.
*
* -   When `strict` is `false`, the resolved slice start is clamped to the slice index bounds (i.e., `[0, len)`).
*
* -   When `strict` is `false`, the resolved slice end is upper bound clamped to `len` (i.e., one greater than the last possible index).
*
* -   When the increment is negative, the resolved slice end value may be `null`, thus indicating that a non-empty slice should include the first index.
*
* -   The function ensures that results satisfy the convention that `:n` combined with `n:` is equivalent to `:` (i.e., selecting all elements).
*
* -   When `len` is zero, the function always returns a Slice object equivalent to `0:0:<increment>`.
*
* @param {string} str - input string
* @param {NonNegativeInteger} len - maximum number of elements allowed in the slice
* @param {boolean} strict - boolean indicating whether to enforce strict bounds checking
* @throws {TypeError} first argument must be a valid subsequence string
* @throws {TypeError} second argument must be a nonnegative integer
* @throws {TypeError} third argument must be a boolean
* @throws {RangeError} a subsequence string must have a non-zero increment
* @throws {RangeError} a subsequence string resolves to a slice which exceeds index bounds
* @returns {Slice} Slice object
*
* @example
* var s = seq2slice( '0:10:1', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 0
*
* v = s.stop;
* // returns 10
*
* v = s.step;
* // returns 1
*
* @example
* var s = seq2slice( '::-1', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 9
*
* v = s.stop;
* // returns null
*
* v = s.step;
* // returns -1
*
* @example
* var s = seq2slice( ':0:-1', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 9
*
* v = s.stop;
* // returns 0
*
* v = s.step;
* // returns -1
*
* @example
* var s = seq2slice( '4::-1', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 4
*
* v = s.stop;
* // returns null
*
* v = s.step;
* // returns -1
*
* @example
* var s = seq2slice( '::', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 0
*
* v = s.stop;
* // returns 10
*
* v = s.step;
* // returns 1
*
* @example
* var s = seq2slice( ':end:', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 0
*
* v = s.stop;
* // returns 10
*
* v = s.step;
* // returns 1
*
* @example
* var s = seq2slice( 'end::-1', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 9
*
* v = s.stop;
* // returns null
*
* v = s.step;
* // returns -1
*
* @example
* var s = seq2slice( 'end-2::-1', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 8
*
* v = s.stop;
* // returns null
*
* v = s.step;
* // returns -1
*
* @example
* var s = seq2slice( 'end/2::-1', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 4
*
* v = s.stop;
* // returns null
*
* v = s.step;
* // returns -1
*
* @example
* var s = seq2slice( 'end:end/2:-1', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 9
*
* v = s.stop;
* // returns 4
*
* v = s.step;
* // returns -1
*
* @example
* var s = seq2slice( ':end/2:-1', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 9
*
* v = s.stop;
* // returns 4
*
* v = s.step;
* // returns -1
*
* @example
* var s = seq2slice( ':end/2:1', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 0
*
* v = s.stop;
* // returns 5
*
* v = s.step;
* // returns 1
*
* @example
* var s = seq2slice( ':end/3', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 0
*
* v = s.stop;
* // returns 3
*
* v = s.step;
* // returns 1
*
* @example
* var s = seq2slice( 'end/3::-1', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 3
*
* v = s.stop;
* // returns null
*
* v = s.step;
* // returns -1
*
* @example
* var s = seq2slice( 'end/3::', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 3
*
* v = s.stop;
* // returns 10
*
* v = s.step;
* // returns 1
*
* @example
* var s = seq2slice( 'end/3::', 9, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 3
*
* v = s.stop;
* // returns 9
*
* v = s.step;
* // returns 1
*
* @example
* var s = seq2slice( 'end/3::-1', 9, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 2
*
* v = s.stop;
* // returns null
*
* v = s.step;
* // returns -1
*
* @example
* var s = seq2slice( '5:5', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 5
*
* v = s.stop;
* // returns 5
*
* v = s.step;
* // returns 1
*
* @example
* var s = seq2slice( '5:5', 0, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 0
*
* v = s.stop;
* // returns 0
*
* v = s.step;
* // returns 1
*
* @example
* var s = seq2slice( 'end:', 10, false );
* // returns <Slice>
*
* var v = s.start;
* // returns 10
*
* v = s.stop;
* // returns 10
*
* v = s.step;
* // returns 1
*/
function seq2slice( str, len, strict ) {
	var code;
	var s;
	if ( !isString( str ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be a valid subsequence string. Value: `%s`.', str ) );
	}
	if ( !isNonNegativeInteger( len ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', len ) );
	}
	if ( !isBoolean( strict ) ) {
		throw new TypeError( format( 'invalid argument. Third argument must be a boolean. Value: `%s`.', strict ) );
	}
	s = base( str, len, strict );
	code = s.code;
	if ( code === void 0 ) {
		return s;
	}
	if ( code === 'ERR_SLICE_INVALID_INCREMENT' ) {
		throw new RangeError( format( 'invalid argument. A subsequence string must have a non-zero increment. Value: `%s`.', str ) );
	}
	if ( code === 'ERR_SLICE_OUT_OF_BOUNDS' ) {
		throw new RangeError( format( 'invalid argument. The subsequence string resolves to a slice which exceeds index bounds. Value: `%s`.', str ) );
	}
	// code === 'ERR_SLICE_INVALID_SUBSEQUENCE'
	throw new TypeError( format( 'invalid argument. First argument must be a valid subsequence string. Value: `%s`.', str ) );
}
 
 
// EXPORTS //
 
module.exports = seq2slice;