UNPKG

@tuzki/cli

Version:

🐇 lowcode-cli is an efficient cli tool for Rabbitpre plugin component secondary development. ❤️

50 lines (49 loc) 1.58 kB
/* * query 处理 * * @Author: nichubiao * @Date: 2023-02-22 13:53:09 * * Copyright © 2014-2023 Rabbitpre.com. All Rights Reserved. */ const encodeReserveRE = /[!'()*]/g; const encodeReserveReplacer = (c) => `%${c.charCodeAt(0).toString(16)}`; const commaRE = /%2C/g; const encode = (str) => encodeURIComponent(str) .replace(encodeReserveRE, encodeReserveReplacer) .replace(commaRE, ','); export function stringifyQuery(obj = null) { const res = typeof obj === 'string' ? obj : obj ? Object.keys(obj) .map((key) => { const val = obj[key]; if (typeof val === undefined) { return ''; } if (val === null) { return encode(key); } if (Array.isArray(val)) { const result = []; val.forEach(val2 => { if (val2 === undefined) { return; } if (val2 === null) { result.push(encode(key)); } else { result.push(`${encode(key)}=${encode(val2)}`); } }); return result.join('&'); } return `${encode(key)}=${encode(val)}`; }) .filter(x => x.length > 0) .join('&') : null; return res ? `?${res}` : ''; }