import { isEqual } from "lodash"; const LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; const LENGTH = 12; export function quoteJavaScript(str: string): string { return "'" + (""+str).replace(/['\\]/g, '\\$1').replace(/\n/g, "\\n") + "'"; } export function quoteHtml(str: string): string { return str.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); } export function quoteRegExp(str: string): string { return (str+'').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&"); } export function generateRandomPadId(length: number = LENGTH): string { let randomPadId = ""; for(let i=0; i, to: Record): void { for(const i in to) delete to[i]; for(const i in from) to[i] = from[i]; } /** * Converts an object { entry: { subentry: "value" } } into { "entry.subentry": "value" } * @param obj {Object} * @return {Object} */ export function flattenObject(obj: Record, _prefix = ""): Record { const ret: Record = { }; for(const i in obj) { if(typeof obj[i] == "object") Object.assign(ret, flattenObject(obj[i], _prefix + i + ".")); else ret[_prefix + i] = obj[i]; } return ret; } interface ObjectDiffItem { index: string; before: any; after: any; } export function getObjectDiff(obj1: Record, obj2: Record): Array { const flat1 = flattenObject(obj1); const flat2 = flattenObject(obj2); const ret: Array = [ ]; for(const i in flat1) { if(flat1[i] != flat2[i] && !(!flat1[i] && !flat2[i])) ret.push({ index: i, before: flat1[i], after: flat2[i] }); } for(const i in flat2) { if(!(i in flat1) && !(!flat1[i] && !flat2[i])) ret.push({ index: i, before: undefined, after: flat2[i] }); } return ret; } export function decodeQueryString(str: string): Record { const obj: Record = { }; for(const segment of str.replace(/^\?/, "").split(/[;&]/)) { const pair = segment.split("="); obj[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); } return obj; } export function encodeQueryString(obj: Record): string { const pairs = [ ]; for(const i in obj) { pairs.push(encodeURIComponent(i) + "=" + encodeURIComponent(obj[i])); } return pairs.join("&"); } export function clone(obj: T): T { return obj != null ? JSON.parse(JSON.stringify(obj)) : obj; }