mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-07 22:18:33 +00:00
49 lines
1.1 KiB
JavaScript
49 lines
1.1 KiB
JavaScript
import { customAlphabet } from 'nanoid';
|
|
|
|
// a customized version of nanoid without using _ and -
|
|
export const uuid = () => {
|
|
// https://github.com/ai/nanoid/blob/main/url-alphabet/index.js
|
|
const urlAlphabet = 'useandom26T198340PX75pxJACKVERYMINDBUSHWOLFGQZbfghjklqvwyzrict';
|
|
const customNanoId = customAlphabet(urlAlphabet, 21);
|
|
|
|
return customNanoId();
|
|
};
|
|
|
|
export const simpleHash = (str) => {
|
|
let hash = 0;
|
|
for (let i = 0; i < str.length; i++) {
|
|
const char = str.charCodeAt(i);
|
|
hash = (hash << 5) - hash + char;
|
|
hash &= hash; // Convert to 32bit integer
|
|
}
|
|
return new Uint32Array([hash])[0].toString(36);
|
|
};
|
|
|
|
export const waitForNextTick = () => {
|
|
return new Promise((resolve, reject) => {
|
|
setTimeout(() => resolve(), 0);
|
|
});
|
|
};
|
|
|
|
export const safeParseJSON = (str) => {
|
|
if(!str || !str.length || typeof str !== 'string') {
|
|
return str;
|
|
}
|
|
try {
|
|
return JSON.parse(str);
|
|
} catch (e) {
|
|
return str;
|
|
}
|
|
};
|
|
|
|
export const safeStringifyJSON = (obj) => {
|
|
if(!obj) {
|
|
return obj;
|
|
}
|
|
try {
|
|
return JSON.stringify(obj);
|
|
} catch (e) {
|
|
return obj;
|
|
}
|
|
}
|