Skip to content

Commit

Permalink
Preventing illegal chars in CSS and Sass variables
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgirault committed Apr 20, 2024
1 parent dcd68ad commit 955faeb
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/lib/utils/strings.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
// Sass is more liberal than CSS in what chars are legal in
// a variable name, but we're taking the stricter approach.
const stripForbiddenChars = (sentence: string) =>
sentence.replaceAll(/[^a-zA-Z0-9_-]/g, '');

export const sentenceCase = (sentence: string) =>
sentence.charAt(0).toUpperCase() +
sentence.slice(1).toLowerCase();

export const dashCase = (sentence: string) =>
sentence
stripForbiddenChars(sentence)
.split(' ')
.map((word) => word.toLowerCase())
.join('-');

export const camelCase = (sentence: string) =>
sentence
stripForbiddenChars(sentence)
.split(' ')
.map((word, index) =>
index === 0 ? word.toLowerCase() : sentenceCase(word)
Expand Down

0 comments on commit 955faeb

Please sign in to comment.