mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 23:00:30 +00:00
5f4c19da35
Co-authored-by: Opender Singh <opender.singh@konghq.com>
35 lines
823 B
TypeScript
35 lines
823 B
TypeScript
export function deterministicStringify(value: any) {
|
|
const t = Object.prototype.toString.call(value);
|
|
|
|
if (t === '[object Object]') {
|
|
const pairs: Array<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: Array<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;
|
|
}
|