mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
[feature] 新增BooleanUtil.isJsFalsy以及isJsTruthy
This commit is contained in:
parent
5b5209e801
commit
4db9c44d18
@ -17,6 +17,8 @@ public class BooleanUtil {
|
|||||||
private static final Set<String> TRUE_SET = SetUtil.of("true", "yes", "y", "t", "ok", "1", "on", "是", "对", "真", "對", "√");
|
private static final Set<String> TRUE_SET = SetUtil.of("true", "yes", "y", "t", "ok", "1", "on", "是", "对", "真", "對", "√");
|
||||||
/** 表示为假的字符串 */
|
/** 表示为假的字符串 */
|
||||||
private static final Set<String> FALSE_SET = SetUtil.of("false", "no", "n", "f", "0", "off", "否", "错", "假", "錯", "×");
|
private static final Set<String> FALSE_SET = SetUtil.of("false", "no", "n", "f", "0", "off", "否", "错", "假", "錯", "×");
|
||||||
|
/** js中表示假值Falsy的部分值 false、0、-0、0n、""、null、undefined 和 NaN */
|
||||||
|
public static final Set<Object> FALSY_SET = SetUtil.of(false, 0, -0, 0L, 0.0D, -0.0D, "", null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取相反值
|
* 取相反值
|
||||||
@ -522,4 +524,28 @@ public class BooleanUtil {
|
|||||||
public static boolean isBoolean(final Class<?> clazz) {
|
public static boolean isBoolean(final Class<?> clazz) {
|
||||||
return (clazz == Boolean.class || clazz == boolean.class);
|
return (clazz == Boolean.class || clazz == boolean.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否为假值(定义来源js)
|
||||||
|
*
|
||||||
|
* @param value 参数
|
||||||
|
* @return 是否为假值
|
||||||
|
* 定义{@see https://developer.mozilla.org/zh-CN/docs/Glossary/Falsy}
|
||||||
|
*/
|
||||||
|
public static boolean isJsFalsy(Object value) {
|
||||||
|
return FALSY_SET.contains(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否为真值(定义来源js)
|
||||||
|
* 所有除 false、0、-0、0n、""、null、undefined 和 NaN 以外的皆为真值
|
||||||
|
* 由于java中无法使用值来代表undefined 和 NaN,因此此处不做判断
|
||||||
|
*
|
||||||
|
* @param value 参数
|
||||||
|
* @return 是否为真值
|
||||||
|
* 定义{@see https://developer.mozilla.org/zh-CN/docs/Glossary/Truthy}
|
||||||
|
*/
|
||||||
|
public static boolean isJsTruthy(Object value) {
|
||||||
|
return false == isJsFalsy(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,7 @@ public class BooleanUtilTest {
|
|||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
@SuppressWarnings("ConstantConditions")
|
||||||
@Test
|
@Test
|
||||||
public void toBooleanObjectTest(){
|
public void toBooleanObjectTest() {
|
||||||
Assert.assertTrue(BooleanUtil.toBooleanObject("yes"));
|
Assert.assertTrue(BooleanUtil.toBooleanObject("yes"));
|
||||||
Assert.assertTrue(BooleanUtil.toBooleanObject("真"));
|
Assert.assertTrue(BooleanUtil.toBooleanObject("真"));
|
||||||
Assert.assertTrue(BooleanUtil.toBooleanObject("是"));
|
Assert.assertTrue(BooleanUtil.toBooleanObject("是"));
|
||||||
@ -95,4 +95,30 @@ public class BooleanUtilTest {
|
|||||||
Assert.assertNull(BooleanUtil.toBooleanObject(null));
|
Assert.assertNull(BooleanUtil.toBooleanObject(null));
|
||||||
Assert.assertNull(BooleanUtil.toBooleanObject("不识别"));
|
Assert.assertNull(BooleanUtil.toBooleanObject("不识别"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isJsFalsyTest() {
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsFalsy(false));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsFalsy(0));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsFalsy(-0));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsFalsy(0L));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsFalsy(0.0D));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsFalsy(0.00D));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsFalsy(-0.00D));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsFalsy(""));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsFalsy(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isJsTruthyTest() {
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsTruthy(true));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsTruthy(1));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsTruthy(-1));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsTruthy("0"));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsTruthy("null"));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsTruthy("undefined"));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsTruthy(1L));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsTruthy(0.1D));
|
||||||
|
Assert.assertTrue(BooleanUtil.isJsTruthy(-0.01D));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user