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

web-id

v2.1.0

Published

Convert strings into web-usable ids.

Downloads

88

Readme

web-id

GitHub issues Build Status Coverage Status dependencies Status

Convert strings into web-usable ids.

Similar to slugify, but with two additions:

  1. strict mode by default. This ensures that the ids work in HTML 4 or XHTML environments.
  2. Adds a method to increase the entropy of your id via shortid.

Usage

Install it in your project.

npm install web-id

Require it and start using it.

const WebId = require("web-id");

// create a new instance
const webid = new WebId();

const myId = webid.generate("1. László Čapek had déjà vu in the Åland Islands");
// laszlo-capek-had-deja-vu-in-the-aland-islands

API

All of the following are instance methods that are available after creating a new WebId().

.generate(str[, options])

Returns the id.

webid.generate("1. László Čapek had déjà vu in the Åland Islands");
// laszlo-capek-had-deja-vu-in-the-aland-islands

If no str is given, it will use the shortid.

webid.generate();
// rya3hx4px

.generateUnique(str[, options])

Returns a unique(ish) id by appending a shortid.
Note that the shortid is appended to the id, but before the suffix if it exists.

webid.generateUnique("1. László Čapek had déjà vu in the Åland Islands");
// laszlo-capek-had-deja-vu-in-the-aland-islands-ryoBZht3l

Functions identically to .generate() when no str is given:

webid.generateUnique();
// rya3hx4px

.parse(str[, options])

Returns the internal webid object.

webid.parse("1. László Čapek had déjà vu in the Åland Islands");
// {
//   id: 'laszlo-capek-love-deja-vu-in-the-aland-islands',
//   unique: 'laszlo-capek-love-deja-vu-in-the-aland-islands-ryxOaGbHz',
//   original: '1. László Čapek ♥ déjà vu in the Åland Islands',
//   slug: 'laszlo-capek-love-deja-vu-in-the-aland-islands',
//   shortid: 'ryxOaGbHz',
//   delimiter: '-',
//   prefix: '',
//   suffix: '',
// }

.configure([options])

Helper for setting the options. See below.

Options

Options can be set at any time with the .configure([options]) method, by using the setters (only delimiter, prefix, and suffix at this time), or during one of the methods. For example, all of the following are equivalent:

webid.configure({ prefix: "wid" });
webid.prefix = "wid";
webid.generate("1. László Čapek had déjà vu in the Åland Islands", {
  prefix: "wid"
});
// wid-laszlo-capek-had-deja-vu-in-the-aland-islands

Options include:

delimiter

Alias: delim | Type: string | Default: '-'
Replace spaces with a delimiter.   In strict mode, only unreserved characters are allowed: ALPHA / DIGIT / '-' / '.' / '_' / '~'.

prefix

Alias: pre | Type: string
Set a prefix for the id.   In strict mode, the prefix must start with a letter (/^[a-z]+/i).

suffix

Alias: suf | Type: string
Set a suffix for the id.

maxLength

Type: number | Default: 128
Set a max length for the string.
This is applied to the final string, keeping any set prefix and/or suffix intact.

strict

Type: boolean | Default: true
Turn on/off strict mode.

lower

Type: boolean | Default: true
Cast everything to lowercase. Applied by slugify.

remove

Type: regex | Default: null
Specify characters to remove. Applied by slugify.

delimiterInShortid

Type: boolean | Default: true Whether the delimiter will be included in the shortid.