• Stars
    star
    385
  • Rank 111,145 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HashMap JavaScript class for Node.js and the browser. The keys can be anything and won't be stringified

HashMap Class for JavaScript

Installation

NPM

Using npm:

$ npm install hashmap

Using bower:

$ bower install hashmap

You can download the last stable version from the releases page.

If you like risk, you can download the latest master version, it's usually stable.

To run the tests:

$ npm test

Description

This project provides a HashMap class that works both on Node.js and the browser. HashMap instances store key/value pairs allowing keys of any type.

Unlike regular objects, keys will not be stringified. For example numbers and strings won't be mixed, you can pass Date's, RegExp's, DOM Elements, anything! (even null and undefined)

HashMap constructor overloads

  • new HashMap() creates an empty hashmap
  • new HashMap(map:HashMap) creates a hashmap with the key-value pairs of map
  • new HashMap(arr:Array) creates a hashmap from the 2D key-value array arr, e.g. [['key1','val1'], ['key2','val2']]
  • new HashMap(key:*, value:*, key2:*, value2:*, ...) creates a hashmap with several key-value pairs

HashMap methods

  • get(key:*) : * returns the value stored for that key.
  • set(key:*, value:*) : HashMap stores a key-value pair
  • multi(key:*, value:*, key2:*, value2:*, ...) : HashMap stores several key-value pairs
  • copy(other:HashMap) : HashMap copies all key-value pairs from other to this instance
  • has(key:*) : Boolean returns whether a key is set on the hashmap
  • search(value:*) : * returns key under which given value is stored (null if not found)
  • delete(key:*) : HashMap deletes a key-value pair by key
  • remove(key:*) : HashMap Alias for delete(key:*) (deprecated)
  • type(key:*) : String returns the data type of the provided key (used internally)
  • keys() : Array<*> returns an array with all the registered keys
  • values() : Array<*> returns an array with all the values
  • entries() : Array<[*,*]> returns an array with [key,value] pairs
  • size : Number the amount of key-value pairs
  • count() : Number returns the amount of key-value pairs (deprecated)
  • clear() : HashMap deletes all the key-value pairs on the hashmap
  • clone() : HashMap creates a new hashmap with all the key-value pairs of the original
  • hash(key:*) : String returns the stringified version of a key (used internally)
  • forEach(function(value, key)) : HashMap iterates the pairs and calls the function for each one

Method chaining

All methods that don't return something, will return the HashMap instance to enable chaining.

Examples

Assume this for all examples below

var map = new HashMap();

If you're using this within Node, you first need to import the class

var HashMap = require('hashmap');

Basic use case

map.set("some_key", "some value");
map.get("some_key"); // --> "some value"

Map size / number of elements

var map = new HashMap();
map.set("key1", "val1");
map.set("key2", "val2");
map.size; // -> 2

Deleting key-value pairs

map.set("some_key", "some value");
map.delete("some_key");
map.get("some_key"); // --> undefined

No stringification

map.set("1", "string one");
map.set(1, "number one");
map.get("1"); // --> "string one"

A regular Object used as a map would yield "number one"

Objects as keys

var key = {};
var key2 = {};
map.set(key, 123);
map.set(key2, 321);
map.get(key); // --> 123

A regular Object used as a map would yield 321

Iterating

map.set(1, "test 1");
map.set(2, "test 2");
map.set(3, "test 3");

map.forEach(function(value, key) {
    console.log(key + " : " + value);
});
// ES6 Iterators version
for (const pair of map) {
    console.log(`${pair.key} : ${pair.value}`)
}

Method chaining

map
  .set(1, "test 1")
  .set(2, "test 2")
  .set(3, "test 3")
  .forEach(function(value, key) {
      console.log(key + " : " + value);
  });

LICENSE

The MIT License (MIT)

Copyright (c) 2012 Ariel Flesler

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF

To-Do

  • (?) Allow extending the hashing function in a AOP way or by passing a service
  • Make tests work on the browser

More Repositories

1

jquery.scrollTo

Lightweight, cross-browser and highly customizable animated scrolling with jQuery
JavaScript
3,673
star
2

jquery.localScroll

Animated anchor navigation made easy with jQuery
JavaScript
615
star
3

jquery.serialScroll

Animated scrolling of series with jQuery
JavaScript
181
star
4

config-node

Flexible lightweight configuration loader for Node
JavaScript
75
star
5

node-spider

Generic web crawler powered by NodeJS
JavaScript
68
star
6

parallel

CLI tool to execute shell commands in parallel, based on GNU parallel command
JavaScript
34
star
7

jquery.rule

JavaScript
17
star
8

jquery.preload

Multi-functional image preloader plugin for jQuery
JavaScript
14
star
9

uver-cli

CLI for uver, bump the version on package.json and other files
JavaScript
13
star
10

XMLWriter

XML generator for Javascript, based on .NET's XMLTextWriter.
JavaScript
13
star
11

connect-pause

Connect/Express middleware to simulate latency for debugging
JavaScript
12
star
12

node-http-codes

Map of HTTP status messages to their code, based on Node's built-in mapping
JavaScript
10
star
13

lucky

CLI tool to measure the state of your luck, at the moment
JavaScript
10
star
14

uver

Bump the version on package.json and other files
JavaScript
7
star
15

stress-node

Stress test library for Node JS
JavaScript
6
star
16

dotfiles

My dotfiles
Shell
5
star
17

word-clock

A responsive HTML5 Word-based clock
JavaScript
5
star
18

stress-node-server

Node Server for running stress tests
JavaScript
4
star
19

taptap

A touch game for dogs and cats
HTML
4
star
20

electron-wi-fine

A connectivity monitoring app, built on Electron
JavaScript
4
star
21

node-http-status

HTTP Status class for NodeJS and Express
JavaScript
3
star
22

vscode-url-encode

Adds commands to URL-encode and URL-decode text
JavaScript
3
star
23

jwt-crack

Multi-threaded brute force JWT cracker in pure Node.js
JavaScript
3
star
24

satoshi-data

Curated dataset of Satoshi Nakamoto responding to users
TypeScript
3
star
25

todo-list

A single HTML page tool to track time spent on tasks
JavaScript
2
star
26

genetic-maze

Build a Maze, then let a Genetic Algorithm solve it
JavaScript
2
star
27

idonethis-form

Very simple mobile-friendly HTML form to submit dones to IDoneThis
JavaScript
2
star
28

infer-gender

Infer the gender of a name based on a large list of names, case insensitive and removes diacritics
JavaScript
2
star
29

xdotool-trigger

A simple wrapper for xdotool to easily trigger some artificial inputs
Shell
1
star
30

remote

Browser to browser keyboard
HTML
1
star
31

node-watch

Transparent hot reload of files in place for NodeJS
JavaScript
1
star
32

tracer

Trace redirects from a URL
JavaScript
1
star
33

jquery.detect

(Bookmarklet) Detect jQuery version and plugins on a page
JavaScript
1
star