Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions lib/factory/token_set_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ String buildTokenSet(

final tokenSets = getTokenSets(tokens, config: config);
final defaultSetData = tokens[config.defaultSetName] as Map<String, dynamic>;
final defaultSys = defaultSetData['sys'] as Map<String, dynamic>;
final defaultSys = defaultSetData['sys'] as Map<String, dynamic>?;
for (final tokenSet in tokenSets) {
final setData = tokens[tokenSet] as Map<String, dynamic>;

Expand Down Expand Up @@ -222,7 +222,7 @@ String generateTokenSetEnum(
// Tuple: (prefix, initial match)
final prefixes = [
...matches.map((match) => Tuple2(match.group(1), match.group(0))),
...nonMatchedSets.map((noMatch) => Tuple2(noMatch, noMatch))
...nonMatchedSets.map((noMatch) => Tuple2(noMatch, noMatch)),
];

// A list of all unique prefixes.
Expand Down
19 changes: 13 additions & 6 deletions lib/parsers/dimension_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import 'package:design_tokens_builder/parsers/design_token_parser.dart';
/// E.g.
/// Figma design tokens:
/// "value": "14" or
/// "value": "14px"
/// "value": "14px" or
/// "value": 14
///
/// Flutter generated code:
/// 14.0
Expand All @@ -32,11 +33,17 @@ class DimensionParser extends DesignTokenParser {

@override
String buildValue(value) {
if (value is String) {
final pixel = double.parse(value.split('px').first);
return pixel.toString();
late final double pixel;
switch (value.runtimeType) {
case String:
pixel = double.parse(value.split('px').first);
break;
case int:
pixel = value.toDouble();
break;
default:
throw Exception('Unable to parse dimension value with data: $value');
}

throw Exception('Unable to parse dimension value with data: $value');
return pixel.toString();
}
}
68 changes: 66 additions & 2 deletions lib/parsers/font_weight_parser.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,77 @@
import 'package:collection/collection.dart';
import 'package:design_tokens_builder/parsers/design_token_parser.dart';

/// [Figma FontWeight Data Type](https://www.figma.com/widget-docs/api/type-FontWeight).
enum FigmaFontWeight {
/// Thin weight.
thin(100, 'thin'),

/// Extra light weight.
extraLight(200, 'extra-light'),

/// Light weight.
light(300, 'light'),

/// Normal weight.
regular(400, 'regular'),

/// Medium weight.
medium(500, 'medium'),

/// Semi bold weight.
semiBold(600, 'semi-bold'),

/// Bold weight.
bold(700, 'bold'),

/// Extra bold weight.
extraBold(800, 'extra-bold'),

/// Black weight.
black(900, 'black');

const FigmaFontWeight(this.numerical, this.string);

/// The numerical value
final int numerical;

/// The String value
final String string;

/// Parse [source] as a FontWeight literal and return its value.
///
/// Example:
/// ```dart
/// var value = FontWeight.tryParse('600'); // FontWeight.semiBold
/// value = FontWeight.tryParse('600.0'); // FontWeight.semiBold
/// value = FontWeight.tryParse('semi-bold'); // FontWeight.semiBold
/// value = FontWeight.tryParse('Semi Bold'); // FontWeight.semiBold
/// value = FontWeight.tryParse('SemiBold'); // FontWeight.semiBold
/// value = FontWeight.tryParse('650'); // null
/// ```
static FigmaFontWeight? tryParse(String source) {
final value = double.tryParse(source);
if (value != null) {
source = value.toInt().toString();
} else {
source = source.toLowerCase().replaceAll('-', '').replaceAll(' ', '');
}

return FigmaFontWeight.values.singleWhereOrNull(
(e) => e.numerical.toString() == source || e.string.replaceAll('-', '') == source,
);
}
}

/// Parses tokens of type `fontWeights` to Flutter code.
///
/// The font weight needs to be part of the specified [_allowedWeights] in order
/// to be parsed correctly.
///
/// E.g.
/// Figma design tokens:
/// "value": "400"
/// "value": "400" or
/// "value": "Regular"
///
/// Flutter generated code:
/// FontWeight.w400
Expand All @@ -26,7 +90,7 @@ class FontWeightParser extends DesignTokenParser {
@override
String buildValue(value) {
if (value is String) {
final abs = double.parse(value).toInt();
final abs = FigmaFontWeight.tryParse(value)?.numerical;

if (_allowedWeights.contains(abs)) {
return 'FontWeight.w$abs';
Expand Down
2 changes: 1 addition & 1 deletion test/builder_config/builder_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void main() {
'fontConfig': [
{'family': 'First Font', 'flutterName': 'FirstFont'},
{'family': 'Second Font', 'flutterName': 'SecondFont'},
]
],
});

print(BuilderConfig.fromYaml(yaml));
Expand Down
14 changes: 7 additions & 7 deletions test/factory/context_extension_factory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ void main() {
'color6': {'value': '#9f35a5', 'type': 'color'},
'color7': {'value': '#73c118', 'type': 'color'},
'color8': {'value': '#ed0c8d', 'type': 'color'},
'color9': {'value': '#8800ff', 'type': 'color'}
}
'color9': {'value': '#8800ff', 'type': 'color'},
},
},
'\$metadata': {
'tokenSetOrder': [
'dark',
]
}
],
},
},
config: config,
);
Expand All @@ -48,13 +48,13 @@ void main() {
'dark': {
'sys': {
'primary': {'value': '#f8e912', 'type': 'color'},
}
},
},
'\$metadata': {
'tokenSetOrder': [
'dark',
]
}
],
},
},
config: config,
);
Expand Down
2 changes: 1 addition & 1 deletion test/factory/token_set_factory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void main() {
'dark',
'allyLight',
'allyDark',
'custom'
'custom',
];

