summaryrefslogtreecommitdiff
path: root/scripts/style-code.js
blob: 156934a24075dc89f83a638bf61f6fa77c1a68f7 (plain)
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
// Global functions //

const fs = require('fs');

global.iff = function (condition, val) {
  return condition() ? val : "";
};

global.camelize = function (str) {
  return str.replace(/(?:^|-)(.)/g, function (_, x) {
    return x.toUpperCase();
  });
};

global.camelizeWithLeadingLowercase = function (str) {
  return str.replace(/-(.)/g, function (_, x) {
    return x.toUpperCase();
  });
};

global.snakeCaseUpper = function snakeCaseUpper(str) {
  return str.replace(/-/g, "_").toUpperCase();
};

global.writeIfModified = function(filename, newContent) {
  try {
    const oldContent = fs.readFileSync(filename, 'utf8');
    if (oldContent == newContent) {
      console.warn(`* Skipping current file '${filename}'`);
      return;
    }
  } catch(err) {
  }
  fs.writeFileSync(filename, newContent);
  console.warn(`* Updating outdated file '${filename}'`);
};