Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toCapitalizeTR() ve String toCapitalizeFirstTR() eklendi. #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 48 additions & 32 deletions lib/turkish.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
final _Turkish turkish = _Turkish();
final _turkish = _Turkish();

extension TurkishStrings on String {
String toUpperCaseTr() => turkish.toUpperCase(this);
String toUpperCaseTR() => _turkish.toUpperCase(this);

String toLowerCaseTr() => turkish.toLowerCase(this);
String toLowerCaseTR() => _turkish.toLowerCase(this);

String toTitleCaseTr() => turkish.toTitleCase(this);
String toTitleCaseTR() => _turkish.toTitleCase(this);

int compareToTr(String other, [bool ignoreCase = false]) =>
turkish.compareToTr(this, other, ignoreCase);
String toCapitalizeTR() => _turkish.capitalize(this) ?? "error";

String toCapitalizeFirstTR() => _turkish.capitalizeFirst(this) ?? "error";

int compareToTR(String other, [bool ignoreCase = false]) => _turkish.compareToTr(this, other, ignoreCase);
}

/// Provides methods for correct Turkish case conversions and collation.
Expand Down Expand Up @@ -39,10 +42,7 @@ class _Turkish {
return buffer.toString();
}

String _toUpper1Length(String input) =>
(input.codeUnitAt(0) == _latinSmallLetterICode)
? "İ"
: input.toUpperCase();
String _toUpper1Length(String input) => (input.codeUnitAt(0) == _latinSmallLetterICode) ? "İ" : input.toUpperCase();

/// Returns lower case form of a Turkish String.
String toLowerCase(String input) {
Expand All @@ -67,13 +67,9 @@ class _Turkish {
return buffer.toString();
}

String _toLower1Length(String input) =>
(input.codeUnitAt(0) == _latinCapitalLetterICode)
? "ı"
: input.toLowerCase();
String _toLower1Length(String input) => (input.codeUnitAt(0) == _latinCapitalLetterICode) ? "ı" : input.toLowerCase();

int compareToTr(String a, String b, bool ignoreCase) =>
_compareToTr(a, b, ignoreCase);
int compareToTr(String a, String b, bool ignoreCase) => _compareToTr(a, b, ignoreCase);

/// Some code is used from Dart core.
static int _compareToTr(String a, String b, bool ignoreCase) {
Expand All @@ -84,13 +80,9 @@ class _Turkish {
var aCodePoint = a.codeUnitAt(i);
var bCodePoint = b.codeUnitAt(i);

final aCodePointTr = ignoreCase != true
? _codeUnitLookup.getOrder(aCodePoint)
: _codeUnitLookup.getOrderIgnoreCase(aCodePoint);
final aCodePointTr = ignoreCase != true ? _codeUnitLookup.getOrder(aCodePoint) : _codeUnitLookup.getOrderIgnoreCase(aCodePoint);

final bCodePointTr = ignoreCase != true
? _codeUnitLookup.getOrder(bCodePoint)
: _codeUnitLookup.getOrderIgnoreCase(bCodePoint);
final bCodePointTr = ignoreCase != true ? _codeUnitLookup.getOrder(bCodePoint) : _codeUnitLookup.getOrderIgnoreCase(bCodePoint);

if (aCodePointTr >= 0 && bCodePointTr >= 0) {
aCodePoint = aCodePointTr;
Expand All @@ -116,20 +108,31 @@ class _Turkish {
}

/// Turkish alphabet aware String Comparator.
final Comparator<String> comparator =
(String a, String b) => _compareToTr(a, b, false);
Comparator<String> get comparator => (String a, String b) => _compareToTr(a, b, false);

/// Case insensitive Turkish alphabet aware String Comparator.
final Comparator<String> comparatorIgnoreCase =
(String a, String b) => _compareToTr(a, b, true);
Comparator<String> get comparatorIgnoreCase => (String a, String b) => _compareToTr(a, b, true);

String? capitalize(String value) {
if (_isNull(value)) return null;
if (_isBlank(value)!) return value;
return value.split(' ').map(capitalizeFirst).join(' ');
}

/// Uppercase first letter inside string and let the others lowercase
/// Example: your name => Your name
String? capitalizeFirst(String s) {
if (_isNull(s)) return null;
if (_isBlank(s)!) return s;
return s[0].toUpperCaseTR() + s.substring(1).toLowerCaseTR();
}
}

final _Lookup _codeUnitLookup = _Lookup();

// English characters and Turkish characters with circumflex are included
// in alphabet.
const alphabet =
"AÂBCÇDEFGĞHIİÎJKLMNOÖPQRSŞTUÛÜVWXYZaâbcçdefgğhıiîjklmnoöpqrsştuûüvwxyz";
const alphabet = "AÂBCÇDEFGĞHIİÎJKLMNOÖPQRSŞTUÛÜVWXYZaâbcçdefgğhıiîjklmnoöpqrsştuûüvwxyz";

class _Lookup {
List<int> orderLookup = List<int>.filled(0x160, -1);
Expand All @@ -146,9 +149,22 @@ class _Lookup {

bool _isOutOfTable(int codeUnit) => codeUnit < 0x41 || codeUnit > 0x15F;

int getOrder(int codeUnit) =>
_isOutOfTable(codeUnit) ? -1 : orderLookup[codeUnit];
int getOrder(int codeUnit) => _isOutOfTable(codeUnit) ? -1 : orderLookup[codeUnit];

int getOrderIgnoreCase(int codeUnit) =>
_isOutOfTable(codeUnit) ? -1 : orderLookupIgnoreCase[codeUnit];
int getOrderIgnoreCase(int codeUnit) => _isOutOfTable(codeUnit) ? -1 : orderLookupIgnoreCase[codeUnit];
}

bool _isNull(dynamic value) => value == null;
bool? _isBlank(dynamic value) {
return _isEmpty(value);
}

bool? _isEmpty(dynamic value) {
if (value is String) {
return value.toString().trim().isEmpty;
}
if (value is Iterable || value is Map) {
return value.isEmpty as bool?;
}
return false;
}