[feature] 新增BooleanUtil.isJsFalsy以及isJsTruthy

This commit is contained in:
VampireAchao 2023-03-13 18:23:14 +08:00
parent 5b5209e801
commit 4db9c44d18
2 changed files with 53 additions and 1 deletions

View File

@ -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)
* 所有除 false0-00n""nullundefined 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);
}
} }

View File

@ -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));
}
} }