Last active
August 30, 2022 15:27
-
-
Save abelflopes/01ae686ab74c1b969c2cf43c2b413f4e to your computer and use it in GitHub Desktop.
Utils to remove repeated items from array & to sort based on equality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getEqualitySorter = | |
<T = unknown>(sortList: T[]) => | |
(a: T, b: T): number => { | |
for (const i in sortList) { | |
const orderItem = sortList[i]; | |
if (a === orderItem && b !== orderItem) return -1; | |
if (a !== orderItem && b === orderItem) return 1; | |
} | |
return 0; | |
}; | |
const removeRepeated = <T = unknown>(arr: T[]): T[] => | |
arr.reduce<T[]>((a, b) => { | |
if (!a.includes(b)) a.push(b); | |
return a; | |
}, []); | |
const data = parameter.map((p) => Object.keys(p)).flat(); | |
console.log("initial data", data); | |
/* initial data [ 'type', 'name', 'default', 'description', 'type', 'name', 'description' ] */ | |
const parsedData = this.removeRepeated(data).sort( | |
this.getEqualitySorter(["name", "type", "default", "description"]) | |
); | |
console.log("parsed data", parsedData); | |
/* parsed data [ 'name', 'type', 'default', 'description' ] */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment