CoastalCommitsPastes/client/lib/get-title-for-post-copy.ts
2022-11-09 18:38:05 -08:00

37 lines
703 B
TypeScript

const replaceLastInString = (
string: string,
search: string,
replace: string
): string => {
const index = string.lastIndexOf(search)
if (index === -1) {
return string
}
return (
string.substring(0, index) +
replace +
string.substring(index + search.length)
)
}
const getTitleForPostCopy = (title?: string) => {
if (!title) return ""
const numberAtEndOfTitle = title.split(" ").pop()
if (numberAtEndOfTitle) {
const number = parseInt(numberAtEndOfTitle)
if (number) {
return replaceLastInString(
title,
numberAtEndOfTitle,
(number + 1).toString()
)
} else {
return title + " 1"
}
} else {
return title + " 1"
}
}
export default getTitleForPostCopy