Files
Paradise/tgui/packages/common/collections.ts
+12
BurzahGitHubAylongwarriorstar-orionArthri/tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>/tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>/tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>/tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>/tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>/tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>Toastical/tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>AylongArthri/tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>Charlie Nolan
18573ef254 Tgui v6 (#29342)
* [TGUI v6 Project] Remove `context` (#29086)

* [TGUI v6 Project] Remove `context`

* I forgot I had to do that.

* [TGUI v6 Project] Move to React (#29090)

* It compiles

* Last Inferno uses

* all works now

* Fixes

* [TGUI v6] Changes files extensions for interfaces from .js to .jsx, Deprecates Inferno State Hooks (#29094)

* Initial Commit

* Apply suggestions from code review

trust me bro

Co-authored-by: Aylong <69762909+AyIong@users.noreply.github.com>
Signed-off-by: Burzah <116982774+Burzah@users.noreply.github.com>

---------

Signed-off-by: Burzah <116982774+Burzah@users.noreply.github.com>
Co-authored-by: Aylong <69762909+AyIong@users.noreply.github.com>

* [TGUI v6] Replaces babel with swc, fixes realtive paths on a few files, new build (#29100)

* Initial commit

* Build

* webpack config and errors and rebuild

* export routingError, hopefully

* fix pda app routing

* pass current tab index to tab headers and content in nanobank

---------

Co-authored-by: warriorstar-orion <orion@snowfrost.garden>

* fix dna modifier bluescreen and library computer tabs (#29108)

* TGUI parity with TG

Need to fix a bunch of things...

* Implement TGUI payload chunking backend

https://github.com/tgstation/tgstation/pull/90295

* Filx chan bottom scroll. Again

* Fix TGchat focus

* Fix rounding

* Fix inputs

* Upgrade deps

* More fixes

* Fix chat lineHeight

* Fix NanoMap

* Update `tgui-core`

* fix autolathe

* fix airlock electronics

* fix intercom

* fix compost bin height

* Fix chat preferences

* Move to TSX without types :clueless:

* test tiny meet your new brother test tgui

* Fix RCD

* fix stackcraft

* fix up beakercontents

* Fix MODsuits

* fix seed extractor

* Merge branch 'tgui-v6' of github.com:ParadiseSS13/Paradise into tgui-v6

* fix holodeck

* fix cookbook pda app

* fix remaining useState args

* Fix DNAModifier

* Fix Sleeper

* Fix SupermatterMonitor

* Remove `vitest`

* Update dev-server

* Fix `IconStack`

* Correct margin

* bundles, please don't break

* prettier

* Revert "Remove `vitest`"

This reverts commit 0609006abb.

* Re-add tests

* Prettier

* fix maplint for test_tgui

* TGchat shine effect

* TGUI Rebuildo

* Fix broken tabs, search boxes, sortable tables, and others (#29450)

* Replace KitchenSink theme state with a React context

* Remove last usage of useLocalState

* Rebind unbound useState calls

* Build and update /tg/ui

* Reformat /tg/ui

* [ci skip]

---------

Co-authored-by: Arthri <41360489+a@users.noreply.github.com>
Co-authored-by: /tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>

* Ports #29217 to tgui v6. Added tgui-setup to CI builds (#29787)

* Add tgui-setup to build scripts

* Fix CORS issue

* Build and update /tg/ui

Co-authored-by: /tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>

* Add one missing word to comment

---------

Co-authored-by: Arthri <41360489+a@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix law manager

* Removes dangling references to `context` (#29793)

* Removes dangling references to `context`

* Build and update /tg/ui (#25)

Co-authored-by: /tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: Arthri <41360489+a@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix messenger and atmos scanner

* swap out line break in examinate

* fix powermonitor

* Bump tgui-core from 3.1.5/3.1.4 to 4.3.4 (#29799)

* Bump tgui-core

* Build and update /tg/ui (#28)

Co-authored-by: /tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: Arthri <41360489+a@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Fixes additional examine text

* oopsie

* Small tgui v6 fixes (#29801)

* Remove flow

* Miscellaneous fixes

* Miscellaneous fixes

* Moved properties placed on the wrong element

* Migrate removed CSS attributes in JSX

* Build and update /tg/ui (#27)

Co-authored-by: /tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: Arthri <41360489+a@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix carbon examine

* fix robot examine

* fix lint

* improve SMES appearance

* fix camera console name order

* more newline fixes

* fix boolean modal cancel not working

* fix random disabled access in RCD

* point tgui to correct passthrough verb names

* Fix missing 0 option for ERTs. (#29819)

* fix newlines in damage flavor

* Revert all changes related to newlines in DM

* fix the actual thing that was wrong with newlines

* fix ERT manager deny input

* [tgui-v6] Makes Agent Card UI more aesthethic, fixes input fields, fixes minimum age slider (#29832)

* agent card fixes and tweaks

* tgui bundle

* Reformat /tg/ui (#33) (#29838)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* restore camera console list appearance

* fixed pai ui paths (#29867)

* /tg/ui fixes no. 3 (#29868)

* Remove IconStack styles that conflict with styles added in tgstation/tgui-core#203

* Fix minesweeper icon

* DNA Modifier fixes

* Fix alignment of icons in PDA games menu

* Expand chem dispenser

* Fix pAI interfaces

* Build and update /tg/ui

---------

Co-authored-by: Arthri <41360489+a@users.noreply.github.com>
Co-authored-by: /tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Toastical <20125180+Toastical@users.noreply.github.com>

* teleporter fix, bundle changes, test

* /tg/ui fixes no.4 (#29877)

* Fix translucent buttons

* Remove stories already present in tgui-core

* Make voting choices not transparent

* Build and update /tg/ui

* Fix size of set ringtone in PDA messenger

* Fix crew monitor highlight colors

* Fix ReagentsEditor search. Small style fixes

* tgui-say radio prefix has special color again

* Expand chameleon mask a little

* Fix search in chameleon mask

* Make list input modal slightly taller

* [ci skip]

* Build /tg/ui

---------

Co-authored-by: Arthri <41360489+a@users.noreply.github.com>
Co-authored-by: /tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>

---------

Signed-off-by: Burzah <116982774+Burzah@users.noreply.github.com>
Co-authored-by: Aylong <69762909+AyIong@users.noreply.github.com>
Co-authored-by: warriorstar-orion <orion@snowfrost.garden>
Co-authored-by: Aylong <alexanderkitsa@gmail.com>
Co-authored-by: Arthri <41360489+Arthri@users.noreply.github.com>
Co-authored-by: Arthri <41360489+a@users.noreply.github.com>
Co-authored-by: /tg/ui Builder <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Charlie Nolan <funnyman3595@gmail.com>
Co-authored-by: Toastical <20125180+Toastical@users.noreply.github.com>
2025-07-27 23:23:20 +00:00

317 lines
8.9 KiB
TypeScript

/**
* @file
* @copyright 2020 Aleksej Komarov
* @license MIT
*/
/**
* Iterates over elements of collection, returning an array of all elements
* iteratee returns truthy for. The predicate is invoked with three
* arguments: (value, index|key, collection).
*
* 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>(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}`);
};
type MapFunction = {
<T, U>(collection: T[], iterateeFn: (value: T, index: number, collection: T[]) => U): U[];
<T, U, K extends string | number>(
collection: Record<K, T>,
iterateeFn: (value: T, index: K, collection: Record<K, T>) => U
): U[];
};
/**
* Creates an array of values by running each element in collection
* thru an iteratee function. The iteratee is invoked with three
* arguments: (value, index|key, collection).
*
* 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 = (collection, iterateeFn) => {
if (collection === null || collection === undefined) {
return 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;
}
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;
}
throw new Error(`map() can't iterate on type ${typeof collection}`);
};
const COMPARATOR = (objA, objB) => {
const criteriaA = objA.criteria;
const criteriaB = objB.criteria;
const length = criteriaA.length;
for (let i = 0; i < length; i++) {
const a = criteriaA[i];
const b = criteriaB[i];
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
}
return 0;
};
/**
* Creates an array of elements, sorted in ascending order by the results
* of running each element in a collection thru each iteratee.
*
* Iteratees are called with one argument (value).
*/
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);
// Unwrap values
const values: T[] = [];
while (length--) {
values[length] = mappedArray[length].value;
}
return values;
};
export const sort = <T>(array: T[]): T[] => sortBy(array);
/**
* Returns a range of numbers from start to end, exclusively.
* For example, range(0, 5) will return [0, 1, 2, 3, 4].
*/
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: ReduceFunction = (array, reducerFn, initialValue?) => {
const length = array.length;
let i: number;
let result;
if (initialValue === undefined) {
i = 1;
result = array[0];
} else {
i = 0;
result = initialValue;
}
for (; i < length; i++) {
result = reducerFn(result, array[i], i, array);
}
return result;
};
/**
* Creates a duplicate-free version of an array, using SameValueZero for
* equality comparisons, in which only the first occurrence of each element
* is kept. The order of result values is determined by the order they occur
* in the array.
*
* It accepts iteratee which is invoked for each element in array to generate
* the criterion by which uniqueness is computed. The order of result values
* is determined by the order they occur in the array. The iteratee is
* invoked with one argument: value.
*/
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;
if (computed === computed) {
let seenIndex = seen.length;
while (seenIndex--) {
if (seen[seenIndex] === computed) {
continue outer;
}
}
if (iterateeFn) {
seen.push(computed);
}
result.push(value);
} else if (!seen.includes(computed)) {
if (seen !== result) {
seen.push(computed);
}
result.push(value);
}
}
return result;
};
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;
}[];
/**
* Creates an array of grouped elements, the first of which contains
* the first elements of the given arrays, the second of which contains
* the second elements of the given arrays, and so on.
*/
export const zip = <T extends unknown[][]>(...arrays: T): Zip<T> => {
if (arrays.length === 0) {
return [];
}
const numArrays = arrays.length;
const numValues = arrays[0].length;
const result: Zip<T> = [];
for (let valueIndex = 0; valueIndex < numValues; valueIndex++) {
const entry: unknown[] = [];
for (let arrayIndex = 0; arrayIndex < numArrays; arrayIndex++) {
entry.push(arrays[arrayIndex][valueIndex]);
}
// I tried everything to remove this any, and have no idea how to do it.
result.push(entry as any);
}
return result;
};
const binarySearch = <T, U = unknown>(getKey: (value: T) => U, collection: readonly T[], inserting: T): number => {
if (collection.length === 0) {
return 0;
}
const insertingKey = getKey(inserting);
let [low, high] = [0, collection.length];
// Because we have checked if the collection is empty, it's impossible
// for this to be used before assignment.
let compare: U = undefined as unknown as U;
let middle = 0;
while (low < high) {
middle = (low + high) >> 1;
compare = getKey(collection[middle]);
if (compare < insertingKey) {
low = middle + 1;
} else if (compare === insertingKey) {
return middle;
} else {
high = middle;
}
}
return compare > insertingKey ? middle : middle + 1;
};
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
* of collections, where the maximum amount of items in each is that second arg
*/
export const paginate = <T>(collection: T[], maxPerPage: number): T[][] => {
const pages: T[][] = [];
let page: T[] = [];
let itemsToAdd = maxPerPage;
for (const item of collection) {
page.push(item);
itemsToAdd--;
if (!itemsToAdd) {
itemsToAdd = maxPerPage;
pages.push(page);
page = [];
}
}
if (page.length) {
pages.push(page);
}
return pages;
};
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 => {
const target = {};
for (const object of objects) {
for (const key of Object.keys(object)) {
const targetValue = target[key];
const objectValue = object[key];
if (Array.isArray(targetValue) && Array.isArray(objectValue)) {
target[key] = [...targetValue, ...objectValue];
} else if (isObject(targetValue) && isObject(objectValue)) {
target[key] = deepMerge(targetValue, objectValue);
} else {
target[key] = objectValue;
}
}
}
return target;
};