Script.fromAnyCode constructor
Returns an instance of the Script class from any valid ISO 15924 code.
The code parameter is required and should be an object representing the
ISO 15924 code for the script.
Provided value could be any type of Object that returns ISO code on
toString() call. For example it could be an instance of StringBuffer,
String, Enum (in case of enum - .name.toUpperCase() will be used):
enum IsoEnum {de, fr, ar} // On (IsoEnum.de) call it will use "DE" input.
Or has a custom toString() override, i.e.:
class CustomIsoCodeClass {
const CustomIsoCodeClass({String code = '123', this.foo}) : _code = code;
final String _code;
final Foo? foo;
@override
String toString() => _code; // Has to override toString() with ISO value.
}
// On (CustomIsoCodeClass(code: ' 321 ')) call it will use "321" input.
The optional scripts parameter can be
used to specify a list of Script objects to search through.
If this optional array is not provided (it's default to null), this will
search in 0(1) access time case-insensitive UpperCaseIsoMap hash-map.
Otherwise it will search in provided array with equivalent of firstWhere
method, which might not be that fast comparing to the hashmap one,
especially for large arrays.
If you only need to add additional custom ISO instances of yours -
consider using the copyWith method with your custom instances in default
maps of this class (i.e. codeMap.copyWith(), map.copyWith() etc.).
This will ensure O(1) performance for your custom instances too.
This method returns the Script instance that corresponds to the given
code, or throws a StateError if no such instance exists.
Example:
final script = Script.fromAnyCode("Latn");
In the above example, the fromAnyCode factory method is called with the
code "Latn". It uses the maybeMapIsoCode method to determine the
appropriate mapping for the code. If the code is numeric, it calls the
fromCodeNumeric factory method to create a Script instance. Otherwise,
it calls the fromCode factory method to create a Script instance. The
resulting Script instance is assigned to the script variable.
Implementation
factory Script.fromAnyCode(Object code, [Iterable<Script>? scripts]) =>
scripts == null
? map.findByCodeOrThrow(code)
: IsoObject(code).maybeMapIsoCode(
orElse: (regular) => Script.fromCode(regular, scripts),
numeric: (numeric) => Script.fromCodeNumeric(numeric, scripts),
maxLength: codeLength,
minLength: IsoStandardized.codeLength,
);