fix: multipart form data sorting (#2067)

* fix: multipart form data sorting

  place all the file types in multipart form data
  body in the last to avoid errors due to map key
  being placed after file type data

* refactor: fp-ify formdata file sort implementation

Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
kyteinsky
2022-01-22 21:00:39 +00:00
committed by GitHub
parent 3bb65f2115
commit 6205b5f163
3 changed files with 83 additions and 19 deletions

View File

@@ -0,0 +1,41 @@
import clone from "lodash/clone"
/**
* Sorts the array based on the sort func.
* NOTE: Creates a new array, if you don't need ref
* to original array, use `arrayUnsafeSort` for better perf
* @param sortFunc Sort function to sort against
*/
export const arraySort =
<T>(sortFunc: (a: T, b: T) => number) =>
(arr: T[]) => {
const newArr = clone(arr)
newArr.sort(sortFunc)
return newArr
}
/**
* Sorts an array based on the sort func.
* Unsafe because this sort mutates the passed array
* and returns it. So use it if you do not want the
* original array for better performance
* @param sortFunc sort function to sort against (same as Array.sort)
*/
export const arrayUnsafeSort =
<T>(sortFunc: (a: T, b: T) => number) =>
(arr: T[]) => {
arr.sort(sortFunc)
return arr
}
/**
* Equivalent to `Array.prototype.flatMap`
* @param mapFunc The map function
* @returns
*/
export const arrayFlatMap =
<T, U>(mapFunc: (value: T, index: number, arr: T[]) => U[]) =>
(arr: T[]) =>
arr.flatMap(mapFunc)