mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-27 14:44:07 +00:00
121 lines
2.8 KiB
JavaScript
121 lines
2.8 KiB
JavaScript
import { customAlphabet } from 'nanoid';
|
|
import xmlFormat from 'xml-formatter';
|
|
|
|
// 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, indent = false) => {
|
|
if (obj === undefined) {
|
|
return obj;
|
|
}
|
|
try {
|
|
if (indent) {
|
|
return JSON.stringify(obj, null, 2);
|
|
}
|
|
return JSON.stringify(obj);
|
|
} catch (e) {
|
|
return obj;
|
|
}
|
|
};
|
|
|
|
export const convertToCodeMirrorJson = (obj) => {
|
|
try {
|
|
return JSON5.stringify(obj).slice(1, -1);
|
|
} catch (e) {
|
|
return obj;
|
|
}
|
|
};
|
|
|
|
export const safeParseXML = (str, options) => {
|
|
if (!str || !str.length || typeof str !== 'string') {
|
|
return str;
|
|
}
|
|
try {
|
|
return xmlFormat(str, options);
|
|
} catch (e) {
|
|
return str;
|
|
}
|
|
};
|
|
|
|
// Remove any characters that are not alphanumeric, spaces, hyphens, or underscores
|
|
export const normalizeFileName = (name) => {
|
|
if (!name) {
|
|
return name;
|
|
}
|
|
|
|
const validChars = /[^\w\s-]/g;
|
|
const formattedName = name.replace(validChars, '-');
|
|
|
|
return formattedName;
|
|
};
|
|
|
|
export const getContentType = (headers) => {
|
|
if (headers && headers.length) {
|
|
let contentType = headers
|
|
.filter((header) => header[0].toLowerCase() === 'content-type')
|
|
.map((header) => {
|
|
return header[1];
|
|
});
|
|
if (contentType && contentType.length) {
|
|
if (typeof contentType[0] == 'string' && /^[\w\-]+\/([\w\-]+\+)?json/.test(contentType[0])) {
|
|
return 'application/ld+json';
|
|
} else if (typeof contentType[0] == 'string' && /^[\w\-]+\/([\w\-]+\+)?xml/.test(contentType[0])) {
|
|
return 'application/xml';
|
|
}
|
|
|
|
return contentType[0];
|
|
}
|
|
}
|
|
|
|
return '';
|
|
};
|
|
|
|
export const startsWith = (str, search) => {
|
|
if (!str || !str.length || typeof str !== 'string') {
|
|
return false;
|
|
}
|
|
|
|
if (!search || !search.length || typeof search !== 'string') {
|
|
return false;
|
|
}
|
|
|
|
return str.substr(0, search.length) === search;
|
|
};
|
|
|
|
export const pluralizeWord = (word, count) => {
|
|
return count === 1 ? word : `${word}s`;
|
|
};
|