GithubHelp home page GithubHelp logo

jsprds / shortener Goto Github PK

View Code? Open in Web Editor NEW

This project forked from studiohyperdrive/shortener

0.0 2.0 0.0 69 KB

Light-weight JavaScript utility to create an url shortener with

License: MIT License

JavaScript 100.00%

shortener's Introduction

Shortener


Light-weight JavaScript utility to create an url shortener with
Only ~3.5kb, no external dependencies, supports NodeJS 6+ and ES2015+


  • Lightweight: ~3.5kb in size, vanilla JavaScript and no external dependencies ๐Ÿœ
  • Fast: generates 100 shortened urls in less than 5ms ๐Ÿšค
  • Configurable: lends ultimate control over the output format of the shortened urls ๐Ÿ› ๏ธ
  • Tested: 100% code coverage ๐ŸŽ‰
  • MIT Licensed: free for personal and commercial use โค๏ธ

How to install

Install with:

  • NPM: npm install @studiohyperdrive/shortener
  • Yarn: yarn add @studiohyperdrive/shortener

How to use

CommonJS (Node 6+)

const Shortener = require("@studiohyperdrive/shortener")

ESModule (ES2015+)

import Shortener from "@studiohyperdrive/shortener"

Configuration

An instance of the shortener can be created by calling the constructor with a configuration.

const shortener = new Shortener(configuration)

The following parameters are accepted in the configuration:

Key Description Default
targetrequired A valid url to append the shortened id to, by default the id is added at the end of the url.

Optionally, you can pass a placeholder {id} to reposition the id anywhere in the url.
Example: https://short.er?id={0} โ†’ https://shorter?id=SP4C3
none
length Length of the shortened id. 5
alphabet Alphabet with allowed characters for generating the shortened id,
you can also choose one of the included presets.
base58

Example:

const shortener = new Shortener({
  target: "https://short.er"
  length: 6,
  alphabet: "0123456789abcdefABCDEF"
})

Presets

The alphabet parameter accepts multiple presets:

Name Value
alphanumeric abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
alphabetic abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
numeric 0123456789
base58 abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789
base56 abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789
base32 ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
base16 ABCDEF0123456789

Example:

const shortener = new Shortener({
  target: "https://short.er"
  alphabet: "alphanumeric"
})

.shorten(url)

Shortens a given url.

Example:

const result = shortener.shorten("https://www.google.com");

result output:

{
  id: "6VYN9",
  original: "https://www.google.com",
  target: "https://short.er/6VYN9"
}

.getInfo()

Gets information about the current configuration

Example:

const info = shortener.getInfo();

info output:

{
  config: {
    target: 'https://short.er',
    length: 5,
    alphabet: 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789'
  },
  collisionInfo: {
    // The amount of unique shortened id's that can be generated
    // with the current configuration of length & alphabet
    possibilities: 656356768,

    //The probability of generating a non-unique shortened id
    probability: 1.5235616493254474e-9
  }
}

Examples

A minimal usage example for Express + Mongoose can be found here

Contributing

Feel free to provide feedback, open issues or create pull-requests to this repository.

License

Shortener is MIT licensed by Studio Hyperdrive.

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.