All files / app/assets/javascripts/ide/lib/editorconfig rules_mapper.js

100% Statements 23/23
88.88% Branches 16/18
100% Functions 11/11
100% Lines 16/16

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    6x 18x   14x   6x 20x   3x   10x 9x           3x 52x 52x 52x 52x   52x       75x 52x      
import { isBoolean, isNumber } from 'lodash';
 
const map = (key, validValues) => (value) =>
  value in validValues ? { [key]: validValues[value] } : {};
 
const bool = (key) => (value) => (isBoolean(value) ? { [key]: value } : {});
 
const int = (key, isValid) => (value) =>
  isNumber(value) && isValid(value) ? { [key]: Math.trunc(value) } : {};
 
const rulesMapper = {
  indent_style: map('insertSpaces', { tab: false, space: true }),
  indent_size: int('tabSize', (n) => n > 0),
  tab_width: int('tabSize', (n) => n > 0),
  trim_trailing_whitespace: bool('trimTrailingWhitespace'),
  end_of_line: map('endOfLine', { crlf: 1, lf: 0 }),
  insert_final_newline: bool('insertFinalNewline'),
};
 
const parseValue = (x) => {
  let value = typeof x === 'string' ? x.toLowerCase() : x;
  if (/^[0-9.-]+$/.test(value)) value = Number(value);
  if (value === 'true') value = true;
  if (value === 'false') value = false;
 
  return value;
};
 
export default function mapRulesToMonaco(rules) {
  return Object.entries(rules).reduce((obj, [key, value]) => {
    return Object.assign(obj, rulesMapper[key]?.(parseValue(value)) || {});
  }, {});
}