insomnia/packages/insomnia-app/app/common/misc.ts

467 lines
12 KiB
TypeScript
Raw Normal View History

import fuzzysort from 'fuzzysort';
2021-07-22 23:04:56 +00:00
import { join as pathJoin } from 'path';
import { Readable, Writable } from 'stream';
import * as uuid from 'uuid';
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
const ESCAPE_REGEX_MATCH = /[-[\]/{}()*+?.\\^$|]/g;
interface Header {
name: string;
value: string;
}
2017-07-19 02:54:03 +00:00
interface Parameter {
name: string;
value: string;
}
export function filterParameters<T extends Parameter>(
parameters: T[],
name: string,
): T[] {
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));
}
export function filterHeaders<T extends Header>(headers: T[], name?: string): T[] {
if (!Array.isArray(headers) || !name || typeof name !== 'string') {
return [];
}
return headers.filter(header => {
// Never match against invalid headers
if (!header || !header.name || typeof header.name !== 'string') {
2016-11-10 17:33:28 +00:00
return false;
}
return header.name.toLowerCase() === name.toLowerCase();
2016-11-10 17:33:28 +00:00
});
}
export function hasContentTypeHeader<T extends Header>(headers: T[]) {
return filterHeaders(headers, 'content-type').length > 0;
}
export function hasContentLengthHeader<T extends Header>(headers: T[]) {
return filterHeaders(headers, 'content-length').length > 0;
}
export function hasAuthHeader<T extends Header>(headers: T[]) {
return filterHeaders(headers, 'authorization').length > 0;
}
export function hasAcceptHeader<T extends Header>(headers: T[]) {
return filterHeaders(headers, 'accept').length > 0;
}
export function hasUserAgentHeader<T extends Header>(headers: T[]) {
return filterHeaders(headers, 'user-agent').length > 0;
}
export function hasAcceptEncodingHeader<T extends Header>(headers: T[]) {
2017-11-12 18:35:01 +00:00
return filterHeaders(headers, 'accept-encoding').length > 0;
}
export function getSetCookieHeaders<T extends Header>(headers: T[]): T[] {
return filterHeaders(headers, 'set-cookie');
}
2016-09-04 21:32:36 +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;
}
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;
}
export function getMethodOverrideHeader<T extends Header>(headers: T[]): T | null {
const matches = filterHeaders(headers, 'x-http-method-override');
return matches.length ? matches[0] : null;
}
export function getHostHeader<T extends Header>(headers: T[]): T | null {
const matches = filterHeaders(headers, 'host');
return matches.length ? matches[0] : null;
}
export function getContentDispositionHeader<T extends Header>(headers: T[]): T | null {
const matches = filterHeaders(headers, 'content-disposition');
return matches.length ? matches[0] : null;
}
export function getContentLengthHeader<T extends Header>(headers: T[]): T | null {
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}
*/
export function generateId(prefix?: string) {
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
const id = uuid.v4().replace(/-/g, '');
2016-09-04 21:32:36 +00:00
if (prefix) {
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
return `${prefix}_${id}`;
2016-09-04 21:32:36 +00:00
} else {
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
return id;
2016-09-04 21:32:36 +00:00
}
}
2016-09-22 19:44:28 +00:00
export function delay(milliseconds: number = DEBOUNCE_MILLIS) {
return new Promise<void>(resolve => setTimeout(resolve, milliseconds));
}
export function removeVowels(str: string) {
return str.replace(/[aeiouyAEIOUY]/g, '');
}
export function formatMethodName(method: string) {
let methodName = method || '';
if (method === METHOD_DELETE || method === METHOD_OPTIONS) {
methodName = method.slice(0, 3);
} else if (method.length > 4) {
methodName = removeVowels(method).slice(0, 4);
}
return methodName;
}
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 = {};
// @ts-expect-error -- TSCONVERSION
const t: T = function(key, ...args) {
2016-11-10 21:03:12 +00:00
results[key] = args;
clearTimeout(timeout);
timeout = setTimeout(() => {
2016-11-10 21:03:12 +00:00
if (!Object.keys(results).length) {
return;
}
callback(results);
results = {};
}, millis);
};
return t;
}
2016-11-10 21:03:12 +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 => {
// 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
callback.apply(null, results.__key__);
}, milliseconds).bind(null, '__key__');
2016-11-10 21:03:12 +00:00
}
2016-11-22 22:26:52 +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;
// NOTE: We multiply these by 2 so we don't end up with
// values like 0 GB
let unit;
if (bytes < 1024 * 2) {
2016-11-22 22:26:52 +00:00
size = bytes;
unit = long ? 'bytes' : 'B';
} else if (bytes < 1024 * 1024 * 2) {
2016-11-22 22:26:52 +00:00
size = bytes / 1024;
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;
unit = long ? 'megabytes' : 'MB';
2016-11-22 22:26:52 +00:00
} else {
size = bytes / 1024 / 1024 / 1024;
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;
return `${rounded} ${unit}`;
2016-11-22 22:26:52 +00:00
}
export function nullFn() {
// Do nothing
}
export function preventDefault(e: Event) {
e.preventDefault();
}
export function xmlDecode(input: string) {
const ESCAPED_CHARACTERS_MAP = {
'&amp;': '&',
'&quot;': '"',
'&lt;': '<',
'&gt;': '>',
};
return input.replace(/(&quot;|&lt;|&gt;|&amp;)/g, (_: string, item: keyof typeof ESCAPED_CHARACTERS_MAP) => (
ESCAPED_CHARACTERS_MAP[item])
);
}
export function fnOrString(v: string | ((...args: any[]) => any), ...args: any[]) {
if (typeof v === 'string') {
return v;
} else {
return v(...args);
}
}
export function compressObject(obj: any) {
const compressed = zlib.gzipSync(JSON.stringify(obj));
return compressed.toString('base64');
}
export function decompressObject(input: string | null): any {
if (typeof input !== 'string') {
return null;
}
const jsonBuffer = zlib.gunzipSync(Buffer.from(input, 'base64'));
2017-07-19 02:54:03 +00:00
return JSON.parse(jsonBuffer.toString('utf8'));
}
export function resolveHomePath(p: string) {
if (p.indexOf('~/') === 0) {
return pathJoin(process.env.HOME || '/', p.slice(1));
} else {
return p;
}
}
2018-06-25 17:42:50 +00:00
export function jsonParseOr(str: string, fallback: any): any {
try {
return JSON.parse(str);
} catch (err) {
return fallback;
}
}
export function escapeHTML(unsafeText: string) {
const div = document.createElement('div');
div.innerText = unsafeText;
return div.innerHTML;
}
/**
* Escape a dynamic string for use inside of a regular expression
* @param str - string to escape
* @returns {string} escaped string
*/
export function escapeRegex(str: string) {
return str.replace(ESCAPE_REGEX_MATCH, '\\$&');
}
export interface FuzzyMatchOptions {
splitSpace?: boolean;
loose?: boolean;
}
2018-06-25 17:42:50 +00:00
export function fuzzyMatch(
searchString: string,
text: string,
options: FuzzyMatchOptions = {},
): null | {
score: number;
indexes: number[];
} {
return fuzzyMatchAll(searchString, [text], options);
}
export function fuzzyMatchAll(
searchString: string,
allText: string[],
options: FuzzyMatchOptions = {},
) {
if (!searchString || !searchString.trim()) {
return null;
}
const words = searchString.split(' ').filter(w => w.trim());
const terms = options.splitSpace ? [...words, searchString] : [searchString];
let maxScore: number | null = null;
let indexes: number[] = [];
let termsMatched = 0;
for (const term of terms) {
let matchedTerm = false;
for (const text of allText.filter(t => !t || t.trim())) {
const result = fuzzysort.single(term, text);
if (!result) {
continue;
}
// Don't match garbage
if (result.score < -8000) {
continue;
}
if (maxScore === null || result.score > maxScore) {
maxScore = result.score;
}
indexes = [...indexes, ...result.indexes];
matchedTerm = true;
}
if (matchedTerm) {
termsMatched++;
}
}
// Make sure we match all provided terms except the last (full) one
if (!options.loose && termsMatched < terms.length - 1) {
return null;
}
if (maxScore === null) {
return null;
}
return {
score: maxScore,
indexes,
target: allText.join(' '),
};
}
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) {
return resolve();
}
// @ts-expect-error -- access of internal values that are intended to be private. We should _not_ do this.
if (stream._writableState?.finished) {
return resolve();
}
stream.on('close', () => {
resolve();
});
stream.on('error', () => {
resolve();
});
});
}
export function chunkArray<T>(arr: T[], chunkSize: number) {
const chunks: T[][] = [];
Version Control (beta) (#1439) * VCS proof of concept underway! * Stuff * Some things * Replace deprecated Electron makeSingleInstance * Rename `window` variables so not to be confused with window object * Don't unnecessarily update request when URL does not change * Regenerate package-lock * Fix tests + ESLint * Publish - insomnia-app@1.0.49 - insomnia-cookies@0.0.12 - insomnia-httpsnippet@1.16.18 - insomnia-importers@2.0.13 - insomnia-libcurl@0.0.23 - insomnia-prettify@0.1.7 - insomnia-url@0.1.6 - insomnia-xpath@1.0.9 - insomnia-plugin-base64@1.0.6 - insomnia-plugin-cookie-jar@1.0.8 - insomnia-plugin-core-themes@1.0.5 - insomnia-plugin-default-headers@1.1.9 - insomnia-plugin-file@1.0.7 - insomnia-plugin-hash@1.0.7 - insomnia-plugin-jsonpath@1.0.12 - insomnia-plugin-now@1.0.11 - insomnia-plugin-os@1.0.13 - insomnia-plugin-prompt@1.1.9 - insomnia-plugin-request@1.0.18 - insomnia-plugin-response@1.0.16 - insomnia-plugin-uuid@1.0.10 * Broken but w/e * Some tweaks * Big refactor. Create local snapshots and push done * POC merging and a lot of improvements * Lots of work done on initial UI/UX * Fix old tests * Atomic writes and size-based batches * Update StageEntry definition once again to be better * Factor out GraphQL query logic * Merge algorithm, history modal, other minor things * Fix test * Merge, checkout, revert w/ user changes now work * Force UI to refresh when switching branches changes active request * Rough draft pull() and some cleanup * E2EE stuff and some refactoring * Add ability to share project with team and fixed tests * VCS now created in root component and better remote project handling * Remove unused definition * Publish - insomnia-account@0.0.2 - insomnia-app@1.1.1 - insomnia-cookies@0.0.14 - insomnia-httpsnippet@1.16.20 - insomnia-importers@2.0.15 - insomnia-libcurl@0.0.25 - insomnia-prettify@0.1.9 - insomnia-sync@0.0.2 - insomnia-url@0.1.8 - insomnia-xpath@1.0.11 - insomnia-plugin-base64@1.0.8 - insomnia-plugin-cookie-jar@1.0.10 - insomnia-plugin-core-themes@1.0.7 - insomnia-plugin-file@1.0.9 - insomnia-plugin-hash@1.0.9 - insomnia-plugin-jsonpath@1.0.14 - insomnia-plugin-now@1.0.13 - insomnia-plugin-os@1.0.15 - insomnia-plugin-prompt@1.1.11 - insomnia-plugin-request@1.0.20 - insomnia-plugin-response@1.0.18 - insomnia-plugin-uuid@1.0.12 * Move some deps around * Fix Flow errors * Update package.json * Fix eslint errors * Fix tests * Update deps * bootstrap insomnia-sync * TRy fixing appveyor * Try something else * Bump lerna * try powershell * Try again * Fix imports * Fixed errors * sync types refactor * Show remote projects in workspace dropdown * Improved pulling of non-local workspaces * Loading indicators and some tweaks * Clean up sync staging modal * Some sync improvements: - No longer store stage - Upgrade Electron - Sync UI/UX improvements * Fix snyc tests * Upgraded deps and hot loader tweaks (it's broken for some reason) * Fix tests * Branches dialog, network refactoring, some tweaks * Fixed merging when other branch is empty * A bunch of small fixes from real testing * Fixed pull merge logic * Fix tests * Some bug fixes * A few small tweaks * Conflict resolution and other improvements * Fix tests * Add revert changes * Deal with duplicate projects per workspace * Some tweaks and accessibility improvements * Tooltip accessibility * Fix API endpoint * Fix tests * Remove jest dep from insomnia-importers
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
export function pluralize(text: string) {
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
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);
} 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 {
// 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;
}
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';
}
/**
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.
*/
export function convertEpochToMilliseconds(epoch: number) {
const expDigitCount = epoch.toString().length;
return parseInt(String(epoch * 10 ** (13 - expDigitCount)), 10);
}
export function snapNumberToLimits(value: number, min?: number, max?: number) {
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;
}
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
}