Reputation: 77
I'd like to validate whether inserted slugs are what they should be and have the following function in Bolt:
slugify(s) = s.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-')
used like:
validate() = $slug == slugify($this.title)
However, the g regex modifier doesn't seem to be supported. Are there any other ways / best practices to achieve my goal?
Upvotes: 1
Views: 416
Reputation: 7034
About the best you can do with the allowed string operators is to ensure that your string looks like a slug.
type Slug extends String {
validate() = this.test(/^([a-z0-9]+-)+[a-z0-9]+$/);
}
Here are some tests against this pattern:
.write('this-is-a-slug')
.succeeds("Typical slug text.")
.write('numbers-2016-ok')
.succeeds("Numbers are ok.")
.write('double--hyphen')
.fails("Double hyphen not ok.")
.write('-leading-hyphen')
.fails("Leading hyphen not ok.")
.write('trailing-hyphen-')
.fails("Trailing hyphen not ok.")
.write('nohyphen')
.fails("Must have at least one hyphen.")
.write('no-Upper')
.fails("No upper case.")
.write('no-special&-char')
.fails("No special characters.")
.write('no spaces')
.fails("No spaces allowed.")
You can find more RegExp examples here
Upvotes: 3