mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 14:49:53 +00:00
35 lines
813 B
TypeScript
35 lines
813 B
TypeScript
export function deterministicStringify(value: any) {
|
|
const t = Object.prototype.toString.call(value);
|
|
|
|
if (t === '[object Object]') {
|
|
const pairs: string[] = [];
|
|
|
|
for (const key of Object.keys(value).sort()) {
|
|
const k = deterministicStringify(key);
|
|
const v = deterministicStringify(value[key]);
|
|
|
|
if (v !== '' && k !== '') {
|
|
pairs.push(`${k}:${v}`);
|
|
}
|
|
}
|
|
|
|
return `{${pairs.join(',')}}`;
|
|
} else if (t === '[object Array]') {
|
|
const items: string[] = [];
|
|
|
|
for (const v of value) {
|
|
const vStr = deterministicStringify(v);
|
|
|
|
if (vStr !== '') {
|
|
items.push(vStr);
|
|
}
|
|
}
|
|
|
|
return `[${items.join(',')}]`;
|
|
}
|
|
|
|
const str = JSON.stringify(value);
|
|
// Only return valid stringifyable things
|
|
return str === undefined ? '' : str;
|
|
}
|