Vector-ts, a Python List -like data structure for Typescript / JavaScript.
Nov 16, 2020
Goal
The aim of the npm package vector-ts is twofold:
- 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.
- 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?
- The npm package is here: https://www.npmjs.com/package/vector-ts
- The Github repo is here: https://github.com/serge-hulne/vector-ts-js
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