final result = generateTokenSetEnum(tokenSets, config: config);
Expand Down
24 changes: 12 additions & 12 deletions test/parsers/box_shadow_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ void main() {
group('build value', () {
test('1 shadow', () {
final map = {
'x': '4',
'y': '2',
'spread': '3',
'x': 4,
'y': 2,
'spread': 3,
'color': '#FFFFFF',
'blur': '5',
'blur': 5,
'type': 'dropShadow',
};

Expand All @@ -29,19 +29,19 @@ void main() {
test('multiple shadows', () {
final map = [
{
'x': '4',
'y': '2',
'spread': '3',
'x': 4,
'y': 2,
'spread': 3,
'color': '#FFFFFF',
'blur': '5',
'blur': 5,
'type': 'dropShadow',
},
{
'x': '4',
'y': '2',
'spread': '3',
'x': 4,
'y': 2,
'spread': 3,
'color': '#FFFFFF',
'blur': '5',
'blur': 5,
'type': 'innerShadow',
},
];
Expand Down
7 changes: 6 additions & 1 deletion test/parsers/dimension_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ void main() {
expect(result, '69.0');
});

test('should succeed when the input value is of type int', () {
final result = parser.buildValue(96);
expect(result, '96.0');
});

test('fails', () {
expect(() => parser.buildValue(1), throwsException);
expect(() => parser.buildValue(1.0), throwsException);
expect(() => parser.buildValue('some value'), throwsException);
});
});
Expand Down
74 changes: 74 additions & 0 deletions test/parsers/font_weight_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,78 @@ void main() {
expect(() => parser.buildValue(400), throwsException);
});
});

group('FigmaFontWeight', () {
test('thin', () {
const fontWeight = FigmaFontWeight.thin;
expect(fontWeight.numerical, 100);
expect(fontWeight.string, 'thin');
});

test('extraLight', () {
const fontWeight = FigmaFontWeight.extraLight;
expect(fontWeight.numerical, 200);
expect(fontWeight.string, 'extra-light');
});

test('light', () {
const fontWeight = FigmaFontWeight.light;
expect(fontWeight.numerical, 300);
expect(fontWeight.string, 'light');
});

test('regular', () {
const fontWeight = FigmaFontWeight.regular;
expect(fontWeight.numerical, 400);
expect(fontWeight.string, 'regular');
});

test('medium', () {
const fontWeight = FigmaFontWeight.medium;
expect(fontWeight.numerical, 500);
expect(fontWeight.string, 'medium');
});

test('semiBold', () {
const fontWeight = FigmaFontWeight.semiBold;
expect(fontWeight.numerical, 600);
expect(fontWeight.string, 'semi-bold');
});

test('bold', () {
const fontWeight = FigmaFontWeight.bold;
expect(fontWeight.numerical, 700);
expect(fontWeight.string, 'bold');
});

test('extraBold', () {
const fontWeight = FigmaFontWeight.extraBold;
expect(fontWeight.numerical, 800);
expect(fontWeight.string, 'extra-bold');
});

test('black', () {
const fontWeight = FigmaFontWeight.black;
expect(fontWeight.numerical, 900);
expect(fontWeight.string, 'black');
});

test('tryParse', () {
expect(FigmaFontWeight.tryParse('thin'), FigmaFontWeight.thin);
expect(FigmaFontWeight.tryParse('extra-light'), FigmaFontWeight.extraLight);
expect(FigmaFontWeight.tryParse('light'), FigmaFontWeight.light);
expect(FigmaFontWeight.tryParse('regular'), FigmaFontWeight.regular);
expect(FigmaFontWeight.tryParse('medium'), FigmaFontWeight.medium);
expect(FigmaFontWeight.tryParse('semi-bold'), FigmaFontWeight.semiBold);
expect(FigmaFontWeight.tryParse('bold'), FigmaFontWeight.bold);
expect(FigmaFontWeight.tryParse('extra-bold'), FigmaFontWeight.extraBold);
expect(FigmaFontWeight.tryParse('black'), FigmaFontWeight.black);
expect(FigmaFontWeight.tryParse('600'), FigmaFontWeight.semiBold);
expect(FigmaFontWeight.tryParse('600.0'), FigmaFontWeight.semiBold);
expect(FigmaFontWeight.tryParse('semiBold'), FigmaFontWeight.semiBold);
expect(FigmaFontWeight.tryParse('Semi Bold'), FigmaFontWeight.semiBold);
expect(FigmaFontWeight.tryParse('SemiBold'), FigmaFontWeight.semiBold);
expect(FigmaFontWeight.tryParse('650'), null);
});
});
}
8 changes: 4 additions & 4 deletions test/utils/token_set_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ void main() {
'small': {'value': 'Some value', 'type': 'someType'},
'medium': {'value': 'Some value', 'type': 'someType 3'},
'display': {
'large': {'value': 'Some value', 'type': 'someType'}
'large': {'value': 'Some value', 'type': 'someType'},
},
},
);

