-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prettier-plugin-jsdoc): function to split text lines ramdification
- Loading branch information
Showing
3 changed files
with
115 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,104 @@ | ||
const R = require('ramda'); | ||
const { ensureArray, replaceLastItem } = require('./utils'); | ||
/** | ||
* This is a utility used inside the reducers in order to take "words" that include line breaks | ||
* and split them into multiple "words". | ||
* | ||
* @callback SplitLineBreaksFn | ||
* @param {string} text The text to process. | ||
* @returns {string[]} | ||
*/ | ||
|
||
/** | ||
* @type {SplitLineBreaksFn} | ||
*/ | ||
const splitLineBreaks = R.compose( | ||
R.dropLast(1), | ||
R.reduce((sacc, item) => [...sacc, item, '\n'], []), | ||
R.map(R.when(R.isEmpty, R.always('\n'))), | ||
R.split('\n'), | ||
); | ||
/** | ||
* This is a reducer function that validates words before adding them to a list. By "validates", | ||
* it checks if the words include a line break, in order to process them with | ||
* {@link splitLineBreaks} before adding them. | ||
* | ||
* @param {string[]} list The list where the words are added (the accumulator). | ||
* @param {string} word The word to validate. | ||
* @returns {string[]} | ||
*/ | ||
const reduceWordsList = (list, word) => R.concat( | ||
list, | ||
R.ifElse( | ||
R.includes('\n'), | ||
splitLineBreaks, | ||
ensureArray, | ||
)(word), | ||
); | ||
|
||
/** | ||
* This is a reducer that generates a list of lines with a specific length. The function tries | ||
* to add the each new word on the same line, unless it would cause the line to exceed the | ||
* _length limit_, in which case it will create a new line. | ||
* | ||
* @callback ReduceSentencesFn | ||
* @param {number} length The length the lines can have. | ||
* @param {string[]} list The current list of words (the accumulator). | ||
* @param {string} word The word to process. | ||
* @returns {string[]} | ||
*/ | ||
|
||
/** | ||
* @type {ReduceSentencesFn} | ||
*/ | ||
const reduceSentences = R.curry((length, list, word) => { | ||
let newList; | ||
if (word === '\n') { | ||
newList = R.append('', list); | ||
} else { | ||
const currentLine = R.last(list).trim(); | ||
const newLine = `${currentLine} ${word}`; | ||
newList = R.ifElse( | ||
R.always(newLine.length > length), | ||
R.append(word), | ||
replaceLastItem(newLine), | ||
)(list); | ||
} | ||
return newList; | ||
}); | ||
/** | ||
* This is a reducer that processes a list of lines and puts them together on a single text by | ||
* validating if they should be added on a new line or not. | ||
* | ||
* @param {string} text The text where the line will be added (the accumulator). | ||
* @param {string} line The line to process. | ||
* @returns {string} | ||
*/ | ||
const reduceText = (text, line) => { | ||
const useLine = line.trim(); | ||
let newText; | ||
if (text) { | ||
const glue = text.substr(-1).match(/\w/) ? ' ' : '\n'; | ||
newText = `${text}${glue}${useLine}`; | ||
} else { | ||
newText = useLine; | ||
} | ||
return newText; | ||
}; | ||
|
||
/** | ||
* Splits a text on a list of lines where none of them exceeds the specified `length`. | ||
* | ||
* @param {string} text The text to split. | ||
* @param {number} length The max length a line cannot exceed. | ||
* @returns {string[]} | ||
*/ | ||
const splitText = (text, length) => text | ||
.split('\n') | ||
.reduce( | ||
(acc, line, index) => { | ||
const useLine = line.trim(); | ||
let nextAcc; | ||
if (index) { | ||
const lastChar = acc.substr(-1); | ||
if (lastChar.match(/\w/)) { | ||
nextAcc = `${acc} ${useLine}`; | ||
} else { | ||
nextAcc = `${acc}\n${useLine}`; | ||
} | ||
} else { | ||
nextAcc = useLine; | ||
} | ||
return nextAcc; | ||
}, | ||
'', | ||
) | ||
.split(/(?<!\{@\w+) /) | ||
.reduce( | ||
(acc, word) => { | ||
let nextAcc; | ||
if (word.includes('\n')) { | ||
const parts = word | ||
.split('\n') | ||
.map((item) => item || '\n') | ||
.reduce((sacc, item) => [...sacc, item, '\n'], []); | ||
parts.pop(); | ||
nextAcc = [...acc, ...parts]; | ||
} else { | ||
nextAcc = [...acc, word]; | ||
} | ||
|
||
return nextAcc; | ||
}, | ||
[], | ||
) | ||
.reduce( | ||
(acc, word) => { | ||
if (word === '\n') { | ||
acc.push(''); | ||
} else { | ||
const currentLine = acc[acc.length - 1]; | ||
const newLine = `${currentLine.trim()} ${word}`; | ||
if (newLine.length > length) { | ||
acc.push(word); | ||
} else { | ||
acc[acc.length - 1] = newLine; | ||
} | ||
} | ||
return acc; | ||
}, | ||
[''], | ||
); | ||
const splitText = (text, length) => R.compose( | ||
R.reduce(reduceSentences(length), ['']), | ||
R.reduce(reduceWordsList, []), | ||
R.split(/(?<!\{@\w+) /), | ||
R.reduce(reduceText, ''), | ||
R.split('\n'), | ||
)(text); | ||
|
||
module.exports.splitText = splitText; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters