Skip to content

Commit

Permalink
Fix keybind, break other things (#2537)
Browse files Browse the repository at this point in the history
* Remove several functions from collections.js which have ES5 equivalents (#82417)

# Conflicts:
#	tgui/packages/tgui/interfaces/ApcControl.jsx
#	tgui/packages/tgui/interfaces/AtmosControlPanel.jsx
#	tgui/packages/tgui/interfaces/BluespaceSender.tsx
#	tgui/packages/tgui/interfaces/BluespaceVendor.tsx
#	tgui/packages/tgui/interfaces/CameraConsole.tsx
#	tgui/packages/tgui/interfaces/Cargo.jsx
#	tgui/packages/tgui/interfaces/CommunicationsConsole.jsx
#	tgui/packages/tgui/interfaces/CrewConsole.jsx
#	tgui/packages/tgui/interfaces/DestinationTagger.tsx
#	tgui/packages/tgui/interfaces/DnaConsole/DnaConsoleStorage.jsx
#	tgui/packages/tgui/interfaces/DnaConsole/MutationInfo.jsx
#	tgui/packages/tgui/interfaces/ExperimentConfigure.jsx
#	tgui/packages/tgui/interfaces/Fabrication/DesignBrowser.tsx
#	tgui/packages/tgui/interfaces/Fabrication/MaterialAccessBar.tsx
#	tgui/packages/tgui/interfaces/Fax.tsx
#	tgui/packages/tgui/interfaces/Filteriffic.jsx
#	tgui/packages/tgui/interfaces/FishCatalog.tsx
#	tgui/packages/tgui/interfaces/Gps.jsx
#	tgui/packages/tgui/interfaces/Hypertorus/Gases.tsx
#	tgui/packages/tgui/interfaces/Jukebox.tsx
#	tgui/packages/tgui/interfaces/LibraryAdmin.tsx
#	tgui/packages/tgui/interfaces/LibraryConsole.jsx
#	tgui/packages/tgui/interfaces/LibraryVisitor.jsx
#	tgui/packages/tgui/interfaces/MatMarket.tsx
#	tgui/packages/tgui/interfaces/MedicalRecords/RecordTabs.tsx
#	tgui/packages/tgui/interfaces/NtosCrewManifest.jsx
#	tgui/packages/tgui/interfaces/NtosMessenger/index.tsx
#	tgui/packages/tgui/interfaces/NtosNetDownloader.tsx
#	tgui/packages/tgui/interfaces/PersonalCrafting.tsx
#	tgui/packages/tgui/interfaces/Photocopier.jsx
#	tgui/packages/tgui/interfaces/PlaneMasterDebug.tsx
#	tgui/packages/tgui/interfaces/PortableChemMixer.tsx
#	tgui/packages/tgui/interfaces/PowerMonitor.jsx
#	tgui/packages/tgui/interfaces/PreferencesMenu/MainPage.tsx
#	tgui/packages/tgui/interfaces/PreferencesMenu/preferences/features/character_preferences/skin_tone.tsx
#	tgui/packages/tgui/interfaces/RequestsConsole/MessageWriteTab.tsx
#	tgui/packages/tgui/interfaces/RestockTracker.jsx
#	tgui/packages/tgui/interfaces/SecurityRecords/RecordTabs.tsx
#	tgui/packages/tgui/interfaces/SeedExtractor.tsx
#	tgui/packages/tgui/interfaces/ShuttleManipulator.jsx
#	tgui/packages/tgui/interfaces/StackCrafting.tsx
#	tgui/packages/tgui/interfaces/StationAlertConsole.jsx
#	tgui/packages/tgui/interfaces/Supermatter.tsx
#	tgui/packages/tgui/interfaces/Techweb.jsx
#	tgui/packages/tgui/interfaces/WarrantConsole.tsx
#	tgui/packages/tgui/interfaces/common/AccessConfig.tsx
#	tgui/packages/tgui/interfaces/common/AccessList.jsx

* minor tweaks

---------

Co-authored-by: Arthri <[email protected]>
  • Loading branch information
AndrewL97 and Arthri authored Jun 20, 2024
1 parent f5a304d commit a7769f6
Show file tree
Hide file tree
Showing 26 changed files with 307 additions and 288 deletions.
231 changes: 117 additions & 114 deletions tgui/packages/common/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,36 @@
* If collection is 'null' or 'undefined', it will be returned "as is"
* without emitting any errors (which can be useful in some cases).
*/
export const filter =
<T>(iterateeFn: (input: T, index: number, collection: T[]) => boolean) =>
(collection: T[]): T[] => {
if (collection === null || collection === undefined) {
return collection;
}
if (Array.isArray(collection)) {
const result: T[] = [];
for (let i = 0; i < collection.length; i++) {
const item = collection[i];
if (iterateeFn(item, i, collection)) {
result.push(item);
}
export const filter = <T>(
collection: T[],
iterateeFn: (input: T, index: number, collection: T[]) => boolean,
): T[] => {
if (collection === null || collection === undefined) {
return collection;
}
if (Array.isArray(collection)) {
const result: T[] = [];
for (let i = 0; i < collection.length; i++) {
const item = collection[i];
if (iterateeFn(item, i, collection)) {
result.push(item);
}
return result;
}
throw new Error(`filter() can't iterate on type ${typeof collection}`);
};
return result;
}
throw new Error(`filter() can't iterate on type ${typeof collection}`);
};

type MapFunction = {
<T, U>(
collection: T[],
iterateeFn: (value: T, index: number, collection: T[]) => U,
): (collection: T[]) => U[];
): U[];

<T, U, K extends string | number>(
collection: Record<K, T>,
iterateeFn: (value: T, index: K, collection: Record<K, T>) => U,
): (collection: Record<K, T>) => U[];
): U[];
};

/**
Expand All @@ -49,44 +52,30 @@ type MapFunction = {
* If collection is 'null' or 'undefined', it will be returned "as is"
* without emitting any errors (which can be useful in some cases).
*/
export const map: MapFunction =
<T, U>(iterateeFn) =>
(collection: T[]): U[] => {
if (collection === null || collection === undefined) {
return collection;
}

if (Array.isArray(collection)) {
return collection.map(iterateeFn);
}
export const map: MapFunction = (collection, iterateeFn) => {
if (collection === null || collection === undefined) {
return collection;
}

if (typeof collection === 'object') {
return Object.entries(collection).map(([key, value]) => {
return iterateeFn(value, key, collection);
});
if (Array.isArray(collection)) {
const result: unknown[] = [];
for (let i = 0; i < collection.length; i++) {
result.push(iterateeFn(collection[i], i, collection));
}
return result;
}

throw new Error(`map() can't iterate on type ${typeof collection}`);
};

/**
* Given a collection, will run each element through an iteratee function.
* Will then filter out undefined values.
*/
export const filterMap = <T, U>(
collection: T[],
iterateeFn: (value: T) => U | undefined,
): U[] => {
const finalCollection: U[] = [];

for (const value of collection) {
const output = iterateeFn(value);
if (output !== undefined) {
finalCollection.push(output);
if (typeof collection === 'object') {
const result: unknown[] = [];
for (let i in collection) {
if (Object.prototype.hasOwnProperty.call(collection, i)) {
result.push(iterateeFn(collection[i], i, collection));
}
}
return result;
}

return finalCollection;
throw new Error(`map() can't iterate on type ${typeof collection}`);
};

const COMPARATOR = (objA, objB) => {
Expand All @@ -112,39 +101,38 @@ const COMPARATOR = (objA, objB) => {
*
* Iteratees are called with one argument (value).
*/
export const sortBy =
<T>(...iterateeFns: ((input: T) => unknown)[]) =>
(array: T[]): T[] => {
if (!Array.isArray(array)) {
return array;
}
let length = array.length;
// Iterate over the array to collect criteria to sort it by
let mappedArray: {
criteria: unknown[];
value: T;
}[] = [];
for (let i = 0; i < length; i++) {
const value = array[i];
mappedArray.push({
criteria: iterateeFns.map((fn) => fn(value)),
value,
});
}
// Sort criteria using the base comparator
mappedArray.sort(COMPARATOR);

// Unwrap values
const values: T[] = [];
while (length--) {
values[length] = mappedArray[length].value;
}
return values;
};
export const sortBy = <T>(
array: T[],
...iterateeFns: ((input: T) => unknown)[]
): T[] => {
if (!Array.isArray(array)) {
return array;
}
let length = array.length;
// Iterate over the array to collect criteria to sort it by
let mappedArray: {
criteria: unknown[];
value: T;
}[] = [];
for (let i = 0; i < length; i++) {
const value = array[i];
mappedArray.push({
criteria: iterateeFns.map((fn) => fn(value)),
value,
});
}
// Sort criteria using the base comparator
mappedArray.sort(COMPARATOR);

export const sort = sortBy();
// Unwrap values
const values: T[] = [];
while (length--) {
values[length] = mappedArray[length].value;
}
return values;
};

export const sortStrings = sortBy<string>();
export const sort = <T>(array: T[]): T[] => sortBy(array);

/**
* Returns a range of numbers from start to end, exclusively.
Expand All @@ -153,12 +141,34 @@ export const sortStrings = sortBy<string>();
export const range = (start: number, end: number): number[] =>
new Array(end - start).fill(null).map((_, index) => index + start);

type ReduceFunction = {
<T, U>(
array: T[],
reducerFn: (
accumulator: U,
currentValue: T,
currentIndex: number,
array: T[],
) => U,
initialValue: U,
): U;
<T>(
array: T[],
reducerFn: (
accumulator: T,
currentValue: T,
currentIndex: number,
array: T[],
) => T,
): T;
};

/**
* A fast implementation of reduce.
*/
export const reduce = (reducerFn, initialValue) => (array) => {
export const reduce: ReduceFunction = (array, reducerFn, initialValue?) => {
const length = array.length;
let i;
let i: number;
let result;
if (initialValue === undefined) {
i = 1;
Expand All @@ -184,15 +194,16 @@ export const reduce = (reducerFn, initialValue) => (array) => {
* is determined by the order they occur in the array. The iteratee is
* invoked with one argument: value.
*/
export const uniqBy =
<T extends unknown>(iterateeFn?: (value: T) => unknown) =>
(array: T[]): T[] => {
const { length } = array;
const result: T[] = [];
const seen: unknown[] = iterateeFn ? [] : result;
let index = -1;
// prettier-ignore
outer:
export const uniqBy = <T extends unknown>(
array: T[],
iterateeFn?: (value: T) => unknown,
): T[] => {
const { length } = array;
const result: T[] = [];
const seen: unknown[] = iterateeFn ? [] : result;
let index = -1;
// prettier-ignore
outer:
while (++index < length) {
let value: T | 0 = array[index];
const computed = iterateeFn ? iterateeFn(value) : value;
Expand All @@ -214,10 +225,10 @@ export const uniqBy =
result.push(value);
}
}
return result;
};
return result;
};

export const uniq = uniqBy();
export const uniq = <T>(array: T[]): T[] => uniqBy(array);

type Zip<T extends unknown[][]> = {
[I in keyof T]: T[I] extends (infer U)[] ? U : never;
Expand Down Expand Up @@ -247,17 +258,6 @@ export const zip = <T extends unknown[][]>(...arrays: T): Zip<T> => {
return result;
};

/**
* This method is like "zip" except that it accepts iteratee to
* specify how grouped values should be combined. The iteratee is
* invoked with the elements of each group.
*/
export const zipWith =
<T, U>(iterateeFn: (...values: T[]) => U) =>
(...arrays: T[][]): U[] => {
return map((values: T[]) => iterateeFn(...values))(zip(...arrays));
};

const binarySearch = <T, U = unknown>(
getKey: (value: T) => U,
collection: readonly T[],
Expand Down Expand Up @@ -293,13 +293,15 @@ const binarySearch = <T, U = unknown>(
return compare > insertingKey ? middle : middle + 1;
};

export const binaryInsertWith =
<T, U = unknown>(getKey: (value: T) => U) =>
(collection: readonly T[], value: T) => {
const copy = [...collection];
copy.splice(binarySearch(getKey, collection, value), 0, value);
return copy;
};
export const binaryInsertWith = <T, U = unknown>(
collection: readonly T[],
value: T,
getKey: (value: T) => U,
): T[] => {
const copy = [...collection];
copy.splice(binarySearch(getKey, collection, value), 0, value);
return copy;
};

/**
* This method takes a collection of items and a number, returning a collection
Expand All @@ -325,7 +327,8 @@ export const paginate = <T>(collection: T[], maxPerPage: number): T[][] => {
return pages;
};

const isObject = (obj: unknown) => typeof obj === 'object' && obj !== null;
const isObject = (obj: unknown): obj is object =>
typeof obj === 'object' && obj !== null;

// Does a deep merge of two objects. DO NOT FEED CIRCULAR OBJECTS!!
export const deepMerge = (...objects: any[]): any => {
Expand Down
24 changes: 0 additions & 24 deletions tgui/packages/common/fp.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,3 @@ export const flow = (...funcs) => (input, ...rest) => {
}
return output;
};

/**
* Composes single-argument functions from right to left.
*
* All functions might accept a context in form of additional arguments.
* If the resulting function is called with more than 1 argument, rest of
* the arguments are passed to all functions unchanged.
*
* @param {...Function} funcs The functions to compose
* @returns {Function} A function obtained by composing the argument functions
* from right to left. For example, compose(f, g, h) is identical to doing
* (input, ...rest) => f(g(h(input, ...rest), ...rest), ...rest)
*/
export const compose = (...funcs) => {
if (funcs.length === 0) {
return (arg) => arg;
}
if (funcs.length === 1) {
return funcs[0];
}
// prettier-ignore
return funcs.reduce((a, b) => (value, ...rest) =>
a(b(value, ...rest), ...rest));
};
48 changes: 0 additions & 48 deletions tgui/packages/common/vector.js

This file was deleted.

Loading

0 comments on commit a7769f6

Please sign in to comment.