All Projects → flesler → Hashmap

flesler / Hashmap

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Hashmap

Map
PHP Map package for easy and elegant handling of PHP arrays as array-like map objects
Stars: ✭ 1,180 (+225.07%)
Mutual labels:  array, map
data-structure-project
自己实现集合框架系列整理总结
Stars: ✭ 29 (-92.01%)
Mutual labels:  array, hashmap
Dyno
Package dyno is a utility to work with dynamic objects at ease.
Stars: ✭ 81 (-77.69%)
Mutual labels:  array, map
Eternal
A C++14 compile-time/constexpr map and hash map with minimal binary footprint
Stars: ✭ 93 (-74.38%)
Mutual labels:  hashmap, map
HashMapC
A tiny library for using easily HashMap, arraylist in the C.
Stars: ✭ 21 (-94.21%)
Mutual labels:  array, hashmap
Collection
A PHP library for representing and manipulating collections.
Stars: ✭ 488 (+34.44%)
Mutual labels:  array, map
Containers
This library provides various containers. Each container has utility functions to manipulate the data it holds. This is an abstraction as to not have to manually manage and reallocate memory.
Stars: ✭ 125 (-65.56%)
Mutual labels:  array, map
Containers
Containers backed by std.experimental.allocator
Stars: ✭ 111 (-69.42%)
Mutual labels:  array, hashmap
array-keyed-map
JS datastructure, like Map, but the keys are arrays
Stars: ✭ 29 (-92.01%)
Mutual labels:  map, array
php-collections
A collection library for php
Stars: ✭ 34 (-90.63%)
Mutual labels:  map, array
Hashmap
A Golang lock-free thread-safe HashMap optimized for fastest read access.
Stars: ✭ 899 (+147.66%)
Mutual labels:  hashmap, map
Kind Of
Get the native JavaScript type of a value, fast. Used by superstruct, micromatch and many others!
Stars: ✭ 268 (-26.17%)
Mutual labels:  array, map
Libdict
C library of key-value data structures.
Stars: ✭ 234 (-35.54%)
Mutual labels:  key-value, map
Pgo
Go library for PHP community with convenient functions
Stars: ✭ 51 (-85.95%)
Mutual labels:  array, map
Stencil Store
Store is a lightweight shared state library by the StencilJS core team. Implements a simple key/value map that efficiently re-renders components when necessary.
Stars: ✭ 107 (-70.52%)
Mutual labels:  key-value, map
simplehstore
🏪 Easy way to use a PostgreSQL database (and the HSTORE feature) from Go
Stars: ✭ 54 (-85.12%)
Mutual labels:  key-value, hashmap
ustd
Micro-standard-library providing minimal and portable array, queue and map for attiny avr, arduinos, esp8266/32 and linux, mac
Stars: ✭ 14 (-96.14%)
Mutual labels:  map, array
Mlib
Library of generic and type safe containers in pure C language (C99 or C11) for a wide collection of container (comparable to the C++ STL).
Stars: ✭ 321 (-11.57%)
Mutual labels:  array, hashmap
Offlinemap
基于MySQL + Node.js + Leaflet的离线地图展示,支持百度、谷歌、高德、腾讯地图
Stars: ✭ 343 (-5.51%)
Mutual labels:  map
Capsule
The Capsule Hash Trie Collections Library
Stars: ✭ 350 (-3.58%)
Mutual labels:  hashmap

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].