Socket
Socket
Sign inDemoInstall

hashlist-typescript

Package Overview
Dependencies
2
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    hashlist-typescript

Simple Typescript Linked List with Hash Table indexing and type templating


Version published
Weekly downloads
0
Maintainers
1
Created
Weekly downloads
 

Readme

Source

hashlist-typescript

Build Status Coverage Status

Simple Typescript Linked List with hash table indexing. Supports generics type templating iterator and iterable protocols.

See Also:

Installation

npm:

npm install --save hashlist-typescript

yarn:

yarn add hashlist-typescript

Building from source

install dev dependencies. There are no production dependencies.

yarn
npm install

build using the options in tsconfig.json

yarn|npm run build

run all package tests

yarn|npm run test

see the test coverage report

yarn|npm run coverage
yarn|npm run coverage:report

Usage

Importing:

import { HashList } from 'hashlist-typescript';
const { HashList } = require('hashlist-typescript')

API

HashList(...values: T[])

HashList()

Create an empty linked list by omitting any arguments during instantiation.

let list = new HashList<number>()
HashList(...values: T[])

Create a new list and initialize it with values. Values will be appended from left to right. i.e. the first argument will be at the head and the last argument will be at the tail.

Specify the type using the typescript templating to enable type-checking of all values going into and out of the list.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
let items: string[] = ['one', 'two', 'three', 'four'];
let list = new HashList<string>(...items);

Typescript will check if the values match the type given to the template when initializing the new list.

let items: = ['one', 'two', 'three', 4];
let list = new HashList<string>(...items); // arguments are not all strings
HashList(...values: Foo[])

Create a new list using custom types or classes. All values are retained as references and not copies so removed values can be compared using strict comparison.

class Foo {
  private val:number;
  constructor(val: number) {
    this.val = val;
  }
  get bar(): number { return this.val }
}

let foo1 = new Foo(1);
let foo2 = new Foo(2);
let foo3 = new Foo(3);

let fooList = new HashList<Foo>(foo1, foo2, foo3)

fooList.head.bar // => 1
fooList.tail.bar // => 3
let val = list.removeHead()
val // => foo1
HashList(...values: any[])

Specify any to allow the list to take values of any type.

let list = new HashList<any>(4, 'hello' { hello: 'world' })
list.length // => 3
list.head // => 4
list.tail // => { hello: 'world' }
HashList#[Symbol.iterator]

The list supports both iterator and iterable protocols allowing it to be used with the for...of and ...spread operators and with deconstruction.

for...of:

let items: number[] = [4, 5, 6];
let list = new HashList<number>(...items);

for (let item of list) {
  console.log(item)
}
//4
//5
//6

...spread:

let items: number[] = [4, 5, 6];
let list = new HashList<number>(...items);

function manyArgs(...args) {
  for (let i in args) {
    console.log(args[i])
  }
}
manyArgs(...list);
//4
//5
//6

deconstruction:

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);

let [a, b, c] = list;
//a => 4
//b => 5
//c => 6
HashList#head :T

Peek at the value at the head of the list. This will not remove the value from the list.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
list.head // => 4
HashList#tail :T

Peek at the value at the tail of the list. This will not remove the value from the list.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
list.tail // => 7
HashList#length :number

Query the length of the list. An empty list will return 0.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
list.length // => 4
HashList#append(val: T, checkDuplicates: boolean = false): string

Append an item to the end of the list. The method returns a key that can be used to retrieve the value at a later time. The new item will replace the previous tail item and subsequent calls to HashList#head will now recall the new item.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
list.length // => 4
list.append(8)
list.length // => 5
list.tail // => 8

The optional argument checkDuplicates is false by default. If set to true, it will check if the new value is already contained in the list. If the value is found to be a duplicate it will not be added and the method will return false.

Values are checked using strict === comparison. Checking for duplicates inserts the list into a [Set][set] and then checks if the value is contained in the set.

Note that by default, duplicates will be added to the list. No collision handling is done as the duplicates are stored in separate nodes of the underlying linked list.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
list.length // => 4
let key = list.append(5, true)
list.length // => 4
list.tail // => 7
key // => uuid key for accessing the value.
HashList#prepend(val: T, checkDuplicates: boolean = false): boolean

Prepend an item to the beginning of the list. The method returns a key that can be used to retrieve the value at a later time. The new item will replace the previous head item and subsequent calls to HashList<T>#head will now recall the new item.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
list.length // => 4
list.prepend(3)
list.length // => 5
list.head // => 3

The optional argument checkDuplicates is false by default. If set to true, it will check if the new value is already contained in the list. If the value is found to be a duplicate it will not be added and the method will return false.

Values are checked using strict === comparison. Checking for duplicates inserts the list into a [Set][set] and then checks if the value is contained in the set.

Note that by default, duplicates will be added to the list. No collision handling is done as the duplicates are stored in separate nodes of the underlying linked list.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
list.length // => 4
let key = list.prepend(4, true)
list.length // => 4
list.head // => 4
key // => uuid key for accessing the value.
HashList#removeHead(): T

Removes the item at the head of the list and returns the item.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
list.length // => 4
let val = list.removeHead()
list.length // => 3
list.head // => 5
val // => 4
HashList#removeTail(): T

Removes the item at the tail of the list and returns the item.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
list.length // => 4
let val = list.removeTail()
list.length // => 3
list.tail // => 6
val // => 7
HashList#remove(key: string): T

Removes the item, using the provided key, from the list and returns the item. If the item can not be located in the list the method will return undefined and the list will not be altered.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
list.length // => 4
let key = list.append(8)
let val = list.remove(key)
list.length // => 4
list.tail // => 7
val // => 8
let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
list.length // => 4
let val = list.remove('')
list.length // => 4
list.tail // => 7
val // => undefined
HashList#toArray(): T[]

This method simply returns [...this].

Converts the list into an array and returns the array representation. This method does not mutate the list in any way.

Objects are not copied, so all non-primitive items in the array are still referencing the list items.

let items: number[] = [4, 5, 6, 7];
let list = new HashList<number>(...items);
let result = list.toArray()
result // => [4, 5, 6, 7]

License

MIT © Michael Sutherland

Keywords

FAQs

Last updated on 23 May 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc