UNPKG

2.96 kBJavaScriptView Raw
1"use strict";
2exports.__esModule = true;
3exports.isKeyframePointAndSetIndentation = exports.convertLine = exports.replaceSpacesOrTabs = exports.getIndentationOffset = exports.replaceWithOffset = exports.getBlockHeaderOffset = void 0;
4var suf_regex_1 = require("suf-regex");
5/** returns the relative distance that the class or id should be at. */
6function getBlockHeaderOffset(distance, tabSize, current, ignoreCurrent) {
7 if (distance === 0) {
8 return 0;
9 }
10 if (tabSize * Math.round(distance / tabSize - 0.1) > current && !ignoreCurrent) {
11 return current - distance;
12 }
13 return tabSize * Math.round(distance / tabSize - 0.1) - distance;
14}
15exports.getBlockHeaderOffset = getBlockHeaderOffset;
16/**
17 * adds or removes whitespace based on the given offset, a positive value adds whitespace a negative value removes it.
18 */
19function replaceWithOffset(text, offset, STATE) {
20 if (offset < 0) {
21 text = text
22 .replace(/\t/g, ' '.repeat(STATE.CONFIG.tabSize))
23 .replace(new RegExp("^ {" + Math.abs(offset) + "}"), '');
24 if (!STATE.CONFIG.insertSpaces) {
25 text = replaceSpacesOrTabs(text, STATE, false);
26 }
27 }
28 else {
29 text = text.replace(/^/, STATE.CONFIG.insertSpaces ? ' '.repeat(offset) : '\t'.repeat(offset / STATE.CONFIG.tabSize));
30 }
31 return text;
32}
33exports.replaceWithOffset = replaceWithOffset;
34/** returns the difference between the current indentation and the indentation of the given text. */
35function getIndentationOffset(text, indentation, tabSize) {
36 var distance = suf_regex_1.getDistance(text, tabSize);
37 return { offset: indentation - distance, distance: distance };
38}
39exports.getIndentationOffset = getIndentationOffset;
40function isKeyframePoint(text, isAtKeyframe) {
41 if (isAtKeyframe === false) {
42 return false;
43 }
44 return /^[\t ]*\d+%/.test(text) || /^[\t ]*from[\t ]*$|^[\t ]*to[\t ]*$/.test(text);
45}
46function replaceSpacesOrTabs(text, STATE, insertSpaces) {
47 if (insertSpaces !== undefined ? insertSpaces : STATE.CONFIG.insertSpaces) {
48 return text.replace(/\t/g, ' '.repeat(STATE.CONFIG.tabSize));
49 }
50 else {
51 return text.replace(new RegExp(' '.repeat(STATE.CONFIG.tabSize), 'g'), '\t');
52 }
53}
54exports.replaceSpacesOrTabs = replaceSpacesOrTabs;
55function convertLine(line, STATE) {
56 return (STATE.CONFIG.convert &&
57 suf_regex_1.isScssOrCss(line.get(), STATE.CONTEXT.convert.wasLastLineCss) &&
58 !suf_regex_1.isComment(line.get()));
59}
60exports.convertLine = convertLine;
61function isKeyframePointAndSetIndentation(line, STATE) {
62 var isAtKeyframesPoint = isKeyframePoint(line.get(), STATE.CONTEXT.keyframes.isIn);
63 if (STATE.CONTEXT.keyframes.isIn && isAtKeyframesPoint) {
64 STATE.CONTEXT.indentation = Math.max(0, STATE.CONTEXT.keyframes.indentation);
65 }
66 return isAtKeyframesPoint;
67}
68exports.isKeyframePointAndSetIndentation = isKeyframePointAndSetIndentation;