Skip to content

Commit db2a0aa

Browse files
committed
refactor: use base implementation
1 parent ac9f95d commit db2a0aa

File tree

9 files changed

+6
-631
lines changed

9 files changed

+6
-631
lines changed

lib/node_modules/@stdlib/slice/seq2slice/docs/types/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ import { Slice } from '@stdlib/types/slice';
5757
* @param len - maximum number of elements allowed in the slice
5858
* @throws first argument must be a valid subsequence string
5959
* @throws second argument must be a nonnegative integer
60-
* @throws subsequence increment cannot be zero
6160
* @returns Slice object
6261
*
6362
* @example

lib/node_modules/@stdlib/slice/seq2slice/lib/main.js

Lines changed: 5 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@
2222

2323
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
2424
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' );
2726
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' );
3227

3328

3429
// MAIN //
@@ -68,7 +63,6 @@ var RE_END = require( './re_end.js' );
6863
* @param {NonNegativeInteger} len - maximum number of elements allowed in the slice
6964
* @throws {TypeError} first argument must be a valid subsequence string
7065
* @throws {TypeError} second argument must be a nonnegative integer
71-
* @throws {RangeError} subsequence increment cannot be zero
7266
* @returns {Slice} Slice object
7367
*
7468
* @example
@@ -332,141 +326,18 @@ var RE_END = require( './re_end.js' );
332326
* // returns 1
333327
*/
334328
function seq2slice( str, len ) {
335-
var parts;
336-
var N;
337-
var v;
329+
var s;
338330
if ( !isString( str ) ) {
339331
throw new TypeError( format( 'invalid argument. First argument must be a valid subsequence string. Value: `%s`.', str ) );
340332
}
341333
if ( !isNonNegativeInteger( len ) ) {
342334
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', len ) );
343335
}
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 ) {
413338
throw new TypeError( format( 'invalid argument. First argument must be a valid subsequence string. Value: `%s`.', str ) );
414339
}
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;
470341
}
471342

472343

lib/node_modules/@stdlib/slice/seq2slice/lib/re_colon_sep.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

lib/node_modules/@stdlib/slice/seq2slice/lib/re_end.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

lib/node_modules/@stdlib/slice/seq2slice/lib/re_end_divide.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)