All files / plugins collapseGroups.ts

12.5% Statements 1/8
0% Branches 0/10
0% Functions 0/3
12.5% Lines 1/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34                                                    1x              
import { JsApi } from '../lib/jsapi';
import { Plugin } from './_types';
 
/*
 * Collapse groups with no attributes.
 * TODO: what about groups w/ a name but no other attributes?
 */
function fn(item: JsApi) {
  if (!item.isElem() || item.isEmpty()) {
    return item;
  }
  item.content.forEach((g, i) => {
    // Collapse non-empty groups with no attributes.
    if (
      g.isElem('group') &&
      !g.isEmpty() &&
      !g.hasAttr() &&
      // TODO: figure out if there is a good way to optimize groups w/ clip-paths...
      !g.content.some(c => c.isElem('clip-path'))
    ) {
      item.spliceContent(i, 1, g.content);
    }
  });
  return item;
}
 
export const collapseGroups: Plugin<undefined> = {
  type: 'perItemReverse',
  active: true,
  description: 'collapses useless groups',
  params: undefined,
  fn,
};