Skip to content

Commit

Permalink
Added ving util appendNumberToString().
Browse files Browse the repository at this point in the history
  • Loading branch information
rizen committed May 3, 2024
1 parent d71a294 commit 73185dd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions ving/docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ outline: deep
* Added prepend/append slots to FormInput for inserting icons into input groups.
* Exposed new props for modifying the classes associated with PanelZone.
* Added a coerce attribute to the FormInput component.
* Added ving util appendNumberToString().

## 2024-04-29
* Implemented: reformat page generator to use panel components on view and edit #139
Expand Down
4 changes: 4 additions & 0 deletions ving/docs/subsystems/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ There are a bunch of utilities libraries in `ving/utils` that solve common probl

## Utility Libraries

### appendNumberToString

Adds a number to the end of a string, or increments it if there's already one there.

### findObject

Provides light wrappers around the javascript `find()` function that throws an error if the object you're looking for isn't found.
Expand Down
17 changes: 17 additions & 0 deletions ving/utils/appendNumberToString.mjs
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;
}

0 comments on commit 73185dd

Please sign in to comment.