1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2024-09-28 14:30:51 +00:00

Scribunto: Use PHP.empty for noCommafy [#372]

This commit is contained in:
gnosygnu 2019-03-02 08:58:28 -05:00
parent 3fd759b020
commit 3488084eca
4 changed files with 83 additions and 6 deletions

View File

@ -15,9 +15,42 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
public class XophpUtility {
// REF.PHP:http://php.net/manual/en/function.empty.php
public static boolean empty(String v) {return v == null || String_.Len_eq_0(v);}
public static boolean empty(byte[] v) {return v == null || v.length == 0;}
public static boolean empty(boolean v) {return v == false;}
public static boolean empty(int v) {return v == 0;}
public static boolean empty(double v) {return v == 0;}
public static boolean empty_obj(Object v) {
if (v == null)
return true;
Class<?> type = Type_.Type_by_obj(v);
if (Type_.Eq(type, Bool_.Cls_ref_type)) {
if (!Bool_.Cast(v))
return true;
}
else if (Type_.Eq(type, Int_.Cls_ref_type)) {
if (Int_.Cast(v) == 0)
return true;
}
else if (Type_.Eq(type, Double_.Cls_ref_type)) {
if (Double_.cast(v) == 0)
return true;
}
else if (Type_.Eq(type, String_.Cls_ref_type)) {
String s = String_.cast(v);
if (String_.Len_eq_0(s) || String_.Eq(s, "0"))
return true;
}
else {
if (Type_.Is_array(type)) {
if (Array_.Len(Array_.cast(v)) == 0)
return true;
}
}
return false;
}
public static boolean isset(byte[] v) {return v != null;}
public static boolean isset(int v) {return v != NULL_INT;}
public static boolean isset(double v) {return v != NULL_DOUBLE;}

View File

@ -0,0 +1,44 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
import org.junit.*; import gplx.core.tests.*;
public class XophpUtilityTest {
private final XophpUtilityFxt fxt = new XophpUtilityFxt();
@Test public void Empty_obj() {
fxt.Test_empty_obj_y(""); // "" (an empty String)
fxt.Test_empty_obj_y(0); // 0 (0 as an integer)
fxt.Test_empty_obj_y(0.0d); // 0.0 (0 as a float)
fxt.Test_empty_obj_y("0"); // "0" (0 as a String)
fxt.Test_empty_obj_y(null); // NULL
fxt.Test_empty_obj_y(false); // FALSE
fxt.Test_empty_obj_y(new int[0]); // array() (an empty array)
fxt.Test_empty_obj_n(Bool_.Y);
fxt.Test_empty_obj_n("a");
fxt.Test_empty_obj_n(9);
fxt.Test_empty_obj_n(0.8d);
fxt.Test_empty_obj_n("7");
fxt.Test_empty_obj_n(true);
fxt.Test_empty_obj_n(new int[1]);
}
}
class XophpUtilityFxt {
public void Test_empty_obj_n(Object o) {Test_empty_obj(Bool_.N, o);}
public void Test_empty_obj_y(Object o) {Test_empty_obj(Bool_.Y, o);}
public void Test_empty_obj(boolean expd, Object o) {
Gftest.Eq__bool(expd, XophpUtility.empty_obj(o), Object_.Xto_str_strict_or_empty(o));
}
}

View File

@ -15,6 +15,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.xtns.scribunto.libs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*;
import gplx.xowa.langs.*;
import gplx.xowa.mediawiki.*;
import gplx.xowa.xtns.pfuncs.times.*; import gplx.xowa.langs.numbers.*; import gplx.xowa.xtns.pfuncs.numbers.*; import gplx.xowa.langs.durations.*;
import gplx.xowa.xtns.scribunto.procs.*;
public class Scrib_lib_language implements Scrib_lib {
@ -182,9 +183,7 @@ public class Scrib_lib_language implements Scrib_lib {
if (num != null) { // MW: if num present, check options table for noCommafy arg;
Keyval[] kv_ary = args.Cast_kv_ary_or_null(2);
if (kv_ary != null) {
Object skip_commafy_obj = Keyval_.Ary_get_by_key_or_null(kv_ary, "noCommafy");
if (skip_commafy_obj != null)
skip_commafy = Bool_.Cast(skip_commafy_obj);
skip_commafy = !XophpUtility.empty_obj(Keyval_.Ary_get_by_key_or_null(kv_ary, "noCommafy"));
}
}
byte[] rv = lang.Num_mgr().Format_num(num, skip_commafy);

View File

@ -91,6 +91,7 @@ public class Scrib_lib_language_tst {
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_formatNum, Object_.Ary("en", "1234"), "1,234"); // String passed (not int)
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_formatNum, Object_.Ary("en", "1234", Keyval_.Ary(Keyval_.new_("noCommafy", true))) , "1234"); // noCommafy.y
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_formatNum, Object_.Ary("en", "1234", Keyval_.Ary(Keyval_.new_("noCommafy", false))) , "1,234"); // noCommafy.n
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_formatNum, Object_.Ary("en", "1234", Keyval_.Ary(Keyval_.new_("noCommafy", "y"))) , "1234"); // noCommafy."y"; ISSUE#:372 DATE:2019-03-02
}
@Test public void FormatDate() {
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_formatDate, Object_.Ary("en", "Y-m-d", "2013-03-17", false), "2013-03-17");