Skip to content
Snippets Groups Projects
Unverified Commit 1fa734e6 authored by Benoit Vinay's avatar Benoit Vinay Committed by GitHub
Browse files

Convert `utils` to TypeScript (#23566)

* Utils converted to TS

* Misc

* Split utils into its package

* Removed nyi method

* Misc
parent dbbe6b5b
No related branches found
No related tags found
No related merge requests found
export function createLookupByProperty(items: any[], property: string) {
const lookup: Record<string, any> = {};
for (const item of items) {
lookup[item[property]] = item;
}
return lookup;
}
export { createLookupByProperty } from "./createLookupByProperty";
export { memoizeClass } from "./memoizeClass";
export { sortObject } from "./sortObject";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
export function nyi(target, key, descriptor) {
const method = descriptor.value;
import { Constructor } from "./types";
descriptor.value = function(...args) {
console.warn(
"Method not yet implemented: " + target.constructor.name + "::" + key,
);
return method.apply(this, args);
};
return descriptor;
}
function getWithFallback(map, key, fallback) {
function getWithFallback(
map: Map<string, any>,
key: string,
fallback: () => void,
) {
if (map.has(key)) {
return map.get(key);
} else {
......@@ -25,29 +16,35 @@ function getWithFallback(map, key, fallback) {
const memoized = new WeakMap();
type Constructor<T> = new (...args: any[]) => T;
const createMap = () => new Map();
export function memoizeClass<T>(...keys: string[]) {
return function(Class: Constructor<T>) {
export function memoizeClass<T>(
...keys: string[]
): (Class: Constructor<T>) => Constructor<T> {
return (Class: Constructor<T>): Constructor<T> => {
const descriptors = Object.getOwnPropertyDescriptors(Class.prototype);
keys.forEach(key => {
// Is targeted method present in Class?
if (!(key in descriptors)) {
throw new TypeError(`${key} is not a member of class`);
}
const descriptor = descriptors[key];
const method = descriptor.value;
// If we don't get a decsriptor.value, it must have a getter (i.e., ES6 class properties)
if (!method) {
// If we don't get a decsriptor.value, it must have a getter
// (i.e., ES6 class properties)
throw new TypeError(`Class properties cannot be memoized`);
}
if (typeof method !== "function") {
// Method should be a function/method
else if (typeof method !== "function") {
throw new TypeError(`${key} is not a method and cannot be memoized`);
}
// Memoize
Object.defineProperty(Class.prototype, key, {
...descriptor,
value: function(...args) {
value: function(...args: any[]) {
const path = [this, method, args.length, ...args];
const last = path.pop();
const map = path.reduce(
......@@ -62,40 +59,3 @@ export function memoizeClass<T>(...keys: string[]) {
return Class;
};
}
const createMap = () => new Map();
// `sortObject`` copies objects for deterministic serialization.
// Objects that have equal keys and values don't necessarily serialize to the
// same string. JSON.strinify prints properties in inserted order. This function
// sorts keys before adding them to the duplicated object to ensure consistent
// serialization.
export function sortObject(obj) {
if (obj === null || typeof obj !== "object") {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(sortObject);
}
const sortedKeyValues = Object.entries(obj).sort(([keyA], [keyB]) =>
keyA.localeCompare(keyB),
);
const o = {};
for (const [k, v] of sortedKeyValues) {
o[k] = sortObject(v);
}
return o;
}
export function createLookupByProperty(items, property) {
const lookup = {};
for (const item of items) {
lookup[item[property]] = item;
}
return lookup;
}
// `sortObject` copies objects for deterministic serialization.
// Objects that have equal keys and values don't necessarily serialize to the
// same string. JSON.stringify prints properties in inserted order. This function
// sorts keys before adding them to the duplicated object to ensure consistent
// serialization.
export function sortObject(obj: any | any[]): any | any[] {
if (obj === null || typeof obj !== "object") {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(sortObject);
}
const sortedKeyValues = Object.entries(obj).sort(([keyA], [keyB]) =>
keyA.localeCompare(keyB),
);
const o: Record<string, any> = {};
for (const [k, v] of sortedKeyValues) {
o[k] = sortObject(v);
}
return o;
}
export type Constructor<T> = new (...args: any[]) => T;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment