npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-enhanced-map

v0.1.4

Published

A custom extension of the JavaScript `Map` class with enhanced functionalities.

Downloads

5

Readme

useEnhancedMap

A custom extension of the JavaScript Map class with enhanced functionalities.

Usage

Installation

npm install use-enhanced-map
or
yarn add use-enhanced-map

Usage Example

Usage in React

import React from 'react';
import { useEnhancedMap } from 'use-enhanced-map';

type Todo = {
  description: string;
  done: boolean;
};

const MyComponent = () => {
  const enhancedMap = useEnhancedMap<string, Todo>();

  // Add some initial tasks when the component loads
  React.useEffect(() => {
    enhancedMap.setMany([
      ['task1', { description: 'Complete assignment', done: false }],
      ['task2', { description: 'Prepare presentation', done: true }],
      ['task3', { description: 'Read a book', done: false }],
    ]);
  }, [enhancedMap]);

  // Function to mark a task as done
  const handleMarkAsDone = (key: string) => {
    enhancedMap.update(key, { done: true });
  };

  // Function to add a new task
  const handleAddTask = () => {
    const newTask: Todo = { description: 'New Task', done: false };
    enhancedMap.add(newTask);
  };

  // Function to delete a task
  const handleDeleteTask = (key: string) => {
    enhancedMap.delete(key);
  };

  // Function to clear all tasks
  const handleClearTasks = () => {
    enhancedMap.clear();
  };

  return (
    <div>
      <h1>Todo List</h1>
      <ul>
        {enhancedMap.mapEntries((key, task) => (
          <li key={key}>
            <input
              type="checkbox"
              checked={task.done}
              onChange={() => handleMarkAsDone(key)}
            />
            <span>{task.description}</span>
            <button onClick={() => handleDeleteTask(key)}>Delete</button>
          </li>
        ))}
      </ul>
      <button onClick={handleAddTask}>Add Task</button>
      <button onClick={handleClearTasks}>Clear All Tasks</button>
      <p>Total Tasks: {enhancedMap.size}</p>
    </div>
  );
};
export default MyComponent;

EnhancedMap Class

An extended Map class offering additional functionalities.

Methods

update(key, newValue): this

Updates the value for a specific key. If the key doesn't exist, it does nothing.

setMany(array): number

Sets multiple key-value pairs from an array. Returns the new size of the map.

mapEntries(): [any, any][]

Maps entries in the map. Returns an array of transformed entries.

mapKeys(): any[]

Maps keys in the map. Returns an array of transformed keys.

mapValues(): any[]

Maps values in the map. Returns an array of transformed values.

toArray(): [any, any][]

Converts the map to an array of key-value pairs.

Note: The other methods (get, set, delete, clear, has, keys, entries, forEach, etc.) are inherited from the native Map class and behave as in the standard Map implementation.

useEnhancedMap Hook

A React hook for managing an instance of EnhancedMap.

Usage

const enhancedMap = useEnhancedMap();

Available Functions

update(key, newValue): any | null

Updates the value for a specific key. Returns the previous value associated with the key, or null if the key doesn't exist.

setMany(array): number

Sets multiple key-value pairs from an array. Returns the new size of the map.

mapEntries(): [any, any][]

Maps entries in the map. Returns an array of transformed entries.

mapKeys(): any[]

Maps keys in the map. Returns an array of transformed keys.

mapValues(): any[]

Maps values in the map. Returns an array of transformed values.

toArray(): [any, any][]

Converts the map to an array of key-value pairs.

Note: The other functions documented are native Map methods provided by the EnhancedMap class and return null instead of undefined for cases where no value is found.