You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-25 05:23:13 +03:00
Apply prettier formatting
This commit is contained in:
85
src/utils.ts
85
src/utils.ts
@@ -63,7 +63,7 @@ export function encodeParams(params: QueryDict, urlSearchParams?: URLSearchParam
|
||||
for (const [key, val] of Object.entries(params)) {
|
||||
if (val !== undefined && val !== null) {
|
||||
if (Array.isArray(val)) {
|
||||
val.forEach(v => {
|
||||
val.forEach((v) => {
|
||||
searchParams.append(key, String(v));
|
||||
});
|
||||
} else {
|
||||
@@ -79,11 +79,7 @@ export type QueryDict = Record<string, string[] | string | number | boolean | un
|
||||
/**
|
||||
* Replace a stable parameter with the unstable naming for params
|
||||
*/
|
||||
export function replaceParam(
|
||||
stable: string,
|
||||
unstable: string,
|
||||
dict: QueryDict,
|
||||
): QueryDict {
|
||||
export function replaceParam(stable: string, unstable: string, dict: QueryDict): QueryDict {
|
||||
const result = {
|
||||
...dict,
|
||||
[unstable]: dict[stable],
|
||||
@@ -128,9 +124,7 @@ export function encodeUri(pathTemplate: string, variables: Record<string, Option
|
||||
if (value === undefined || value === null) {
|
||||
continue;
|
||||
}
|
||||
pathTemplate = pathTemplate.replace(
|
||||
key, encodeURIComponent(value),
|
||||
);
|
||||
pathTemplate = pathTemplate.replace(key, encodeURIComponent(value));
|
||||
}
|
||||
return pathTemplate;
|
||||
}
|
||||
@@ -145,11 +139,7 @@ export function encodeUri(pathTemplate: string, variables: Record<string, Option
|
||||
* @param reverse - True to search in reverse order.
|
||||
* @returns True if an element was removed.
|
||||
*/
|
||||
export function removeElement<T>(
|
||||
array: T[],
|
||||
fn: (t: T, i?: number, a?: T[]) => boolean,
|
||||
reverse?: boolean,
|
||||
): boolean {
|
||||
export function removeElement<T>(array: T[], fn: (t: T, i?: number, a?: T[]) => boolean, reverse?: boolean): boolean {
|
||||
let i: number;
|
||||
if (reverse) {
|
||||
for (i = array.length - 1; i >= 0; i--) {
|
||||
@@ -226,7 +216,7 @@ export function deepCompare(x: any, y: any): boolean {
|
||||
}
|
||||
|
||||
// special-case NaN (since NaN !== NaN)
|
||||
if (typeof x === 'number' && isNaN(x) && isNaN(y)) {
|
||||
if (typeof x === "number" && isNaN(x) && isNaN(y)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -290,7 +280,7 @@ export function deepCompare(x: any, y: any): boolean {
|
||||
* @returns The entries, sorted by key.
|
||||
*/
|
||||
export function deepSortedObjectEntries(obj: any): [string, any][] {
|
||||
if (typeof(obj) !== "object") return obj;
|
||||
if (typeof obj !== "object") return obj;
|
||||
|
||||
// Apparently these are object types...
|
||||
if (obj === null || obj === undefined || Array.isArray(obj)) return obj;
|
||||
@@ -313,7 +303,7 @@ export function deepSortedObjectEntries(obj: any): [string, any][] {
|
||||
* @returns whether or not value is a finite number without type-coercion
|
||||
*/
|
||||
export function isNumber(value: any): value is number {
|
||||
return typeof value === 'number' && isFinite(value);
|
||||
return typeof value === "number" && isFinite(value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -324,7 +314,7 @@ export function isNumber(value: any): value is number {
|
||||
*/
|
||||
export function removeHiddenChars(str: string): string {
|
||||
if (typeof str === "string") {
|
||||
return unhomoglyph(str.normalize('NFD').replace(removeHiddenCharsRegex, ''));
|
||||
return unhomoglyph(str.normalize("NFD").replace(removeHiddenCharsRegex, ""));
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@@ -335,7 +325,7 @@ export function removeHiddenChars(str: string): string {
|
||||
*/
|
||||
export function removeDirectionOverrideChars(str: string): string {
|
||||
if (typeof str === "string") {
|
||||
return str.replace(/[\u202d-\u202e]/g, '');
|
||||
return str.replace(/[\u202d-\u202e]/g, "");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@@ -343,11 +333,13 @@ export function removeDirectionOverrideChars(str: string): string {
|
||||
export function normalize(str: string): string {
|
||||
// Note: we have to match the filter with the removeHiddenChars() because the
|
||||
// function strips spaces and other characters (M becomes RN for example, in lowercase).
|
||||
return removeHiddenChars(str.toLowerCase())
|
||||
// Strip all punctuation
|
||||
.replace(/[\\'!"#$%&()*+,\-./:;<=>?@[\]^_`{|}~\u2000-\u206f\u2e00-\u2e7f]/g, "")
|
||||
// We also doubly convert to lowercase to work around oddities of the library.
|
||||
.toLowerCase();
|
||||
return (
|
||||
removeHiddenChars(str.toLowerCase())
|
||||
// Strip all punctuation
|
||||
.replace(/[\\'!"#$%&()*+,\-./:;<=>?@[\]^_`{|}~\u2000-\u206f\u2e00-\u2e7f]/g, "")
|
||||
// We also doubly convert to lowercase to work around oddities of the library.
|
||||
.toLowerCase()
|
||||
);
|
||||
}
|
||||
|
||||
// Regex matching bunch of unicode control characters and otherwise misleading/invisible characters.
|
||||
@@ -371,24 +363,20 @@ export function globToRegexp(glob: string, extended = false): string {
|
||||
// https://github.com/matrix-org/synapse/blob/abbee6b29be80a77e05730707602f3bbfc3f38cb/synapse/push/__init__.py#L132
|
||||
// Because micromatch is about 130KB with dependencies,
|
||||
// and minimatch is not much better.
|
||||
const replacements: ([RegExp, string | ((substring: string, ...args: any[]) => string) ])[] = [
|
||||
[/\\\*/g, '.*'],
|
||||
[/\?/g, '.'],
|
||||
const replacements: [RegExp, string | ((substring: string, ...args: any[]) => string)][] = [
|
||||
[/\\\*/g, ".*"],
|
||||
[/\?/g, "."],
|
||||
];
|
||||
if (!extended) {
|
||||
replacements.push([
|
||||
/\\\[(!|)(.*)\\]/g,
|
||||
(_match: string, neg: string, pat: string): string => [
|
||||
'[',
|
||||
neg ? '^' : '',
|
||||
pat.replace(/\\-/, '-'),
|
||||
']',
|
||||
].join(''),
|
||||
(_match: string, neg: string, pat: string): string =>
|
||||
["[", neg ? "^" : "", pat.replace(/\\-/, "-"), "]"].join(""),
|
||||
]);
|
||||
}
|
||||
return replacements.reduce(
|
||||
// https://github.com/microsoft/TypeScript/issues/30134
|
||||
(pat, args) => args ? pat.replace(args[0], args[1] as any) : pat,
|
||||
(pat, args) => (args ? pat.replace(args[0], args[1] as any) : pat),
|
||||
escapeRegExp(glob),
|
||||
);
|
||||
}
|
||||
@@ -406,9 +394,9 @@ export function ensureNoTrailingSlash(url?: string): string | undefined {
|
||||
|
||||
// Returns a promise which resolves with a given value after the given number of ms
|
||||
export function sleep<T>(ms: number, value?: T): Promise<T> {
|
||||
return new Promise((resolve => {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms, value);
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
export function isNullOrUndefined(val: any): boolean {
|
||||
@@ -451,7 +439,7 @@ export function promiseTry<T>(fn: () => T | Promise<T>): Promise<T> {
|
||||
export async function chunkPromises<T>(fns: (() => Promise<T>)[], chunkSize: number): Promise<T[]> {
|
||||
const results: T[] = [];
|
||||
for (let i = 0; i < fns.length; i += chunkSize) {
|
||||
results.push(...(await Promise.all(fns.slice(i, i + chunkSize).map(fn => fn()))));
|
||||
results.push(...(await Promise.all(fns.slice(i, i + chunkSize).map((fn) => fn()))));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
@@ -466,14 +454,17 @@ export async function chunkPromises<T>(fns: (() => Promise<T>)[], chunkSize: num
|
||||
* @returns The promise for the retried operation.
|
||||
*/
|
||||
export function simpleRetryOperation<T>(promiseFn: (attempt: number) => Promise<T>): Promise<T> {
|
||||
return promiseRetry((attempt: number) => {
|
||||
return promiseFn(attempt);
|
||||
}, {
|
||||
forever: true,
|
||||
factor: 2,
|
||||
minTimeout: 3000, // ms
|
||||
maxTimeout: 15000, // ms
|
||||
});
|
||||
return promiseRetry(
|
||||
(attempt: number) => {
|
||||
return promiseFn(attempt);
|
||||
},
|
||||
{
|
||||
forever: true,
|
||||
factor: 2,
|
||||
minTimeout: 3000, // ms
|
||||
maxTimeout: 15000, // ms
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// String averaging inspired by https://stackoverflow.com/a/2510816
|
||||
@@ -487,7 +478,7 @@ export function simpleRetryOperation<T>(promiseFn: (attempt: number) => Promise<
|
||||
*/
|
||||
export const DEFAULT_ALPHABET = ((): string => {
|
||||
let str = "";
|
||||
for (let c = 0x20; c <= 0x7E; c++) {
|
||||
for (let c = 0x20; c <= 0x7e; c++) {
|
||||
str += String.fromCharCode(c);
|
||||
}
|
||||
return str;
|
||||
@@ -570,7 +561,7 @@ export function stringToBase(s: string, alphabet = DEFAULT_ALPHABET): bigint {
|
||||
|
||||
// We add 1 to the char index to offset the whole numbering scheme. We unpack this in
|
||||
// the baseToString() function.
|
||||
result += BigInt(1 + charIndex) * (len ** j);
|
||||
result += BigInt(1 + charIndex) * len ** j;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user