@momo-kits/native-kits 0.112.1-beta.2 → 0.112.1-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/native-kits",
3
- "version": "0.112.1-beta.2",
3
+ "version": "0.112.1-beta.4",
4
4
  "private": false,
5
5
  "dependencies": {},
6
6
  "devDependencies": {},
@@ -1,175 +0,0 @@
1
- package vn.momo.compose.utils.datamapping
2
-
3
- import kotlinx.serialization.json.JsonArray
4
- import kotlinx.serialization.json.JsonElement
5
- import kotlinx.serialization.json.JsonNull
6
- import kotlinx.serialization.json.JsonObject
7
- import kotlinx.serialization.json.JsonPrimitive
8
- import kotlinx.serialization.json.booleanOrNull
9
- import kotlinx.serialization.json.contentOrNull
10
- import kotlinx.serialization.json.doubleOrNull
11
- import kotlinx.serialization.json.floatOrNull
12
- import kotlinx.serialization.json.intOrNull
13
- import kotlinx.serialization.json.jsonArray
14
- import kotlinx.serialization.json.jsonObject
15
- import kotlinx.serialization.json.jsonPrimitive
16
- import kotlinx.serialization.json.longOrNull
17
-
18
- fun emptyJsonObject() = JsonObject(mapOf())
19
-
20
- val JsonObject.Companion.EMPTY
21
- get() = JsonObject(emptyMap())
22
-
23
- val JsonArray.Companion.EMPTY
24
- get() = JsonArray(emptyList())
25
-
26
- fun JsonObject.tryToGetObject(key: String): JsonObject? {
27
- return try {
28
- this[key]?.toJsonElement()?.jsonObject
29
- } catch (_: Exception) {
30
- JsonObject.EMPTY
31
- }
32
- }
33
-
34
- fun JsonObject.tryToGetArray(key: String): JsonArray? {
35
- return try {
36
- this[key]?.toJsonElement()?.jsonArray
37
- } catch (_: Exception) {
38
- JsonArray.EMPTY
39
- }
40
- }
41
-
42
- fun JsonObject.tryToGetString(key: String): String {
43
- return try {
44
- this.getOrElse(key) { "" }.toJsonElement().jsonPrimitive.content
45
- } catch (_: Exception) {
46
- ""
47
- }
48
- }
49
-
50
- fun JsonObject.tryToGetStringNullable(key: String): String? {
51
- return try {
52
- this.getOrElse(key) { null }.toJsonElement().jsonPrimitive.contentOrNull
53
- } catch (_: Exception) {
54
- null
55
- }
56
- }
57
-
58
- fun JsonObject.tryToGetLong(key: String): Long? {
59
- return try {
60
- this.getOrElse(key) { "" }.toJsonElement().jsonPrimitive.longOrNull
61
- } catch (_: Exception) {
62
- null
63
- }
64
- }
65
-
66
- fun JsonObject.tryToGetLong(key: String, default: Long = 0L): Long {
67
- return try {
68
- this.getOrElse(key) { default }.toJsonElement().jsonPrimitive.longOrNull ?: default
69
- } catch (_: Exception) {
70
- default
71
- }
72
- }
73
-
74
- fun JsonObject.tryToGetDoubleNullable(key: String): Double? {
75
- return try {
76
- this.getOrElse(key) { "" }.toJsonElement().jsonPrimitive.doubleOrNull
77
- } catch (_: Exception) {
78
- null
79
- }
80
- }
81
-
82
- fun JsonObject.tryToGetDouble(key: String, default: Double = 0.0): Double {
83
- return try {
84
- this.getOrElse(key) { default }.toJsonElement().jsonPrimitive.doubleOrNull ?: default
85
- } catch (_: Exception) {
86
- default
87
- }
88
- }
89
-
90
- fun JsonObject.tryToGetBoolean(key: String): Boolean {
91
- return try {
92
- this[key]?.jsonPrimitive?.booleanOrNull ?: false
93
- } catch (_: Exception) {
94
- false
95
- }
96
- }
97
-
98
- fun JsonObject.tryToGetBooleanNullable(key: String): Boolean? {
99
- return try {
100
- this[key]?.jsonPrimitive?.booleanOrNull
101
- } catch (_: Exception) {
102
- null
103
- }
104
- }
105
-
106
- internal fun JsonElement.toPureMap(): Map<String, Any?> {
107
- return (this as? JsonObject)?.toPureMap() ?: emptyMap()
108
- }
109
-
110
- fun JsonObject.toPureMap(): Map<String, Any?> {
111
- val map: MutableMap<String, Any?> = mutableMapOf()
112
- this.jsonObject.entries.forEach { (key, value) ->
113
- when (value) {
114
- is Map<*, *> -> map[key] = value.toPureMap()
115
- is Collection<*> -> map[key] = value.toList().map { it.toJsonElement().toPureMap() }
116
- is JsonPrimitive -> map[key] =
117
- if (value.isString) value.content else value.intOrNull ?: value.doubleOrNull
118
- ?: value.floatOrNull ?: value.booleanOrNull
119
- is JsonNull -> map[key] = null
120
- else -> throw IllegalStateException("Can't serialize unknown type: $value")
121
- }
122
- }
123
- return map
124
- }
125
-
126
- fun JsonElement.tryToString(): String {
127
- return try {
128
- this.jsonPrimitive.content
129
- } catch (_: Exception) {
130
- ""
131
- }
132
- }
133
-
134
- internal fun JsonElement.toCollection(): Collection<Any?> {
135
- val list: MutableList<Any?> = mutableListOf()
136
- (this as? JsonArray)?.forEach { value ->
137
- when (value) {
138
- is Map<*, *> -> list.add(value.toMap())
139
- is Collection<*> -> list.add(value.toCollection())
140
- is JsonPrimitive -> list.add(
141
- if (value.isString) value.content else value.booleanOrNull ?: value.intOrNull
142
- ?: value.longOrNull ?: value.doubleOrNull ?: value.floatOrNull
143
- )
144
-
145
- is JsonNull -> list.add(null)
146
- else -> throw IllegalStateException("Can't serialize unknown type: $value")
147
- }
148
- }
149
- return list
150
- }
151
-
152
- internal fun JsonElement.toMap(): Map<String, Any?> {
153
- val map: MutableMap<String, Any?> = mutableMapOf()
154
- (this as? JsonObject)?.entries?.forEach { (key, value) ->
155
- when (value) {
156
- is Map<*, *> -> map[key] = value.toMap()
157
- is Collection<*> -> map[key] = value.toCollection()
158
- is JsonPrimitive -> map[key] =
159
- if (value.isString) value.content else value.booleanOrNull ?: value.intOrNull
160
- ?: value.longOrNull ?: value.doubleOrNull ?: value.floatOrNull
161
-
162
- is JsonNull -> map[key] = null
163
- else -> throw IllegalStateException("Can't serialize unknown type: $value")
164
- }
165
- }
166
- return map
167
- }
168
-
169
- fun JsonElement.tryForceToStringStringMap(): Map<String, String?>? = try {
170
- this.toMap() as Map<String, String?>?
171
- } catch (e: Exception) {
172
- null
173
- }
174
-
175
-