-
-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathidentifier.rs
More file actions
62 lines (53 loc) · 1.6 KB
/
identifier.rs
File metadata and controls
62 lines (53 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use oxc_syntax::identifier::{is_identifier_name, is_identifier_part, is_identifier_start};
/// Convert a String to a valid identifier name.
///
/// Based on Babel's [`toIdentifier`] function.
///
/// [`toIdentifier`]: https://github.com/babel/babel/blob/3bcfee232506a4cebe410f02042fb0f0adeeb0b1/packages/babel-types/src/converters/toIdentifier.ts#L4-L26
pub fn to_identifier(input: String) -> String {
if is_identifier_name(&input) {
return input;
}
let mut name = String::with_capacity(input.len());
let mut capitalize_next = false;
let mut chars = input.chars();
if let Some(first) = chars.next() {
if is_identifier_start(first) {
name.push(first);
} else {
capitalize_next = true;
}
}
for c in chars {
if !is_identifier_part(c) {
capitalize_next = true;
} else if capitalize_next {
name.push(c.to_ascii_uppercase());
capitalize_next = false;
} else {
name.push(c);
}
}
if name.is_empty() {
return "_".to_string();
}
name
}
#[test]
fn test() {
let cases = &[
("foo", "foo"),
("fooBar", "fooBar"),
("fooBar1", "fooBar1"),
("foo-bar", "fooBar"),
("foo bar", "fooBar"),
("foo-bar-1", "fooBar1"),
("1foo-bar", "FooBar"),
("1-foo-bar", "FooBar"),
("-- --", "_"),
("_output$headers$x-amzn-requestid", "_output$headers$xAmznRequestid"),
];
for &(input, expected) in cases {
assert_eq!(to_identifier(input.to_string()), expected);
}
}