Skip to content

Commit

Permalink
feat: string to upper case snake case conversion function
Browse files Browse the repository at this point in the history
  • Loading branch information
beauraines committed Oct 13, 2024
1 parent 52b1b36 commit 8cd6f85
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ function toTitleCase(str) {
);
}

const toUpperSnakeCase = (str) => {
return str
.replace(/([a-z])([A-Z])/g, '$1_$2') // Add underscore before capital letters
.replace(/[^a-zA-Z0-9_]/g, '_') // Replace non-alphanumeric characters with underscore
.toUpperCase(); // Convert to uppercase
}


/**
* Removes newline characters \r and/or \n from a string
* @param {string} string to remove newlines from
Expand Down Expand Up @@ -213,6 +221,7 @@ module.exports = {
streamToBuffer,
stripNewLines,
toTitleCase,
toUpperSnakeCase,
unixTimestamp,
writeFile
}
8 changes: 8 additions & 0 deletions src/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,13 @@ describe('helpers',()=> {
expect(helper.sparkline(input,label, options)).toBe(expectedOutput)
})

it('should return an upper case snake case string',()=>{
const expectedOutput = 'NEXT_WEEK';
const inputs = ['nextWeek','next-week','next_week'];
inputs.forEach( i => {
expect(helper.toUpperSnakeCase(i)).toBe(expectedOutput)
})
})

})

0 comments on commit 8cd6f85

Please sign in to comment.