Skip to content

Commit

Permalink
refactor: replace JSON filter with JSON deepFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOF committed Dec 5, 2023
1 parent 25edc55 commit 5c87ffd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Source/AppleFetcher/Implement/ScreenFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension ScreenFetcher {
Promise.async(on: .main) { () -> UIInterfaceOrientation in
if #available(iOS 13.0, macCatalyst 13.0, tvOS 13.0, *) {
return UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.deepFilter { $0.activationState == .foregroundActive }
.first { $0 is UIWindowScene }
.flatMap { $0 as? UIWindowScene }?
.interfaceOrientation ?? .portrait
Expand Down
6 changes: 3 additions & 3 deletions Source/JSON/Modify/JSONFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import Foundation

extension JSON {
public func filter(_ condition: (JSON) -> Bool) -> JSON? {
public func deepFilter(_ condition: (JSON) -> Bool) -> JSON? {
if case .object(let value) = self {
let filteredValue = JSON.object(value.compactMapValues { $0.filter(condition) })
let filteredValue = JSON.object(value.compactMapValues { $0.deepFilter(condition) })
let result = condition(filteredValue) ? filteredValue : (nil as JSON?)
return result
}
else if case .array(let value) = self {
let filteredValue = JSON.array(value.compactMap { $0.filter(condition) })
let filteredValue = JSON.array(value.compactMap { $0.deepFilter(condition) })
let result = condition(filteredValue) ? filteredValue : (nil as JSON?)
return result
}
Expand Down
8 changes: 4 additions & 4 deletions Test/JSON/Modify/JSONFilterTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import XCTest
@testable import SabyJSON

final class JSONFilterTest: XCTestCase {
func test__filter() {
func test__deep_filter() {
let json = JSON.from([
"a": nil,
"b": 1,
Expand All @@ -32,9 +32,9 @@ final class JSONFilterTest: XCTestCase {
])

let filtered = json
.filter { $0 != .null }!
.filter { $0 != [:] }!
.filter { $0 != [] }!
.deepFilter { $0 != .null }!
.deepFilter { $0 != [:] }!
.deepFilter { $0 != [] }!

XCTAssertEqual(filtered, [
"b": 1,
Expand Down

0 comments on commit 5c87ffd

Please sign in to comment.