UNPKG

h-query-api

Version:

An API for JavaScript to simply parse and handle URL parameters

73 lines (40 loc) 1.79 kB
# h-query-api ## Install ```base npm install h-query-api --save ``` **Not `npm install hqueryapi`!!!!!** ## ES Module Example for how to import the ES module from another module: ```javascript import hQuery from "h-query-api"; hQuery.parse("name=哈哈&sex=男&age=25") // { name: "哈哈", sex: "男", age: 25 } ``` ```javascript import hQuery from "h-query-api"; hQuery.stringify({ name: "哈哈", sex: "男", age: 25 }) // "name=哈哈&sex=男&age=25" ``` perhaps ```javascript import { parse } from "h-query-api"; parse("name=哈哈&sex=男&age=25") // { name: "哈哈", sex: "男", age: 25 } ``` ```javascript import { stringify } from "h-query-api"; stringify({ name: "哈哈", sex: "男", age: 25 }) // "name=哈哈&sex=男&age=25" ``` use encodeuriccomponent encoding ```javascript import { stringify } from "h-query-api"; // encode true 开启, false 关闭,默认为 false console.log(stringify({ name: "小安", id: 1, a: undefined, file: null, adress: "就是肯定能接收到的绝对是女生"}, { encode: true, isEmpty: false })); // 返回:name=%E5%B0%8F%E5%AE%89&id=1%2C3%2C4%2C5%2C6&a=&file=&adress=%E5%B0%B1%E6%98%AF%E8%82%AF%E5%AE%9A%E8%83%BD%E6%8E%A5%E6%94%B6%E5%88%B0%E7%9A%84%E7%BB%9D%E5%AF%B9%E6%98%AF%E5%A5%B3%E7%94%9F ``` use decodeuricComponent decoding ```javascript import { parse } from "h-query-api"; // decode true 开启, false 关闭,默认为 false console.log(parse( 'name=%E5%B0%8F%E5%AE%89&id=1%2C3%2C4%2C5%2C6& adress=%E5%B0%B1%E6%98%AF%E8%82%AF%E5%AE%9A%E8%83%BD%E6%8E%A5%E6%94%B6%E5%88%B0%E7%9A%84%E7%BB%9D%E5%AF%B9%E6%98%AF%E5%A5%B3%E7%94%9F', { decode: true, sort: (a, b) => (b - a) })) // 返回:{ name: "小安", id: 1, a: "", file: "", adress: "就是肯定能接收到的绝对是女生" ```