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

@japan-d2/id2

v0.0.4

Published

the ID converter that can convert between IDs with different orders and character types and unique numeric sequences.

Downloads

1

Readme

id2

the ID converter that can convert between IDs with different orders and character types and unique numeric sequences.

install

npm install @japan-d2/id2

or

yarn add @japan-d2/id2

example

Human Readable ID

Instantiate

const id2 = new ID2.Converter({
  format: ID2.Converter.format('XXXX-00000', {
    X: 'ACFHJKPRSTUVWXYZ',
    0: '234567'
  })
})

Convert number to string

id2.stringify(0)
// => 'AAAA-22222'
id2.stringify(1)
// => 'AAAA-22223'
id2.stringify(1234567)
// => 'AATY-65553'
id2.stringify(id2.numberOfAvailableIDs - BigInt(1))
// => 'ZZZZ-77777'

Convert string to number

id2.parse('AAAA-22222')
// => 0n
id2.parse('AAAA-22223')
// => 1n
id2.parse('AATY-65553')
// => 1234567n
id2.parse('ZZZZ-77777')
// => 509607935n

UUID v4

Instantiate

import ID2 from '@japan-d2/id2'
const id2 = new ID2.Converter({
  format: ID2.Converter.format('RRRRRRRR-RRRR-4RRR-rRRR-RRRRRRRRRRRR', {
    R: '0123456789abcdef',
    r: '89ab'
  })
})

Convert number to string

id2.stringify(0)
// => '00000000-0000-4000-8000-000000000000'
id2.stringify(BigInt('3881947731413173145134900932847109277'))
// => 'bae8b334-a84b-4d25-ba49-3b943cfc849d'
id2.stringify(id2.numberOfAvailableIDs - BigInt(1))
// => 'ffffffff-ffff-4fff-bfff-ffffffffffff'

Convert string to number

id2.parse('00000000-0000-4000-8000-000000000000')
// => 0n
id2.parse(BigInt('3881947731413173145134900932847109277'))
// => 3881947731413173145134900932847109277n
id2.parse('ffffffff-ffff-4fff-bfff-ffffffffffff')
// => 5316911983139663491615228241121378303n

classes

class Converter

new Converter (options: Options)

Options
format

for ID pattern [A-Z]{3}-[0-9]{4}

new ID2.Converter({
  format: [
    {
      characters: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
      repeats: 3
    },
    {
      characters: '-',
    },
    {
      characters: '0123456789',
      repeats: 4
    }
  ]
})

Or use the more manageable pattern helper.

// Same format as above.
new ID2.Converter({
  format: ID2.Converter.format('XXX-0000', {
    X: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
    0: '0123456789'
  })
}

stringify (n: number | bigint): string

Converts the given number to a string.

parse (id: string): bigint

Converts the given id to a number.

lengthOfID: number

ID length specified by format.

numberOfAvailableIDs: number

Maximum number of IDs specified by format.

license

MIT