case.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const camelCase_1 = __importDefault(require("lodash/camelCase"));
  7. const kebabCase_1 = __importDefault(require("lodash/kebabCase"));
  8. const snakeCase_1 = __importDefault(require("lodash/snakeCase"));
  9. const upperFirst_1 = __importDefault(require("lodash/upperFirst"));
  10. const startCase_1 = __importDefault(require("lodash/startCase"));
  11. exports.default = ensureCase;
  12. function ensureCase(raw = '', target = 'lowercase') {
  13. // We delete any content together with quotes because he can contains proper names (example `refactor: `Eslint` configuration`).
  14. // We need trim string because content with quotes can be at the beginning or end of a line
  15. const input = String(raw)
  16. .replace(/`.*?`|".*?"|'.*?'/g, '')
  17. .trim();
  18. const transformed = toCase(input, target);
  19. if (transformed === '' || transformed.match(/^\d/)) {
  20. return true;
  21. }
  22. return transformed === input;
  23. }
  24. function toCase(input, target) {
  25. switch (target) {
  26. case 'camel-case':
  27. return camelCase_1.default(input);
  28. case 'kebab-case':
  29. return kebabCase_1.default(input);
  30. case 'snake-case':
  31. return snakeCase_1.default(input);
  32. case 'pascal-case':
  33. return upperFirst_1.default(camelCase_1.default(input));
  34. case 'start-case':
  35. return startCase_1.default(input);
  36. case 'upper-case':
  37. case 'uppercase':
  38. return input.toUpperCase();
  39. case 'sentence-case':
  40. case 'sentencecase':
  41. return input.charAt(0).toUpperCase() + input.slice(1);
  42. case 'lower-case':
  43. case 'lowercase':
  44. case 'lowerCase': // Backwards compat config-angular v4
  45. return input.toLowerCase();
  46. default:
  47. throw new TypeError(`ensure-case: Unknown target case "${target}"`);
  48. }
  49. }
  50. //# sourceMappingURL=case.js.map