Files
azuredatastudio/extensions/query-history/src/utils.ts
Charles Gagnon d6d75d8817 Add query history persistence (#20700)
* initial

* Fix and revert secret storage changes

* persist to file

* Add debounce and write on dispose

* Fix run

* No ext dependencies and show warning message

* Remove test stuff

* comments

* Fix tests and console logs
2022-09-30 16:48:55 -07:00

46 lines
1.4 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/**
* Removes all newlines from the given string, replacing them with spaces
* @param str The original string
* @returns The string with all newlines replaced by spaces
*/
export function removeNewLines(str: string): string {
return str.replace(/\r\n/g, ' ').replace(/\n/g, ' ');
}
function decorate(decorator: (fn: Function, key: string) => Function): Function {
return (_target: any, key: string, descriptor: any): void => {
let fnKey: string | null = null;
let fn: Function | null = null;
if (typeof descriptor.value === 'function') {
fnKey = 'value';
fn = descriptor.value;
} else if (typeof descriptor.get === 'function') {
fnKey = 'get';
fn = descriptor.get;
}
if (!fn || !fnKey) {
throw new Error('not supported');
}
descriptor[fnKey] = decorator(fn, key);
};
}
export function debounce(delay: number): Function {
return decorate((fn, key) => {
const timerKey = `$debounce$${key}`;
return function (this: any, ...args: any[]): void {
clearTimeout(this[timerKey]);
this[timerKey] = setTimeout(() => fn.apply(this, args), delay);
};
});
}