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

@kenmori/copy-name-manager

v0.4.1

Published

A versatile JavaScript/TypeScript library for managing and generating sequentially named copies with customizable naming patterns. Perfect for applications requiring dynamic duplication and renaming.

Downloads

1,018

Readme

CopyNameManager

CopyNameManager is a lightweight JavaScript/TypeScript library that helps manage unique copy names in applications. This is particularly useful when you need to create multiple copies of items with distinct names and want to ensure that each copy has a unique name, even after some copies are deleted.

Features

  • Automatically generates unique copy names in the format name, nameのコピー, nameのコピー2, etc.
  • Handles deletion of copies and reuses deleted copy numbers when new copies are added.
  • Provides easy integration with frontend frameworks like React.

Installation

You can install the library via npm:

npm install @kenmori/copy-name-manager

Usage

Basic Example

Here is how you can use CopyNameManager in a JavaScript/TypeScript project:

import { CopyNameManager } from "copy-name-manager";

const copyNameManager = new CopyNameManager();

const firstCopy = copyNameManager.addCopy("Document");
console.log(firstCopy); // Output: "Document"

const secondCopy = copyNameManager.addCopy("Document");
console.log(secondCopy); // Output: "Documentのコピー"

const thirdCopy = copyNameManager.addCopy("Document");
console.log(thirdCopy); // Output: "Documentのコピー2"

// Removing a copy
copyNameManager.removeCopy("Documentのコピー");
console.log(copyNameManager.getCopies()); // Output: ["Document", "Documentのコピー2"]

// Adding a new copy after deletion
const newCopy = copyNameManager.addCopy("Document");
console.log(newCopy); // Output: "Documentのコピー"

React Example

CopyNameManager can be easily integrated into React applications. Below is an example of how to use it:

import React, { useState } from "react";
import { CopyNameManager } from "copy-name-manager";

const CopyNameManagerComponent = () => {
  const copyNameManager = new CopyNameManager();
  const [copies, setCopies] = useState<string[]>([]);

  const handleAddCopy = () => {
    const newCopy = copyNameManager.addCopy("Document");
    setCopies([...copyNameManager.getCopies()]);
  };

  const handleRemoveCopy = (name: string) => {
    copyNameManager.removeCopy(name);
    setCopies([...copyNameManager.getCopies()]);
  };

  return (
    <div>
      <button onClick={handleAddCopy}>コピー</button>
      <ul>
        {copies.map((copy, index) => (
          <li key={index}>
            {copy} <button onClick={() => handleRemoveCopy(copy)}>削除</button>
          </li>
        ))}
      </ul>
    </div>
  );
};

export default CopyNameManagerComponent;

Visualization

To better understand how CopyNameManager works, here's a visual example:

API

addCopy(name: string): string

Adds a new copy with a unique name based on the given name. Returns the generated copy name.

removeCopy(name: string): boolean

Removes the specified copy. Returns true if the copy was successfully removed, otherwise returns false.

getCopies(): string[]

Returns an array of all the current copies.

License

This project is licensed under the MIT License.