Files
outline/shared/utils/rfc6902/util.ts
T
Tom Moor f9a2cbc1b3 chore: resolve remaining unbound-method lint warnings (#12206)
* chore: resolve remaining unbound-method lint warnings

Apply targeted fixes per call pattern: arrow wrappers when passing a
method as a callback, arrow-function class fields when the method
doesn't depend on `this`, and `.bind()` when capturing for later
invocation.

Also replaces the rfc6902 hasOwnProperty re-export with a small wrapper
function so callers don't reference an unbound prototype method.

* chore: memoize history.goBack callbacks

Stable identity prevents Button re-renders and avoids re-subscribing
the global keydown handler in RegisterKeyDown when the parent renders.
2026-04-28 20:49:35 -04:00

64 lines
2.1 KiB
TypeScript

export const hasOwn = (obj: object, key: PropertyKey): boolean =>
Object.prototype.hasOwnProperty.call(obj, key);
export function objectType(object: unknown) {
if (object === undefined) {
return "undefined";
}
if (object === null) {
return "null";
}
if (Array.isArray(object)) {
return "array";
}
return typeof object;
}
function isNonPrimitive(value: unknown): value is object {
// loose-equality checking for null is faster than strict checking for each of null/undefined/true/false
// checking null first, then calling typeof, is faster than vice-versa
return value !== null && typeof value === "object";
}
/**
* Recursively copy a value.
*
* @param source - should be a JavaScript primitive, Array, Date, or (plain old) Object.
* @returns copy of source where every Array and Object have been recursively
* reconstructed from their constituent elements.
*/
export function clone<T>(source: T): T {
if (!isNonPrimitive(source)) {
// short-circuiting is faster than a single return
return source;
}
// x.constructor == Array is the fastest way to check if x is an Array
if (source.constructor === Array) {
// construction via imperative for-loop is faster than source.map(arrayVsObject)
const length = (source as Array<unknown>).length;
// setting the Array length during construction is faster than just `[]` or `new Array()`
const arrayTarget = new Array(length) as T;
for (let i = 0; i < length; i++) {
(arrayTarget as Array<unknown>)[i] = clone((source as Array<unknown>)[i]);
}
return arrayTarget;
}
// Date
if (source.constructor === Date) {
const dateTarget = new Date(+(source as Date)) as T;
return dateTarget;
}
// Object
const objectTarget = {} as T;
// declaring the variable (with const) inside the loop is faster
for (const key in source) {
// hasOwn costs a bit of performance, but it's semantically necessary
if (hasOwn(source, key)) {
(objectTarget as Record<string, unknown>)[key] = clone(
(source as Record<string, unknown>)[key]
);
}
}
return objectTarget;
}