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 e64b614
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/lib/utils/strings.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
// Sass is more liberal than CSS in what chars are legal in
// a variable name, but we're taking the stricter approach.
const stripForbiddenChars = (text: string) =>
text.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
.split(' ')
.map(stripForbiddenChars)
.map((word) => word.toLowerCase())
.join('-');

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

0 comments on commit e64b614

Please sign in to comment.