Skip to content

Commit

Permalink
Create decomposed
Browse files Browse the repository at this point in the history
  • Loading branch information
yalishanda42 committed Aug 31, 2021
1 parent b42b47d commit 3fe622d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ let allPossibleBlueColorCombinations = Options<CardColor>.allContaining(.blue) /
let favoriteColorCombo: Options<CardColor> = [.white, .blue, .red]
let allPossibleFavoriteColorCombinations = Options<CardColor>.allContaining(favoriteColorCombo) // returns a list of all elements which contain `[.white, .blue, .red]`, sorted ascendingly by their raw values
let sameAsAbove = favoriteColorCombo.allCombinationsContainingSelf

let favoriteComboArray = favoriteColorCombo.decomposed // this is a [CardColor] array containing [.white, .blue, .red]
```

## Limitations
Expand Down
5 changes: 5 additions & 0 deletions Sources/Options/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,10 @@ public struct Options<EnumType: CaseIterable & Hashable>: OptionSet {
.map(Self.init(rawValue:))
.filter { $0.contains(option) }
}

/// Convenience method returning an array of all cases contained in `self`, ordered by their respective option's raw value.
public var decomposed: [EnumType] {
EnumType.allCases.filter { contains([$0]) }
}

}
23 changes: 23 additions & 0 deletions Tests/OptionsTests/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,28 @@ final class OptionsTests: XCTestCase {
XCTAssertEqual(result2.sorted { $0.rawValue < $1.rawValue }, result2)
XCTAssertEqual(result2, expect2)
}

func testDecompose() {
let option0: Options<MagicColor> = []
let option1: Options<MagicColor> = [.blue]
let option2 = jeskai
let option3: Options<MagicColor> = .all

let expect0: [MagicColor] = []
let expect1: [MagicColor] = [.blue]
let expect2: [MagicColor] = [.white, .blue, .red]
let expect3: [MagicColor] = [.white, .blue, .black, .red, .green]

let result0 = option0.decomposed
let result1 = option1.decomposed
let result2 = option2.decomposed
let result3 = option3.decomposed

XCTAssertEqual(result0, expect0)
XCTAssertEqual(result1, expect1)
XCTAssertEqual(result2, expect2)
XCTAssertEqual(result3, expect3)
}

static var allTests = [
("testNoValue", testNoValue),
Expand All @@ -213,5 +235,6 @@ final class OptionsTests: XCTestCase {
("testAllValues", testAllValues),
("testUnion", testUnion),
("testContains", testContains),
("testAllOptionsContainingOption", testAllOptionsContainingOption),
]
}

0 comments on commit 3fe622d

Please sign in to comment.