@zenenwjaimes/array-stream-traversal

1.37.0 • Public • Published

array-stream-traversal

Adds the ability to traverse an array with file stream like abilities such as seek, read, tell.

Example Usage

import {ArrayStreamT} from 'array-stream-traversal';

const exampleArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

try {
  const ast = ArrayStreamT.load(exampleArray);
  const res = ast.read(4); // res = [0, 1, 2, 3]
  
  ast.seek(1); // pointer should be at idx 1
  
  const res2 = ast.read(1); // [1] and move pointer to idx 2
  
  ast.seek(2, ArrayStreamT.SeekPos.CURR); // Seeks from current position, so it adds on instead of resettting to beginning
  
  console.log(ast.tell()); // 4
  console.log(ast.read()); // 5
}
catch(err) {
  // do error things here
} 

ArrayStreamT

External ArrayStreamT object

ArrayStreamT~SeekPos ⇒ object

Seek whence constant SET: Starts seek from beginning of array CURR: Seek from current position

Kind: inner enum of ArrayStreamT
Properties

Name Type Default
SET integer 0
CURR integer 1

ArrayStreamT~InvalidLengthException(message) ⇒ Error

Returns custom invalid length exception

Kind: inner method of ArrayStreamT

Param Type Description
message string exception message

ArrayStreamT~InvalidPositionException(message) ⇒ Error

Returns custom invalid position exception

Kind: inner method of ArrayStreamT

Param Type Description
message string exception message

ArrayStreamT~load(data, makeCopy:, position:, whence:) ⇒ object

ArrayStreamT Object

Kind: inner method of ArrayStreamT
Returns: object - returns array, read/seek/tell methods

Param Type Description
data Array
makeCopy: boolean Copies the array (recommended), default true
position: integer Starting position, default 0
whence: integer Where to start seeking from, default SeekPos.SET

ArrayStreamT~read(len:) ⇒ array

Read x amount of bytes/items

Kind: inner method of ArrayStreamT

Param Type Description
len: integer -1 reads til eof, has to be 0+

ArrayStreamT~seek(offset:, whence:) ⇒ this

Sets the current position

Kind: inner method of ArrayStreamT

Param Type Description
offset: integer position
whence: integer where to start seeking (start, current)

ArrayStreamT~tell() ⇒ integer

Returns current position of the pointer

Kind: inner method of ArrayStreamT

Package Sidebar

Install

npm i @zenenwjaimes/array-stream-traversal

Weekly Downloads

1

Version

1.37.0

License

GPL-3.0-or-later

Unpacked Size

337 kB

Total Files

14

Last publish

Collaborators

  • zenenwjaimes