Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

kinoid

ThornDuke92MIT3.0.8

An ultra-lightweight JavaScript library for generating unique, URL-friendly, and time-sortable IDs. Kinoid is designed for Node.js and browser environments, offering decodable, timestamp-based IDs in a compact base36 format.

unique-id, id-generator, url-friendly, timestamp-id, sortable-id, lightweight, time-based-id, base36, nodejs-library, browser-library, javascript-library, npm-package, frontend, backend, cli, uid, uniqid, identifier, time-sortable, compact-id, cross-platform

readme

kinoid

NPM Version NPM Downloads jsDelivr hits (npm) Unpkg Powered Codesandbox Powered NPM License npm bundle size

An ultra-light, url-friendly unique IDs generator. For node and browsers.

Overview

The kinoid library generates unique IDs as strings made up of numbers and the 26 lowercase characters of the english alphabet; every string generated by kinoid can be considered as a base36 number.

The generated IDs are unique because, no matter how many IDs are generated, each ID is different from all the others generated before or at the same time on the same machine.

Features

Each ID is composed of a timestamp (representing milliseconds since the UNIX epoch), a number that identifies the process in which the program runs, and a final number which can be said a serialization value or a singularity factor that guarantees the uniqueness of the ID.

IDs are sortable by time, because they are based on the time they were created. Additionally, the time an ID was created can be calculated from the ID itself thanks to the decodeId() utility which returns an object containing, among other things, the date on which that ID was generated.

Warning

IDs are not passwords! Kinoid ensures that each generated ID is unique, but not necessarily unpredictable.

This depends on a logical limit that affects all libraries: an algorithm that produces unpredictable strings cannot guarantee their uniqueness and, vice versa, an algorithm that produces unique strings cannot guarantee their unpredictability.

Libraries that produce IDs which are both unique and unpredictable typically achieve this by combining two separate algorithms: one that generates a unique string (e.g., based on timestamps or counters) and another that generates an unpredictable string (e.g., using cryptographic randomness). These two strings are then concatenated to form the final ID, ensuring both properties are satisfied.

If you need a library for creating cryptographically secure passwords, consider crypto-pwd-generator

Installation

# with npm
npm install kinoid

# with yarn
yarn add kinoid

How to use

CommonJS

const { newId, decodeId } = require('kinoid')();

const newBook = {
  title: 'The absence of non-existent unthoughts',
  author: 'John White',
  publisher: 'Hypercubes',
  id: newId(),
};

db.add(newBook);

console.log(`Generated id '${id}' on ${decodeId(id).date.toDateString()}`);
// Generated id 'cohb4z87mvoyf1zjy' on Tue Nov 19 2024

Modules

import kinoid from 'kinoid';
const { newId, decodeId } = kinoid();

const id = newId();

console.log(id);
// cohb4z87mvoyf1zjy

console.log(`The id '${id}' was generated on ${decodeId(id).date.toDateString()}`);
// The id 'cohb4z87mvoyf1zjy' was generated on Tue Nov 19 2024

console.log(decodeId(id));
// {
//   id: 'cohb4z87mvoyf1zjy',
//   date: 2024-11-19T16:52:19.962Z,
//   singularity: 1144,
//   pid: 5438
// }

const invalidId = 'c1vz87moyfzjyoHB4';
console.log(decodeId(invalidId));
// {
//   id: 'c1vz87moyfzjyoHB4',
//   error: 'the string c1vz87moyfzjyoHB4 is not a valid ID'
// }

Using the browser

Open in SandBox

<!doctype html>
<html lang="en">
  <head>
    <title>ID generator</title>
    <script src="https://cdn.jsdelivr.net/npm/kinoid@3"></script>
    <!--
    you may also use
    <script src="https://unpkg.com/kinoid@3"></script>
    -->
    <script>
      const { newId, decodeId } = kinoid();
      function clickHandler() {
        const id = newId();
        const idStruct = decodeId(id);

        document.getElementById('id-viewer').innerHTML = `
<pre>
ID:          <b>${id}</b>
time:        ${idStruct.date.toISOString()}
singularity: ${idStruct.singularity}
process:     ${idStruct.pid}
</pre>`;
      }
    </script>
  </head>

  <body>
    <button onclick="clickHandler()">get new ID</button>
    <div id="id-viewer" style="font-family: monospace">
      <p>here will be an ID</p>
    </div>
  </body>
</html>

