import { Node } from "figma-js" ; import { Shortcuts , ShortcutType } from "./types" ; export function uniqBy ( arr , key ) { const set = new Set ( ) ; return arr . filter ( el => ( v => ! set . has ( v ) && set . add ( v ) ) ( el [ key ] ) ) ; } export function groupBy ( arr , key ) { return arr . reduce ( function ( rv , x ) { ( rv [ x [ key ] ] = rv [ x [ key ] ] || [ ] ) . push ( x ) ; return rv ; } , { } ) ; } export const groupNodes = ( nodes ) : Shortcuts => parseShortcutKeys ( groupBy ( uniqBy ( nodes , "id" ) , "type" ) ) ; export const parseShortcutKeys = ( obj ) : Shortcuts => { const mapKeys = { DOCUMENT : "documents" , COMPONENT : "components" , CANVAS : "pages" , LINE : "lines" , INSTANCE : "instances" , FRAME : "frames" , GROUP : "groups" , VECTOR : "vectors" , BOOLEAN : "booleans" , STAR : "stars" , ELLIPSE : "ellipses" , REGULAR_POLYGON : "regularPolygon" , RECTANGLE : "rectangles" , TEXT : "texts" , SLICE : "slices" , STYLE : "styles" , } ; return Object . fromEntries ( Object . entries ( obj ) . map ( ( [ k , v ] ) => [ mapKeys [ k as ShortcutType ] , v ] ) ) as Shortcuts ; } ;