Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including links-shortener with all npm packages installed. Try it out:

var linksShortener = require("links-shortener")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

links-shortener v1.0.666666

JS library for shortening links depending on the search phrase

links-shortener

Java Script library for shortening links depending on the search phrase.

github issue dependencies Status license npm version npm bundle size

Features

  • Very easy to understand
  • Configurable max length of link
  • Only one dependency

Installation

  • download from GitHub
  • npm: npm install links-shortener

makeURLShorter

makeURLShorter - main function for shortening link. Parameters:

  • link
  • searchValue
  • maxLength

See examples below

Examples

The examples below showing how the link is shortening depending on where the search phrase (searchValue) is.

Search phrase in url domain

    var linkShortener = require('links-shortener');

    var link = "http://www.subdomain.domain.com/directory2018/home/test/cat3?param1&param2&param3#lastpartofverylongurl";
    var maxLength = 40;      // max lenght of link
    var searchValue = 'com';

    var shortenedLink = linkShortener.makeURLShorter(link, searchValue, maxLength);
    console.log(shortenedLink);

    //prints 'www.subdomain.domain.com/directory2018/home/test/cat3?param1&param2&param3#lastpartofverylongurl'

Search phrase in url path

    var searchValue = 'test';

    var shortenedLink = linkShortener.makeURLShorter(link, searchValue, maxLength);
    console.log(shortenedLink);

    // prints '...domain.domain.com/...home/test/cat3?param1&param2&param3#lastpartofverylongurl'    

Search phrase in url query

    var searchValue = 'param2';

    var shortenedLink = linkShortener.makeURLShorter(link, searchValue, maxLength);
    console.log(shortenedLink);

    // prints '...domain.com/directory...?...ram1&param2&param#lastpartofverylongurl'

Search phrase(searchValue) in url fragment

    var searchValue = 'part';

    var shortenedLink = linkShortener.makeURLShorter(link, searchValue, maxLength);
    console.log(shortenedLink);

    //prints '...ain.com/direct...?param1...#lastpartofverylongurl'
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free