npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

url-variables

v1.0.3

Published

URLVariables class for TypeScript

Downloads

1,069

Readme

URLVariables

GitHub license npm version Downloads Build Status Chat on Gitter

URLVariables, it's the most simple, concise and eledic library for URL-encoding & URL-decoding. You can parse or encode url-string very easily with this URLVariables.

import URLVariables = require("url-variables");

let url_str: string = "https://some_url?id=samchon&age=29&is_crazy=true";
let author: IAuthor = URLVariables.parse<IAuthor>(url_str);

console.log(author.id); // "samchon" => string
console.log(author.age); // 29 => number
console.log(author.is_crazy); // true => boolean

interface IAuthor
{
    id: string;
    age: number;
    is_crazy: boolean;
}

Installation

NPM Module

Installing URLVariables, it's very easy. Just install with the npm

# Install URLVariables from the NPM module
npm install --save url-variables

Usage

API Reference

To use URLVariables, just remember that below structure, then you'll understand how to use it.

If you want to know more about the deatiled features, then utilize auto-completion of TypeScript or read the Guide Documents. You also want to know about the std.HashMap, base class of the URLVariables, then visit the TSTL proejct.

namespace URLVariables
{
    // DYNAMIC OBJECT
    function parse(str: string, autoCase: boolean = true): Object;
    function stringify(obj: Object): string;
}

class URLVariables extends std.HashMap<string, string>
{
    // CONSTRUCTORS & CONVERTERS
    public constructor();
    public constructor(url_encoded_str: string); // PARSE
    public toString(); // RETURNS URL-ENCODED STRING

    // ACCESSORS
    public has(key: string): boolean;
    public get(key: string): string;
    public set(key: string, value: string): string;
}

Example Code

In this section, we'll see global functions URLVariables.parse() and URLVariables.stringify(), handling dynamic objects, first. After that, we'll study URLVariables class, extended from std.HashMap class, who is much safer then handling dynamic objects.

example.ts

import std = require("tstl");
import URLVariables = require("url-variables");

type Element = std.Entry<string, string>;

interface IAuthor
{
    name: string;
    age: number;
    git: string;
    homepage: string;
    memo: string;
    is_crazy: boolean;
}

function main(): void
{
    let author: IAuthor = 
    {
        name: "Samchon (Jeongho Nam)",
        age: 29,
        git: "https://github.com/samchon/tstl",
        homepage: "http://samchon.org",
        memo: "Hello, I'm the best programmer in Korea.",
        is_crazy: true
    };
    
    test_global(author);
    test_class(author);
}

function test_global(author: IAuthor): void
{
    //----
    // STRINGIFY & PARSE
    //----
    // STRINGIFY -> OBJECT TO URL-ENCODED STRING
    let url_encoded_str: string = URLVariables.stringify(author);

    console.log(url_encoded_str);
    console.log("----------------------------------\n");

    // PARSE -> URL-ENCODED STRING TO OBJECT
    let obj: IAuthor = URLVariables.parse(url_encoded_str, true);

    //----
    // VALIDATE
    //----
    // VALIDATE STRINGIFY
    if (url_encoded_str != URLVariables.stringify(obj))
        throw new std.DomainError("Error on URLVariables.decode().");

    // VALIDATE PARSE -> (AUTHOR == OBJ)?
    for (let key in author)
        if (author[key] != obj[key])
            throw new std.DomainError("Error on URLVariables.parse().");

    // PRINT A DYNAMIC OBJECT, CREATED BY URL-ENCODED STRING
    console.log("Re-generated Dynamic Object by url-encoding & decoding:\n");
    console.log(obj);
}

function test_class(author: IAuthor): void
{
    console.log("\n----------------------------------\n");

    //----
    // GENERATE URL-VARIABLES OBJECT
    //----
    let dict: URLVariables = new URLVariables();
    
    // FILL ELEMENTS
    dict.set("name", author.name);
    dict.set("age", String(author.age)); // MUST BE STRING
    dict.set("git", author.git);
    dict.set("homepage", author.homepage);
    dict.set("memo", author.memo);
    dict.set("is_crazy", String(author.is_crazy));

    // CONVERT THE URL-VARIABLES OBJECT TO URL-ENCODED STRING
    let url_encoded_str: string = dict.toString();

    //----
    // VALIDATIONS
    //----
    // CREATE A NEW URL-VARIABLES OBJECT 
    // BY PARSING THE URL-ENCODED STRING
    let vars: URLVariables = new URLVariables(url_encoded_str);

    // VALIDATE SIZE
    if (dict.size() != vars.size())
        throw new std.LengthError("Size are different.");

    // ALL ELEMENTS ARE EQUAL
    let equal: boolean = std.equal
    (
        dict.begin(), dict.end(), vars.begin(), 
        function (x: Element, y: Element): boolean
        {
            return x.first == y.first && x.second == y.second;
        }
    );
    if (equal == false)
        throw new std.InvalidArgument("Elements are different.");

    // ALL ELEMENTS ARE EQUAL, THEN ENCODINGS MUST BE SAME
    if (dict.toString() != vars.toString())
        throw new std.DomainError("Error on URLVariables.toString().");

    //----
    // ACCESSORS
    //----
    // ACCESS TO MEMBERS BY URLVariables.get()
    console.log
    (
        `${dict.get("memo")} \n` + 
        `I'm ${dict.get("name")} and ${dict.get("age")} years old. \n` +
        `I've published my libraries on: \n` +
        `  - GitHub: ${dict.get("git")} \n` +
        `  - Homepage: ${dict.get("homepage")} \n`
    );

    // TEST WHETHER SOME VARIABLES ARE REGISTERED OR NOT
    console.log("Am I crazy?:", dict.has("is_crazy"));
    console.log("Has name?:", dict.has("name"));
    console.log("Has nickname?:", dict.has("nickname"));
}

main();

output

name=Samchon%20(Jeongho%20Nam)&age=29&git=https%3A%2F%2Fgithub.com%2Fsamchon%2Ftstl&homepage=http%3A%2F%2Fsamchon.org&memo=Hello%2C%20I'm%20the%20best%20programmer%20in%20Korea.&is_crazy=true
----------------------------------

Re-generated Dynamic Object by url-encoding & decoding:

{ name: 'Samchon (Jeongho Nam)',
  age: 29,
  git: 'https://github.com/samchon/tstl',
  homepage: 'http://samchon.org',
  memo: 'Hello, I\'m the best programmer in Korea.',
  is_crazy: true }

----------------------------------

Hello, I'm the best programmer in Korea.
I'm Samchon (Jeongho Nam) and 29 years old.
I've published my libraries on:
  - GitHub: https://github.com/samchon/tstl
  - Homepage: http://samchon.org

Am I crazy?: true
Has name?: true
Has nickname?: false

References