Skip to content

Commit

Permalink
💫 Update: RNIDictionarySynthesizable+Default
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed May 1, 2023
1 parent 43c299a commit 9e6ff84
Showing 1 changed file with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,46 @@ extension RNIDictionarySynthesizable {

if let synthesizableDict = value as? (any RNIDictionarySynthesizable) {
return synthesizableDict.synthesizedDictionary(isJSDict: isJSDict);
};

} else if isJSDict, let rawValue = value as? any RawRepresentable {
if isJSDict, let rawValue = value as? any RawRepresentable {
return rawValue.rawValue;

} else if isJSDict, let array = value as? Array<Any> {
};

if isJSDict, let array = value as? Array<Any> {
return array.map {
return Self.recursivelyParseValue($0, isJSDict: isJSDict);
};
};

if let encodable = value as? Encodable,
let dict = encodable.asDictionary {

return dict;
};

return value;
};

// MARK: - Public Functions
// ------------------------
private func mergeInlinedProperties(
withDict baseDict: Dictionary<String, Any>,
isJSDict: Bool
) -> Dictionary<String, Any> {

public func synthesizedDictionary(
var baseDict = baseDict;

Self.synthesizedDictionaryInlinedProperties.forEach {
guard let value = self[keyPath: $0] as? (any RNIDictionarySynthesizable)
else { return };

let inlinedDict = value.synthesizedDictionary(isJSDict: isJSDict);
baseDict = baseDict.merging(inlinedDict){ old, _ in old };
};

return baseDict;
};

private func synthesizedDictionaryUsingDictIgnore(
isJSDict: Bool
) -> Dictionary<String, Any> {

Expand Down Expand Up @@ -77,21 +100,27 @@ extension RNIDictionarySynthesizable {
return (propertyKey, parsedValue);
};

var baseDict = Dictionary(
let baseDict = Dictionary(
uniqueKeysWithValues: propertyValueMap.compactMap { $0 }
);

Self.synthesizedDictionaryInlinedProperties.forEach {
guard let value = self[keyPath: $0] as? (any RNIDictionarySynthesizable)
else { return };

let inlinedDict = value.synthesizedDictionary(isJSDict: isJSDict);
baseDict = baseDict.merging(inlinedDict){ old, _ in old };
};

return baseDict;

return self.mergeInlinedProperties(
withDict: baseDict,
isJSDict: isJSDict
);
};

// MARK: - Public Functions
// ------------------------

public func synthesizedDictionary(
isJSDict: Bool
) -> Dictionary<String, Any> {

return self.synthesizedDictionaryUsingDictIgnore(isJSDict: isJSDict);
};


public var synthesizedJSDictionary: Dictionary<String, Any> {
self.synthesizedDictionary(isJSDict: true);
};
Expand Down

0 comments on commit 9e6ff84

Please sign in to comment.