[MIRROR] Remove several functions from collections.js which have ES5 equivalents #2957
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Mirrored on Nova: NovaSector/NovaSector#2054
Original PR: tgstation/tgstation#82417
About The Pull Request
This PR should not affect players: it purely serves to clean up some old functions mentioned in https://tgstation13.org/phpBB/viewtopic.php?p=724810.
In summary: Refactor of
collections.ts
and refactor of collection manipulation. The following paragraphs go into the nuances of the PR and reading it is optional.I originally wanted to remove
flow
fromfp.js
, but I've decided against it until I have a more permanent solution forsortBy
anduniq
/uniqBy
common/collections.ts
Refactorsort(array)
is the same.sortStrings(array)
has been replaced bysort(array)
.reduce(...)
.map(...)
prior to #60961.f(...args)(array)
tof(array, ...args)
.ECMAScript 5
Array.prototype.*
functionsMost of the functions have direct equivalents in ES5, but behavior varies slightly. Modern JS engines such as V8 and SpiderMonkey(I've not checked for IE) uses optimized objects for array functions such that
typeof [].map(() => {})
is anobject
which isarray
-like, in contrast, the collection functions return anarray
.All replacement functions are part of ES5(caniuse), which is supported natively by Internet Explorer version 10 and version 11(MSHTML version 6 and version 7 respectively).
collections.ts
filter((element, index, array) => {})(array)
array.filter((element, index, array) => {})
map(function)(array)
array.map(function)
map((value, key, object) => {})(object)
Object.entries(object).map(([key. value], entries) => {})
filterMap(array, (element) => {})
array.map((element) => {}).filter((element) => element !== undefined)
reduce((accumulator, currentValue, currentIndex, array) => {}, initialValue)(array)
array.reduce((accumulator, currentValue, currentIndex, array) => {}, initialValue)
zipWith((element1, element2, element3) => {})(array1, array2, array3)
zip(array1, array2, array3).map(([element1, element2, element3]) => {})
sort(array)
array.sort()
sortStrings(array)
array.sort()
*: the second argument of the arrow function is
Object.entries(object)
rather thanobject
.**: there may be some differences in the way these functions sort(such as culture and casing considerations), but they should remain largely the same
Polyfills
Contrary to the statement on the original forum post, I've not decided to include Lodash because the project also encourages questionable practices. Lodash has a newer competitor, Radash, but I've decided against including that too as it's not mature enough to provide significant benefit over the functions defined in /tg/ui.
unique
is a function present in core-js(a polyfill for https://github.com/tc39/proposal-array-unique), but I've decided against using it because it lacks TypeScript definitions, and the proposal has been in Stage 1 for quite a while.Vectors
Rewrote
vecAdd
,vecSubtract
,vecMultiply
, andvecDivide
. The following compares the current and new implementation ofvecAdd
.Current Implementation:
Reduce followed by element-wise zip(zip and addition step combined, otherwise the diagram wouldn't fit)
New Implementation:
Zip followed by element-wise reduce
Fixed
vecNormalize
. Previously, it would attempt to divide a vector by a scalar(which is not a valid argument forvecDivide
). The function has been corrected to properly each vector's component by the scalar.Other Changes
NtosNetDownloader.tsx
for brevity.LibraryAdmin.tsx
to fix type errors.fp.js
'compose
function.StackCrafting.tsx
's filterRecipeList.Why It's Good For The Game
Reduced code debt is good.