You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-26 17:03:12 +03:00
Convert utils.js -> utils.ts
This commit is contained in:
4
src/@types/unhomoglyph/index.d.ts
vendored
Normal file
4
src/@types/unhomoglyph/index.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
declare module 'unhomoglyph' {
|
||||||
|
const unhomoglyph: (s: string) => string;
|
||||||
|
export = unhomoglyph;
|
||||||
|
}
|
||||||
@@ -20,7 +20,7 @@ limitations under the License.
|
|||||||
* @module utils
|
* @module utils
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import unhomoglyph from 'unhomoglyph';
|
import * as unhomoglyph from 'unhomoglyph';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode a dictionary of query parameters.
|
* Encode a dictionary of query parameters.
|
||||||
@@ -28,7 +28,7 @@ import unhomoglyph from 'unhomoglyph';
|
|||||||
* {"foo": "bar", "baz": "taz"}
|
* {"foo": "bar", "baz": "taz"}
|
||||||
* @return {string} The encoded string e.g. foo=bar&baz=taz
|
* @return {string} The encoded string e.g. foo=bar&baz=taz
|
||||||
*/
|
*/
|
||||||
export function encodeParams(params) {
|
export function encodeParams(params: {[key: string]: string}): string {
|
||||||
let qs = "";
|
let qs = "";
|
||||||
for (const key in params) {
|
for (const key in params) {
|
||||||
if (!params.hasOwnProperty(key)) {
|
if (!params.hasOwnProperty(key)) {
|
||||||
@@ -48,7 +48,8 @@ export function encodeParams(params) {
|
|||||||
* variables with. E.g. { "$bar": "baz" }.
|
* variables with. E.g. { "$bar": "baz" }.
|
||||||
* @return {string} The result of replacing all template variables e.g. '/foo/baz'.
|
* @return {string} The result of replacing all template variables e.g. '/foo/baz'.
|
||||||
*/
|
*/
|
||||||
export function encodeUri(pathTemplate, variables) {
|
export function encodeUri(pathTemplate: string,
|
||||||
|
variables: {[key: string]: string}): string {
|
||||||
for (const key in variables) {
|
for (const key in variables) {
|
||||||
if (!variables.hasOwnProperty(key)) {
|
if (!variables.hasOwnProperty(key)) {
|
||||||
continue;
|
continue;
|
||||||
@@ -67,7 +68,7 @@ export function encodeUri(pathTemplate, variables) {
|
|||||||
* the array with the signature <code>fn(element){...}</code>
|
* the array with the signature <code>fn(element){...}</code>
|
||||||
* @return {Array} A new array with the results of the function.
|
* @return {Array} A new array with the results of the function.
|
||||||
*/
|
*/
|
||||||
export function map(array, fn) {
|
export function map<T, S>(array: T[], fn: (t: T) => S): S[] {
|
||||||
const results = new Array(array.length);
|
const results = new Array(array.length);
|
||||||
for (let i = 0; i < array.length; i++) {
|
for (let i = 0; i < array.length; i++) {
|
||||||
results[i] = fn(array[i]);
|
results[i] = fn(array[i]);
|
||||||
@@ -83,8 +84,9 @@ export function map(array, fn) {
|
|||||||
* looks like <code>fn(element, index, array){...}</code>.
|
* looks like <code>fn(element, index, array){...}</code>.
|
||||||
* @return {Array} A new array with the results of the function.
|
* @return {Array} A new array with the results of the function.
|
||||||
*/
|
*/
|
||||||
export function filter(array, fn) {
|
export function filter<T>(array: T[],
|
||||||
const results = [];
|
fn: (t: T, i?: number, a?: T[]) => boolean): T[] {
|
||||||
|
const results: T[] = [];
|
||||||
for (let i = 0; i < array.length; i++) {
|
for (let i = 0; i < array.length; i++) {
|
||||||
if (fn(array[i], i, array)) {
|
if (fn(array[i], i, array)) {
|
||||||
results.push(array[i]);
|
results.push(array[i]);
|
||||||
@@ -98,7 +100,7 @@ export function filter(array, fn) {
|
|||||||
* @param {Object} obj The object to get the keys for.
|
* @param {Object} obj The object to get the keys for.
|
||||||
* @return {string[]} The keys of the object.
|
* @return {string[]} The keys of the object.
|
||||||
*/
|
*/
|
||||||
export function keys(obj) {
|
export function keys(obj: object): string[] {
|
||||||
const keys = [];
|
const keys = [];
|
||||||
for (const key in obj) {
|
for (const key in obj) {
|
||||||
if (!obj.hasOwnProperty(key)) {
|
if (!obj.hasOwnProperty(key)) {
|
||||||
@@ -114,7 +116,7 @@ export function keys(obj) {
|
|||||||
* @param {Object} obj The object to get the values for.
|
* @param {Object} obj The object to get the values for.
|
||||||
* @return {Array<*>} The values of the object.
|
* @return {Array<*>} The values of the object.
|
||||||
*/
|
*/
|
||||||
export function values(obj) {
|
export function values<T>(obj: {[keys: string]: T}): T[] {
|
||||||
const values = [];
|
const values = [];
|
||||||
for (const key in obj) {
|
for (const key in obj) {
|
||||||
if (!obj.hasOwnProperty(key)) {
|
if (!obj.hasOwnProperty(key)) {
|
||||||
@@ -131,7 +133,7 @@ export function values(obj) {
|
|||||||
* @param {Function} fn The function to invoke for each element. Has the
|
* @param {Function} fn The function to invoke for each element. Has the
|
||||||
* function signature <code>fn(element, index)</code>.
|
* function signature <code>fn(element, index)</code>.
|
||||||
*/
|
*/
|
||||||
export function forEach(array, fn) {
|
export function forEach<T>(array: T[], fn: (t: T, i: number) => void) {
|
||||||
for (let i = 0; i < array.length; i++) {
|
for (let i = 0; i < array.length; i++) {
|
||||||
fn(array[i], i);
|
fn(array[i], i);
|
||||||
}
|
}
|
||||||
@@ -148,7 +150,11 @@ export function forEach(array, fn) {
|
|||||||
* @return {*} The first value in the array which returns <code>true</code> for
|
* @return {*} The first value in the array which returns <code>true</code> for
|
||||||
* the given function.
|
* the given function.
|
||||||
*/
|
*/
|
||||||
export function findElement(array, fn, reverse) {
|
export function findElement<T>(
|
||||||
|
array: T[],
|
||||||
|
fn: (t: T, i?: number, a?: T[]) => boolean,
|
||||||
|
reverse?: boolean
|
||||||
|
) {
|
||||||
let i;
|
let i;
|
||||||
if (reverse) {
|
if (reverse) {
|
||||||
for (i = array.length - 1; i >= 0; i--) {
|
for (i = array.length - 1; i >= 0; i--) {
|
||||||
@@ -175,7 +181,11 @@ export function findElement(array, fn, reverse) {
|
|||||||
* @param {boolean} reverse True to search in reverse order.
|
* @param {boolean} reverse True to search in reverse order.
|
||||||
* @return {boolean} True if an element was removed.
|
* @return {boolean} True if an element was removed.
|
||||||
*/
|
*/
|
||||||
export function removeElement(array, fn, reverse) {
|
export function removeElement<T>(
|
||||||
|
array: T[],
|
||||||
|
fn: (t: T, i?: number, a?: T[]) => boolean,
|
||||||
|
reverse?: boolean
|
||||||
|
) {
|
||||||
let i;
|
let i;
|
||||||
let removed;
|
let removed;
|
||||||
if (reverse) {
|
if (reverse) {
|
||||||
@@ -203,7 +213,7 @@ export function removeElement(array, fn, reverse) {
|
|||||||
* @param {*} value The thing to check.
|
* @param {*} value The thing to check.
|
||||||
* @return {boolean} True if it is a function.
|
* @return {boolean} True if it is a function.
|
||||||
*/
|
*/
|
||||||
export function isFunction(value) {
|
export function isFunction(value: any) {
|
||||||
return Object.prototype.toString.call(value) == "[object Function]";
|
return Object.prototype.toString.call(value) == "[object Function]";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,7 +222,7 @@ export function isFunction(value) {
|
|||||||
* @param {*} value The thing to check.
|
* @param {*} value The thing to check.
|
||||||
* @return {boolean} True if it is an array.
|
* @return {boolean} True if it is an array.
|
||||||
*/
|
*/
|
||||||
export function isArray(value) {
|
export function isArray(value: any) {
|
||||||
return Array.isArray ? Array.isArray(value) :
|
return Array.isArray ? Array.isArray(value) :
|
||||||
Boolean(value && value.constructor === Array);
|
Boolean(value && value.constructor === Array);
|
||||||
}
|
}
|
||||||
@@ -223,7 +233,7 @@ export function isArray(value) {
|
|||||||
* @param {string[]} keys The list of keys that 'obj' must have.
|
* @param {string[]} keys The list of keys that 'obj' must have.
|
||||||
* @throws If the object is missing keys.
|
* @throws If the object is missing keys.
|
||||||
*/
|
*/
|
||||||
export function checkObjectHasKeys(obj, keys) {
|
export function checkObjectHasKeys(obj: object, keys: string[]) {
|
||||||
for (let i = 0; i < keys.length; i++) {
|
for (let i = 0; i < keys.length; i++) {
|
||||||
if (!obj.hasOwnProperty(keys[i])) {
|
if (!obj.hasOwnProperty(keys[i])) {
|
||||||
throw new Error("Missing required key: " + keys[i]);
|
throw new Error("Missing required key: " + keys[i]);
|
||||||
@@ -237,7 +247,7 @@ export function checkObjectHasKeys(obj, keys) {
|
|||||||
* @param {string[]} allowedKeys The list of allowed key names.
|
* @param {string[]} allowedKeys The list of allowed key names.
|
||||||
* @throws If there are extra keys.
|
* @throws If there are extra keys.
|
||||||
*/
|
*/
|
||||||
export function checkObjectHasNoAdditionalKeys(obj, allowedKeys) {
|
export function checkObjectHasNoAdditionalKeys(obj: object, allowedKeys: string[]): void {
|
||||||
for (const key in obj) {
|
for (const key in obj) {
|
||||||
if (!obj.hasOwnProperty(key)) {
|
if (!obj.hasOwnProperty(key)) {
|
||||||
continue;
|
continue;
|
||||||
@@ -254,7 +264,7 @@ export function checkObjectHasNoAdditionalKeys(obj, allowedKeys) {
|
|||||||
* @param {Object} obj The object to deep copy.
|
* @param {Object} obj The object to deep copy.
|
||||||
* @return {Object} A copy of the object without any references to the original.
|
* @return {Object} A copy of the object without any references to the original.
|
||||||
*/
|
*/
|
||||||
export function deepCopy(obj) {
|
export function deepCopy(obj: object): object {
|
||||||
return JSON.parse(JSON.stringify(obj));
|
return JSON.parse(JSON.stringify(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,7 +276,7 @@ export function deepCopy(obj) {
|
|||||||
*
|
*
|
||||||
* @return {boolean} true if the two objects are equal
|
* @return {boolean} true if the two objects are equal
|
||||||
*/
|
*/
|
||||||
export function deepCompare(x, y) {
|
export function deepCompare(x: any, y: any): boolean {
|
||||||
// Inspired by
|
// Inspired by
|
||||||
// http://stackoverflow.com/questions/1068834/object-comparison-in-javascript#1144249
|
// http://stackoverflow.com/questions/1068834/object-comparison-in-javascript#1144249
|
||||||
|
|
||||||
@@ -377,7 +387,7 @@ export function runPolyfills() {
|
|||||||
// SOURCE:
|
// SOURCE:
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
|
||||||
if (!Array.prototype.filter) {
|
if (!Array.prototype.filter) {
|
||||||
Array.prototype.filter = function(fun/*, thisArg*/) {
|
Array.prototype.filter = function(fun: Function/*, thisArg*/) {
|
||||||
if (this === void 0 || this === null) {
|
if (this === void 0 || this === null) {
|
||||||
throw new TypeError();
|
throw new TypeError();
|
||||||
}
|
}
|
||||||
@@ -572,7 +582,7 @@ export function runPolyfills() {
|
|||||||
* prototype.
|
* prototype.
|
||||||
* @param {function} superCtor Constructor function to inherit prototype from.
|
* @param {function} superCtor Constructor function to inherit prototype from.
|
||||||
*/
|
*/
|
||||||
export function inherits(ctor, superCtor) {
|
export function inherits(ctor: Function, superCtor: Function) {
|
||||||
// Add util.inherits from Node.js
|
// Add util.inherits from Node.js
|
||||||
// Source:
|
// Source:
|
||||||
// https://github.com/joyent/node/blob/master/lib/util.js
|
// https://github.com/joyent/node/blob/master/lib/util.js
|
||||||
@@ -596,7 +606,7 @@ export function inherits(ctor, superCtor) {
|
|||||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
ctor.super_ = superCtor;
|
(ctor as any).super_ = superCtor;
|
||||||
ctor.prototype = Object.create(superCtor.prototype, {
|
ctor.prototype = Object.create(superCtor.prototype, {
|
||||||
constructor: {
|
constructor: {
|
||||||
value: ctor,
|
value: ctor,
|
||||||
@@ -617,7 +627,7 @@ export function inherits(ctor, superCtor) {
|
|||||||
* @param {any} SuperType The type to act as a super instance
|
* @param {any} SuperType The type to act as a super instance
|
||||||
* @param {any} params Arguments to supply to the super type's constructor
|
* @param {any} params Arguments to supply to the super type's constructor
|
||||||
*/
|
*/
|
||||||
export function polyfillSuper(thisArg, SuperType, ...params) {
|
export function polyfillSuper(thisArg: any, SuperType: any, ...params: any) {
|
||||||
try {
|
try {
|
||||||
SuperType.call(thisArg, ...params);
|
SuperType.call(thisArg, ...params);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -633,7 +643,7 @@ export function polyfillSuper(thisArg, SuperType, ...params) {
|
|||||||
* @param {*} value the value to test
|
* @param {*} value the value to test
|
||||||
* @return {boolean} whether or not value is a finite number without type-coercion
|
* @return {boolean} whether or not value is a finite number without type-coercion
|
||||||
*/
|
*/
|
||||||
export function isNumber(value) {
|
export function isNumber(value: any): boolean {
|
||||||
return typeof value === 'number' && isFinite(value);
|
return typeof value === 'number' && isFinite(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -643,7 +653,7 @@ export function isNumber(value) {
|
|||||||
* @param {string} str the string to remove hidden characters from
|
* @param {string} str the string to remove hidden characters from
|
||||||
* @return {string} a string with the hidden characters removed
|
* @return {string} a string with the hidden characters removed
|
||||||
*/
|
*/
|
||||||
export function removeHiddenChars(str) {
|
export function removeHiddenChars(str: string): string {
|
||||||
return unhomoglyph(str.normalize('NFD').replace(removeHiddenCharsRegex, ''));
|
return unhomoglyph(str.normalize('NFD').replace(removeHiddenCharsRegex, ''));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -656,11 +666,11 @@ export function removeHiddenChars(str) {
|
|||||||
// Zero width no-break space (BOM) U+FEFF
|
// Zero width no-break space (BOM) U+FEFF
|
||||||
const removeHiddenCharsRegex = /[\u2000-\u200F\u202A-\u202F\u0300-\u036f\uFEFF\s]/g;
|
const removeHiddenCharsRegex = /[\u2000-\u200F\u202A-\u202F\u0300-\u036f\uFEFF\s]/g;
|
||||||
|
|
||||||
export function escapeRegExp(string) {
|
export function escapeRegExp(string: string): string {
|
||||||
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function globToRegexp(glob, extended) {
|
export function globToRegexp(glob: string, extended: any): string {
|
||||||
extended = typeof(extended) === 'boolean' ? extended : true;
|
extended = typeof(extended) === 'boolean' ? extended : true;
|
||||||
// From
|
// From
|
||||||
// https://github.com/matrix-org/synapse/blob/abbee6b29be80a77e05730707602f3bbfc3f38cb/synapse/push/__init__.py#L132
|
// https://github.com/matrix-org/synapse/blob/abbee6b29be80a77e05730707602f3bbfc3f38cb/synapse/push/__init__.py#L132
|
||||||
@@ -679,7 +689,7 @@ export function globToRegexp(glob, extended) {
|
|||||||
return pat;
|
return pat;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ensureNoTrailingSlash(url) {
|
export function ensureNoTrailingSlash(url: string): string {
|
||||||
if (url && url.endsWith("/")) {
|
if (url && url.endsWith("/")) {
|
||||||
return url.substr(0, url.length - 1);
|
return url.substr(0, url.length - 1);
|
||||||
} else {
|
} else {
|
||||||
@@ -688,13 +698,13 @@ export function ensureNoTrailingSlash(url) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns a promise which resolves with a given value after the given number of ms
|
// Returns a promise which resolves with a given value after the given number of ms
|
||||||
export function sleep(ms, value) {
|
export function sleep<T>(ms: number, value: T): Promise<T> {
|
||||||
return new Promise((resolve => {
|
return new Promise((resolve => {
|
||||||
setTimeout(resolve, ms, value);
|
setTimeout(resolve, ms, value);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isNullOrUndefined(val) {
|
export function isNullOrUndefined(val: any): boolean {
|
||||||
return val === null || val === undefined;
|
return val === null || val === undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -711,12 +721,15 @@ export function defer() {
|
|||||||
return {resolve, reject, promise};
|
return {resolve, reject, promise};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function promiseMapSeries(promises, fn) {
|
export async function promiseMapSeries<T>(
|
||||||
|
promises: Promise<T>[],
|
||||||
|
fn: (t: T) => void
|
||||||
|
): Promise<void> {
|
||||||
for (const o of await promises) {
|
for (const o of await promises) {
|
||||||
await fn(await o);
|
await fn(await o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function promiseTry(fn) {
|
export function promiseTry<T>(fn: () => T): Promise<T> {
|
||||||
return new Promise((resolve) => resolve(fn()));
|
return new Promise((resolve) => resolve(fn()));
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user