Contributing

Contributions to this project are welcomed!

Whether you have

  • questions, concerns, or suggestions for improving this library
  • want to report a bug
  • submit a fix
  • propose new features

please don't hesitate to reach out to me on GitHub and open an issue.

Disclaimer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

changelog

Change Log

[3.0.8] - 2025-04-22

Changed

  • package.json: improved description for SEO, optimized keywords with relevant terms, included sideEffects for tree-shaking, added exports for module compatibility, updated engines with minimum npm version, and added new scripts for a message to the user.
  • index.js: small aesthetic changes
  • README.md: distinction between commonJs and modules

Added

  • index.js {decodeId()}: A clearer and more descriptive error message is issued when an element of the ID is out of the range

[3.0.7] - 2025-04-13

Changed

  • index.js: rewritten the jsdoc documentation

[3.0.6] - 2025-04-13

Added

  • kinoid.test.js: Jest tests
  • index.js {logError()}: custom logger

Changed

  • index.js {isNode}: now it's a boolean constant, not a function
  • index.js {decodeIOd()}: better documentation
  • index.js {decodeIOd()}: better code structure

[3.0.5] - 2025-01-13

Changed

  • README.md: deleted some unuseful target="_blank" in anchor tags
  • README.md: changed the first paragraph, now equal to the package.json description
  • README.md: one more badge
  • index.js {int36ToBigInt}: fixed the documentation

[3.0.4] - 2024-12-20

Changed

  • index.js: redefined bigint zero: some web services do not recognize the 0n syntax
  • index.js: deleted the object {error: ...}; added a validId field into the return of decodeId()
  • README.md: a better button label
  • README.md: simplified the HTML example
  • README.md: updated with the new decodeId() return object
  • README.md: badges correctly link to external services
  • README.md: a deeper example of decodeId()
  • README.md: updated the sandbox with the actual example
  • package.json: description optimized for some services

Added

  • README.md: a new badge: unpkg
  • package.json: new tags: cli and backend

[3.0.3] - 2024-12-13

Changed

  • index.js: some strings now have a better language style
  • README.md: some strings now have a better language style

Added

  • index.js: a lot of documentation
  • index.js {int36ToBigInt()}: error checking; a better documentation
  • index.js {decodeId()}: error checking
  • index.js {idRe}: the format for an ID
  • index.js {hasIdStructure()}
  • README.md: link to a sandbox
  • README.md: jsdelivr badge

Removed

  • package.json: the bin field is not necessary, as index.js is not an executable file.

[3.0.2] - 2024-12-06

Added

  • README.md: a warning about the "import" feature
  • README.md: the Node version badge
  • README.md: added a second example of using CDN
  • package.json: some scripts

Changed

  • index.js: better coding style
  • index.js: the documentation was entirely written in jsdoc
  • tests/: a folder specifically intended for test files
  • README.md: fully restructured

Removed

  • todo.md: purged from the repo

[3.0.1] - 2024-11-30

Added

  • index.js: the code has been restructured so that it can run in both Node and the browser
  • README.md: added an example of how to use kinoid in the browser

Changed

  • index.js: changed some variables name
  • README.md: better style
  • package.json: The minimum version of Node now is 14

[2.0.1] - 2024-11-24

Changed

  • index.js: refactoring
  • README.md: Rewritten some paragraphs

Added

  • package.json: new keyword

Removed

  • README.md: one badge less

[2.0.0] - 2024-11-22

Changed

  • index.js: more elegance in the code
  • index.js: the IDs can be sorted based on the generation date
  • index.js: changed the length of the ID fields
  • index.js: fully documented code
  • index.js: starting time is 2024-11-01

Added

  • README.md: one more badge
  • index.js: decodeId() decodes an id returning the generation date, singularity factor and process number
  • README.md: a chapter about "features";
  • README.md: changed the examples into the chapter "How to use"
  • benchmark.js: a new set of tests
  • index.js: error checking and handling

Removed

  • index.js: eliminated all mechanisms for generating random strings

[1.1.1] - 2024-11-18

Changed

  • index.js: the singularity is resetted every time the timeStr changes
  • tests.js: renaming

Fixed

  • index.js: better documentation
  • package.json: typo
  • README.md: better documentation

[1.1.0] - 2024-11-17

The library is fully working as an NPM package

Added

  • CHANGELOG.md

[1.0.0] - 2024-11-16

Library created and working