mirror of
https://github.com/hack-chat/main.git
synced 2024-03-22 13:20:33 +08:00
22 lines
542 B
JavaScript
22 lines
542 B
JavaScript
/**
|
|
* Check and trim string provided by remote client
|
|
* @public
|
|
* @param {string} text - Subject string
|
|
* @return {string|null}
|
|
*/
|
|
export const parseText = (text) => {
|
|
// verifies user input is text
|
|
if (typeof text !== 'string') {
|
|
return null;
|
|
}
|
|
|
|
let sanitizedText = text;
|
|
|
|
// strip newlines from beginning and end
|
|
sanitizedText = sanitizedText.replace(/^\s*\n|^\s+$|\n\s*$/g, '');
|
|
// replace 3+ newlines with just 2 newlines
|
|
sanitizedText = sanitizedText.replace(/\n{3,}/g, '\n\n');
|
|
|
|
return sanitizedText;
|
|
};
|