2.0.2 • Published 7 years ago

@alexsasharegan/uri-parser v2.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
7 years ago

URI Parser

A handful of utility functions for creating & parsing URI's.

// ESM only
import UriParser from "@alexsasharegan/uri-parser"

/**
* [ string ] encode( [ Object, optional ] data = {} )
* URI encode an object to a flat string.
*/
let encodedData = UriParser.encode({
    number: 36,
    string: "John Doe",
    boolean: false,
    nullValue: null,
    undefinedValue: undefined,
    list: [1, 2, 3]
})

console.log(encodedData)
// number=36&string=John%20Doe&boolean=false&nullValue=null&list[]=1&list[]=2&list[]=3

/**
* [ Object ] parse( [ string, optional ] queryString = window.location.search )
* Parse a query string to a native object
*/
let parsedData = UriParser.parse(encodedData)

console.log(parsedData)
// { number: '36',
//   string: 'John Doe',
// 	 boolean: 'false',
// 	 nullValue: 'null',
// 	 list: [ '1', '2', '3' ] }

/**
* [ string ] invalidateCache( [ string ] link, [ string, optional ] cacheBustKey = '_' )
* Appends a Unix timestamp query string to a URL to invalidate cache
*/
let url = "http://placekitten.com/200/300"
let invalidatedURL = UriParser.invalidateCache(url)
let differentKey = UriParser.invalidateCache(url, "cacheBust")

console.log(invalidatedURL)
// http://placekitten.com/200/300?_=1487215265686
console.log(differentKey)
// http://placekitten.com/200/300?cacheBust=1487215265686

let options = UriParser.encode({
    imageid: 3546,
    width: 1000,
    height: 1500
})

let urlWithQ = `https://example.com/api/resize?${options}#hash`
// https://example.com/api/resize?imageid=3546&width=1000&height=1500

console.log(UriParser.parse(urlWithQ))
// { imageid: '3546', width: '1000', height: '1500' }

console.log(UriParser.invalidateCache(urlWithQ))
// https://example.com/api/resize?imageid=3546&width=1000&height=1500&_=1487216846706#hash
2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago