2021-05-12 06:35:00 +00:00
|
|
|
export function deterministicStringify(value: any) {
|
2019-04-18 00:50:03 +00:00
|
|
|
const t = Object.prototype.toString.call(value);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
if (t === '[object Object]') {
|
2021-05-12 06:35:00 +00:00
|
|
|
const pairs: Array<string> = [];
|
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
for (const key of Object.keys(value).sort()) {
|
|
|
|
const k = deterministicStringify(key);
|
|
|
|
const v = deterministicStringify(value[key]);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
if (v !== '' && k !== '') {
|
|
|
|
pairs.push(`${k}:${v}`);
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
return `{${pairs.join(',')}}`;
|
|
|
|
} else if (t === '[object Array]') {
|
2021-05-12 06:35:00 +00:00
|
|
|
const items: Array<string> = [];
|
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
for (const v of value) {
|
|
|
|
const vStr = deterministicStringify(v);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
if (vStr !== '') {
|
|
|
|
items.push(vStr);
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
return `[${items.join(',')}]`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const str = JSON.stringify(value);
|
|
|
|
// Only return valid stringifyable things
|
|
|
|
return str === undefined ? '' : str;
|
|
|
|
}
|