Skip to content

Commit 0a622bc

Browse files
author
Capacitor+ Bot
committed
chore: sync upstream PR ionic-team#7638 from @robingenz
2 parents 4c7c126 + e983b43 commit 0a622bc

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

android/capacitor/src/main/java/com/getcapacitor/PluginConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ public int getInt(String configKey, int defaultValue) {
6565
return JSONUtils.getInt(config, configKey, defaultValue);
6666
}
6767

68+
/**
69+
* Get a double value for a plugin in the Capacitor config.
70+
*
71+
* @param configKey The key of the value to retrieve
72+
* @param defaultValue A default value to return if the key does not exist in the config
73+
* @return The value from the config, if key exists. Default value returned if not
74+
*/
75+
public double getDouble(String configKey, double defaultValue) {
76+
return JSONUtils.getDouble(config, configKey, defaultValue);
77+
}
78+
6879
/**
6980
* Get a string array value for a plugin in the Capacitor config.
7081
*

android/capacitor/src/main/java/com/getcapacitor/util/JSONUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@ public static int getInt(JSONObject jsonObject, String key, int defaultValue) {
7575
return defaultValue;
7676
}
7777

78+
/**
79+
* Get a double value from the given JSON object.
80+
*
81+
* @param jsonObject A JSON object to search
82+
* @param key A key to fetch from the JSON object
83+
* @param defaultValue A default value to return if the key cannot be found
84+
* @return The value at the given key in the JSON object, or the default value
85+
*/
86+
public static double getDouble(JSONObject jsonObject, String key, double defaultValue) {
87+
String k = getDeepestKey(key);
88+
try {
89+
JSONObject o = getDeepestObject(jsonObject, key);
90+
return o.getDouble(k);
91+
} catch (JSONException ignore) {
92+
// value was not found
93+
}
94+
95+
return defaultValue;
96+
}
97+
7898
/**
7999
* Get a JSON object value from the given JSON object.
80100
*

ios/Capacitor/Capacitor/PluginConfig.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ import Foundation
3030
return defaultValue
3131
}
3232

33+
@objc public func getDouble(_ configKey: String, _ defaultValue: Double) -> Double {
34+
if let val = (self.config)[keyPath: KeyPath(configKey)] as? Double {
35+
return val
36+
}
37+
return defaultValue
38+
}
39+
3340
public func getArray(_ configKey: String, _ defaultValue: JSArray? = nil) -> JSArray? {
3441
if let val = (self.config)[keyPath: KeyPath(configKey)] as? JSArray {
3542
return val

0 commit comments

Comments
 (0)