Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 6 additions & 6 deletions Sources/Segment/Utilities/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ extension JSON {
/// - Parameters:
/// - keys: A dictionary containing key mappings, in the format of ["Old": "New"].
/// - valueTransform: An optional value transform closure. Key represents the new key name.
public func mapKeys(_ keys: [String: String], valueTransform: ((_ key: String, _ value: Any) -> Any)? = nil) throws -> JSON {
public func mapTransform(_ keys: [String: String], valueTransform: ((_ key: String, _ value: Any) -> Any)? = nil) throws -> JSON {
guard let dict = self.dictionaryValue else { return self }
let mapped = try dict.mapKeys(keys, valueTransform: valueTransform)
let mapped = try dict.mapTransform(keys, valueTransform: valueTransform)
let result = try JSON(mapped)
return result
}
Expand All @@ -285,7 +285,7 @@ extension JSON {
// MARK: - Helpers

extension Dictionary where Key == String, Value == Any {
internal func mapKeys(_ keys: [String: String], valueTransform: ((_ key: Key, _ value: Value) -> Any)? = nil) throws -> [Key: Value] {
internal func mapTransform(_ keys: [String: String], valueTransform: ((_ key: Key, _ value: Value) -> Any)? = nil) throws -> [Key: Value] {
let mapped = Dictionary(uniqueKeysWithValues: self.map { key, value -> (Key, Value) in
var newKey = key
var newValue = value
Expand All @@ -299,7 +299,7 @@ extension Dictionary where Key == String, Value == Any {
}
// is this value a dictionary?
if let dictValue = value as? [Key: Value] {
if let r = try? dictValue.mapKeys(keys, valueTransform: valueTransform) {
if let r = try? dictValue.mapTransform(keys, valueTransform: valueTransform) {
// if so, lets recurse...
newValue = r
}
Expand All @@ -309,7 +309,7 @@ extension Dictionary where Key == String, Value == Any {
newValue = arrayValue.map { item -> Value in
var newValue = item
if let dictValue = item as? [Key: Value] {
if let r = try? dictValue.mapKeys(keys, valueTransform: valueTransform) {
if let r = try? dictValue.mapTransform(keys, valueTransform: valueTransform) {
newValue = r
}
}
Expand All @@ -318,7 +318,7 @@ extension Dictionary where Key == String, Value == Any {
}

if !(newValue is [Key: Value]), let transform = valueTransform {
// it's not a dictionary apply our transform.
// it's not a dictionary so apply our transform.

// note: if it's an array, we've processed any dictionaries inside
// already, but this gives the opportunity to apply a transform to the other
Expand Down
4 changes: 2 additions & 2 deletions Tests/Segment-Tests/JSON_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class JSONTests: XCTestCase {

let json = try! JSON(dict)

let output = try! json.mapKeys(keys).dictionaryValue
let output = try! json.mapTransform(keys).dictionaryValue

XCTAssertTrue(output!["AKey1"] as! Int == 1)
XCTAssertTrue(output!["AKey2"] as! Int == 2)
Expand All @@ -153,7 +153,7 @@ class JSONTests: XCTestCase {

let json = try! JSON(dict)

let output = try! json.mapKeys(keys, valueTransform: { key, value in
let output = try! json.mapTransform(keys, valueTransform: { key, value in
var newValue = value
if let v = newValue as? Int {
if v == 1 {
Expand Down