UNPKG

qs-like

Version:

A tiny query string parsing and stringifying library

62 lines (54 loc) 1.36 kB
/* * qs-like.js v1.0.1 * (c) 2018-2019 Jesse Feng * Released under the MIT License. */ import append from 'celia/_append'; import isObject from 'celia/isObject'; import forOwn from 'celia/_forOwn'; import escape from './escape.js'; var stringify = JSON.stringify; function convert(value, encode) { switch (typeof value) { case 'string': return encode(value); case 'number': if (isNaN(value)) { return ''; } case 'boolean': return value.toString(); case 'object': return value === null ? '' : encode(stringify(value)); case 'undefined': return ''; // case 'function': // return encode(value.toString()); default: return encode(value.toString()); } } var isArray = Array.isArray; function stringify$1 (obj, sep, eq, options) { if (isObject(obj)) { sep = sep || '&'; eq = eq || '='; options = options || {}; var encode = options.encodeURIComponent || escape; var arr = []; forOwn(obj, function (value, key) { if (key) { if (isArray(value)) { value.forEach(function (val) { append(arr, key + eq + convert(val, encode)); }); } else { append(arr, key + eq + convert(value, encode)); } } }); return arr.join(sep); } return ''; } export default stringify$1;