Skip to content

Instantly share code, notes, and snippets.

@abelflopes
Last active August 30, 2022 15:27
Show Gist options
  • Save abelflopes/01ae686ab74c1b969c2cf43c2b413f4e to your computer and use it in GitHub Desktop.
Save abelflopes/01ae686ab74c1b969c2cf43c2b413f4e to your computer and use it in GitHub Desktop.
Utils to remove repeated items from array & to sort based on equality
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