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
Merge branch 'develop' of github.com:matrix-org/matrix-js-sdk into t3chguy/ts/12
Conflicts: src/client.ts src/interactive-auth.ts src/models/search-result.ts
This commit is contained in:
49
src/utils.ts
49
src/utils.ts
@@ -31,15 +31,28 @@ import type NodeCrypto from "crypto";
|
||||
* @return {string} The encoded string e.g. foo=bar&baz=taz
|
||||
*/
|
||||
export function encodeParams(params: Record<string, string>): string {
|
||||
let qs = "";
|
||||
for (const key in params) {
|
||||
if (!params.hasOwnProperty(key)) {
|
||||
continue;
|
||||
}
|
||||
qs += "&" + encodeURIComponent(key) + "=" +
|
||||
encodeURIComponent(params[key]);
|
||||
return new URLSearchParams(params).toString();
|
||||
}
|
||||
|
||||
export type QueryDict = Record<string, string | string[]>;
|
||||
|
||||
/**
|
||||
* Decode a query string in `application/x-www-form-urlencoded` format.
|
||||
* @param {string} query A query string to decode e.g.
|
||||
* foo=bar&via=server1&server2
|
||||
* @return {Object} The decoded object, if any keys occurred multiple times
|
||||
* then the value will be an array of strings, else it will be an array.
|
||||
* This behaviour matches Node's qs.parse but is built on URLSearchParams
|
||||
* for native web compatibility
|
||||
*/
|
||||
export function decodeParams(query: string): QueryDict {
|
||||
const o: QueryDict = {};
|
||||
const params = new URLSearchParams(query);
|
||||
for (const key of params.keys()) {
|
||||
const val = params.getAll(key);
|
||||
o[key] = val.length === 1 ? val[0] : val;
|
||||
}
|
||||
return qs.substring(1);
|
||||
return o;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,10 +129,10 @@ export function isFunction(value: any) {
|
||||
* @throws If the object is missing keys.
|
||||
*/
|
||||
// note using 'keys' here would shadow the 'keys' function defined above
|
||||
export function checkObjectHasKeys(obj: object, keys_: string[]) {
|
||||
for (let i = 0; i < keys_.length; i++) {
|
||||
if (!obj.hasOwnProperty(keys_[i])) {
|
||||
throw new Error("Missing required key: " + keys_[i]);
|
||||
export function checkObjectHasKeys(obj: object, keys: string[]) {
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
if (!obj.hasOwnProperty(keys[i])) {
|
||||
throw new Error("Missing required key: " + keys[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -464,7 +477,7 @@ export async function promiseMapSeries<T>(
|
||||
}
|
||||
}
|
||||
|
||||
export function promiseTry<T>(fn: () => T): Promise<T> {
|
||||
export function promiseTry<T>(fn: () => T | Promise<T>): Promise<T> {
|
||||
return new Promise((resolve) => resolve(fn()));
|
||||
}
|
||||
|
||||
@@ -670,3 +683,13 @@ export function lexicographicCompare(a: string, b: string): number {
|
||||
// hidden the operation in this function.
|
||||
return (a < b) ? -1 : ((a === b) ? 0 : 1);
|
||||
}
|
||||
|
||||
const collator = new Intl.Collator();
|
||||
/**
|
||||
* Performant language-sensitive string comparison
|
||||
* @param a the first string to compare
|
||||
* @param b the second string to compare
|
||||
*/
|
||||
export function compare(a: string, b: string): number {
|
||||
return collator.compare(a, b);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user