Vector-ts, a Python List -like data structure for Typescript / JavaScript.

Serge Hulne
Nov 16, 2020
(C) Serge Hulne.

Goal

The aim of the npm package vector-ts is twofold:

  1. It aims at providing a data structure for Typescript and JavaScript which emulates a Python List, in which one can: add elements, delete them, append, prepend, insert, … them by value as well as by index.
  2. It provide a convenience class (which is a wrapper around the Array class) whose goal is to make operations on Arrays safer and more expressive / readable than with the Array.splice method (which is not very intuitive).

Where can I get It?

How can I use it?

It can be installed from npm with the command:

npm i vector-ts

Examples of use:

import {Vector} from “vector-ts”let v = new Vector<string>()v.append("A");v.append("B");v.append("C");console.log(v.a);

returns:

[ 'A', 'B', 'C' ]

Further:

v.delete("B");console.log(v.a);

returns:

[ ‘A’, ‘C’ ]

Further:

let t = new Vector<string>();
let u = new Vector<string>();
t.append(“A”);t.append(“B”);t.append(“C”);u.append(“X”);u.append(“Y”);u.append(“Z”);let s = t.concat(u);console.log(s);

returns:

[ 'A', 'B', 'C', 'X', 'Y', 'Z' ]

The list of available operations / methods is:

constructorappendclearconcatdeletedeleteAtdeleteAtEnddeleteAtStartforEachfromArraygetValueincludesinsertAtinsertAtEndinsertAtStartlengthprependsetValuesortsubstitutesubstituteAtswapAt

https://serge-hulne.github.io/vector-ts-js/classes/vector.html

A list of examples is available at:

https://github.com/serge-hulne/vector-ts-js/blob/main/vector_test.ts

Bugs (or missing features or feedback) can be reported here:

https://github.com/serge-hulne/vector-ts-js/issues

Happy coding!

--

--

Serge Hulne

Author, scientist (Physics PhD), philosophy, Sci-Fi, thrillers, humor, blues and Irish music, green energy, origins of consciousness.