Giter Club home page Giter Club logo

param-to-string's Introduction

param-to-string

Generates a unique string from a parameter.

Warning

I cannot say this library will not change. Please only use this for short term hashing purposes, or lock the version you are using of this library.

Usage

Given a parameter in javascript if we wanted to hash it we would first need to convert it into a string, that's what this library aims to do. It will create a hashable string from just about anything.

const paramToString = require('param-to-string');

paramToString('hello there');
// string:hello%20there;

We can create a string from arrays, objects and more.

const paramToString = require('param-to-string');

paramToString([11, 21, 'hello there']);
// array:number:11;number:21;string:hello%20there;;

Special cases

array

'array:{paramToString(param[index])};{...etc};;'

object

Object keys are sorted alphabetically.

'object:{key}:{paramToString(param[key])};{...etc};;'

date

'date:{utc(param)};'

null

'null;'

undefined

'undefined;'

function

Functions are rendered to strings.

'function:{param.name}:{escape(stringify(param))};'

Array cases

The following types are converted into an array prepended with their actual type. ['uint8array', 'uint8clampedarray', 'int8array', 'uint16array', 'int16array', 'uint32array', 'int32array', 'float32array', 'float64array', 'arraybuffer', 'set', 'weakset'] In the following format.

'type(param):array:{...etc};;'

Object cases

The following types are converted into an object prepended with their actual type. ['map', 'weakmap'] In the following format.

'type(param):object:{...etc};;'

Nesting

You can nest arrays and objects.

General case

'{type(param)}:{escape(stringify(param))};'

Considerations

Object keys are sorted alphabetically. It doesn't matter what order parameters are inserted in the object, this is true for how Map and Weakmap are handled as well, insertion order is ignored. Functions are rendered to strings.

Contribute

Sure!

param-to-string's People

Watchers

 avatar  avatar

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.