Skip to content

Commit e988255

Browse files
committed
set unicode flag to allow non-ASCII charcters
1 parent f18b9b8 commit e988255

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/super_string.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension SuperString on String {
3333
/// print('This@123'.isAlNum); // false
3434
/// ```
3535
///
36-
bool get isAlNum => RegExp(r"^[a-zA-Z0-9]+$").hasMatch(this);
36+
bool get isAlNum => RegExp(r"^[\p{L}\p{N}]+$", unicode: true).hasMatch(this);
3737

3838
/// Return `true` if all the characters are alphabets letter (a-z).
3939
///
@@ -45,7 +45,7 @@ extension SuperString on String {
4545
/// print('This@123'.isAlpha); // false
4646
/// ```
4747
///
48-
bool get isAlpha => RegExp(r"^[a-zA-Z]+$").hasMatch(this);
48+
bool get isAlpha => RegExp(r"^\p{L}+$", unicode: true).hasMatch(this);
4949

5050
/// Return `true` if all the characters are [int] (0-9).
5151
///
@@ -57,7 +57,7 @@ extension SuperString on String {
5757
/// print('This@123'.isAlNum); // false
5858
/// ```
5959
///
60-
bool get isInteger => RegExp(r"^[0-9]+$").hasMatch(this);
60+
bool get isInteger => RegExp(r"^\p{N}+$", unicode: true).hasMatch(this);
6161

6262
/// Return `true` if the string contains only alphanumeric letters (a-z) , (0-9) and
6363
/// underscores (_).
@@ -75,7 +75,8 @@ extension SuperString on String {
7575
/// ```
7676
///
7777
bool get isIdentifier =>
78-
RegExp(r"^[_a-zA-Z0-9]+$").hasMatch(this) && !this[0].isInteger;
78+
RegExp(r"^[\p{L}\p{N}_]+$", unicode: true).hasMatch(this) &&
79+
!this[0].isInteger;
7980

8081
/// Return a `String` where first character of every words is converted to upperCase.
8182
///

test/super_string_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ void main() {
1414
const String upperCaseSentence = 'THIS SENTENCE CONTAIN MULTIPLY WORDS';
1515
const String multiCaseSentence = 'This Sentence contain Multiply words';
1616
const String identifier = 'hello_123';
17+
const String identifier2 = '_hello123';
1718
const String camel = 'hello world';
1819
const String tab = 'H\te\tl\tl\to';
20+
const String nonASCIICharacter1 = 'こんにちは世界';
21+
const String nonASCIICharacter2 = '٩٣';
22+
const String nonASCIICharacter3 = 'مرحبا٩٣';
1923

2024
final Matcher throwsAssertionError = throwsA(isA<AssertionError>());
2125

@@ -83,6 +87,9 @@ void main() {
8387

8488
/// Expected True when a words conatining Alphabets and numbers is called.
8589
expect(multiCaseWord.isAlNum, true);
90+
expect(nonASCIICharacter1.isAlNum, true);
91+
expect(nonASCIICharacter2.isAlNum, true);
92+
expect(nonASCIICharacter3.isAlNum, true);
8693
expect(multiCaseWordWithNumber.isAlNum, true);
8794

8895
/// Expected False when a senetence is called.
@@ -98,6 +105,9 @@ void main() {
98105

99106
/// Expected True when a words conatining only Alphabets called.
100107
expect(multiCaseWord.isAlpha, true);
108+
expect(nonASCIICharacter3.isAlpha, false);
109+
expect(nonASCIICharacter2.isAlpha, false);
110+
expect(nonASCIICharacter1.isAlpha, true);
101111

102112
/// Expected False when a words conatining both Alphabets and Number called.
103113
expect(multiCaseWordWithNumber.isAlpha, false);
@@ -127,6 +137,11 @@ void main() {
127137

128138
/// Expected False when a special character is called.
129139
expect(specialCharacter.isInteger, false);
140+
141+
/// Expected True when other language's number is called.
142+
expect(nonASCIICharacter2.isInteger, true);
143+
expect(nonASCIICharacter3.isInteger, false);
144+
expect(nonASCIICharacter1.isInteger, false);
130145
});
131146

132147
test('isIdentifier', () async {
@@ -136,12 +151,15 @@ void main() {
136151
/// Expected False when a number is called. Since Indentifier should
137152
/// not start with numbers.
138153
expect(number.isIdentifier, false);
154+
expect(nonASCIICharacter2.isIdentifier, false);
139155

140156
/// Expected True when a words conatining only Alphabets called.
141157
expect(multiCaseWord.isIdentifier, true);
158+
expect(nonASCIICharacter1.isIdentifier, true);
142159

143160
/// Expected True when a words conatining both Alphabets and Number called.
144161
expect(multiCaseWordWithNumber.isIdentifier, true);
162+
expect(nonASCIICharacter3.isIdentifier, true);
145163

146164
/// Expected False when a senetence is called.
147165
expect(multiCaseSentence.isIdentifier, false);
@@ -154,6 +172,7 @@ void main() {
154172

155173
/// Expected True when a word containing `_` is called.
156174
expect(identifier.isIdentifier, true);
175+
expect(identifier2.isIdentifier, true);
157176
});
158177

159178
test('title', () {

0 commit comments

Comments
 (0)