Changeset 116
- Timestamp:
- 02/23/07 15:17:18 (6 years ago)
- Location:
- jala/trunk
- Files:
-
- 2 added
- 3 modified
-
code/HopObject.js (added)
-
code/Utilities.js (modified) (1 diff)
-
code/all.js (modified) (1 diff)
-
tests/HopObject.js (added)
-
tests/Utilities.js (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
jala/trunk/code/Utilities.js
r99 r116 72 72 */ 73 73 jala.util = new jala.Utilities(); 74 75 /**76 * Constructs a name from an object which77 * is unique in the underlying HopObject collection.78 * @param {Object} obj The object representing or containing the alias name.79 * Possible object types include:80 * <ul>81 * <li>File</li>82 * <li>helma.File</li>83 * <li>java.io.File</li>84 * <li>String</li>85 * <li>java.lang.String</li>86 * </ul>87 * @param {HopObject} collection The collection the alias88 * potentially should be added to89 * @param {Number} maxLength The maximum length of the alias90 * @returns The resulting alias91 * @type String92 */93 jala.Utilities.prototype.getAlias = function(obj, collection, maxLength) {94 var name;95 var clazz = obj.constructor || obj.getClass();96 switch (clazz) {97 case File:98 case helma.File:99 case java.io.File:100 case Packages.helma.util.MimePart:101 // first fix bloody ie/win file paths containing backslashes102 var rawName = obj.getName().replace(/\\/g, "/");103 rawName = rawName.split("/");104 name = rawName.pop();105 if (name.contains("."))106 name = name.substring(0, name.lastIndexOf("."));107 break;108 109 case String:110 case java.lang.String:111 name = obj;112 break;113 114 default:115 name = obj.toString();116 }117 118 // convert alias to lowercase and clean it from any invalid characters119 var alias = name.toLowerCase().toFileName();120 var len = alias.length;121 var counter = 0;122 123 var getAlias = function() {124 var overflow;125 if (maxLength) {126 if (counter) {127 len += counter.toString().length;128 }129 if ((overflow = len - maxLength) > 0) {130 alias = alias.substring(0, alias.length - overflow);131 len = alias.length;132 }133 }134 return (counter ? alias + counter.toString() : alias);135 }136 137 if (collection) {138 var alias;139 do {140 alias = getAlias();141 counter += 1;142 } while (collection[alias] || collection.get(alias));143 return alias;144 }145 return getAlias();146 };147 74 148 75 /** -
jala/trunk/code/all.js
r115 r116 42 42 "History", 43 43 "HtmlDocument", 44 "HopObject", 44 45 "i18n", 45 46 "ImageFilter", -
jala/trunk/tests/Utilities.js
r114 r116 30 30 var tests = [ 31 31 "testCreatePassword", 32 "testGetAlias",33 32 "testDiffObjects", 34 33 "testPatchObject" … … 47 46 assertEqual(jala.util.createPassword(null, 2).length, 8); 48 47 assertEqual(jala.util.createPassword(100, 2).length, 100); 49 return;50 };51 52 /**53 * Unit test for #jala.util.getAlias.54 */55 var testGetAlias = function() {56 var name = "foobar";57 assertEqual(jala.util.getAlias(name), name);58 assertEqual(jala.util.getAlias(name, null, 3), name.substr(0, 3));59 60 var collection = new HopObject;61 assertEqual(jala.util.getAlias(name, collection), name);62 // Test alias with the same name as a default HopObject method63 assertNotEqual(jala.util.getAlias("get", collection), "get");64 assertEqual(jala.util.getAlias("get", collection), "get1");65 // Set a custom property of the collection and test it66 collection[name] = true;67 assertNotEqual(jala.util.getAlias(name, collection), name);68 assertEqual(jala.util.getAlias(name, collection), name + "1");69 70 // Set custom properties equally to the method's numbering71 collection[name + "1"] = true;72 collection[name + "12"] = true;73 assertNotEqual(jala.util.getAlias(name, collection), name + "1");74 assertNotEqual(jala.util.getAlias(name, collection), name + "12");75 assertEqual(jala.util.getAlias(name, collection), name + "123");76 77 assertNotEqual(jala.util.getAlias(name, collection, name.length), name);78 assertEqual(jala.util.getAlias(name, collection, name.length), "fooba1");79 48 return; 80 49 };
![(please configure the [header_logo] section in trac.ini)](/jala/chrome/common/trac_banner.png)