All files / plugins removeDefaults.ts

94.12% Statements 16/17
80% Branches 12/15
100% Functions 2/2
94.12% Lines 16/17
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69          1x                                                                 1x 6x     6x 6x 6x 6x 42x 42x 42x 42x   42x 42x 21x         6x     1x              
import { JsApi } from '../lib/jsapi';
import { Plugin } from './_types';
 
const defaultAttrs: {
  [index: string]: { [index: string]: string | number };
} = {
  vector: {
    alpha: 1,
    autoMirrored: 'false',
    tintMode: 'src_in',
  },
  group: {
    pivotX: 0,
    pivotY: 0,
    rotation: 0,
    scaleX: 1,
    scaleY: 1,
    translateX: 0,
    translateY: 0,
  },
  path: {
    fillAlpha: 1,
    fillColor: '#00000000',
    fillType: 'nonZero',
    strokeAlpha: 1,
    strokeColor: '#00000000',
    strokeLineCap: 'butt',
    strokeLineJoin: 'miter',
    strokeMiterLimit: 4,
    trimPathStart: 0,
    trimPathEnd: 1,
    trimPathOffset: 0,
  },
};
 
/**
 * Remove attributes with default values.
 */
export function fn(item: JsApi) {
  Iif (!item.isElem() || !item.attrs) {
    return item;
  }
  Eif (item.isElem('vector') || item.isElem('group') || item.isElem('path')) {
    const elemType = item.elem as 'vector' | 'group' | 'path';
    const defaults = defaultAttrs[elemType];
    Object.keys(defaults).forEach(key => {
      const attrName = `android:${key}`;
      Eif (item.hasAttr(attrName)) {
        const defaultValue = defaults[key];
        const actualValue = item.attr(attrName).value;
        const convertedValue =
          typeof defaultValue === 'number' ? +actualValue : actualValue;
        if (defaultValue === convertedValue) {
          item.removeAttr(attrName);
        }
      }
    });
  }
  return item;
}
 
export const removeDefaults: Plugin<undefined> = {
  type: 'perItem',
  active: true,
  description: 'remove attributes with default values',
  params: undefined,
  fn,
};