Skip to content

Commit 993ccc9

Browse files
committed
Sync with underscore-java.
1 parent f3e8568 commit 993ccc9

File tree

5 files changed

+130
-29
lines changed

5 files changed

+130
-29
lines changed

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

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ public class U<T> extends com.github.underscore.U<T> {
9696
public enum Mode {
9797
REPLACE_SELF_CLOSING_WITH_NULL,
9898
REPLACE_SELF_CLOSING_WITH_EMPTY,
99-
REPLACE_EMPTY_VALUE_WITH_NULL
99+
REPLACE_EMPTY_VALUE_WITH_NULL,
100+
FORCE_ATTRIBUTE_USAGE
100101
}
101102

102103
public U(final Iterable<T> iterable) {
@@ -2168,14 +2169,7 @@ public static Map<String, Object> fromXmlMap(final String xml) {
21682169
@SuppressWarnings("unchecked")
21692170
public static Map<String, Object> fromXmlMap(final String xml, final Xml.FromType fromType) {
21702171
final Object object = Xml.fromXml(xml, fromType);
2171-
final Map<String, Object> result;
2172-
if (object instanceof Map) {
2173-
result = (Map<String, Object>) object;
2174-
} else {
2175-
result = newLinkedHashMap();
2176-
result.put("value", object);
2177-
}
2178-
return result;
2172+
return getStringObjectMap(object);
21792173
}
21802174

21812175
@SuppressWarnings("unchecked")
@@ -2223,6 +2217,10 @@ public Object fromJson() {
22232217
@SuppressWarnings("unchecked")
22242218
public static Map<String, Object> fromJsonMap(final String string) {
22252219
final Object object = Json.fromJson(string);
2220+
return getStringObjectMap(object);
2221+
}
2222+
2223+
private static Map<String, Object> getStringObjectMap(Object object) {
22262224
final Map<String, Object> result;
22272225
if (object instanceof Map) {
22282226
result = (Map<String, Object>) object;
@@ -2241,16 +2239,27 @@ public Object fromXml() {
22412239
return Xml.fromXml(getString().get());
22422240
}
22432241

2244-
public static String jsonToXml(String json, Xml.XmlStringBuilder.Step identStep) {
2245-
Object result = Json.fromJson(json);
2246-
if (result instanceof Map) {
2247-
return Xml.toXml((Map) result, identStep);
2242+
@SuppressWarnings("unchecked")
2243+
public static String jsonToXml(String json, Xml.XmlStringBuilder.Step identStep, Mode mode) {
2244+
Object object = Json.fromJson(json);
2245+
final String result;
2246+
if (object instanceof Map) {
2247+
if (mode == Mode.FORCE_ATTRIBUTE_USAGE) {
2248+
result = Xml.toXml(forceAttributeUsage((Map) object), identStep);
2249+
} else {
2250+
result = Xml.toXml((Map) object, identStep);
2251+
}
2252+
return result;
22482253
}
2249-
return Xml.toXml((List) result, identStep);
2254+
return Xml.toXml((List) object, identStep);
2255+
}
2256+
2257+
public static String jsonToXml(String json, Mode mode) {
2258+
return jsonToXml(json, Xml.XmlStringBuilder.Step.TWO_SPACES, mode);
22502259
}
22512260

22522261
public static String jsonToXml(String json) {
2253-
return jsonToXml(json, Xml.XmlStringBuilder.Step.TWO_SPACES);
2262+
return jsonToXml(json, Xml.XmlStringBuilder.Step.TWO_SPACES, null);
22542263
}
22552264

22562265
@SuppressWarnings("unchecked")
@@ -2443,6 +2452,35 @@ private static Object makeObjectEmptyValue(Object value) {
24432452
return result;
24442453
}
24452454

2455+
@SuppressWarnings("unchecked")
2456+
public static Map<String, Object> forceAttributeUsage(Map<String, Object> map) {
2457+
Map<String, Object> outMap = newLinkedHashMap();
2458+
for (Map.Entry<String, Object> entry : map.entrySet()) {
2459+
outMap.put(!(entry.getValue() instanceof Map || entry.getValue() instanceof List
2460+
|| String.valueOf(entry.getKey()).startsWith("-"))
2461+
? "-" + entry.getKey() : String.valueOf(entry.getKey()),
2462+
makeAttributeUsage(entry.getValue()));
2463+
}
2464+
return outMap;
2465+
}
2466+
2467+
@SuppressWarnings("unchecked")
2468+
private static Object makeAttributeUsage(Object value) {
2469+
final Object result;
2470+
if (value instanceof List) {
2471+
List<Object> values = newArrayList();
2472+
for (Object item : (List) value) {
2473+
values.add(item instanceof Map ? forceAttributeUsage((Map) item) : item);
2474+
}
2475+
result = values;
2476+
} else if (value instanceof Map) {
2477+
result = forceAttributeUsage((Map) value);
2478+
} else {
2479+
result = value;
2480+
}
2481+
return result;
2482+
}
2483+
24462484
public static long gcd(long value1, long value2) {
24472485
if (value1 == 0) {
24482486
return value2;

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@ public void fetchGetHttps() {
483483
assertEquals("{\n"
484484
+ " \"error\": {\n"
485485
+ " \"message\": \"Missing authentication\",\n"
486-
+ " \"status_code\": 401\n"
486+
+ " \"status_code\": 401,\n"
487+
+ " \"code\": \"unauthorized\"\n"
487488
+ " }\n"
488489
+ "}", result.text());
489490
}
@@ -741,6 +742,35 @@ public void formatXml() {
741742
U.formatXml("<a>\n <b></b>\n <b></b>\n</a>", Xml.XmlStringBuilder.Step.TABS));
742743
assertEquals("<a number=\"true\">1.00</a>", U.formatXml("<a number=\"true\">1.00</a>"));
743744
assertEquals("<a number=\"true\">2.01</a>", U.formatXml("<a number=\"true\">2.01</a>"));
745+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
746+
+ "<RootElm>\n"
747+
+ " <author DOB=\"\" EMailID=\"\" PlaceId=\"\" SSN=\"\">\n"
748+
+ " <extn externalSystemCode=\"\"/>\n"
749+
+ " </author>\n"
750+
+ "</RootElm>", U.jsonToXml("{\n"
751+
+ " \"RootElm\": {\n"
752+
+ " \"author\": {\n"
753+
+ " \"DOB\": \"\",\n"
754+
+ " \"EMailID\": \"\",\n"
755+
+ " \"PlaceId\": \"\",\n"
756+
+ " \"SSN\": \"\",\n"
757+
+ " \"extn\": {\n"
758+
+ " \"externalSystemCode\": \"\",\n"
759+
+ " \"-self-closing\": \"true\"\n"
760+
+ " }\n"
761+
+ " }\n"
762+
+ " }\n"
763+
+ "}", U.Mode.FORCE_ATTRIBUTE_USAGE));
764+
Map<String, Object> map = U.newLinkedHashMap();
765+
List<Object> list = U.newArrayList();
766+
list.add(U.newLinkedHashMap());
767+
map.put("list", list);
768+
U.forceAttributeUsage(map);
769+
Map<String, Object> map2 = U.newLinkedHashMap();
770+
List<Object> list2 = U.newArrayList();
771+
list2.add(U.newArrayList());
772+
map2.put("list", list2);
773+
U.forceAttributeUsage(map2);
744774
}
745775

746776
@Test

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

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2019 Valentyn Kolesnikov
4+
* Copyright 2015-2020 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -112,6 +112,39 @@ public void head() {
112112
assertEquals(5, resultInt);
113113
}
114114

115+
/*
116+
_.singleOrNull([5, 4, 3, 2, 1]);
117+
=> null
118+
_.singleOrNull([5]);
119+
=> 5
120+
*/
121+
@Test
122+
public void singleOrNull() {
123+
U<Integer> uWithMoreElement = new U<Integer>(asList(1, 2, 3));
124+
U<Integer> uWithOneElement = new U<Integer>(asList(1));
125+
126+
final Integer result1 = U.singleOrNull(asList(1, 2, 3, 4));
127+
assertEquals(result1, null);
128+
final int result2 = U.singleOrNull(asList(1));
129+
assertEquals(result2, 1);
130+
final Integer result3 = U.singleOrNull(new ArrayList<Integer>());
131+
assertEquals(result3, null);
132+
final Integer result4 = U.singleOrNull(asList(1, 2, 3), number -> number % 2 == 1);
133+
assertEquals(result4, null);
134+
final int result5 = U.singleOrNull(asList(1, 2, 3), number -> number % 2 == 0);
135+
assertEquals(result5, 2);
136+
final Integer result6 = U.singleOrNull(asList(1, 2, 3), number -> number == 5);
137+
assertEquals(result6, null);
138+
final Integer result7 = uWithMoreElement.singleOrNull();
139+
assertEquals(result7, null);
140+
final Integer result8 = uWithOneElement.singleOrNull();
141+
assertEquals(result8, Integer.valueOf(1));
142+
final Integer result9 = uWithMoreElement.singleOrNull(item -> item % 2 == 0);
143+
assertEquals(result9, Integer.valueOf(2));
144+
final Integer result10 = uWithMoreElement.singleOrNull(item -> item % 2 == 1);
145+
assertEquals(result10, null);
146+
}
147+
115148
/*
116149
_.rest([5, 4, 3, 2, 1]);
117150
=> [4, 3, 2, 1]
@@ -619,22 +652,22 @@ public void findLastIndex() {
619652
*/
620653
@Test
621654
public void range() {
622-
final int[] result = U.range(10);
623-
assertArrayEquals(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, result);
655+
final List<Integer> result = U.range(10);
656+
assertEquals("[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]", result.toString());
624657
final List<Integer> resultChain = U.chain("").range(10).value();
625658
assertEquals("[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]", resultChain.toString());
626-
final int[] result2 = U.range(1, 11);
627-
assertArrayEquals(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, result2);
659+
final List<Integer> result2 = U.range(1, 11);
660+
assertEquals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", result2.toString());
628661
final List<Integer> result2Chain = U.chain("").range(1, 11).value();
629662
assertEquals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", result2Chain.toString());
630-
final int[] result3 = U.range(0, 30, 5);
631-
assertArrayEquals(new int[] {0, 5, 10, 15, 20, 25}, result3);
663+
final List<Integer> result3 = U.range(0, 30, 5);
664+
assertEquals("[0, 5, 10, 15, 20, 25]", result3.toString());
632665
final List<Integer> result3Chain = U.chain("").range(0, 30, 5).value();
633666
assertEquals("[0, 5, 10, 15, 20, 25]", result3Chain.toString());
634-
final int[] result4 = U.range(0, -10, -1);
635-
assertArrayEquals(new int[] {0, -1, -2, -3, -4, -5, -6, -7, -8, -9}, result4);
636-
final int[] result5 = U.range(0);
637-
assertArrayEquals(new int[] {}, result5);
667+
final List<Integer> result4 = U.range(0, -10, -1);
668+
assertEquals("[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]", result4.toString());
669+
final List<Integer> result5 = U.range(0);
670+
assertEquals("[]", result5.toString());
638671
}
639672

640673
/*

src/test/java8/com/github/underscore/ChainingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2019 Valentyn Kolesnikov
4+
* Copyright 2015-2020 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2019 Valentyn Kolesnikov
4+
* Copyright 2015-2020 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)