query-string.js 1.02 KB
"use strict";var strictUriEncode=require("strict-uri-encode"),objectAssign=require("object-assign");function encode(e,r){return r.encode?r.strict?strictUriEncode(e):encodeURIComponent(e):e}exports.extract=function(e){return e.split("?")[1]||""},exports.parse=function(e){var o=Object.create(null);return"string"!=typeof e||(e=e.trim().replace(/^(\?|#|&)/,""))&&e.split("&").forEach(function(e){var r=e.replace(/\+/g," ").split("="),n=r.shift(),t=0<r.length?r.join("="):void 0;n=decodeURIComponent(n),t=void 0===t?null:decodeURIComponent(t),void 0===o[n]?o[n]=t:Array.isArray(o[n])?o[n].push(t):o[n]=[o[n],t]}),o},exports.stringify=function(t,o){return o=objectAssign({encode:!0,strict:!0},o),t?Object.keys(t).sort().map(function(r){var e=t[r];if(void 0===e)return"";if(null===e)return encode(r,o);if(Array.isArray(e)){var n=[];return e.slice().forEach(function(e){void 0!==e&&(null===e?n.push(encode(r,o)):n.push(encode(r,o)+"="+encode(e,o)))}),n.join("&")}return encode(r,o)+"="+encode(e,o)}).filter(function(e){return 0<e.length}).join("&"):""};