Skip to content

Commit 23592a5

Browse files
Mark Pospeselmpospese
authored andcommitted
[Issue-64] Initialize FontWeight from string
1 parent 0902c79 commit 23592a5

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

Sources/YMatterType/Typography/Typography+Enums.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,36 @@ extension Typography {
3636
case heavy = 800
3737
/// black weight (900)
3838
case black = 900
39+
40+
/// Creates a new instance from a string.
41+
///
42+
/// This will be useful for converting Figma tokens to `Typography` objects.
43+
/// Common synonyms will be accepted, e.g. both "SemiBold" and "DemiBold" map to `.semibold`.
44+
/// - Parameter weightName: the case-insensitive weight name, e.g. "Bold"
45+
init?(_ weightName: String) {
46+
switch weightName.lowercased(with: Locale(identifier: "en_US")) {
47+
case "ultralight", "extralight":
48+
self = .ultralight
49+
case "thin":
50+
self = .thin
51+
case "light":
52+
self = .light
53+
case "regular":
54+
self = .regular
55+
case "medium":
56+
self = .medium
57+
case "semibold", "demibold":
58+
self = .semibold
59+
case "bold":
60+
self = .bold
61+
case "heavy", "extrabold", "ultrabold":
62+
self = .heavy
63+
case "black":
64+
self = .black
65+
default:
66+
return nil
67+
}
68+
}
3969
}
4070

4171
/// Capitalization to be applied to user-facing text
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Typography+EnumTests.swift
3+
// YMatterTypeTests
4+
//
5+
// Created by Mark Pospesel on 3/17/23.
6+
// Copyright © 2023 Y Media Labs. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import YMatterType
11+
12+
final class TypographyEnumTests: XCTestCase {
13+
func test_fontWeightInit_garbageReturnsNil() {
14+
["", "nonsense", "garbage", "not a weight"].forEach {
15+
XCTAssertNil(Typography.FontWeight($0))
16+
}
17+
}
18+
19+
func test_fontWeightInit_isCaseInsensitive() {
20+
["Regular", "regular", "REGULAR", "ReGuLaR"].forEach {
21+
XCTAssertEqual(Typography.FontWeight($0), .regular)
22+
}
23+
24+
["Semibold", "semibold", "SEMIBOLD", "SemiBold"].forEach {
25+
XCTAssertEqual(Typography.FontWeight($0), .semibold)
26+
}
27+
}
28+
29+
func test_fontWeightInit_acceptsSynonyms() {
30+
["ExtraLight", "UltraLight"].forEach {
31+
XCTAssertEqual(Typography.FontWeight($0), .ultralight)
32+
}
33+
34+
["SemiBold", "DemiBold"].forEach {
35+
XCTAssertEqual(Typography.FontWeight($0), .semibold)
36+
}
37+
38+
["Heavy", "ExtraBold", "UltraBold"].forEach {
39+
XCTAssertEqual(Typography.FontWeight($0), .heavy)
40+
}
41+
}
42+
43+
func test_fontWeightInit_acceptsFontFamilyWeightNames() {
44+
let sut = DefaultFontFamily(familyName: "Any")
45+
46+
for weight in sut.supportedWeights {
47+
let weightName = sut.weightName(for: weight)
48+
XCTAssertEqual(Typography.FontWeight(weightName), weight)
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)