Revision: 4697 http://sourceforge.net/p/vexi/code/4697 Author: mkpg2 Date: 2014-05-15 18:02:33 +0000 (Thu, 15 May 2014) Log Message: ----------- New method vexi.js.equals(). Implements element-wise equals.
Modified Paths: -------------- branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Methods.java branches/vexi3/org.vexi-library.js/src/main/jpp/org/vexi/js/VexiJS.jpp branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/TestObject.java Added Paths: ----------- branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/equals.js Modified: branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Methods.java =================================================================== --- branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Methods.java 2014-05-10 23:08:29 UTC (rev 4696) +++ branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/Methods.java 2014-05-15 18:02:33 UTC (rev 4697) @@ -9,6 +9,7 @@ import java.net.URLDecoder; import java.net.URLEncoder; +import org.ibex.js.JS.Keys; import org.ibex.js.parse.Function; /** @@ -128,6 +129,29 @@ return NC_NaN; } + static public boolean equalElements(JS a, JS b) throws JSExn{ + Keys ka = a.keys(); + Keys kb = b.keys(); + if(ka.size()!=kb.size()) return false; + JS.Enumeration ea = a.keys().iterator(); + while(ea.hasNext()){ + JS key = ea.next(); + JS ca = a.get(key); + JS cb = b.get(key); + if(!equals(ca, cb)) return false; + } + return true; + } + + static public boolean equals(JS a, JS b) throws JSExn{ + if(a==null && b==null) return true; + if(a==null || b==null) return false; + + if((a instanceof JS.Obj) && (b instanceof JS.Obj)) return equalElements(a, b); + if((a instanceof JSArray) && (b instanceof JSArray)) return equalElements(a, b); + return a.equals(b); + } + /** * Evaluates a String as JS code using an optional scope * Modified: branches/vexi3/org.vexi-library.js/src/main/jpp/org/vexi/js/VexiJS.jpp =================================================================== --- branches/vexi3/org.vexi-library.js/src/main/jpp/org/vexi/js/VexiJS.jpp 2014-05-10 23:08:29 UTC (rev 4696) +++ branches/vexi3/org.vexi-library.js/src/main/jpp/org/vexi/js/VexiJS.jpp 2014-05-15 18:02:33 UTC (rev 4697) @@ -580,6 +580,9 @@ * */ case "copy": return METHOD; + case "equals": return METHOD; + + /* <p>Evaluates the given string as JS.</p> * * <p><b>Hint:</b> if you want the evaluated JS to return its result then @@ -618,10 +621,6 @@ return super.get(key); } public JS callMethod(JS this_, JS method, JS[] args) throws JSExn { - //#switch(JSU.toString(method)) - case "eval": return Methods.eval(args); - //#end - switch(args.length) { case 1: { //#switch(JSU.toString(method)) @@ -632,7 +631,15 @@ case "copy": return JSU.copy(args[0]); //#end } + case 2: { + //#switch(JSU.toString(method)) + case "equals": return JSU.B(Methods.equals(args[0], args[1])); + //#end } + } + //#switch(JSU.toString(method)) + case "eval": return Methods.eval(args); + //#end return super.callMethod(this_, method, args); } }; Modified: branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/TestObject.java =================================================================== --- branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/TestObject.java 2014-05-10 23:08:29 UTC (rev 4696) +++ branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/TestObject.java 2014-05-15 18:02:33 UTC (rev 4697) @@ -15,7 +15,7 @@ public static void main(String[] args) throws Throwable { JSTestSuite jts = new JSTestSuite(TestObject.class); - TestCase t = jts.createTestCase(jts.getResourceDirs(), "copy.js"); + TestCase t = jts.createTestCase(jts.getResourceDirs(), "equals.js"); t.runBare(); } } Added: branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/equals.js =================================================================== --- branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/equals.js (rev 0) +++ branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/equals.js 2014-05-15 18:02:33 UTC (rev 4697) @@ -0,0 +1,9 @@ + +var o0 = { key:123, foo:"bar", arr: [1, 8, {a:1}] }; +var o1 = sys.js.deepcopy(o0); +assert(o0 != o1); +assert(sys.js.equals(o0,o1)); + +o1["arr"][2]["b"] = true; +trace(sys.js.equals(o0,o1)); +assert(!sys.js.equals(o0,o1)); Property changes on: branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/equals.js ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ "Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE Instantly run your Selenium tests across 300+ browser/OS combos. Get unparalleled scalability from the best Selenium testing platform available Simple to use. Nothing to install. Get started now for free." http://p.sf.net/sfu/SauceLabs _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn