1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports.formatFlowSingleQuoted = exports.formatFlowPlain = exports.formatFlowDoubleQuoted = exports.formatBlockLiteral = exports.formatBlockFolded = void 0;
|
---|
5 | var _ramda = require("ramda");
|
---|
6 | var _ramdaAdjunct = require("ramda-adjunct");
|
---|
7 | var _unraw = require("unraw");
|
---|
8 | /**
|
---|
9 | * Helpers.
|
---|
10 | */
|
---|
11 |
|
---|
12 | const blockStyleRegExp = /^(?<style>[|>])(?<chomping>[+-]?)(?<indentation>[0-9]*)\s/;
|
---|
13 | const getIndentationIndicator = content => {
|
---|
14 | const matches = content.match(blockStyleRegExp);
|
---|
15 | const indicator = (0, _ramda.pathOr)('', ['groups', 'indentation'], matches);
|
---|
16 | return (0, _ramdaAdjunct.isEmptyString)(indicator) ? undefined : parseInt(indicator, 10);
|
---|
17 | };
|
---|
18 | const getIndentation = content => {
|
---|
19 | const explicitIndentationIndicator = getIndentationIndicator(content);
|
---|
20 |
|
---|
21 | // we have explicit indentation indicator
|
---|
22 | if ((0, _ramdaAdjunct.isInteger)(explicitIndentationIndicator)) {
|
---|
23 | return (0, _ramdaAdjunct.repeatStr)(' ', explicitIndentationIndicator);
|
---|
24 | }
|
---|
25 |
|
---|
26 | // we assume indentation indicator from first line
|
---|
27 | const firstLine = (0, _ramda.pathOr)('', [1], content.split('\n'));
|
---|
28 | const implicitIndentationIndicator = (0, _ramda.pathOr)(0, ['groups', 'indentation', 'length'], firstLine.match(/^(?<indentation>[ ]*)/));
|
---|
29 | return (0, _ramdaAdjunct.repeatStr)(' ', implicitIndentationIndicator);
|
---|
30 | };
|
---|
31 | const getChompingIndicator = content => {
|
---|
32 | const matches = content.match(blockStyleRegExp);
|
---|
33 | const indicator = (0, _ramda.pathOr)('', ['groups', 'chomping'], matches);
|
---|
34 | return (0, _ramdaAdjunct.isEmptyString)(indicator) ? undefined : indicator;
|
---|
35 | };
|
---|
36 | const chomp = (indicator, content) => {
|
---|
37 | // clip (single newline at end)
|
---|
38 | if ((0, _ramdaAdjunct.isUndefined)(indicator)) {
|
---|
39 | return `${(0, _ramdaAdjunct.trimEnd)(content)}\n`;
|
---|
40 | }
|
---|
41 | // strip (no newline at end)
|
---|
42 | if (indicator === '-') {
|
---|
43 | return (0, _ramdaAdjunct.trimEnd)(content);
|
---|
44 | }
|
---|
45 | // keep (all newlines from end)
|
---|
46 | if (indicator === '+') {
|
---|
47 | return content;
|
---|
48 | }
|
---|
49 | return content;
|
---|
50 | };
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Normalizes lines breaks.
|
---|
54 | * https://yaml.org/spec/1.2/spec.html#line%20break/normalization/
|
---|
55 | */
|
---|
56 | // @ts-ignore
|
---|
57 | const normalizeLineBreaks = val => val.replace(/\r\n/g, '\n');
|
---|
58 |
|
---|
59 | // prevent escaped line breaks from being converted to a space
|
---|
60 | const preventLineBreakCollapseToSpace = val => val.replace(/\\\n\s*/g, '');
|
---|
61 |
|
---|
62 | // collapse line breaks into spaces
|
---|
63 | const collapseLineBreakToSpace = val => {
|
---|
64 | /**
|
---|
65 | * Safari doesn't support negative lookbehind, thus we use mimicking technique:
|
---|
66 | *
|
---|
67 | * - https://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript
|
---|
68 | *
|
---|
69 | * Ideally we want to use following replace, but that's not currently possible:
|
---|
70 | *
|
---|
71 | * .replace(/[^\n]\n([^\n]+)/g, (match: string, p1: string) => ` ${p1.trimLeft()}`)
|
---|
72 | */
|
---|
73 | return val.replace(/(\n)?\n([^\n]+)/g, (match, p1, p2) => p1 ? match : ` ${p2.trimStart()}`).replace(/[\n]{2}/g, '\n');
|
---|
74 | };
|
---|
75 | const removeQuotes = (0, _ramda.curry)((quoteType, val) => val.replace(new RegExp(`^${quoteType}`), '').replace(new RegExp(`${quoteType}$`), ''));
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Formats Flow Scalar Plain style.
|
---|
79 | * https://yaml.org/spec/1.2/spec.html#id2788859
|
---|
80 | */
|
---|
81 | const formatFlowPlain = exports.formatFlowPlain = (0, _ramda.pipe)(normalizeLineBreaks, _ramda.trim, collapseLineBreakToSpace, (0, _ramda.split)('\n'), (0, _ramda.map)(_ramdaAdjunct.trimStart), (0, _ramda.join)('\n'));
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Formats Flow Scalar Single-Quoted style.
|
---|
85 | * https://yaml.org/spec/1.2/spec.html#id2788097
|
---|
86 | */
|
---|
87 |
|
---|
88 | const formatFlowSingleQuoted = exports.formatFlowSingleQuoted = (0, _ramda.pipe)(normalizeLineBreaks, _ramda.trim, collapseLineBreakToSpace, (0, _ramda.split)('\n'), (0, _ramda.map)(_ramdaAdjunct.trimStart), (0, _ramda.join)('\n'), removeQuotes("'"));
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Formats Flow Scalar Double-Quoted style.
|
---|
92 | * https://yaml.org/spec/1.2/spec.html#id2787109
|
---|
93 | */
|
---|
94 | const formatFlowDoubleQuoted = exports.formatFlowDoubleQuoted = (0, _ramda.pipe)(normalizeLineBreaks, _ramda.trim, preventLineBreakCollapseToSpace, collapseLineBreakToSpace, _unraw.unraw, (0, _ramda.split)('\n'), (0, _ramda.map)(_ramdaAdjunct.trimStart), (0, _ramda.join)('\n'), removeQuotes('"'));
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Formats Block Scalar Literal style.
|
---|
98 | * https://yaml.org/spec/1.2/spec.html#id2795688
|
---|
99 | */
|
---|
100 | const formatBlockLiteral = content => {
|
---|
101 | const indentation = getIndentation(content);
|
---|
102 | const chompingIndicator = getChompingIndicator(content);
|
---|
103 | const normalized = normalizeLineBreaks(content);
|
---|
104 | const lines = (0, _ramda.tail)(normalized.split('\n')); // first line only contains indicators
|
---|
105 | const transducer = (0, _ramda.compose)((0, _ramda.map)((0, _ramdaAdjunct.trimCharsStart)(indentation)), (0, _ramda.map)((0, _ramdaAdjunct.concatRight)('\n')));
|
---|
106 | // @ts-ignore
|
---|
107 | const deindented = (0, _ramda.transduce)(transducer, _ramda.concat, '', lines);
|
---|
108 | return chomp(chompingIndicator, deindented);
|
---|
109 | };
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Formats BLock Scalar Folded style.
|
---|
113 | * https://yaml.org/spec/1.2/spec.html#id2796251
|
---|
114 | */
|
---|
115 | exports.formatBlockLiteral = formatBlockLiteral;
|
---|
116 | const formatBlockFolded = content => {
|
---|
117 | const indentation = getIndentation(content);
|
---|
118 | const chompingIndicator = getChompingIndicator(content);
|
---|
119 | const normalized = normalizeLineBreaks(content);
|
---|
120 | const lines = (0, _ramda.tail)(normalized.split('\n')); // first line only contains indicators
|
---|
121 | const transducer = (0, _ramda.compose)((0, _ramda.map)((0, _ramdaAdjunct.trimCharsStart)(indentation)), (0, _ramda.map)((0, _ramdaAdjunct.concatRight)('\n')));
|
---|
122 | // @ts-ignore
|
---|
123 | const deindented = (0, _ramda.transduce)(transducer, _ramda.concat, '', lines);
|
---|
124 | const collapsed = collapseLineBreakToSpace(deindented);
|
---|
125 | return chomp(chompingIndicator, collapsed);
|
---|
126 | };
|
---|
127 | exports.formatBlockFolded = formatBlockFolded; |
---|