-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ving util appendNumberToString().
- Loading branch information
Showing
3 changed files
with
22 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { isArray } from '#ving/utils/identify.mjs'; | ||
|
||
/** | ||
* Appends a number to the end of a string, or increments the number if there's already one at the end of the string. | ||
* @param {string} input - Any string | ||
* @example | ||
* appendNumberToString('foo') // foo2 | ||
* appendNumberToString('foo2') // foo3 | ||
*/ | ||
|
||
export default (input) => { | ||
const matches = input.match(/^(.*?)(\d+)$/); | ||
let num = (isArray(matches) && matches.length == 3) ? parseInt(matches[2]) : 1; | ||
let str = (isArray(matches) && matches.length == 3) ? matches[1] : input; | ||
num++; | ||
return str + num; | ||
} |