|
22 | 22 |
|
23 | 23 | var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; |
24 | 24 | var isString = require( '@stdlib/assert/is-string' ).isPrimitive; |
25 | | -var Slice = require( '@stdlib/slice/ctor' ); |
26 | | -var trim = require( '@stdlib/string/base/trim' ); |
| 25 | +var base = require( '@stdlib/slice/base/seq2slice' ); |
27 | 26 | var format = require( '@stdlib/string/format' ); |
28 | | -var resolveEnd = require( './resolve_end.js' ); |
29 | | -var RE_COLON_SEP = require( './re_colon_sep.js' ); |
30 | | -var RE_INTEGER = require( './re_integer.js' ); |
31 | | -var RE_END = require( './re_end.js' ); |
32 | 27 |
|
33 | 28 |
|
34 | 29 | // MAIN // |
@@ -68,7 +63,6 @@ var RE_END = require( './re_end.js' ); |
68 | 63 | * @param {NonNegativeInteger} len - maximum number of elements allowed in the slice |
69 | 64 | * @throws {TypeError} first argument must be a valid subsequence string |
70 | 65 | * @throws {TypeError} second argument must be a nonnegative integer |
71 | | -* @throws {RangeError} subsequence increment cannot be zero |
72 | 66 | * @returns {Slice} Slice object |
73 | 67 | * |
74 | 68 | * @example |
@@ -332,141 +326,18 @@ var RE_END = require( './re_end.js' ); |
332 | 326 | * // returns 1 |
333 | 327 | */ |
334 | 328 | function seq2slice( str, len ) { |
335 | | - var parts; |
336 | | - var N; |
337 | | - var v; |
| 329 | + var s; |
338 | 330 | if ( !isString( str ) ) { |
339 | 331 | throw new TypeError( format( 'invalid argument. First argument must be a valid subsequence string. Value: `%s`.', str ) ); |
340 | 332 | } |
341 | 333 | if ( !isNonNegativeInteger( len ) ) { |
342 | 334 | throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', len ) ); |
343 | 335 | } |
344 | | - parts = trim( str ).split( RE_COLON_SEP ); |
345 | | - N = parts.length; |
346 | | - |
347 | | - // Disallow providing standalone components (e.g., '1', 'end', etc) and strings having too many components (e.g., '1:2:3:4')... |
348 | | - if ( N < 2 || N > 3 ) { |
349 | | - throw new TypeError( format( 'invalid argument. First argument must be a valid subsequence string. Value: `%s`.', str ) ); |
350 | | - } |
351 | | - |
352 | | - // Process the increment... |
353 | | - if ( N === 3 ) { |
354 | | - v = parts[ 2 ]; |
355 | | - if ( v.length === 0 ) { // empty increment |
356 | | - parts[ 2 ] = 1; // default increment |
357 | | - } else { |
358 | | - if ( RE_INTEGER.test( v ) === false ) { |
359 | | - throw new TypeError( format( 'invalid argument. First argument must be a valid subsequence string. Value: `%s`.', str ) ); |
360 | | - } |
361 | | - v = parseInt( v, 10 ); |
362 | | - if ( v === 0 ) { |
363 | | - throw new RangeError( format( 'invalid argument. Subsequence increment cannot be zero. Value: `%s`.', str ) ); |
364 | | - } |
365 | | - parts[ 2 ] = v; |
366 | | - } |
367 | | - } else { |
368 | | - parts.push( 1 ); // default increment |
369 | | - } |
370 | | - |
371 | | - // Process the starting index... |
372 | | - v = parts[ 0 ]; |
373 | | - if ( v.length === 0 ) { // empty starting index |
374 | | - // If the increment is negative, we need to iterate from the last index... |
375 | | - if ( parts[ 2 ] < 0 && len > 0 ) { |
376 | | - parts[ 0 ] = len - 1; |
377 | | - } else { |
378 | | - parts[ 0 ] = 0; // default starting index (inclusive) |
379 | | - } |
380 | | - } |
381 | | - // Check for the use of the "end" keyword... |
382 | | - else if ( RE_END.test( v ) ) { |
383 | | - v = resolveEnd( v, len, parts[ 2 ] < 0 ); |
384 | | - if ( v === -1 ) { |
385 | | - throw new TypeError( format( 'invalid argument. First argument must be a valid subsequence string. Value: `%s`.', str ) ); |
386 | | - } |
387 | | - if ( parts[ 2 ] < 0 && v >= len ) { |
388 | | - v -= 1; // clamp to the last index |
389 | | - } |
390 | | - parts[ 0 ] = v; |
391 | | - } |
392 | | - // Check for an integer character sequence... |
393 | | - else if ( RE_INTEGER.test( v ) ) { |
394 | | - v = parseInt( v, 10 ); |
395 | | - |
396 | | - // Check whether we need to resolve the starting index relative to the last index... |
397 | | - if ( v < 0 ) { |
398 | | - v = len + v; |
399 | | - |
400 | | - // If the computed index exceeds the index bounds, clamp to the first index... |
401 | | - if ( v < 0 ) { |
402 | | - v = 0; |
403 | | - } |
404 | | - } |
405 | | - // If the index exceeds the index bounds, clamp to "index" following the last index... |
406 | | - else if ( v >= len ) { |
407 | | - v = len; |
408 | | - } |
409 | | - parts[ 0 ] = v; |
410 | | - } |
411 | | - // The "start" component includes invalid/unsupported characters/operations... |
412 | | - else { |
| 336 | + s = base( str, len ); |
| 337 | + if ( s === null ) { |
413 | 338 | throw new TypeError( format( 'invalid argument. First argument must be a valid subsequence string. Value: `%s`.', str ) ); |
414 | 339 | } |
415 | | - |
416 | | - // Processing the ending index... |
417 | | - v = parts[ 1 ]; |
418 | | - if ( v.length === 0 ) { // empty ending index |
419 | | - // If the increment is negative, we need to iterate toward the first index... |
420 | | - if ( parts[ 2 ] < 0 ) { |
421 | | - parts[ 1 ] = null; |
422 | | - } else { |
423 | | - parts[ 1 ] = len; // default ending index (exclusive) |
424 | | - } |
425 | | - } |
426 | | - // Check for the use of the "end" keyword... |
427 | | - else if ( RE_END.test( v ) ) { |
428 | | - v = resolveEnd( v, len, parts[ 2 ] < 0 ); |
429 | | - if ( v === -1 ) { |
430 | | - throw new TypeError( format( 'invalid argument. First argument must be a valid subsequence string. Value: `%s`.', str ) ); |
431 | | - } |
432 | | - parts[ 1 ] = v; |
433 | | - } |
434 | | - // Check for an integer character sequence... |
435 | | - else if ( RE_INTEGER.test( v ) ) { |
436 | | - v = parseInt( v, 10 ); |
437 | | - |
438 | | - // Check whether we need to resolve the starting index relative to the last index... |
439 | | - if ( v < 0 ) { |
440 | | - v = len + v; |
441 | | - |
442 | | - // Check whether the computed index exceeds the index bounds... |
443 | | - if ( v < 0 ) { |
444 | | - // If the increment is positive, clamp the ending index to the first index (exclusive)... |
445 | | - if ( parts[ 2 ] > 0 ) { |
446 | | - v = 0; |
447 | | - } else { |
448 | | - // If the increment is negative, the ending index should resolve to the first index (inclusive)... |
449 | | - v = null; |
450 | | - } |
451 | | - } |
452 | | - } |
453 | | - // If the index exceeds the index bounds, clamp to the last "index" (exclusive)... |
454 | | - else if ( v > len ) { |
455 | | - v = len; |
456 | | - } |
457 | | - parts[ 1 ] = v; |
458 | | - } |
459 | | - // The "end" component includes invalid/unsupported characters/operations... |
460 | | - else { |
461 | | - throw new TypeError( format( 'invalid argument. First argument must be a valid subsequence string. Value: `%s`.', str ) ); |
462 | | - } |
463 | | - |
464 | | - // Handle empty slice (note: this is at the end rather than at the beginning in order to ensure that `start` and `stop` are always validated)... |
465 | | - if ( len === 0 ) { |
466 | | - return new Slice( 0, 0, parts[ 2 ] ); |
467 | | - } |
468 | | - |
469 | | - return new Slice( parts[ 0 ], parts[ 1 ], parts[ 2 ] ); |
| 340 | + return s; |
470 | 341 | } |
471 | 342 |
|
472 | 343 |
|
|
0 commit comments