-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathspecifications_builder.dart
More file actions
87 lines (74 loc) · 2.56 KB
/
specifications_builder.dart
File metadata and controls
87 lines (74 loc) · 2.56 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import 'dart:io';
import 'specifications.dart';
String _template = '''
// GENERATED FILE DO NOT CHANGE MANUALLY SEE tools/specifications_builder.dart
part of iban;
/// A list of all known IBAN specifications.
const Map<String, Specification> specifications = {
INSERT_SPECS_HERE
};
''';
int main(List<String> args) {
String out = 'lib/src/specification_definitions.gen.dart';
if (args.isNotEmpty) {
if (args[0] == '-h' || args[0] == '--help') {
print('Generates iban specifications in $out.');
print(
'If an argument (other than -h or --help) is given, it is used as output-file.');
return 0;
}
out = args[0];
}
var specs = getAllSpecs().map((spec) {
final String countryCode = spec.countryCode;
final int length = spec.length;
final String structure = spec.structure;
final String example = spec.example;
final String regExpString = _parseStructure(countryCode, structure);
return "'$countryCode': Specification('$countryCode', $length, '$structure', '$example', r'$regExpString')";
}).join(',\n ');
var outFile = File(out);
outFile.writeAsStringSync(_template.replaceFirst('INSERT_SPECS_HERE', specs));
return 0;
}
/// Parse the BBAN structure used to configure each IBAN Specification and returns a matching regular expression.
/// A structure is composed of blocks of 3 characters (one letter and 2 digits). Each block represents
/// a logical group in the typical representation of the BBAN. For each group, the letter indicates which characters
/// are allowed in this group and the following 2-digits number tells the length of the group.
// This is a dart version of parseStructure in
// https://github.com/arhs/iban.js/blob/master/iban.js
String _parseStructure(String countryCode, String structure) {
// split in blocks of 3 chars
var regex = RegExp(r'(.{3})').allMatches(structure).map((match) {
String block = match[1]!;
// parse each structure block (1-char + 2-digits)
var format;
var pattern = block[0];
var repeats = block.substring(1);
switch (pattern) {
case "A":
format = "0-9A-Za-z";
break;
case "B":
format = "0-9A-Z";
break;
case "C":
format = "A-Za-z";
break;
case "F":
format = "0-9";
break;
case "L":
format = "a-z";
break;
case "U":
format = "A-Z";
break;
case "W":
format = "0-9a-z";
break;
}
return '([' + format + ']{' + repeats + '})';
});
return '^$countryCode[0-9]{2}${regex.join()}\$';
}