All files / packages/sds-stylelint-config/src/utils search.js

0% Statements 0/86
0% Branches 0/44
0% Functions 0/27
0% Lines 0/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                                                                                                                                                                                                                                                                                                                                                                                         
const metadata = require('../metadata/metadata.js');
 
/**
 * Store keys based on metadata
 * @param {Object[]} arr An array of words from the css custom property
 * @returns {Object} An object of keys based on metadata
 */
const storeSearchableKeys = (arr) => {
  const local = {
    scope: null,
    context: null,
    variant: null,
    element: null,
    category: null,
    property: null,
    attribute: null,
    state: null,
    psuedoState: null,
  };
 
  arr.map((el, key) => {
    metadata.component.valid.scope.find((val) => {
      if (el === val) {
        local.scope = key;
      }
    });
    metadata.component.valid.category.find((val) => {
      if (el === val) {
        local.category = key;
      }
    });
    metadata.component.valid.property.find((val) => {
      if (el === val) {
        local.property = key;
      }
    });
    // Check for compound attributes
    const nextValue = arr[key + 1];
    const compoundValue = nextValue ? `${el}-${nextValue}` : el;
    metadata.component.valid.attribute.find((val) => {
      if (compoundValue === val || el === val) {
        local.attribute = key;
      }
    });
    metadata.component.valid.state.find((val) => {
      if (el === val) {
        local.state = key;
      }
    });
    metadata.component.valid.psuedoState.find((val) => {
      if (el === val) {
        local.psuedoState = key;
      }
    });
  });
  return local;
};
 
/**
 * Store keys based on metadata
 * @param {Object[]} arr An array of words from the css custom property
 * @returns {Object} An object of keys based on metadata
 */
const storeSearchableGlobalKeys = (arr) => {
  const local = {
    scope: null,
    category: null,
    property: null,
    pairing: null,
    role: null,
    attribute: null,
    variant: null,
    range: null,
  };
 
  arr.map((el, key) => {
    metadata.global.valid.scope.find((val) => {
      if (el === val) {
        local.scope = key;
      }
    });
    metadata.global.valid.category.find((val) => {
      if (el === val) {
        local.category = key;
      }
    });
    metadata.global.valid.property.find((val) => {
      if (el === val) {
        local.property = key;
      }
    });
    metadata.global.valid.pairing.find((val) => {
      if (el === val) {
        local.pairing = key;
      }
    });
    metadata.global.valid.role.find((val) => {
      if (el === val) {
        local.role = key;
      }
    });
    metadata.global.valid.context.find((val) => {
      if (el === val) {
        local.context = key;
      }
    });
    metadata.global.valid.attribute.find((val) => {
      if (el === val) {
        local.attribute = key;
      }
    });
    metadata.global.valid.range.find((val) => {
      if (el === val) {
        local.range = key;
      }
    });
  });
  return local;
};
 
const analyzeFuzzy = (obj) => {
  const suggested = [];
  const checkFuzzyKeys = obj.fuzzyKeys.values();
 
  obj.fuzzyKeys.forEach((it) => {
    const suggest = {
      word: null,
      stored: null,
      key: null,
      lastKey: null,
    };
    if (obj.groups[checkFuzzyKeys.next().value]) {
      if (!Object.values(obj.keys).includes(it)) {
        suggest.word = obj.groups[it];
        suggest.stored = Object.values(obj.keys).includes(it);
        suggest.key = it;
        suggest.lastKey = it === obj.lastKey;
      }
    }
    suggested.push(suggest);
  });
  return suggested;
};
 
/**
 * Retrieve missing keys from initial index storage based on metadata
 * @param {*} set
 * @param {*} total
 * @returns {Object[]} Set of unused keys
 */
const getMissingKeys = (set, total) => {
  let keySet = [];
  Object.keys(set).map((key) => {
    if (set[key]) {
      keySet.push(set[key]);
    }
  });
  if (!keySet.includes(total)) {
    keySet = [...keySet, total];
  }
  const range = Array.from(Array(Math.max(...keySet)).keys());
  const missing = range
    .map((i) => {
      if (keySet.indexOf(i) < 0 && i > Math.min(...keySet)) {
        return i;
      }
    })
    .filter((f) => f);
  return missing;
};
 
const removeFromArray = (arr, value) => {
  return arr.filter((el) => el !== value);
};
 
const removeFromSet = (set, value) => {
  set.delete(value);
  return set;
};
 
module.exports = {
  analyzeFuzzy,
  getMissingKeys,
  removeFromArray,
  removeFromSet,
  storeSearchableKeys,
  storeSearchableGlobalKeys,
};