2018-05-23 06:39:01 +00:00
|
|
|
import fuzzysort from 'fuzzysort';
|
2021-07-22 23:04:56 +00:00
|
|
|
import { join as pathJoin } from 'path';
|
|
|
|
import { Readable, Writable } from 'stream';
|
2020-06-30 19:36:15 +00:00
|
|
|
import * as uuid from 'uuid';
|
2017-06-30 03:30:22 +00:00
|
|
|
import zlib from 'zlib';
|
2021-07-22 23:04:56 +00:00
|
|
|
|
|
|
|
import { DEBOUNCE_MILLIS, METHOD_DELETE, METHOD_OPTIONS } from './constants';
|
2016-09-21 00:03:26 +00:00
|
|
|
|
2017-08-04 16:54:11 +00:00
|
|
|
const ESCAPE_REGEX_MATCH = /[-[\]/{}()*+?.\\^$|]/g;
|
2017-02-16 17:38:59 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface Header {
|
|
|
|
name: string;
|
|
|
|
value: string;
|
|
|
|
}
|
2017-07-19 02:54:03 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface Parameter {
|
|
|
|
name: string;
|
|
|
|
value: string;
|
|
|
|
}
|
2018-03-28 20:48:58 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function filterParameters<T extends Parameter>(
|
2021-05-18 20:32:18 +00:00
|
|
|
parameters: T[],
|
2021-05-12 06:35:00 +00:00
|
|
|
name: string,
|
2021-05-18 20:32:18 +00:00
|
|
|
): T[] {
|
2018-03-28 20:48:58 +00:00
|
|
|
if (!Array.isArray(parameters) || !name) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
return parameters.filter(h => (!h || !h.name ? false : h.name === name));
|
2018-03-28 20:48:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function filterHeaders<T extends Header>(headers: T[], name?: string): T[] {
|
2021-05-12 06:35:00 +00:00
|
|
|
if (!Array.isArray(headers) || !name || typeof name !== 'string') {
|
2016-09-03 05:14:48 +00:00
|
|
|
return [];
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
return headers.filter(header => {
|
2020-05-02 18:19:18 +00:00
|
|
|
// Never match against invalid headers
|
2021-05-12 06:35:00 +00:00
|
|
|
if (!header || !header.name || typeof header.name !== 'string') {
|
2016-11-10 17:33:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-02 18:19:18 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
return header.name.toLowerCase() === name.toLowerCase();
|
2016-11-10 17:33:28 +00:00
|
|
|
});
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-03 05:14:48 +00:00
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function hasContentTypeHeader<T extends Header>(headers: T[]) {
|
2017-04-11 21:20:01 +00:00
|
|
|
return filterHeaders(headers, 'content-type').length > 0;
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function hasContentLengthHeader<T extends Header>(headers: T[]) {
|
2017-04-11 21:20:01 +00:00
|
|
|
return filterHeaders(headers, 'content-length').length > 0;
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function hasAuthHeader<T extends Header>(headers: T[]) {
|
2016-10-02 20:57:00 +00:00
|
|
|
return filterHeaders(headers, 'authorization').length > 0;
|
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function hasAcceptHeader<T extends Header>(headers: T[]) {
|
2016-12-08 20:29:40 +00:00
|
|
|
return filterHeaders(headers, 'accept').length > 0;
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function hasUserAgentHeader<T extends Header>(headers: T[]) {
|
2016-12-08 20:29:40 +00:00
|
|
|
return filterHeaders(headers, 'user-agent').length > 0;
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function hasAcceptEncodingHeader<T extends Header>(headers: T[]) {
|
2017-11-12 18:35:01 +00:00
|
|
|
return filterHeaders(headers, 'accept-encoding').length > 0;
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function getSetCookieHeaders<T extends Header>(headers: T[]): T[] {
|
2016-10-02 20:57:00 +00:00
|
|
|
return filterHeaders(headers, 'set-cookie');
|
|
|
|
}
|
2016-09-04 21:32:36 +00:00
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function getLocationHeader<T extends Header>(headers: T[]): T | null {
|
2017-11-13 23:10:53 +00:00
|
|
|
const matches = filterHeaders(headers, 'location');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function getContentTypeHeader<T extends Header>(headers: T[]): T | null {
|
2016-11-10 17:33:28 +00:00
|
|
|
const matches = filterHeaders(headers, 'content-type');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function getMethodOverrideHeader<T extends Header>(headers: T[]): T | null {
|
2020-03-11 19:23:52 +00:00
|
|
|
const matches = filterHeaders(headers, 'x-http-method-override');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function getHostHeader<T extends Header>(headers: T[]): T | null {
|
2018-02-08 09:09:13 +00:00
|
|
|
const matches = filterHeaders(headers, 'host');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function getContentDispositionHeader<T extends Header>(headers: T[]): T | null {
|
2017-09-18 18:19:04 +00:00
|
|
|
const matches = filterHeaders(headers, 'content-disposition');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function getContentLengthHeader<T extends Header>(headers: T[]): T | null {
|
2016-11-22 19:42:10 +00:00
|
|
|
const matches = filterHeaders(headers, 'content-length');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2016-09-04 21:32:36 +00:00
|
|
|
/**
|
|
|
|
* Generate an ID of the format "<MODEL_NAME>_<TIMESTAMP><RANDOM>"
|
|
|
|
* @param prefix
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-05-12 06:35:00 +00:00
|
|
|
export function generateId(prefix?: string) {
|
2016-10-21 17:20:36 +00:00
|
|
|
const id = uuid.v4().replace(/-/g, '');
|
2016-09-04 21:32:36 +00:00
|
|
|
|
|
|
|
if (prefix) {
|
2016-10-21 17:20:36 +00:00
|
|
|
return `${prefix}_${id}`;
|
2016-09-04 21:32:36 +00:00
|
|
|
} else {
|
2016-10-21 17:20:36 +00:00
|
|
|
return id;
|
2016-09-04 21:32:36 +00:00
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-22 19:44:28 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function delay(milliseconds: number = DEBOUNCE_MILLIS) {
|
|
|
|
return new Promise<void>(resolve => setTimeout(resolve, milliseconds));
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-10-26 17:49:49 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function removeVowels(str: string) {
|
2016-10-26 17:49:49 +00:00
|
|
|
return str.replace(/[aeiouyAEIOUY]/g, '');
|
|
|
|
}
|
2016-11-09 03:18:25 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function formatMethodName(method: string) {
|
2019-12-04 18:45:09 +00:00
|
|
|
let methodName = method || '';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-10-31 16:46:48 +00:00
|
|
|
if (method === METHOD_DELETE || method === METHOD_OPTIONS) {
|
|
|
|
methodName = method.slice(0, 3);
|
|
|
|
} else if (method.length > 4) {
|
|
|
|
methodName = removeVowels(method).slice(0, 4);
|
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-10-31 16:46:48 +00:00
|
|
|
return methodName;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function keyedDebounce<T extends Function>(callback: T, millis: number = DEBOUNCE_MILLIS): T {
|
2018-03-29 17:59:32 +00:00
|
|
|
let timeout;
|
2016-11-10 21:03:12 +00:00
|
|
|
let results = {};
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
|
|
|
const t: T = function(key, ...args) {
|
2016-11-10 21:03:12 +00:00
|
|
|
results[key] = args;
|
2016-11-09 03:18:25 +00:00
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(() => {
|
2016-11-10 21:03:12 +00:00
|
|
|
if (!Object.keys(results).length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(results);
|
|
|
|
results = {};
|
2016-11-09 03:18:25 +00:00
|
|
|
}, millis);
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2021-05-12 06:35:00 +00:00
|
|
|
return t;
|
2016-11-09 03:18:25 +00:00
|
|
|
}
|
2016-11-10 21:03:12 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function debounce<T extends Function>(
|
|
|
|
callback: T,
|
|
|
|
milliseconds: number = DEBOUNCE_MILLIS,
|
|
|
|
): T {
|
2016-11-10 21:03:12 +00:00
|
|
|
// For regular debounce, just use a keyed debounce with a fixed key
|
|
|
|
return keyedDebounce(results => {
|
2021-05-12 06:35:00 +00:00
|
|
|
// eslint-disable-next-line prefer-spread -- don't know if there was a "this binding" reason for this being this way so I'm leaving it alone
|
2020-04-09 17:32:19 +00:00
|
|
|
callback.apply(null, results.__key__);
|
2021-05-12 06:35:00 +00:00
|
|
|
}, milliseconds).bind(null, '__key__');
|
2016-11-10 21:03:12 +00:00
|
|
|
}
|
2016-11-22 22:26:52 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function describeByteSize(bytes: number, long = false) {
|
2016-11-22 22:26:52 +00:00
|
|
|
bytes = Math.round(bytes * 10) / 10;
|
|
|
|
let size;
|
2016-11-30 23:11:36 +00:00
|
|
|
// NOTE: We multiply these by 2 so we don't end up with
|
|
|
|
// values like 0 GB
|
2020-05-02 18:19:18 +00:00
|
|
|
let unit;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-11-30 23:11:36 +00:00
|
|
|
if (bytes < 1024 * 2) {
|
2016-11-22 22:26:52 +00:00
|
|
|
size = bytes;
|
2017-06-09 21:42:19 +00:00
|
|
|
unit = long ? 'bytes' : 'B';
|
2016-11-30 23:11:36 +00:00
|
|
|
} else if (bytes < 1024 * 1024 * 2) {
|
2016-11-22 22:26:52 +00:00
|
|
|
size = bytes / 1024;
|
2017-06-09 21:42:19 +00:00
|
|
|
unit = long ? 'kilobytes' : 'KB';
|
2016-12-30 23:06:27 +00:00
|
|
|
} else if (bytes < 1024 * 1024 * 1024 * 2) {
|
2016-11-22 22:26:52 +00:00
|
|
|
size = bytes / 1024 / 1024;
|
2017-06-09 21:42:19 +00:00
|
|
|
unit = long ? 'megabytes' : 'MB';
|
2016-11-22 22:26:52 +00:00
|
|
|
} else {
|
|
|
|
size = bytes / 1024 / 1024 / 1024;
|
2017-06-09 21:42:19 +00:00
|
|
|
unit = long ? 'gigabytes' : 'GB';
|
2016-11-22 22:26:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const rounded = Math.round(size * 10) / 10;
|
2017-03-03 20:09:08 +00:00
|
|
|
return `${rounded} ${unit}`;
|
2016-11-22 22:26:52 +00:00
|
|
|
}
|
2017-02-28 21:32:23 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function nullFn() {
|
2017-02-28 21:32:23 +00:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function preventDefault(e: Event) {
|
2017-02-28 21:32:23 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
2017-03-28 22:45:23 +00:00
|
|
|
|
2021-09-09 15:02:42 +00:00
|
|
|
export function xmlDecode(input: string) {
|
|
|
|
const ESCAPED_CHARACTERS_MAP = {
|
|
|
|
'&': '&',
|
|
|
|
'"': '"',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
};
|
|
|
|
|
|
|
|
return input.replace(/("|<|>|&)/g, (_: string, item: keyof typeof ESCAPED_CHARACTERS_MAP) => (
|
|
|
|
ESCAPED_CHARACTERS_MAP[item])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function fnOrString(v: string | ((...args: any[]) => any), ...args: any[]) {
|
2017-06-09 01:10:12 +00:00
|
|
|
if (typeof v === 'string') {
|
|
|
|
return v;
|
|
|
|
} else {
|
|
|
|
return v(...args);
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 03:30:22 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function compressObject(obj: any) {
|
2017-11-23 21:57:08 +00:00
|
|
|
const compressed = zlib.gzipSync(JSON.stringify(obj));
|
2017-06-30 03:30:22 +00:00
|
|
|
return compressed.toString('base64');
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:25:02 +00:00
|
|
|
export function decompressObject(input: string | null): any {
|
|
|
|
if (typeof input !== 'string') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-23 21:57:08 +00:00
|
|
|
const jsonBuffer = zlib.gunzipSync(Buffer.from(input, 'base64'));
|
2017-07-19 02:54:03 +00:00
|
|
|
return JSON.parse(jsonBuffer.toString('utf8'));
|
2017-06-30 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function resolveHomePath(p: string) {
|
2017-07-20 01:55:40 +00:00
|
|
|
if (p.indexOf('~/') === 0) {
|
|
|
|
return pathJoin(process.env.HOME || '/', p.slice(1));
|
|
|
|
} else {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 21:44:55 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function jsonParseOr(str: string, fallback: any): any {
|
2017-08-03 21:44:55 +00:00
|
|
|
try {
|
|
|
|
return JSON.parse(str);
|
|
|
|
} catch (err) {
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
}
|
2017-08-04 16:54:11 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function escapeHTML(unsafeText: string) {
|
2020-04-09 17:32:19 +00:00
|
|
|
const div = document.createElement('div');
|
2018-09-08 19:01:34 +00:00
|
|
|
div.innerText = unsafeText;
|
|
|
|
return div.innerHTML;
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:54:11 +00:00
|
|
|
/**
|
|
|
|
* Escape a dynamic string for use inside of a regular expression
|
|
|
|
* @param str - string to escape
|
|
|
|
* @returns {string} escaped string
|
|
|
|
*/
|
2021-05-12 06:35:00 +00:00
|
|
|
export function escapeRegex(str: string) {
|
2017-08-04 16:54:11 +00:00
|
|
|
return str.replace(ESCAPE_REGEX_MATCH, '\\$&');
|
|
|
|
}
|
2017-08-21 18:11:52 +00:00
|
|
|
|
2021-08-26 05:43:56 +00:00
|
|
|
export interface FuzzyMatchOptions {
|
|
|
|
splitSpace?: boolean;
|
|
|
|
loose?: boolean;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function fuzzyMatch(
|
|
|
|
searchString: string,
|
2018-06-27 05:13:48 +00:00
|
|
|
text: string,
|
2021-08-26 05:43:56 +00:00
|
|
|
options: FuzzyMatchOptions = {},
|
2021-05-12 06:35:00 +00:00
|
|
|
): null | {
|
|
|
|
score: number;
|
2021-05-18 20:32:18 +00:00
|
|
|
indexes: number[];
|
2021-05-12 06:35:00 +00:00
|
|
|
} {
|
2018-06-27 05:13:48 +00:00
|
|
|
return fuzzyMatchAll(searchString, [text], options);
|
|
|
|
}
|
2017-10-13 13:01:27 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
export function fuzzyMatchAll(
|
|
|
|
searchString: string,
|
2021-05-18 20:32:18 +00:00
|
|
|
allText: string[],
|
2021-08-26 05:43:56 +00:00
|
|
|
options: FuzzyMatchOptions = {},
|
2021-05-12 06:35:00 +00:00
|
|
|
) {
|
2018-06-27 05:13:48 +00:00
|
|
|
if (!searchString || !searchString.trim()) {
|
|
|
|
return null;
|
2018-05-23 06:39:01 +00:00
|
|
|
}
|
2017-10-13 13:01:27 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
const words = searchString.split(' ').filter(w => w.trim());
|
|
|
|
const terms = options.splitSpace ? [...words, searchString] : [searchString];
|
2021-05-12 06:35:00 +00:00
|
|
|
let maxScore: number | null = null;
|
2021-05-18 20:32:18 +00:00
|
|
|
let indexes: number[] = [];
|
2018-06-27 05:13:48 +00:00
|
|
|
let termsMatched = 0;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
for (const term of terms) {
|
|
|
|
let matchedTerm = false;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
for (const text of allText.filter(t => !t || t.trim())) {
|
|
|
|
const result = fuzzysort.single(term, text);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
if (!result) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-23 06:39:01 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
// Don't match garbage
|
|
|
|
if (result.score < -8000) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-23 06:39:01 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
if (maxScore === null || result.score > maxScore) {
|
|
|
|
maxScore = result.score;
|
|
|
|
}
|
2018-05-23 06:39:01 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
indexes = [...indexes, ...result.indexes];
|
|
|
|
matchedTerm = true;
|
|
|
|
}
|
2018-05-23 06:39:01 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
if (matchedTerm) {
|
|
|
|
termsMatched++;
|
|
|
|
}
|
2017-10-13 13:01:27 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
// Make sure we match all provided terms except the last (full) one
|
|
|
|
if (!options.loose && termsMatched < terms.length - 1) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-03-28 20:57:05 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
if (maxScore === null) {
|
|
|
|
return null;
|
2018-05-23 06:39:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 18:19:18 +00:00
|
|
|
return {
|
|
|
|
score: maxScore,
|
|
|
|
indexes,
|
|
|
|
target: allText.join(' '),
|
|
|
|
};
|
2017-08-21 18:11:52 +00:00
|
|
|
}
|
2017-11-18 22:47:54 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function waitForStreamToFinish(stream: Readable | Writable) {
|
|
|
|
return new Promise<void>(resolve => {
|
|
|
|
// @ts-expect-error -- access of internal values that are intended to be private. We should _not_ do this.
|
|
|
|
if (stream._readableState?.finished) {
|
2017-11-21 17:49:17 +00:00
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- access of internal values that are intended to be private. We should _not_ do this.
|
|
|
|
if (stream._writableState?.finished) {
|
2017-11-21 17:49:17 +00:00
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
stream.on('close', () => {
|
2017-11-21 17:49:17 +00:00
|
|
|
resolve();
|
|
|
|
});
|
2021-05-12 06:35:00 +00:00
|
|
|
stream.on('error', () => {
|
2017-11-21 17:49:17 +00:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-11-06 02:39:50 +00:00
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function chunkArray<T>(arr: T[], chunkSize: number) {
|
|
|
|
const chunks: T[][] = [];
|
2019-04-18 00:50:03 +00:00
|
|
|
|
|
|
|
for (let i = 0, j = arr.length; i < j; i += chunkSize) {
|
|
|
|
chunks.push(arr.slice(i, i + chunkSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunks;
|
|
|
|
}
|
2020-04-26 20:33:39 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function pluralize(text: string) {
|
2021-01-25 03:27:36 +00:00
|
|
|
let trailer = 's';
|
|
|
|
let chop = 0;
|
|
|
|
|
|
|
|
// Things already ending with 's' stay that way
|
|
|
|
if (text.match(/s$/)) {
|
|
|
|
trailer = '';
|
|
|
|
chop = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Things ending in 'y' convert to ies
|
|
|
|
if (text.match(/y$/)) {
|
|
|
|
trailer = 'ies';
|
|
|
|
chop = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the trailer for pluralization
|
|
|
|
return `${text.slice(0, text.length - chop)}${trailer}`;
|
|
|
|
}
|
Differential (Additive/Removal) patching when switching between design / debug (#3124)
* fix(import) do differential patching for design activity imports
This patch adds differential patching for imports that occur during the switching between
design and debug tabs inside of Designer. As reported through #2971, and others, this patch
favors existing data over imported data, values that exist already on the document will remain
unchanged, only new values (including array based values) will be added / removed.
This also includes a bypass feature for urls, currently these options are not exposed through
the interface but could be. This feature is an object to allow for future properties for
preference-based patching.
- Adds `options.enableDiffBasedPatching` and `options.enableDiffDeep` to `importRaw` for backwards compat.
- Adds `options.bypassDiffProps.url` for url bypassing and use an object for future items.
- Adds `diffPatchObj` based on differential patching for objects (works with arrays as well).
Future ideas:
- `hasBeenModifiedByUser` property map object to allow changing properties that haven't been touched by the user with options.
fixes: #2971, #2882, #3038, #2442
* adds some basic tests
- also adds jest (which was (mistakenly) not there before)
- does not call `.hasOwnProperty` directly, per https://eslint.org/docs/rules/no-prototype-builtins (which, we will add more formally at a later date)
* don't special-case workspaces
this ensures the behavior of the initial PR is more preserved
Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
2021-03-03 17:57:09 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function diffPatchObj(baseObj: {}, patchObj: {}, deep = false) {
|
Differential (Additive/Removal) patching when switching between design / debug (#3124)
* fix(import) do differential patching for design activity imports
This patch adds differential patching for imports that occur during the switching between
design and debug tabs inside of Designer. As reported through #2971, and others, this patch
favors existing data over imported data, values that exist already on the document will remain
unchanged, only new values (including array based values) will be added / removed.
This also includes a bypass feature for urls, currently these options are not exposed through
the interface but could be. This feature is an object to allow for future properties for
preference-based patching.
- Adds `options.enableDiffBasedPatching` and `options.enableDiffDeep` to `importRaw` for backwards compat.
- Adds `options.bypassDiffProps.url` for url bypassing and use an object for future items.
- Adds `diffPatchObj` based on differential patching for objects (works with arrays as well).
Future ideas:
- `hasBeenModifiedByUser` property map object to allow changing properties that haven't been touched by the user with options.
fixes: #2971, #2882, #3038, #2442
* adds some basic tests
- also adds jest (which was (mistakenly) not there before)
- does not call `.hasOwnProperty` directly, per https://eslint.org/docs/rules/no-prototype-builtins (which, we will add more formally at a later date)
* don't special-case workspaces
this ensures the behavior of the initial PR is more preserved
Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
2021-03-03 17:57:09 +00:00
|
|
|
const clonedBaseObj = JSON.parse(JSON.stringify(baseObj));
|
|
|
|
|
|
|
|
for (const prop in baseObj) {
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(baseObj, prop)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(patchObj, prop)) {
|
|
|
|
const left = baseObj[prop];
|
|
|
|
const right = patchObj[prop];
|
|
|
|
|
|
|
|
if (right !== left) {
|
|
|
|
if (deep && isObject(left) && isObject(right)) {
|
|
|
|
clonedBaseObj[prop] = diffPatchObj(left, right, deep);
|
2021-03-03 22:27:04 +00:00
|
|
|
} else if (isObject(left) && !isObject(right)) {
|
|
|
|
// when right is empty but left isn't, prefer left to avoid a sparse array
|
|
|
|
clonedBaseObj[prop] = left;
|
Differential (Additive/Removal) patching when switching between design / debug (#3124)
* fix(import) do differential patching for design activity imports
This patch adds differential patching for imports that occur during the switching between
design and debug tabs inside of Designer. As reported through #2971, and others, this patch
favors existing data over imported data, values that exist already on the document will remain
unchanged, only new values (including array based values) will be added / removed.
This also includes a bypass feature for urls, currently these options are not exposed through
the interface but could be. This feature is an object to allow for future properties for
preference-based patching.
- Adds `options.enableDiffBasedPatching` and `options.enableDiffDeep` to `importRaw` for backwards compat.
- Adds `options.bypassDiffProps.url` for url bypassing and use an object for future items.
- Adds `diffPatchObj` based on differential patching for objects (works with arrays as well).
Future ideas:
- `hasBeenModifiedByUser` property map object to allow changing properties that haven't been touched by the user with options.
fixes: #2971, #2882, #3038, #2442
* adds some basic tests
- also adds jest (which was (mistakenly) not there before)
- does not call `.hasOwnProperty` directly, per https://eslint.org/docs/rules/no-prototype-builtins (which, we will add more formally at a later date)
* don't special-case workspaces
this ensures the behavior of the initial PR is more preserved
Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
2021-03-03 17:57:09 +00:00
|
|
|
} else {
|
2021-03-03 22:27:04 +00:00
|
|
|
// otherwise prefer right when both elements aren't objects to ensure values don't get overwritten
|
Differential (Additive/Removal) patching when switching between design / debug (#3124)
* fix(import) do differential patching for design activity imports
This patch adds differential patching for imports that occur during the switching between
design and debug tabs inside of Designer. As reported through #2971, and others, this patch
favors existing data over imported data, values that exist already on the document will remain
unchanged, only new values (including array based values) will be added / removed.
This also includes a bypass feature for urls, currently these options are not exposed through
the interface but could be. This feature is an object to allow for future properties for
preference-based patching.
- Adds `options.enableDiffBasedPatching` and `options.enableDiffDeep` to `importRaw` for backwards compat.
- Adds `options.bypassDiffProps.url` for url bypassing and use an object for future items.
- Adds `diffPatchObj` based on differential patching for objects (works with arrays as well).
Future ideas:
- `hasBeenModifiedByUser` property map object to allow changing properties that haven't been touched by the user with options.
fixes: #2971, #2882, #3038, #2442
* adds some basic tests
- also adds jest (which was (mistakenly) not there before)
- does not call `.hasOwnProperty` directly, per https://eslint.org/docs/rules/no-prototype-builtins (which, we will add more formally at a later date)
* don't special-case workspaces
this ensures the behavior of the initial PR is more preserved
Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
2021-03-03 17:57:09 +00:00
|
|
|
clonedBaseObj[prop] = right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const prop in patchObj) {
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(patchObj, prop)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(baseObj, prop)) {
|
|
|
|
clonedBaseObj[prop] = patchObj[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clonedBaseObj;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function isObject(obj: unknown) {
|
Differential (Additive/Removal) patching when switching between design / debug (#3124)
* fix(import) do differential patching for design activity imports
This patch adds differential patching for imports that occur during the switching between
design and debug tabs inside of Designer. As reported through #2971, and others, this patch
favors existing data over imported data, values that exist already on the document will remain
unchanged, only new values (including array based values) will be added / removed.
This also includes a bypass feature for urls, currently these options are not exposed through
the interface but could be. This feature is an object to allow for future properties for
preference-based patching.
- Adds `options.enableDiffBasedPatching` and `options.enableDiffDeep` to `importRaw` for backwards compat.
- Adds `options.bypassDiffProps.url` for url bypassing and use an object for future items.
- Adds `diffPatchObj` based on differential patching for objects (works with arrays as well).
Future ideas:
- `hasBeenModifiedByUser` property map object to allow changing properties that haven't been touched by the user with options.
fixes: #2971, #2882, #3038, #2442
* adds some basic tests
- also adds jest (which was (mistakenly) not there before)
- does not call `.hasOwnProperty` directly, per https://eslint.org/docs/rules/no-prototype-builtins (which, we will add more formally at a later date)
* don't special-case workspaces
this ensures the behavior of the initial PR is more preserved
Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
2021-03-03 17:57:09 +00:00
|
|
|
return obj !== null && typeof obj === 'object';
|
|
|
|
}
|
2021-03-09 21:50:35 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
/**
|
|
|
|
Finds epoch's digit count and converts it to make it exactly 13 digits.
|
2021-05-25 16:16:43 +00:00
|
|
|
Which is the epoch millisecond representation.
|
2021-05-12 06:35:00 +00:00
|
|
|
*/
|
2021-04-05 17:03:34 +00:00
|
|
|
export function convertEpochToMilliseconds(epoch: number) {
|
|
|
|
const expDigitCount = epoch.toString().length;
|
2021-05-12 06:35:00 +00:00
|
|
|
return parseInt(String(epoch * 10 ** (13 - expDigitCount)), 10);
|
2021-04-05 17:03:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function snapNumberToLimits(value: number, min?: number, max?: number) {
|
2021-03-09 21:50:35 +00:00
|
|
|
const moreThanMax = max && !Number.isNaN(max) && value > max;
|
|
|
|
const lessThanMin = min && !Number.isNaN(min) && value < min;
|
|
|
|
|
|
|
|
if (moreThanMax) {
|
|
|
|
return max;
|
|
|
|
} else if (lessThanMin) {
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
2021-03-10 04:33:38 +00:00
|
|
|
|
2021-08-05 17:38:58 +00:00
|
|
|
export function isNotNullOrUndefined<ValueType>(
|
|
|
|
value: ValueType | null | undefined
|
|
|
|
): value is ValueType {
|
|
|
|
if (value === null || value === undefined) return false;
|
|
|
|
|
|
|
|
return true;
|
2021-08-08 05:08:56 +00:00
|
|
|
}
|