expect(result, {
'small': {'value': 'Some value', 'type': 'someType'},
'display': {
'large': {'value': 'Some value', 'type': 'someType'}
'large': {'value': 'Some value', 'type': 'someType'},
},
});
expect(result['medium'], null);
Expand All @@ -119,7 +119,7 @@ void main() {
'small': {'value': 'Some new value', 'type': 'someType'},
'medium': {'value': 'Some value', 'type': 'someType 3'},
'display': {
'large': {'value': 'Some value', 'type': 'someType'}
'large': {'value': 'Some value', 'type': 'someType'},
},
},
fallbackSetData: {
Expand All @@ -132,7 +132,7 @@ void main() {
expect(result['small'], {'value': 'Some new value', 'type': 'someType'});
expect(result['large'], {'value': 'Some value', 'type': 'someType'});
expect(result['display'], {
'large': {'value': 'Some value', 'type': 'someType'}
'large': {'value': 'Some value', 'type': 'someType'},
});
expect(result['medium'], null);
});
Expand Down
6 changes: 3 additions & 3 deletions test/utils/typography_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main() {
'textCase': 'none',
'textDecoration': 'none',
},
'type': 'typography'
'type': 'typography',
},
},
});
Expand All @@ -35,8 +35,8 @@ void main() {
'textCase': 'none',
'textDecoration': 'none',
},
'type': 'typography'
}
'type': 'typography',
},
});
});
}