int2slice

Convert an integer to a Slice object.

Usage

var int2slice = require( '@stdlib/slice/base/int2slice' );

int2slice( value, max, strict )

Converts an integer to a Slice object, where max specifies the index upper bound.

var s = int2slice( -4, 10, false );
// returns <Slice>

var start = s.start;
// returns 6

var stop = s.stop;
// returns 7

var step = s.step;
// returns 1

When strict is true, the function returns an error object if an input value exceeds index bounds.

var s = int2slice( 100, 10, true );
// returns { 'code': 'ERR_SLICE_OUT_OF_BOUNDS' }

A returned error object may have one of the following error codes:

  • ERR_SLICE_OUT_OF_BOUNDS: a slice exceeds index bounds.

Examples

var int2slice = require( '@stdlib/slice/base/int2slice' );

var s = int2slice( -1, 7, false );
console.log( '%s', s.toString() );

s = int2slice( 3, 5, false );
console.log( '%s', s.toString() );

s = int2slice( -3, 5, false );
console.log( '%s', s.toString() );

s = int2slice( 10, 5, false );
console.log( '%s', s.toString() );

s = int2slice( -10, 5, false );
console.log( '%s', s.toString() );
Did you find this page helpful?