{"id":13760151,"url":"https://github.com/flesler/hashmap","last_synced_at":"2025-05-16T09:04:50.224Z","repository":{"id":2274278,"uuid":"3231063","full_name":"flesler/hashmap","owner":"flesler","description":"HashMap JavaScript class for Node.js and the browser. The keys can be anything and won't be stringified","archived":false,"fork":false,"pushed_at":"2021-02-20T03:10:08.000Z","size":78,"stargazers_count":383,"open_issues_count":4,"forks_count":69,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-23T14:01:54.392Z","etag":null,"topics":["array","flesler","hashmap","javascript","key-value","map","node","nodejs","npm"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flesler.png","metadata":{"files":{"readme":"Readme.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-01-21T00:13:17.000Z","updated_at":"2025-04-21T19:15:35.000Z","dependencies_parsed_at":"2022-08-31T00:01:37.459Z","dependency_job_id":null,"html_url":"https://github.com/flesler/hashmap","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flesler%2Fhashmap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flesler%2Fhashmap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flesler%2Fhashmap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flesler%2Fhashmap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flesler","download_url":"https://codeload.github.com/flesler/hashmap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254501557,"owners_count":22081528,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","flesler","hashmap","javascript","key-value","map","node","nodejs","npm"],"created_at":"2024-08-03T13:01:04.311Z","updated_at":"2025-05-16T09:04:45.215Z","avatar_url":"https://github.com/flesler.png","language":"JavaScript","readme":"# HashMap Class for JavaScript\r\n\r\n## Installation\r\n\r\n[![NPM](https://nodei.co/npm/hashmap.png?compact=true)](https://npmjs.org/package/hashmap)\r\n\r\nUsing [npm](https://npmjs.org/package/hashmap):\r\n\r\n    $ npm install hashmap\r\n\r\nUsing bower:\r\n\r\n    $ bower install hashmap\r\n\r\nYou can download the last stable version from the [releases page](https://github.com/flesler/hashmap/releases).\r\n\r\nIf you like risk, you can download the [latest master version](https://raw.github.com/flesler/hashmap/master/hashmap.js), it's usually stable.\r\n\r\nTo run the tests:\r\n\r\n    $ npm test\r\n\r\n## Description\r\n\r\nThis project provides a `HashMap` class that works both on __Node.js__ and the __browser__.\r\nHashMap instances __store key/value pairs__ allowing __keys of any type__.\r\n\r\nUnlike 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`)\r\n\r\n## HashMap constructor overloads\r\n- `new HashMap()` creates an empty hashmap\r\n- `new HashMap(map:HashMap)` creates a hashmap with the key-value pairs of `map`\r\n- `new HashMap(arr:Array)` creates a hashmap from the 2D key-value array `arr`, e.g. `[['key1','val1'], ['key2','val2']]`\r\n- `new HashMap(key:*, value:*, key2:*, value2:*, ...)` creates a hashmap with several key-value pairs\r\n\r\n## HashMap methods\r\n\r\n- `get(key:*) : *` returns the value stored for that key.\r\n- `set(key:*, value:*) : HashMap` stores a key-value pair\r\n- `multi(key:*, value:*, key2:*, value2:*, ...) : HashMap` stores several key-value pairs\r\n- `copy(other:HashMap) : HashMap` copies all key-value pairs from other to this instance\r\n- `has(key:*) : Boolean` returns whether a key is set on the hashmap\r\n- `search(value:*) : *` returns key under which given value is stored (`null` if not found)\r\n- `delete(key:*) : HashMap` deletes a key-value pair by key\r\n- `remove(key:*) : HashMap` Alias for `delete(key:*)` *(deprecated)*\r\n- `type(key:*) : String` returns the data type of the provided key (used internally)\r\n- `keys() : Array\u003c*\u003e` returns an array with all the registered keys\r\n- `values() : Array\u003c*\u003e` returns an array with all the values\r\n- `entries() : Array\u003c[*,*]\u003e` returns an array with [key,value] pairs\r\n- `size : Number` the amount of key-value pairs\r\n- `count() : Number` returns the amount of key-value pairs *(deprecated)*\r\n- `clear() : HashMap` deletes all the key-value pairs on the hashmap\r\n- `clone() : HashMap` creates a new hashmap with all the key-value pairs of the original\r\n- `hash(key:*) : String` returns the stringified version of a key (used internally)\r\n- `forEach(function(value, key)) : HashMap` iterates the pairs and calls the function for each one\r\n\r\n### Method chaining\r\n\r\nAll methods that don't return something, will return the HashMap instance to enable chaining.\r\n\r\n## Examples\r\n\r\nAssume this for all examples below\r\n\r\n```js\r\nvar map = new HashMap();\r\n```\r\n\r\nIf you're using this within Node, you first need to import the class\r\n\r\n```js\r\nvar HashMap = require('hashmap');\r\n```\r\n\r\n### Basic use case\r\n\r\n```js\r\nmap.set(\"some_key\", \"some value\");\r\nmap.get(\"some_key\"); // --\u003e \"some value\"\r\n```\r\n\r\n### Map size / number of elements\r\n\r\n```js\r\nvar map = new HashMap();\r\nmap.set(\"key1\", \"val1\");\r\nmap.set(\"key2\", \"val2\");\r\nmap.size; // -\u003e 2\r\n```\r\n\r\n### Deleting key-value pairs\r\n\r\n```js\r\nmap.set(\"some_key\", \"some value\");\r\nmap.delete(\"some_key\");\r\nmap.get(\"some_key\"); // --\u003e undefined\r\n```\r\n\r\n### No stringification\r\n\r\n```js\r\nmap.set(\"1\", \"string one\");\r\nmap.set(1, \"number one\");\r\nmap.get(\"1\"); // --\u003e \"string one\"\r\n```\r\n\r\nA regular `Object` used as a map would yield `\"number one\"`\r\n\r\n### Objects as keys\r\n\r\n```js\r\nvar key = {};\r\nvar key2 = {};\r\nmap.set(key, 123);\r\nmap.set(key2, 321);\r\nmap.get(key); // --\u003e 123\r\n```\r\nA regular `Object` used as a map would yield `321`\r\n\r\n### Iterating\r\n\r\n```js\r\nmap.set(1, \"test 1\");\r\nmap.set(2, \"test 2\");\r\nmap.set(3, \"test 3\");\r\n\r\nmap.forEach(function(value, key) {\r\n    console.log(key + \" : \" + value);\r\n});\r\n// ES6 Iterators version\r\nfor (const pair of map) {\r\n    console.log(`${pair.key} : ${pair.value}`)\r\n}\r\n```\r\n\r\n### Method chaining\r\n\r\n```js\r\nmap\r\n  .set(1, \"test 1\")\r\n  .set(2, \"test 2\")\r\n  .set(3, \"test 3\")\r\n  .forEach(function(value, key) {\r\n      console.log(key + \" : \" + value);\r\n  });\r\n```\r\n\r\n## LICENSE\r\n\r\nThe MIT License (MIT)\r\n\r\nCopyright (c) 2012 Ariel Flesler\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining a copy\r\nof this software and associated documentation files (the \"Software\"), to deal\r\nin the Software without restriction, including without limitation the rights\r\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\ncopies of the Software, and to permit persons to whom the Software is\r\nfurnished to do so, subject to the following conditions:\r\n\r\nThe above copyright notice and this permission notice shall be included in all\r\ncopies or substantial portions of the Software.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\nOUT OF\r\n\r\n## To-Do\r\n\r\n* (?) Allow extending the hashing function in a AOP way or by passing a service\r\n* Make tests work on the browser\r\n","funding_links":[],"categories":["Data Structure","Data Structure [🔝](#readme)","数据结构"],"sub_categories":["Runner","运行器","运行器e2e测试"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflesler%2Fhashmap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflesler%2Fhashmap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflesler%2Fhashmap/lists"}