Skip to content

Commit 12b5ad3

Browse files
authored
Sync with underscore-java
1 parent 6074aa0 commit 12b5ad3

File tree

9 files changed

+100
-50
lines changed

9 files changed

+100
-50
lines changed

src/main/java/com/github/underscore/Json.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -695,22 +695,21 @@ private Number readNumber() {
695695
return result;
696696
}
697697

698-
private boolean readFraction() {
698+
private void readFraction() {
699699
if (!readChar('.')) {
700-
return false;
700+
return;
701701
}
702702
if (!readDigit()) {
703703
throw expected(DIGIT);
704704
}
705705
while (readDigit()) {
706706
// ignored
707707
}
708-
return true;
709708
}
710709

711-
private boolean readExponent() {
710+
private void readExponent() {
712711
if (!readChar('e') && !readChar('E')) {
713-
return false;
712+
return;
714713
}
715714
if (!readChar('+')) {
716715
readChar('-');
@@ -721,7 +720,6 @@ private boolean readExponent() {
721720
while (readDigit()) {
722721
// ignored
723722
}
724-
return true;
725723
}
726724

727725
private boolean readChar(char ch) {

src/main/java/com/github/underscore/U.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public class U<T> extends Underscore<T> {
9494
private static final String LOWER = "[a-z\\xdf-\\xf6\\xf8-\\xff]+";
9595
private static final String SELF_CLOSING = "-self-closing";
9696
private static final String NIL_KEY = "-nil";
97+
private static final String OMIT_XML_DECL = "#omit-xml-declaration";
9798
private static final java.util.regex.Pattern RE_WORDS =
9899
java.util.regex.Pattern.compile(
99100
UPPER
@@ -242,7 +243,7 @@ public Chain<T> compact() {
242243

243244
@Override
244245
public Chain<T> compact(final T falsyValue) {
245-
return new Chain<>(Underscore.compact(value(), falsyValue));
246+
return new Chain<>(Underscore.compactList(value(), falsyValue));
246247
}
247248

248249
@Override
@@ -2749,6 +2750,20 @@ public static String xmlToJson(String xml) {
27492750
return xmlToJson(xml, Json.JsonStringBuilder.Step.TWO_SPACES, null);
27502751
}
27512752

2753+
@SuppressWarnings("unchecked")
2754+
public static String xmlToJsonMinimum(String xml, Json.JsonStringBuilder.Step identStep) {
2755+
Object object = Xml.fromXml(xml);
2756+
if (object instanceof Map) {
2757+
((Map) object).remove(OMIT_XML_DECL);
2758+
return Json.toJson(replaceSelfClosingWithEmpty((Map) object), identStep);
2759+
}
2760+
return Json.toJson((List) object, identStep);
2761+
}
2762+
2763+
public static String xmlToJsonMinimum(String xml) {
2764+
return xmlToJsonMinimum(xml, Json.JsonStringBuilder.Step.TWO_SPACES);
2765+
}
2766+
27522767
public static String xmlToJson(String xml, Json.JsonStringBuilder.Step identStep) {
27532768
return xmlToJson(xml, identStep, null);
27542769
}
@@ -2885,7 +2900,7 @@ public static Map<String, Object> removeMinusesAndConvertNumbers(Map<String, Obj
28852900
newKey = entry.getKey();
28862901
}
28872902
if (!entry.getKey().equals(SELF_CLOSING)
2888-
&& !entry.getKey().equals("#omit-xml-declaration")) {
2903+
&& !entry.getKey().equals(OMIT_XML_DECL)) {
28892904
outMap.put(newKey, makeObject(entry.getValue()));
28902905
}
28912906
}

src/main/java/com/github/underscore/Underscore.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,21 +1490,21 @@ public static <E> E[] compact(final E... array) {
14901490
return (E[]) compact(Arrays.asList(array)).toArray();
14911491
}
14921492

1493-
public static <E> List<E> compact(final List<E> list, final E falsyValue) {
1493+
public static <E> List<E> compactList(final List<E> list, final E falsyValue) {
14941494
return filter(list, arg -> !(Objects.equals(arg, falsyValue)));
14951495
}
14961496

14971497
@SuppressWarnings("unchecked")
14981498
public static <E> E[] compact(final E[] array, final E falsyValue) {
1499-
return (E[]) compact(Arrays.asList(array), falsyValue).toArray();
1499+
return (E[]) compactList(Arrays.asList(array), falsyValue).toArray();
15001500
}
15011501

15021502
public List<T> compact() {
15031503
return compact((List<T>) iterable);
15041504
}
15051505

15061506
public List<T> compact(final T falsyValue) {
1507-
return compact((List<T>) iterable, falsyValue);
1507+
return compactList((List<T>) iterable, falsyValue);
15081508
}
15091509

15101510
/*
@@ -1920,11 +1920,11 @@ public static List<Character> range(char start, char stop, int step) {
19201920
return list;
19211921
}
19221922
if (start < stop) {
1923-
for (char value = start; value < stop; value += step) {
1923+
for (char value = start; value < stop; value += (char) step) {
19241924
list.add(value);
19251925
}
19261926
} else {
1927-
for (char value = start; value > stop; value += step) {
1927+
for (char value = start; value > stop; value += (char) step) {
19281928
list.add(value);
19291929
}
19301930
}
@@ -2898,7 +2898,7 @@ public Chain<T> compact() {
28982898
}
28992899

29002900
public Chain<T> compact(final T falsyValue) {
2901-
return new Chain<>(Underscore.compact(list, falsyValue));
2901+
return new Chain<>(Underscore.compactList(list, falsyValue));
29022902
}
29032903

29042904
@SuppressWarnings("unchecked")

src/test/java/com/github/underscore/ArraysTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ void lastOrNull() {
659659
void compact() {
660660
final List<?> result = Underscore.compact(asList(0, 1, false, 2, "", 3));
661661
assertEquals("[1, 2, 3]", result.toString());
662-
final List<?> result2 = Underscore.compact(Arrays.<Object>asList(0, 1, false, 2, "", 3), 1);
662+
final List<?> result2 = Underscore.compactList(Arrays.<Object>asList(0, 1, false, 2, "", 3), 1);
663663
assertEquals("[0, false, 2, , 3]", result2.toString());
664664
final List<?> result3 = Underscore.compact(asList(0, 1, null, 2, "", 3));
665665
assertEquals("[1, 2, 3]", result3.toString());

src/test/java/com/github/underscore/CollectionsTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
*
5454
* @author Valentyn Kolesnikov
5555
*/
56+
@SuppressWarnings("java:S1186")
5657
class CollectionsTest {
5758

5859
/*
@@ -1718,7 +1719,9 @@ public Integer next() {
17181719
return array[index++];
17191720
}
17201721

1721-
public void remove() {}
1722+
@Override
1723+
public void remove() {
1724+
}
17221725
};
17231726
assertEquals(6, Underscore.size(iterable));
17241727
assertEquals(5, Underscore.size(new Integer[] {5, 4, 3, 2, 1}));

src/test/java/com/github/underscore/LodashTest.java

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -616,20 +616,24 @@ void fetchGet() {
616616
"https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json");
617617
result.json();
618618
result.jsonMap();
619-
// assertEquals("{\"gameId\":483159,\"knight\":{\"name\":"
620-
// + "\"Sir. Russell Jones of
621-
// Alberta\",\"attack\":2,\"armor\":7,\"agility\":3,\"endurance\":8}}",
622-
// result.text());
623-
// assertEquals("Sir. Russell Jones of Alberta",
624-
// (String) Underscore.get((Map<String, Object>) result.json(), "knight.name"));
619+
assertEquals("{\n"
620+
+ " \"fruit\": \"Apple\",\n"
621+
+ " \"size\": \"Large\",\n"
622+
+ " \"color\": \"Red\"\n"
623+
+ "}",
624+
result.text());
625+
assertEquals("Apple",
626+
U.get((Map<String, Object>) result.json(), "fruit"));
625627
U.Chain<?> resultChain =
626628
U.chain(
627629
"https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json")
628630
.fetch();
629-
// assertEquals("{\"gameId\":483159,\"knight\":{\"name\":"
630-
// + "\"Sir. Russell Jones of
631-
// Alberta\",\"attack\":2,\"armor\":7,\"agility\":3,\"endurance\":8}}",
632-
// resultChain.item());
631+
assertEquals("{\n"
632+
+ " \"fruit\": \"Apple\",\n"
633+
+ " \"size\": \"Large\",\n"
634+
+ " \"color\": \"Red\"\n"
635+
+ "}",
636+
resultChain.item());
633637
U.chain(
634638
"https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json")
635639
.fetch();
@@ -662,10 +666,12 @@ void fetchGetWithTimeouts() {
662666
"https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json",
663667
30000,
664668
30000);
665-
// assertEquals("{\"gameId\":483159,\"knight\":{\"name\":"
666-
// + "\"Sir. Russell Jones of
667-
// Alberta\",\"attack\":2,\"armor\":7,\"agility\":3,\"endurance\":8}}",
668-
// result.text());
669+
assertEquals("{\n"
670+
+ " \"fruit\": \"Apple\",\n"
671+
+ " \"size\": \"Large\",\n"
672+
+ " \"color\": \"Red\"\n"
673+
+ "}",
674+
result.text());
669675
}
670676

671677
@Test
@@ -725,9 +731,7 @@ void fetchPut() {
725731
+ " \"fireBreath\": 10"
726732
+ " }"
727733
+ "}");
728-
// assertEquals("{\"status\":\"Victory\",\"message\":\"Dragon was successful in a
729-
// glorious battle\"}",
730-
// result.text());
734+
assertEquals(403, result.getStatus());
731735
U.FetchResponse result2 =
732736
U.fetch(
733737
"https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json",
@@ -743,10 +747,8 @@ void fetchPut() {
743747
null,
744748
null,
745749
null);
746-
// assertEquals("{\"status\":\"Defeat\",\"message\":"
747-
// + "\"No dragon showed up, knight dealt his deeds as he pleased.\"}",
748-
// result2.text());
749-
U.Chain resultChain =
750+
assertEquals(403, result2.getStatus());
751+
U.Chain<String> resultChain =
750752
U.chain(
751753
"http://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json")
752754
.fetch(
@@ -759,9 +761,13 @@ void fetchPut() {
759761
+ " \"fireBreath\": 10"
760762
+ " }"
761763
+ "}");
762-
// assertEquals("{\"status\":\"Victory\",\"message\":\"Dragon was successful in a
763-
// glorious battle\"}",
764-
// resultChain.item());
764+
assertEquals("<html>\n"
765+
+ "<head><title>301 Moved Permanently</title></head>\n"
766+
+ "<body>\n"
767+
+ "<center><h1>301 Moved Permanently</h1></center>\n"
768+
+ "<hr><center>cloudflare</center>\n"
769+
+ "</body>\n"
770+
+ "</html>\n", resultChain.item().replace("\r\n", "\n"));
765771
}
766772

767773
@Test
@@ -1022,6 +1028,31 @@ void xmpToJson4() {
10221028
+ "</z:catalog>"));
10231029
}
10241030

1031+
@Test
1032+
void xmlToJsonMinimum() {
1033+
assertEquals(
1034+
"{\n"
1035+
+ " \"root\": {\n"
1036+
+ " \"element\": [\n"
1037+
+ " \"1\",\n"
1038+
+ " \"2\"\n"
1039+
+ " ],\n"
1040+
+ " \"a\": \"\"\n"
1041+
+ " }\n"
1042+
+ "}",
1043+
U.xmlToJsonMinimum("<root><element>1</element><element>2</element><a/></root>"));
1044+
assertEquals(
1045+
"[\n"
1046+
+ " \"a\",\n"
1047+
+ " \"b\"\n"
1048+
+ "]",
1049+
U.xmlToJsonMinimum("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1050+
+ "<root>\n"
1051+
+ " <element>a</element>\n"
1052+
+ " <element>b</element>\n"
1053+
+ "</root>"));
1054+
}
1055+
10251056
@Test
10261057
void xmlOrJsonToJson() {
10271058
assertEquals(

src/test/java/com/github/underscore/ObjectsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ class Test {}
266266

267267
@Test
268268
void cloneError2() {
269+
@SuppressWarnings("java:S1172")
269270
class Test implements Cloneable {
270271
public Object clone(String arg) {
271272
return null;

src/test/java/com/github/underscore/StringTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,16 +3569,12 @@ void fromXmlStackoverflowObject() throws IOException {
35693569
}
35703570

35713571
private String repeat(String s, int times) {
3572-
StringBuilder stringBuilder = new StringBuilder(s.length() * times);
3573-
for (int i = 0; i < times; i++) {
3574-
stringBuilder.append(s);
3575-
}
3576-
return stringBuilder.toString();
3572+
return s.repeat(Math.max(0, times));
35773573
}
35783574

35793575
@SuppressWarnings("unchecked")
35803576
@Test
3581-
void testParseDeeplyNestedArrays() throws IOException {
3577+
void testParseDeeplyNestedArrays() {
35823578
int times = 1000;
35833579
// [[[ ... ]]]
35843580
String json = repeat("[", times) + repeat("]", times);
@@ -3598,7 +3594,7 @@ void testParseDeeplyNestedArrays() throws IOException {
35983594

35993595
@SuppressWarnings("unchecked")
36003596
@Test
3601-
void testParseDeeplyNestedObjects() throws IOException {
3597+
void testParseDeeplyNestedObjects() {
36023598
int times = 1000;
36033599
// {"a":{"a": ... {"a":null} ... }}
36043600
String json = repeat("{\"a\":", times) + "null" + repeat("}", times);

src/test/java/com/github/underscore/UnderscoreTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,10 @@ public Integer next() {
463463
return array[index++];
464464
}
465465

466-
public void remove() {}
466+
@Override
467+
public void remove() {
468+
// ignored
469+
}
467470
};
468471
final Optional<Integer> result = Underscore.findLast(iterable, item -> item % 2 == 0);
469472
assertEquals("Optional[6]", result.toString());
@@ -501,7 +504,9 @@ public T next() {
501504
}
502505

503506
@Override
504-
public void remove() {}
507+
public void remove() {
508+
// ignored
509+
}
505510
};
506511
}
507512
}
@@ -541,6 +546,7 @@ void optional() {
541546
Optional.empty().get();
542547
fail("IllegalStateException expected");
543548
} catch (NoSuchElementException ignored) {
549+
// ignored
544550
}
545551
assertFalse(Optional.<Integer>empty().filter(arg -> true).isPresent());
546552
assertTrue(Optional.<Integer>empty().filter(arg -> false).isEmpty());
@@ -792,7 +798,7 @@ void testPropertiesToMapWithEmptyProperties() {
792798
Map<String, Object> map = U.propertiesToMap(properties);
793799
assertEquals(0, map.size());
794800
Map<String, Object> map2 = U.propertiesToMap(null);
795-
assertEquals(0, map.size());
801+
assertEquals(0, map2.size());
796802
}
797803

798804
@Test

0 commit comments

Comments
 (0)