All files / src/plugins _tools.ts

10.87% Statements 5/46
0% Branches 0/36
0% Functions 0/7
11.11% Lines 5/45
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129              1x                                                     1x                                                 1x           1x                                                                                                           1x                  
/**
 * Encode plain SVG data string into Data URI string.
 *
 * @param {String} str input string
 * @param {String} type Data URI type
 * @return {String} output string
 */
export function encodeSVGDatauri(str, type) {
  var prefix = 'data:image/svg+xml';
 
  // base64
  if (!type || type === 'base64') {
    prefix += ';base64,';
 
    str = prefix + new Buffer(str).toString('base64');
 
    // URI encoded
  } else if (type === 'enc') {
    str = prefix + ',' + encodeURIComponent(str);
 
    // unencoded
  } else if (type === 'unenc') {
    str = prefix + ',' + str;
  }
 
  return str;
}
 
/**
 * Decode SVG Data URI string into plain SVG string.
 *
 * @param {string} str input string
 * @return {String} output string
 */
export function decodeSVGDatauri(str) {
  var regexp = /data:image\/svg\+xml(;charset=[^;,]*)?(;base64)?,(.*)/;
  var match = regexp.exec(str);
 
  // plain string
  if (!match) return str;
 
  var data = match[3];
 
  // base64
  if (match[2]) {
    str = new Buffer(data, 'base64').toString('utf8');
 
    // URI encoded
  } else if (data.charAt(0) === '%') {
    str = decodeURIComponent(data);
 
    // unencoded
  } else if (data.charAt(0) === '<') {
    str = data;
  }
 
  return str;
}
 
export function intersectArrays(a, b) {
  return a.filter(function(n) {
    return b.indexOf(n) > -1;
  });
}
 
export function cleanupOutData(
  data: number[],
  params: { leadingZero: boolean; negativeExtraSpace: boolean },
) {
  let str = '';
  let delimiter: string;
  let prev: number;
 
  data.forEach((item, i) => {
    // Space delimiter by default.
    delimiter = ' ';
 
    // No extra space in front of first number.
    if (i === 0) {
      delimiter = '';
    }
 
    // Remove floating-point numbers leading zeros.
    // 0.5 → .5
    // -0.5 → -.5
    if (params.leadingZero) {
      item = removeLeadingZero(item);
    }
 
    // No extra space in front of negative number or
    // in front of a floating number if a previous number is floating too.
    if (
      params.negativeExtraSpace &&
      (item < 0 || (String(item).charCodeAt(0) === 46 && prev % 1 !== 0))
    ) {
      delimiter = '';
    }
 
    // Save prev item value.
    prev = item;
    str += delimiter + item;
  });
 
  return str;
}
 
/**
 * Remove floating-point numbers leading zero.
 *
 * @example
 * 0.5 → .5
 *
 * @example
 * -0.5 → -.5
 *
 * @param {Float} num input number
 *
 * @return {String} output number as string
 */
export function removeLeadingZero(num) {
  var strNum = num.toString();
  if (0 < num && num < 1 && strNum.charCodeAt(0) == 48) {
    strNum = strNum.slice(1);
  } else if (-1 < num && num < 0 && strNum.charCodeAt(1) == 48) {
    strNum = strNum.charAt(0) + strNum.slice(2);
  }
  return strNum;
}