JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 35
  • Score
    100M100P100Q54547F
  • License MIT

Convert strings into web-usable ids.

Package Exports

  • web-id

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (web-id) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

web-id

Convert strings into web-usable ids.

Usage

$ npm install web-id
const WebId = require('web-id');
const webid = new WebId({
    prefix: 'wid'
});
const myId = webid.generate('1. László Čapek had déjà vu in the Åland Islands');
// wid-laszlo-capek-had-deja-vu-in-the-aland-islands

Properties

.original - The original, unaltered string.

.id - The web safe id.

webid.id;                // laszlo-capek-had-deja-vu-in-the-aland-islands

.delimiter or .delim - Get or set the current delimiter. Default is -. Note that only unreserved characters are allowed: ALPHA / DIGIT / '-' / '.' / '_' / '~'.

webid.delimiter          // -
webid.delimiter = '_';   // set the delimiter to _
webid.id;                // laszlo_capek_had_deja_vu_in_the_aland_islands
webid.delimiter = '&';   // assertion error

.prefix - Set a prefix for the id. Prefixes are run through WebId.cleanString();

webid.prefix = 'myId';
webid.id;                // my-id-laszlo-capek-had-deja-vu-in-the-aland-islands

`.iterated` or `.iter` - Get the iterated id (see the `.iterate()` method).
```javascript
webid.iterated           // laszlo-capek-had-deja-vu-in-the-aland-islands-1

.iterator - Get the current iterator (init = 0).

.unique - Get the web safe id with a shortid appended to the end.

webid.unique             // laszlo-capek-had-deja-vu-in-the-aland-islands-r1yb6dtne

Methods

.iterate() - increment the internal iterator. Retrieve the current iterator with .iterator. Retrieve the iterated id with the .iterated or .iter getters. .id will remain untouched.

for (let i = 0; i < 5; i += 1) {
    webid.iterate();
}
webid.iterated;          // laszlo-capek-had-deja-vu-in-the-aland-islands-5

.generate(str) - Generate a web id.

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

.generateUnique(str) - (re)generate a unique id by appending a shortid to the .id.

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

If the str parameter is omitted, it will just return the shortid.

webid.generateUnique();
// rya3hx4px

WebId.cleanString(str) (static) - An intermediary cleanup step that runs lodash functions: trim, lowerCase, deburr, and kebabCase.

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