UNPKG

2.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.toCamelCase = exports.toUpperFirst = exports.splitWords = exports.freeText = void 0;
4// Will use the shortest indention as an axis
5const freeText = (text, skipIndentation = false) => {
6 if (text instanceof Array) {
7 text = text.join('');
8 }
9 // This will allow inline text generation with external functions, same as ctrl+shift+c
10 // As long as we surround the inline text with ==>text<==
11 text = text.replace(/( *)==>((?:.|\n)*?)<==/g, (_match, baseIndent, content) => {
12 return content
13 .split('\n')
14 .map(line => `${baseIndent}${line}`)
15 .join('\n');
16 });
17 if (skipIndentation) {
18 if (text.trim() === '') {
19 return '';
20 }
21 return text;
22 }
23 const lines = text.split('\n');
24 const minIndent = lines
25 .filter(line => line.trim())
26 .reduce((minIndent, line) => {
27 var _a;
28 const currIndent = (_a = line.match(/^ */)) === null || _a === void 0 ? void 0 : _a[0].length;
29 if (currIndent == null) {
30 return minIndent;
31 }
32 return currIndent < minIndent ? currIndent : minIndent;
33 }, Infinity);
34 return lines
35 .map(line => line.slice(minIndent))
36 .join('\n')
37 .trim()
38 .replace(/\n +\n/g, '\n\n');
39};
40exports.freeText = freeText;
41// foo_barBaz -> ['foo', 'bar', 'Baz']
42const splitWords = (str) => {
43 return str.replace(/[A-Z]/, ' $&').split(/[^a-zA-Z0-9]+/);
44};
45exports.splitWords = splitWords;
46// upper -> Upper
47const toUpperFirst = (str) => {
48 return str.slice(0, 1).toUpperCase() + str.slice(1).toLowerCase();
49};
50exports.toUpperFirst = toUpperFirst;
51// foo-bar-baz -> fooBarBaz
52const toCamelCase = (str) => {
53 var _a, _b;
54 const words = (0, exports.splitWords)(str);
55 const first = (_b = (_a = words.shift()) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : '';
56 const rest = words.map(exports.toUpperFirst);
57 return [first, ...rest].join('');
58};
59exports.toCamelCase = toCamelCase;