aboutsummaryrefslogtreecommitdiff
path: root/src/server/helper/clean.go
blob: 35937de71e2f0b9f5459e02f845e37a8951c56d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package helper

import (
	"regexp"
)

func removeTrailingWhitespace(str string) string {
	re := regexp.MustCompile(`(^\s+)|(\s+$)`)
	return re.ReplaceAllString(str, "")
}

func removeDuplicateWhitespace(str string) string {
	re := regexp.MustCompile(`\s{2,}`)
	return re.ReplaceAllString(str, " ")
}

func removeNewlines(str string) string {
	re := regexp.MustCompile(`\n+`)
	return re.ReplaceAllString(str, "")
}

func removeDuplicateNewlines(str string) string {
	re := regexp.MustCompile(`\n{2,}`)
	return re.ReplaceAllString(str, "\n")
}

func CleanTitle(title string) string {
	title = removeDuplicateWhitespace(title)
	title = removeNewlines(title)
	return removeTrailingWhitespace(title)
}

func CleanContents(contents string) string {
	contents = removeDuplicateWhitespace(contents)
	contents = removeDuplicateNewlines(contents)
	return removeTrailingWhitespace(contents)
}