Giter Site home page Giter Site logo

sqids / sqids-javascript Goto Github PK

View Code? Open in Web Editor NEW
534.0 5.0 11.0 1.33 MB

Official JavaScript port of Sqids. Generate short unique IDs from numbers.

Home Page: https://sqids.org/javascript

License: MIT License

TypeScript 99.41% Shell 0.59%
id-generator javascript sqids typescript hashids short-id short-url unique-id unique-id-generator id

sqids-javascript's Introduction

Sqids Specification

Github Actions

This is the main repository for Sqids specification. It is meant to be the guide for future ports of different languages.

The code is optimized for readability and clarity; individual implementations should optimize for performance as needed.

All unit tests should have matching results.

๐Ÿ‘ฉโ€๐Ÿ’ป Get started

npm install
npm test

The main Sqids library is in src/index.ts; unit tests are in src/tests.

Use the following to format & check changes:

npm run format
npm run lint

๐Ÿšง Improvements (over Hashids)

  1. The user is not required to provide randomized input anymore (there's still support for custom IDs).
  2. Better internal alphabet shuffling function.
  3. With default alphabet - Hashids is using base 49 for encoding-only, whereas Sqids is using base 61.
  4. Safer public IDs, with support for custom blocklist of words.
  5. Separators are no longer limited to characters "c, s, f, h, u, i, t". Instead, it's one rotating separator assigned on the fly.
  6. Simpler & smaller implementation: only "encode" & "decode" functions.

๐Ÿ”ฌ How it works

Sqids is basically a decimal to hexademical conversion, but with a few extra features. The alphabet is larger, it supports encoding several numbers into a single ID, and it makes sure generated IDs are URL-safe (no common profanity).

Here's how encoding works:

  1. An offset index is chosen from the given input
  2. Alphabet is split into two pieces using that offset and those two halfs are swapped
  3. Alphabet is reversed
  4. For each input number:
    1. The first character from the alphabet is reserved to be used as separator
    2. The rest of the alphabet is used to encode the number into an ID
    3. If this is not the last number in the input array, the separator character is appended
    4. The alphabet is shuffled
  5. If the generated ID does not meet the minLength requirement:
    • The separator character is appended
    • If still does not meet requirement:
      • Another shuffle occurs
      • The separator character is again appended to the remaining id + however many characters needed to meet the requirement
  6. If the blocklist function matches the generated ID:
    • offset index is incremented by 1, but never more than the length of the alphabet (in that case throw error)
    • Re-encode (repeat the whole procedure again)

Decoding is the same process but in reverse. A few things worth noting:

  • If two separators are right next to each other within the ID, that's fine - it just means the rest of the ID are junk characters used to satisfy the minLength requirement
  • The decoding function does not check if ID is valid/canonical, because we want blocked IDs to still be decodable (the user can check for this stuff themselves by re-encoding decoded numbers)

๐Ÿ“ฆ How to port Sqids to another language?

Implementations of new languages are more than welcome! To start:

  1. Make sure the language is not already implemented. At this point, if you see a Hashids implementation, but not a Sqids implementation: we could use your help on converting it.
  2. The main spec is here: https://github.com/sqids/sqids-spec/blob/main/src/index.ts. It's ~300 lines of code and heavily commented. Comments are there for clarity, they don't have to exist in your own implementation.
  3. Fork the repository/language you'd like to implement to your own Github account. If the repository/language does not exist under the Sqids Github account, open a new issue under the spec repo so we can create a blank repo first.
  4. Implement the main library + unit tests + Github Actions (if applicable). You do not need to port tests in the internal folder; they are there to test the algorithm itself.
  5. Add a README.md -- you can re-use any of the existing ones.
  6. Please use the blocklist from https://github.com/sqids/sqids-blocklist. It will contain the most up-to-date list. Do not copy and paste the blocklist from other implementations, as they might not be up-to-date.
  7. Create a pull request, so we can review & merge it.
  8. If the repo has no active maintainers, we'll invite you to manage it (and maybe even merge your own PR).
  9. Once the library is ready, we'll update the website.

๐Ÿ“‹ Notes

  • The reason prefix character is used is to randomize sequential inputs (eg: [0, 1], [0, 2], [0, 3]). Without the extra prefix character embedded into the ID, the output would start with the same characters.
  • Internal shuffle function does not use random input. It consistently produces the same output.
  • If new words are blocked (or removed from the blocklist), the encode() function might produce new IDs, but the decode() function would still work for old/blocked IDs, plus new IDs. So, there's more than one ID that can be produced for same numbers.
  • FAQ section is here: https://sqids.org/faq

๐Ÿป License

Every official Sqids library is MIT-licensed.

sqids-javascript's People

Contributors

4kimov avatar coolzjy avatar dependabot[bot] avatar evanhahn avatar niieani avatar pcoffline avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

sqids-javascript's Issues

ReferenceError: Blob is not defined (NodeJS 16)

It doesn't run in NodeJS, because Blob is used globally without being imported.

/home/toilal/projects/pysae/driver/node_modules/sqids/src/sqids.ts:584
    if (new Blob([alphabet]).size !== alphabet.length) {
    ^

ReferenceError: Blob is not defined
    at Sqids (/home/toilal/projects/pysae/driver/node_modules/sqids/src/sqids.ts:584:5)
   ...

Create a function to check if the const encoded is valid

As an update, it is worth inserting a function that can validate the value being decoded. If this value is not created with the minLenght and Alphabet, it could return an empty value, or length == 0. This way we could receive n values , but only decode information that respects standards.

First push

Hey @niieani, I made a good mess in this repo (out of all the awesomeness you have going on in https://github.com/niieani/hashids.js)

A few notes:

  • Tests have been replaced with those from the spec (maybe we should copy over your custom ones too?)
  • I did not copy over src/tests/tests-mjs (mostly because I don't know what they are)
  • I'll copy over the basic README (like the one Go has), but we should also copy over a lot of the details you have in the original README
  • For tests I kept vitest in there (should we use something else?)

Do you think you could take a look when you get a chance so this can become a proper repo? In the meantime, I'm still cleaning up random bits and pieces.

Sqids does not support Node.js >= 16

When running the following code with Node.js 16, an error occurs:

const Sqids = require("sqids").default;

const sqids = new Sqids({ minLength: 6 });

The error has the following stack-trace:

ReferenceError: Blob is not defined
    at new Sqids (node_modules/sqids/cjs/sqids.js:576:9)

where Blob is being used. In Node.js 16, Blob is not a global object and needs to be imported using

import { Blob } from 'node:buffer';
// or const { Blob } = require('node:buffer');

Note that the example above is using CommonJS syntax, but this issue occurs when using ES6 imports and when trying to import from sqids/esm/sqids as well.

Missing Node Support

hashIDs supported Node.js use, which makes sense in my opinion for this library. Removing Blob from the js code may be the solution to be able to use it in Node as well.

Broken packaging: Unexpected token 'export'

How do we package all this stuff? Tests are passing, but when using via npm/yarn in external projects, getting:

.../node_modules/sqids/esm/sqids.js:1
export const defaultOptions = {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.