Skip to content

Commit 6d05b0e

Browse files
committed
Logical improvement
1 parent 8c02ba1 commit 6d05b0e

File tree

5 files changed

+77
-52
lines changed

5 files changed

+77
-52
lines changed

src/main/kotlin/ninja/sakib/jsonq/ext/JsonArrayExt.kt

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ninja.sakib.jsonq.ext
33
import com.eclipsesource.json.Json
44
import com.eclipsesource.json.JsonArray
55
import com.eclipsesource.json.JsonValue
6+
import ninja.sakib.jsonq.utils.*
67

78
/**
89
* := Coded with love by Sakib Sami on 5/21/18.
@@ -14,39 +15,39 @@ import com.eclipsesource.json.JsonValue
1415
fun JsonArray.whereGt(key: String, v: Any): MutableList<Any> {
1516
val res = mutableListOf<Any>()
1617
this
17-
.filter { it -> it.isCountable(v, key, ninja.sakib.jsonq.GREATER) }
18+
.filter { it -> it.isCountable(v, key, GREATER) }
1819
.mapTo(res) { it -> it.asObject() }
1920
return res
2021
}
2122

2223
fun JsonArray.whereLt(key: String, v: Any): MutableList<Any> {
2324
val res = mutableListOf<Any>()
2425
this
25-
.filter { it -> it.isCountable(v, key, ninja.sakib.jsonq.LESS) }
26+
.filter { it -> it.isCountable(v, key, LESS) }
2627
.mapTo(res) { it -> it.asObject() }
2728
return res
2829
}
2930

3031
fun JsonArray.whereLe(key: String, v: Any): MutableList<Any> {
3132
val res = mutableListOf<Any>()
3233
this
33-
.filter { it -> it.isCountable(v, key, ninja.sakib.jsonq.LESS_EQ) }
34+
.filter { it -> it.isCountable(v, key, LESS_EQ) }
3435
.mapTo(res) { it -> it.asObject() }
3536
return res
3637
}
3738

3839
fun JsonArray.whereGe(key: String, v: Any): MutableList<Any> {
3940
val res = mutableListOf<Any>()
4041
this
41-
.filter { it -> it.isCountable(v, key, ninja.sakib.jsonq.GREATER_EQ) }
42+
.filter { it -> it.isCountable(v, key, GREATER_EQ) }
4243
.mapTo(res) { it -> it.asObject() }
4344
return res
4445
}
4546

4647
fun JsonArray.whereEq(key: String, v: Any): MutableList<Any> {
4748
val res = mutableListOf<Any>()
4849
this
49-
.filter { it -> it.isCountable(v, key, ninja.sakib.jsonq.EQ) }
50+
.filter { it -> it.isCountable(v, key, EQ) }
5051
.mapTo(res) { it -> it.asObject() }
5152
return res
5253
}
@@ -75,7 +76,7 @@ fun JsonArray.contains(key: String, v: Any): MutableList<Any> {
7576
return res
7677
}
7778

78-
fun JsonArray.sum(): Double {
79+
fun JsonArray.sum(key: String): Double {
7980
var sum = 0.0
8081
this
8182
.forEach { it ->
@@ -84,47 +85,66 @@ fun JsonArray.sum(): Double {
8485
it.isLong() -> sum += it.asLong()
8586
it.isFloat() -> sum += it.asFloat()
8687
it.isDouble() -> sum += it.asDouble()
88+
it.isObject -> {
89+
val nIt = it.asObject().get(key)
90+
when {
91+
nIt.isInt() -> sum += nIt.asInt()
92+
nIt.isLong() -> sum += nIt.asLong()
93+
nIt.isFloat() -> sum += nIt.asFloat()
94+
nIt.isDouble() -> sum += nIt.asDouble()
95+
}
96+
}
8797
}
8898
}
8999
return sum
90100
}
91101

92-
fun JsonArray.min(): JsonValue {
93-
if (this.size() == 0)
94-
return Json.value(0)
95-
var min = this[0]
96-
for (v in this) {
97-
if (v.isInt() && min.isInt() && v.asInt() < min.asInt()) {
98-
min = v
99-
} else if (v.isLong() && min.isLong() && v.asLong() < min.asLong()) {
100-
min = v
101-
} else if (v.isFloat() && min.isFloat() && v.asFloat() < min.asFloat()) {
102-
min = v
103-
} else if (v.isDouble() && min.isDouble() && v.asDouble() < min.asDouble()) {
104-
min = v
105-
}
106-
}
102+
fun JsonArray.min(key: String): JsonValue {
103+
var min = Json.value(Double.MAX_VALUE)
104+
this
105+
.forEach { it ->
106+
when {
107+
it.isObject -> {
108+
val nIt = it.asObject().get(key)
109+
when {
110+
nIt.isInt() && nIt.asInt() < min.asInt() -> min = nIt
111+
nIt.isLong() && nIt.asLong() < min.asLong() -> min = nIt
112+
nIt.isFloat() && nIt.asFloat() < min.asFloat() -> min = nIt
113+
nIt.isDouble() && nIt.asDouble() < min.asDouble() -> min = nIt
114+
}
115+
}
116+
it.isInt() && it.asInt() < min.asInt() -> min = it
117+
it.isLong() && it.asLong() < min.asLong() -> min = it
118+
it.isFloat() && it.asFloat() < min.asFloat() -> min = it
119+
it.isDouble() && it.asDouble() < min.asDouble() -> min = it
120+
}
121+
}
107122
return min
108123
}
109124

110-
fun JsonArray.max(): JsonValue {
111-
if (this.size() == 0)
112-
return Json.value(0)
113-
var max = this[this.size() - 1]
114-
for (v in this) {
115-
if (v.isInt() && max.isInt() && v.asInt() > max.asInt()) {
116-
max = v
117-
} else if (v.isLong() && max.isLong() && v.asLong() > max.asLong()) {
118-
max = v
119-
} else if (v.isFloat() && max.isFloat() && v.asFloat() > max.asFloat()) {
120-
max = v
121-
} else if (v.isDouble() && max.isDouble() && v.asDouble() > max.asDouble()) {
122-
max = v
123-
}
124-
}
125+
fun JsonArray.max(key: String): JsonValue {
126+
var max = Json.value(Double.MIN_VALUE)
127+
this
128+
.forEach { it ->
129+
when {
130+
it.isObject -> {
131+
val nIt = it.asObject().get(key)
132+
when {
133+
nIt.isInt() && nIt.asInt() > max.asInt() -> max = nIt
134+
nIt.isLong() && nIt.asLong() > max.asLong() -> max = nIt
135+
nIt.isFloat() && nIt.asFloat() > max.asFloat() -> max = nIt
136+
nIt.isDouble() && nIt.asDouble() > max.asDouble() -> max = nIt
137+
}
138+
}
139+
it.isInt() && it.asInt() > max.asInt() -> max = it
140+
it.isLong() && it.asLong() > max.asLong() -> max = it
141+
it.isFloat() && it.asFloat() > max.asFloat() -> max = it
142+
it.isDouble() && it.asDouble() > max.asDouble() -> max = it
143+
}
144+
}
125145
return max
126146
}
127147

128-
fun JsonArray.avg(): JsonValue {
129-
return Json.value(this.sum() / this.size())
148+
fun JsonArray.avg(key: String): JsonValue {
149+
return Json.value(this.sum(key) / this.size())
130150
}

src/main/kotlin/ninja/sakib/jsonq/ext/JsonValueExt.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ninja.sakib.jsonq.ext
22

33
import com.eclipsesource.json.JsonValue
4-
import ninja.sakib.jsonq.*
4+
import ninja.sakib.jsonq.utils.*
55
import java.lang.Exception
66

77
/**
@@ -16,7 +16,6 @@ fun JsonValue.isInt(): Boolean {
1616
this.asInt()
1717
return true
1818
} catch (e: Exception) {
19-
2019
}
2120
return false
2221
}

src/main/kotlin/ninja/sakib/jsonq/Operators.kt renamed to src/main/kotlin/ninja/sakib/jsonq/utils/OperatorUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ninja.sakib.jsonq
1+
package ninja.sakib.jsonq.utils
22

33
/**
44
* := Coded with love by Sakib Sami on 5/27/18.

src/test/kotlin/ninja/sakib/jsonq/JSONQTest.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,31 @@ class JSONQTest {
121121

122122
@Test
123123
fun Sum() {
124-
val res = jsonq.from("arr").sum()
124+
val res = jsonq.from("products").sum("price")
125+
Assert.assertEquals(448000, res.toInt())
126+
}
127+
128+
@Test
129+
fun Sum2() {
130+
val res = jsonq.from("arr").sum("")
125131
Assert.assertEquals(10, res.toInt())
126132
}
127133

128134
@Test
129135
fun Min() {
130-
val res = jsonq.from("arr").min()
131-
Assert.assertEquals(1, res.asInt())
136+
val res = jsonq.from("products").min("price")
137+
Assert.assertEquals(12000.2, res.asDouble(), 0.0)
132138
}
133139

134140
@Test
135141
fun Max() {
136-
val res = jsonq.from("arr").max()
137-
Assert.assertEquals(4, res.asInt())
142+
val res = jsonq.from("products").max("price")
143+
Assert.assertEquals(150000.1, res.asDouble(), 0.0)
138144
}
139145

140146
@Test
141147
fun Avg() {
142-
val res = jsonq.from("arr").avg()
148+
val res = jsonq.from("arr").avg("")
143149
Assert.assertEquals(2.5, res.asDouble(), 0.0)
144150
}
145151
}

src/test/resources/data.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,35 +65,35 @@
6565
"city": null,
6666
"name": "macbook pro",
6767
"cat": 2,
68-
"price": 150000
68+
"price": 150000.1
6969
},
7070
{
7171
"id": 3,
7272
"city": "dhk",
7373
"name": "Redmi 3S Prime",
7474
"cat": 1,
75-
"price": 12000
75+
"price": 12000.2
7676
},
7777
{
7878
"id": 4,
7979
"city": null,
8080
"name": "Redmi 4X",
8181
"cat": 1,
82-
"price": 15000
82+
"price": 15000.0
8383
},
8484
{
8585
"id": 5,
8686
"city": "bsl",
8787
"name": "macbook air",
8888
"cat": 2,
89-
"price": 110000
89+
"price": 110000.00
9090
},
9191
{
9292
"id": 6,
9393
"city": null,
9494
"name": "macbook air 1",
9595
"cat": 2,
96-
"price": 81000
96+
"price": 81000.00
9797
}
9898
],
9999
"cities": [

0 commit comments

Comments
 (0)