Git Product home page Git Product logo

count-map's Introduction

CountMap

Data structure for keeping a count of items.

npm install count-map
yarn add count-map

API

init

import CountMap from 'count-map';

const map = new CountMap({ array, hash, allowNegativeCounts });
var type default use
array Array [] Populate map right away with this array.
hash Function(key) => string (key) => key.toString() Hashing function. Used to group similar keys together, or to distinguish objects.
allowNegativeCounts boolean false Allow negative counts. Otherwise, the minimum is 0.

Key management

  • map.set(key, amount) – Sets ket count to amount. Creates entry if doesn't exist.
  • map.delete(key) => boolean – Removes key from map if it exists. Returns false if key didn't exist.
  • map.add(key, amount = 1) => count – Adds to key count. By default, increments by 1. Can provide amount to increment by that. Returns the new count for key.
  • map.subtract(key, amount = 1) => count – Subtracts from key count. By default, decrements by 1. Can provide amount to decrement by that. Returns the new count for key.
  • map.has(key) => boolean – Check if map has key.
  • map.get(key) => count – Get count for key. If key doesn't exist, returns 0.
  • map.concat(array, inPlace = false) => CountMap – Add contents of array to map. Operates on and returns clone unless inPlace is true.

Other

  • map.keys() => Array – Returns array of unique keys. NOTE: If the hash sets two keys to be equal of each other, there will only be one instance of that saved as a representaive of the hash.
  • map.entries() => Array[key, count] – Returns unique entries within the map.
  • map.toArray() => Array – Creates an array, duplicating keys as many times as the count indicates. Leaves out keys with negative or empty counts.
  • map.equals(otherMap) => boolean – Test if two CountMaps are equal. Specifically:
    • Are the hash functions the same?
    • Are the two negative modes equal?
    • Does each have the same keys and counts? NOTE: Keys may differ, but still return true depending on the hash function.
  • map.clone() => CountMap – Duplicate CountMap.
  • map.rehash() => this – Rehashes map. Use this if the hash function changes.

count-map's People

Contributors

czycha avatar

Watchers

 avatar  avatar

count-map's Issues

Add rangeFilter method?

Hi! Thanks for your project, it is very useful!
Could you maybe add a .rangeFilter(min, max) method that returns a new CountMap with only values that are in range? If one or both limits are null or undefined, their check should be ignored. Thus, .rangeFilter(null, null) would effectively clone the map.

Edit: Even nicer would be if I could give multiple acceptable ranges in one call, saving the concat. This could be done by providing an array as the first argument like .rangeFilter(ranges) where ranges = [ [1, 2], [8, null] ] which would give all entries with value 1, 2, 8 or more than 8.

Clone config but not data

Hi! While implementing my own filter extensions, I discovered that it would be nice to have a method to clone just the config, without entries. That way I could copy the ones I'd like to keep, rather than copying all and then deleting the ones I don't need.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.