Skip to content

Commit 37a3ea0

Browse files
committed
validateFlags function
1 parent 6680028 commit 37a3ea0

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod validator;
22

3-
pub use validator::ESRegexValidator;
3+
pub use validator::EcmaRegexValidator;
44

55
#[cfg(test)]
66
mod tests {

src/validator.rs

+54-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Copyright (C) 2020 Quentin M. Kniep <[email protected]>
22
// Distributed under terms of the MIT license.
33

4-
enum ESVersion {
4+
use std::collections::HashSet;
5+
6+
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
7+
enum EcmaVersion {
58
ES5,
69
ES2015,
710
ES2016,
@@ -12,18 +15,56 @@ enum ESVersion {
1215
ES2021,
1316
}
1417

15-
pub struct ESRegexValidator {
16-
es_version: ESVersion,
18+
#[derive(Debug)]
19+
pub struct EcmaRegexValidator {
20+
ecma_version: EcmaVersion,
1721
}
1822

19-
impl ESRegexValidator {
20-
fn new(es_version: ESVersion) -> Self {
21-
ESRegexValidator {
22-
es_version
23+
impl EcmaRegexValidator {
24+
fn new(ecma_version: EcmaVersion) -> Self {
25+
EcmaRegexValidator {
26+
ecma_version
2327
}
2428
}
2529

26-
fn validate(&self, regex: &str) {
30+
fn validatePattern(&self, pattern: &str, ) -> bool {
31+
return true;
32+
}
33+
34+
///
35+
fn validateFlags(&self, flags: &str) -> bool {
36+
let mut existing_flags = HashSet::<char>::new();
37+
let mut global = false;
38+
let mut ignore_case = false;
39+
let mut multiline = false;
40+
let mut sticky = false;
41+
let mut unicode = false;
42+
let mut dot_all = false;
43+
44+
for flag in flags.chars() {
45+
if existing_flags.contains(&flag) {
46+
return false; // duplicate flag
47+
}
48+
existing_flags.insert(flag);
49+
50+
if flag == 'g' {
51+
global = true;
52+
} else if flag == 'i' {
53+
ignore_case = true;
54+
} else if flag == 'm' {
55+
multiline = true;
56+
} else if flag == 'u' && self.ecma_version >= EcmaVersion::ES2015 {
57+
unicode = true;
58+
} else if flag == 'y' && self.ecma_version >= EcmaVersion::ES2015 {
59+
sticky = true;
60+
} else if flag == 's' && self.ecma_version >= EcmaVersion::ES2018 {
61+
dot_all = true;
62+
} else {
63+
return false; // invalid flag
64+
}
65+
}
66+
67+
return true;
2768
}
2869
}
2970

@@ -33,6 +74,10 @@ mod tests {
3374
use super::*;
3475

3576
#[test]
36-
fn it_works() {
77+
fn validate_flags() {
78+
let validator = EcmaRegexValidator::new(EcmaVersion::ES2018);
79+
assert_eq!(validator.validateFlags("gimuys"), true);
80+
assert_eq!(validator.validateFlags("gimgu"), false);
81+
assert_eq!(validator.validateFlags("gimuf"), false);
3782
}
3883
}

0 commit comments

Comments
 (0)