|
|
AIWROKÈí¼þ¶ÔÏ󹤾ߺ¯Êý¿âÀý×Ó
- /**
- * //🍎½»Á÷QQȺ711841924Ⱥһ£¬Æ»¹ûÄÚ²âȺ£¬528816639
- * ͨÓöÔÏó²Ù×÷¹¤¾ßº¯Êý¿â
- * ÊÊÓñ¾ÎĵµES5ϵͳ°²×¿ JavaScriptÒýÇæRhino
- * ÌṩʵÓõĶÔÏó²Ù×÷·½·¨£¬½â¾öʵ¼ÊÏîÄ¿Öеij£¼ûÎÊÌâ
- */
- print.log("¿ªÊ¼Ö´ÐÐÈÕÖ¾´°¿Ú²Ù×÷");
- print.log("1. ÏÔʾÈÕÖ¾´°¿Ú");
- printl( logWindow.show())
- print.log("ÈÕÖ¾´°¿ÚÏÔʾÍê³É");
- print.log("2. Çå¿ÕÈÕÖ¾´°¿Ú");
- printl(logWindow.clear())
- print.log("ÈÕÖ¾´°¿ÚÇå¿ÕÍê³É");
- print.log("3. ÉèÖÃÈÕÖ¾´°¿Ú¸ß¶ÈΪ2500");
- printl(logWindow.setHeight(2500))
- print.log("ÈÕÖ¾´°¿Ú¸ß¶ÈÉèÖÃÍê³É");
- print.log("4. ÉèÖÃÈÕÖ¾´°¿Ú¿í¶ÈΪ1000");
- printl(logWindow.setWidth(1000))
- print.log("ÈÕÖ¾´°¿Ú¿í¶ÈÉèÖÃÍê³É");
- print.log("5. ÉèÖÃÈÕÖ¾´°¿ÚΪ²»¿Éµã»÷ģʽ");
- printl(logWindow.setNoClickModel())
- print.log("ÈÕÖ¾´°¿Ú²»¿Éµã»÷ģʽÉèÖÃÍê³É");
- print.log("ËùÓÐÈÕÖ¾´°¿Ú²Ù×÷Ö´ÐÐÍê±Ï");
-
- // ºÏ²¢¶ÔÏó - ½«¶à¸ö¶ÔÏóºÏ²¢ÎªÒ»¸öжÔÏó
- function mergeObjects() {
- print.log("Ö´ÐÐmergeObjectsº¯Êý - ºÏ²¢¶à¸ö¶ÔÏó");
- var target = {};
- var i, source;
-
- for (i = 0; i < arguments.length; i++) {
- source = arguments[i];
- print.log(" - ºÏ²¢µÚ" + (i + 1) + "¸ö¶ÔÏó: " + safeStringify(source));
- if (source != null && typeof source === 'object') {
- for (var key in source) {
- if (source.hasOwnProperty(key)) {
- target[key] = source[key];
- }
- }
- }
- }
-
- print.log(" - ºÏ²¢½á¹û: " + safeStringify(target));
- print.log(" - ½á¹û: ¿½±´³É¹¦£¬Ä¿±ê¶ÔÏó±äΪ: " + safeStringify(target));
- return target;
- }
- // »ñÈ¡¶ÔÏó¼üÖµ¶Ô - ·µ»Ø[[key1, value1], [key2, value2], ...]¸ñʽÊý×é
- function getObjectEntries(obj) {
- print.log("Ö´ÐÐgetObjectEntriesº¯Êý - »ñÈ¡¶ÔÏó¼üÖµ¶Ô");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- var entries = [];
- var key;
-
- if (obj != null && typeof obj === 'object') {
- for (key in obj) {
- if (obj.hasOwnProperty(key)) {
- entries.push([key, obj[key]]);
- }
- }
- }
-
- print.log(" - ½á¹û: " + safeStringify(entries));
- return entries;
- }
- // »ñÈ¡¶ÔÏó¼üÊý×é
- function getObjectKeys(obj) {
- print.log("Ö´ÐÐgetObjectKeysº¯Êý - »ñÈ¡¶ÔÏó¼üÊý×é");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- var keys = [];
- var key;
-
- if (obj != null && typeof obj === 'object') {
- for (key in obj) {
- if (obj.hasOwnProperty(key)) {
- keys.push(key);
- }
- }
- }
-
- print.log(" - ½á¹û: " + safeStringify(keys));
- return keys;
- }
- // »ñÈ¡¶ÔÏóÖµÊý×é
- function getObjectValues(obj) {
- print.log("Ö´ÐÐgetObjectValuesº¯Êý - »ñÈ¡¶ÔÏóÖµÊý×é");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- var values = [];
- var key;
-
- if (obj != null && typeof obj === 'object') {
- for (key in obj) {
- if (obj.hasOwnProperty(key)) {
- values.push(obj[key]);
- }
- }
- }
-
- print.log(" - ½á¹û: " + safeStringify(values));
- return values;
- }
- // ¶³½á¶ÔÏó - ·ÀÖ¹¶ÔÏó±»ÐÞ¸Ä
- function freezeObject(obj) {
- print.log("Ö´ÐÐfreezeObjectº¯Êý - ¶³½á¶ÔÏó");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- if (obj != null && typeof obj === 'object') {
- var frozen = Object.freeze(obj);
- print.log(" - ½á¹û: ¶ÔÏóÒѶ³½á");
- return frozen;
- }
- print.log(" - ½á¹û: ¶ÔÏóΪnull»ò·Ç¶ÔÏóÀàÐÍ£¬Ö±½Ó·µ»Ø");
- print.log(" - ½á¹û: " + safeStringify(obj));
- return obj;
- }
- // ¼ì²é¶ÔÏóÊÇ·ñ±»¶³½á
- function isObjectFrozen(obj) {
- print.log("Ö´ÐÐisObjectFrozenº¯Êý - ¼ì²é¶ÔÏóÊÇ·ñ±»¶³½á");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- var result;
- if (obj != null && typeof obj === 'object') {
- result = Object.isFrozen(obj);
- } else {
- result = false;
- }
- print.log(" - ½á¹û: " + (result ? "¶ÔÏóÒѱ»¶³½á" : "¶ÔÏóδ±»¶³½á"));
- print.log(" - ½á¹û: " + safeStringify(result));
- print.log(" - ½á¹û: " + safeStringify(result));
- print.log(" - ½á¹û: ת»»³É¹¦");
- print.log(" - ½á¹û: ±âƽ»¯³É¹¦£¬±âƽ¶ÔÏó: " + safeStringify(result));
- return result;
- }
- // Éî¶È¿Ë¡¶ÔÏó - Ö§³ÖǶÌ×¶ÔÏóºÍÊý×é
- function deepCloneObject(obj) {
- print.log("Ö´ÐÐdeepCloneObjectº¯Êý - Éî¶È¿Ë¡¶ÔÏó");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- var clone;
- var i, key;
-
- if (obj == null || typeof obj !== 'object') {
- print.log(" - ½á¹û: ¶ÔÏóΪnull»ò·Ç¶ÔÏóÀàÐÍ£¬Ö±½Ó·µ»Ø");
- return obj;
- }
-
- // ´¦ÀíÊý×é
- if (obj instanceof Array) {
- clone = [];
- for (i = 0; i < obj.length; i++) {
- clone[i] = deepCloneObject(obj[i]);
- }
- }
- // ´¦Àí¶ÔÏó
- else {
- clone = {};
- for (key in obj) {
- if (obj.hasOwnProperty(key)) {
- clone[key] = deepCloneObject(obj[key]);
- }
- }
- }
-
- print.log(" - ½á¹û: ¿Ë¡³É¹¦");
- return clone;
- }
- // 1. ¶ÔÏóÊôÐÔ°²È«»ñÈ¡ - ±ÜÃâundefined´íÎó
- function getSafe(obj, path, defaultValue) {
- print.log("Ö´ÐÐgetSafeº¯Êý - °²È«»ñÈ¡¶ÔÏóÊôÐÔ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - ·¾¶: " + path);
-
- if (obj == null) {
- print.log(" - ½á¹û: ¶ÔÏóΪnull£¬·µ»ØÄ¬ÈÏÖµ: " + defaultValue);
- return defaultValue;
- }
-
- var keys = path.split('.');
- var current = obj;
-
- for (var i = 0; i < keys.length; i++) {
- if (current == null || !current.hasOwnProperty(keys[i])) {
- print.log(" - ½á¹û: ·¾¶²»´æÔÚ£¬·µ»ØÄ¬ÈÏÖµ: " + defaultValue);
- return defaultValue;
- }
- current = current[keys[i]];
- }
-
- print.log(" - ½á¹û: »ñÈ¡³É¹¦£¬ÖµÎª: " + safeStringify(current));
- return current;
- }
- // 2. ¶ÔÏó±È½Ï - Éî¶È±È½ÏÁ½¸ö¶ÔÏóÊÇ·ñÏàµÈ
- function isEqual(obj1, obj2) {
- print.log("Ö´ÐÐisEqualº¯Êý - Éî¶È±È½ÏÁ½¸ö¶ÔÏóÊÇ·ñÏàµÈ");
- print.log(" - ¶ÔÏó1: " + safeStringify(obj1));
- print.log(" - ¶ÔÏó2: " + safeStringify(obj2));
- if (obj1 === obj2) {
- print.log(" - ½á¹û: ¶ÔÏóÒýÓÃÏàͬ£¬Ö±½Ó·µ»Øtrue");
- print.log(" - ½á¹û: ¶ÔÏóÉî¶È±È½Ï£¬ÏàµÈ");
- return true;
- }
-
- if (obj1 == null || obj2 == null) {
- print.log(" - ½á¹û: ÆäÖÐÒ»¸ö¶ÔÏóΪnull£¬·µ»Øfalse");
- return false;
- }
-
- if (typeof obj1 !== typeof obj2) {
- print.log(" - ½á¹û: ¶ÔÏóÀàÐͲ»Í¬£¬·µ»Øfalse");
- return false;
- }
-
- if (typeof obj1 !== 'object') {
- var result = obj1 === obj2;
- print.log(" - ½á¹û: »ù´¡ÀàÐͱȽϣ¬" + (result ? "ÏàµÈ" : "²»ÏàµÈ"));
- return result;
- }
-
- if (obj1 instanceof Array && obj2 instanceof Array) {
- if (obj1.length !== obj2.length) return false;
- for (var i = 0; i < obj1.length; i++) {
- if (!isEqual(obj1[i], obj2[i])) return false;
- }
- return true;
- }
-
- if (obj1 instanceof Array || obj2 instanceof Array) return false;
-
- var keys1 = getObjectKeys(obj1);
- var keys2 = getObjectKeys(obj2);
-
- if (keys1.length !== keys2.length) return false;
-
- for (var j = 0; j < keys1.length; j++) {
- var key = keys1[j];
- if (!obj2.hasOwnProperty(key) || !isEqual(obj1[key], obj2[key])) {
- return false;
- }
- }
-
- return true;
- }
- // 3. ¶ÔÏó¹ýÂË - ±£Áô·ûºÏÌõ¼þµÄÊôÐÔ
- function filterObject(obj, predicate) {
- print.log("Ö´ÐÐfilterObjectº¯Êý - ¹ýÂ˶ÔÏóÊôÐÔ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - ¹ýÂËÌõ¼þ: " + predicate.toString());
- if (obj == null || typeof obj !== 'object' || typeof predicate !== 'function') {
- print.log(" - ½á¹û: ²ÎÊýÎÞЧ£¬·µ»Ø¿Õ¶ÔÏó");
- return {};
- }
-
- var result = {};
- for (var key in obj) {
- if (obj.hasOwnProperty(key) && predicate(obj[key], key, obj)) {
- result[key] = obj[key];
- }
- }
- return result;
- }
- // 4. ¶ÔÏóת»»ÎªMap - ÊʺÏÐèÒª¼üÖµ¶Ô²Ù×÷µÄ³¡¾°
- function objectToMap(obj) {
- print.log("Ö´ÐÐobjectToMapº¯Êý - ¶ÔÏóת»»ÎªMap");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- if (obj == null || typeof obj !== 'object') {
- print.log(" - ½á¹û: ¶ÔÏóΪnull»ò·Ç¶ÔÏóÀàÐÍ£¬·µ»Ø¿ÕMap");
- return new HashMap();
- }
-
- var map = new HashMap();
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- map.put(key, obj[key]);
- }
- }
- print.log(" - ½á¹û: ת»»³É¹¦£¬Map´óС: " + map.size());
- return map;
- }
- // 5. Mapת»»Îª¶ÔÏó
- function mapToObject(map) {
- print.log("Ö´ÐÐmapToObjectº¯Êý - Mapת»»Îª¶ÔÏó");
- print.log(" - Map: " + map.toString());
- if (map == null || typeof map !== 'object' || typeof map.entrySet !== 'function') {
- print.log(" - ½á¹û: MapÎÞЧ£¬·µ»Ø¿Õ¶ÔÏó");
- return {};
- }
-
- var obj = {};
- var entries = map.entrySet().toArray();
- for (var i = 0; i < entries.length; i++) {
- var entry = entries[i];
- obj[entry.getKey()] = entry.getValue();
- }
- return obj;
- }
- // 6. ¶ÔÏóÊôÐÔ±éÀú - ¶Ôÿ¸öÊôÐÔÖ´Ðлص÷º¯Êý
- function forEachObject(obj, callback) {
- print.log("Ö´ÐÐforEachObjectº¯Êý - ±éÀú¶ÔÏóÊôÐÔ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - »Øµ÷º¯Êý: " + callback.toString());
- if (obj == null || typeof obj !== 'object' || typeof callback !== 'function') {
- print.log(" - ½á¹û: ²ÎÊýÎÞЧ£¬Ö±½Ó·µ»Ø");
- return;
- }
-
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- print.log(" - ´¦ÀíÊôÐÔ: " + key + " = " + safeStringify(obj[key]));
- callback(obj[key], key, obj);
- }
- }
- print.log(" - ½á¹û: ±éÀúÍê³É");
- }
- // 7. ¶ÔÏóÊôÐÔת»» - ת»»¶ÔÏóµÄÊôÐÔÖµ
- function transformObject(obj, transformer) {
- print.log("Ö´ÐÐtransformObjectº¯Êý - ת»»¶ÔÏóÊôÐÔÖµ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - ת»»º¯Êý: " + transformer.toString());
- if (obj == null || typeof obj !== 'object' || typeof transformer !== 'function') {
- print.log(" - ½á¹û: ²ÎÊýÎÞЧ£¬·µ»Ø¿Õ¶ÔÏó");
- return {};
- }
-
- var result = {};
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- result[key] = transformer(obj[key], key, obj);
- }
- }
- return result;
- }
- // 8. ¼ì²é¶ÔÏóÊÇ·ñ°üº¬Ö¸¶¨ÊôÐÔ
- function hasOwnProperty(obj, key) {
- print.log("Ö´ÐÐhasOwnPropertyº¯Êý - ¼ì²é¶ÔÏóÊôÐÔÊÇ·ñ´æÔÚ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - ÊôÐÔ: " + key);
- var result;
- if (obj == null || typeof obj !== 'object') {
- result = false;
- } else {
- result = obj.hasOwnProperty(key);
- }
- print.log(" - ½á¹û: " + (result ? "´æÔÚ" : "²»´æÔÚ"));
- return result;
- }
- // 9. »ñÈ¡¶ÔÏó´óС£¨ÊôÐÔÊýÁ¿£©
- function getObjectSize(obj) {
- print.log("Ö´ÐÐgetObjectSizeº¯Êý - »ñÈ¡¶ÔÏóÊôÐÔÊýÁ¿");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- var count = 0;
- if (obj == null || typeof obj !== 'object') {
- count = 0;
- } else {
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) count++;
- }
- }
- print.log(" - ½á¹û: ÊôÐÔÊýÁ¿Îª" + count);
- return count;
- }
- // 10. ¶ÔÏóÐòÁл¯ÔöÇ¿ - ´¦ÀíÑ»·ÒýÓúÍÌØÊâÀàÐÍ
- function safeStringify(obj, space) {
- print.log("Ö´ÐÐsafeStringifyº¯Êý - ¶ÔÏóÐòÁл¯");
- try {
- var cache = [];
- var result = JSON.stringify(obj, function(key, value) {
- if (typeof value === 'object' && value !== null) {
- if (cache.indexOf(value) !== -1) {
- return '[Circular Reference]';
- }
- cache.push(value);
- }
- return value;
- }, space);
- cache = null; // ÊÍ·ÅÄÚ´æ
- return result;
- } catch (e) {
- print.log(" - ½á¹û: ÐòÁл¯Ê§°Ü£¬·µ»Ø¿Õ¶ÔÏó×Ö·û´®");
- return '{}';
- }
- }
- // 11. ¶ÔÏóÊôÐÔ¸üР- °²È«¸üжÔÏóÊôÐÔ£¬Ö§³ÖǶÌ×·¾¶
- function updateObject(obj, path, value) {
- print.log("Ö´ÐÐupdateObjectº¯Êý - ¸üжÔÏóÊôÐÔ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - ·¾¶: " + path);
- print.log(" - ÐÂÖµ: " + safeStringify(value));
- if (obj == null || typeof obj !== 'object') {
- print.log(" - ½á¹û: ¶ÔÏóΪnull»ò·Ç¶ÔÏóÀàÐÍ£¬Ö±½Ó·µ»Ø");
- return obj;
- }
-
- var keys = path.split('.');
- var current = obj;
-
- for (var i = 0; i < keys.length - 1; i++) {
- var key = keys[i];
- if (!current[key] || typeof current[key] !== 'object') {
- current[key] = {};
- }
- current = current[key];
- }
-
- current[keys[keys.length - 1]] = value;
- print.log(" - ½á¹û: ¸üгɹ¦£¬¶ÔÏó±äΪ: " + safeStringify(obj));
- return obj;
- }
- // 12. ¶ÔÏóÊôÐÔɾ³ý - °²È«É¾³ý¶ÔÏóÊôÐÔ£¬Ö§³ÖǶÌ×·¾¶
- function deleteProperty(obj, path) {
- print.log("Ö´ÐÐdeletePropertyº¯Êý - ɾ³ý¶ÔÏóÊôÐÔ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - ·¾¶: " + path);
- if (obj == null || typeof obj !== 'object') {
- print.log(" - ½á¹û: ¶ÔÏóΪnull»ò·Ç¶ÔÏóÀàÐÍ£¬Ö±½Ó·µ»Ø");
- return obj;
- }
-
- var keys = path.split('.');
- var current = obj;
-
- for (var i = 0; i < keys.length - 1; i++) {
- var key = keys[i];
- if (!current[key] || typeof current[key] !== 'object') {
- print.log(" - ½á¹û: ·¾¶²»´æÔÚ£¬Ö±½Ó·µ»ØÔ¶ÔÏó");
- return obj;
- }
- current = current[key];
- }
-
- delete current[keys[keys.length - 1]];
- print.log(" - ½á¹û: ɾ³ý³É¹¦£¬¶ÔÏó±äΪ: " + safeStringify(obj));
- return obj;
- }
- // 13. ¶ÔÏóÊôÐÔ±éÀú²¢×ª»» - Éî¶Èת»»¶ÔÏóµÄËùÓÐÊôÐÔ
- function deepTransform(obj, transformer) {
- print.log("Ö´ÐÐdeepTransformº¯Êý - Éî¶Èת»»¶ÔÏóÊôÐÔ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - ת»»º¯Êý: " + transformer.toString());
- if (obj == null || typeof obj !== 'object' || typeof transformer !== 'function') {
- print.log(" - ½á¹û: ²ÎÊýÎÞЧ£¬Ö±½Ó·µ»Ø");
- return obj;
- }
-
- if (obj instanceof Array) {
- return obj.map(function(item) {
- return deepTransform(item, transformer);
- });
- }
-
- var result = {};
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- var value = obj[key];
- if (typeof value === 'object' && value !== null) {
- result[key] = deepTransform(value, transformer);
- } else {
- result[key] = transformer(value, key, obj);
- }
- }
- }
- return result;
- }
- // 14. ¶ÔÏó±âƽ»¯ - ½«Ç¶Ì×¶ÔÏóת»»Îª±âƽ½á¹¹
- function flattenObject(obj, prefix) {
- print.log("Ö´ÐÐflattenObjectº¯Êý - ±âƽ»¯Ç¶Ì×¶ÔÏó");
- print.log(" - ǶÌ×¶ÔÏó: " + safeStringify(obj));
- if (obj == null || typeof obj !== 'object') {
- print.log(" - ½á¹û: ²ÎÊýÎÞЧ£¬Ö±½Ó·µ»Ø");
- return obj;
- }
-
- var result = {};
- prefix = prefix || '';
-
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- var fullPath = prefix ? prefix + '.' + key : key;
- var value = obj[key];
-
- if (typeof value === 'object' && value !== null && !(value instanceof Array)) {
- var flatChild = flattenObject(value, fullPath);
- for (var childKey in flatChild) {
- if (flatChild.hasOwnProperty(childKey)) {
- result[childKey] = flatChild[childKey];
- }
- }
- } else {
- result[fullPath] = value;
- }
- }
- }
- return result;
- }
- // 15. ±âƽ¶ÔÏó»¹Ô - ½«±âƽ½á¹¹×ª»»ÎªÇ¶Ì×¶ÔÏó
- function unflattenObject(flatObj) {
- print.log("Ö´ÐÐunflattenObjectº¯Êý - »¹Ô±âƽ¶ÔÏó");
- print.log(" - ±âƽ¶ÔÏó: " + safeStringify(flatObj));
- if (flatObj == null || typeof flatObj !== 'object') {
- print.log(" - ½á¹û: ²ÎÊýÎÞЧ£¬Ö±½Ó·µ»Ø");
- return flatObj;
- }
-
- var result = {};
-
- for (var key in flatObj) {
- if (flatObj.hasOwnProperty(key)) {
- print.log(" - »¹ÔÊôÐÔ: " + key + " = " + safeStringify(flatObj[key]));
- updateObject(result, key, flatObj[key]);
- }
- }
- print.log(" - ½á¹û: »¹Ô³É¹¦£¬Ç¶Ì×¶ÔÏó: " + safeStringify(result));
- return result;
- }
- // 16. ¶ÔÏóÊôÐÔ´æÔÚÐÔ¼ì²é - ¼ì²éǶÌ×ÊôÐÔÊÇ·ñ´æÔÚ
- function hasNestedProperty(obj, path) {
- print.log("Ö´ÐÐhasNestedPropertyº¯Êý - ¼ì²éǶÌ×ÊôÐÔÊÇ·ñ´æÔÚ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - ·¾¶: " + path);
- if (obj == null || typeof obj !== 'object') {
- print.log(" - ½á¹û: ¶ÔÏóΪnull»ò·Ç¶ÔÏóÀàÐÍ£¬·µ»Øfalse");
- return false;
- }
-
- var keys = path.split('.');
- var current = obj;
-
- for (var i = 0; i < keys.length; i++) {
- var key = keys[i];
- if (!current.hasOwnProperty(key)) {
- print.log(" - ½á¹û: ·¾¶" + keys.slice(0, i + 1).join('.') + "²»´æÔÚ£¬·µ»Øfalse");
- return false;
- }
- current = current[key];
- }
- print.log(" - ½á¹û: ·¾¶´æÔÚ");
- return true;
- }
- // 17. ¶ÔÏóÊôÐÔ¿½±´ - ´ÓÒ»¸ö¶ÔÏó¿½±´ÊôÐÔµ½ÁíÒ»¸ö¶ÔÏó
- function copyProperties(source, target, properties) {
- print.log("Ö´ÐÐcopyPropertiesº¯Êý - ¿½±´¶ÔÏóÊôÐÔ");
- print.log(" - Ô´¶ÔÏó: " + safeStringify(source));
- print.log(" - Ä¿±ê¶ÔÏó: " + safeStringify(target));
- print.log(" - Ö¸¶¨ÊôÐÔ: " + safeStringify(properties));
- if (source == null || typeof source !== 'object' || target == null || typeof target !== 'object') {
- print.log(" - ½á¹û: ²ÎÊýÎÞЧ£¬·µ»ØÄ¿±ê¶ÔÏó");
- return target;
- }
-
- if (!properties || !(properties instanceof Array)) {
- // ¿½±´ËùÓÐÊôÐÔ
- for (var key in source) {
- if (source.hasOwnProperty(key)) {
- target[key] = source[key];
- }
- }
- } else {
- // Ö»¿½±´Ö¸¶¨ÊôÐÔ
- for (var i = 0; i < properties.length; i++) {
- var key = properties[i];
- if (source.hasOwnProperty(key)) {
- target[key] = source[key];
- }
- }
- }
- return target;
- }
- // 18. ¶ÔÏóÊôÐÔÖØÃüÃû - ÖØÃüÃû¶ÔÏóµÄÊôÐÔ
- function renameProperties(obj, mapping) {
- print.log("Ö´ÐÐrenamePropertiesº¯Êý - ÖØÃüÃû¶ÔÏóÊôÐÔ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - Ó³Éä¹ØÏµ: " + safeStringify(mapping));
- if (obj == null || typeof obj !== 'object' || mapping == null || typeof mapping !== 'object') {
- print.log(" - ½á¹û: ²ÎÊýÎÞЧ£¬Ö±½Ó·µ»Ø");
- return obj;
- }
-
- var result = {};
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- var newKey = mapping[key] || key;
- print.log(" - ÖØÃüÃû: " + key + " -> " + newKey + " = " + safeStringify(obj[key]));
- result[newKey] = obj[key];
- }
- }
- print.log(" - ½á¹û: ÖØÃüÃû³É¹¦£¬¶ÔÏó±äΪ: " + safeStringify(result));
- return result;
- }
- // 19. ¶ÔÏóÀàÐÍת»» - ½«¶ÔÏóÊôÐÔת»»ÎªÖ¸¶¨ÀàÐÍ
- function convertPropertyTypes(obj, typeMap) {
- print.log("Ö´ÐÐconvertPropertyTypesº¯Êý - ת»»¶ÔÏóÊôÐÔÀàÐÍ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - ÀàÐÍÓ³Éä: " + safeStringify(typeMap));
- if (obj == null || typeof obj !== 'object' || typeMap == null || typeof typeMap !== 'object') {
- print.log(" - ½á¹û: ²ÎÊýÎÞЧ£¬Ö±½Ó·µ»Ø");
- return obj;
- }
-
- var result = {};
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- var value = obj[key];
- var type = typeMap[key];
-
- if (type && typeof type === 'function') {
- print.log(" - ת»»ÀàÐÍ: " + key + " = " + safeStringify(value) + " -> " + safeStringify(type(value)));
- result[key] = type(value);
- } else {
- result[key] = value;
- }
- }
- }
- print.log(" - ½á¹û: ÀàÐÍת»»³É¹¦£¬¶ÔÏó±äΪ: " + safeStringify(result));
- return result;
- }
- // 20. ¶ÔÏóÊôÐÔÑéÖ¤ - ÑéÖ¤¶ÔÏóÊôÐÔÊÇ·ñ·ûºÏÌõ¼þ
- function validateObject(obj, validations) {
- print.log("Ö´ÐÐvalidateObjectº¯Êý - ÑéÖ¤¶ÔÏóÊôÐÔ");
- print.log(" - ¶ÔÏó: " + safeStringify(obj));
- print.log(" - ÑéÖ¤¹æÔò: " + safeStringify(validations));
- if (obj == null || typeof obj !== 'object' || validations == null || typeof validations !== 'object') {
- print.log(" - ½á¹û: ²ÎÊýÎÞЧ£¬·µ»ØÑé֤ʧ°Ü");
- return {isValid: false, errors: []};
- }
-
- var errors = [];
-
- for (var key in validations) {
- if (validations.hasOwnProperty(key)) {
- var validator = validations[key];
- var value = obj[key];
-
- if (typeof validator === 'function') {
- if (!validator(value, key, obj)) {
- errors.push({key: key, message: 'ÊôÐÔÑé֤ʧ°Ü', value: value});
- }
- } else if (validator.required && !obj.hasOwnProperty(key)) {
- errors.push({key: key, message: 'ÊôÐÔ±ØÌî', value: value});
- } else if (validator.type && typeof value !== validator.type) {
- errors.push({key: key, message: 'ÊôÐÔÀàÐÍ´íÎó', value: value});
- }
- }
- }
-
- print.log(" - ½á¹û: ÑéÖ¤" + (errors.length === 0 ? "ͨ¹ý" : "ʧ°Ü"));
- if (errors.length > 0) {
- print.log(" - ´íÎóÐÅÏ¢: " + safeStringify(errors));
- }
- return {isValid: errors.length === 0, errors: errors};
- }
- // ʹÓÃʾÀý - չʾʵ¼ÊÏîÄ¿ÖеÄÓ¦Óó¡¾°
- function objectUtilsExample() {
- print.log("Ö´ÐÐobjectUtilsExampleº¯Êý - ¶ÔÏ󹤾ߺ¯ÊýʾÀý");
- printl("=== ¶ÔÏ󹤾ߺ¯ÊýʹÓÃʾÀý ===");
-
- // ʾÀý1: °²È«»ñȡǶÌ×ÊôÐÔ - ±ÜÃâundefined´íÎó
- printl("\n1. °²È«»ñȡǶÌ×ÊôÐÔ:");
- var user = {
- name: "ÕÅÈý",
- contact: {
- email: "zhangsan@example.com"
- }
- };
- var phone = getSafe(user, "contact.phone", "δÉèÖÃ");
- var email = getSafe(user, "contact.email", "δÉèÖÃ");
- printl(" Óû§µç»°: " + phone);
- printl(" Óû§ÓÊÏä: " + email);
-
- // ʾÀý2: ¶ÔÏóÉî¶È±È½Ï - ÑéÖ¤Êý¾ÝÒ»ÖÂÐÔ
- printl("\n2. ¶ÔÏóÉî¶È±È½Ï:");
- var serverData = {id: 1, name: "²úÆ·A", price: 100};
- var localData = {id: 1, name: "²úÆ·A", price: 100};
- var isSame = isEqual(serverData, localData);
- printl(" ·þÎñÆ÷Êý¾Ý: " + safeStringify(serverData));
- printl(" ±¾µØÊý¾Ý: " + safeStringify(localData));
- printl(" Êý¾ÝÊÇ·ñÒ»ÖÂ: " + (isSame ? "ÊÇ" : "·ñ"));
-
- // ʾÀý3: ¶ÔÏó¹ýÂË - »ñÈ¡ÓÐЧÊý¾Ý
- printl("\n3. ¶ÔÏó¹ýÂË:");
- var products = {
- "product1": {name: "ÊÖ»ú", price: 2000, inStock: true},
- "product2": {name: "µçÄÔ", price: 5000, inStock: false},
- "product3": {name: "ƽ°å", price: 3000, inStock: true}
- };
- var availableProducts = filterObject(products, function(product) {
- return product.inStock && product.price < 4000;
- });
- printl(" ¿â´æ³ä×ãÇÒ¼Û¸ñµÍÓÚ4000µÄ²úÆ·: " + safeStringify(availableProducts));
-
- // ʾÀý4: ¶ÔÏóÊôÐÔ¸üР- ¶¯Ì¬¸üÐÂÅäÖÃ
- printl("\n4. ¶ÔÏóÊôÐÔ¸üÐÂ:");
- var config = {
- server: {
- host: "localhost",
- port: 8080
- },
- timeout: 3000
- };
- updateObject(config, "server.port", 9090);
- updateObject(config, "server.ssl", true);
- printl(" ¸üкóµÄÅäÖÃ: " + safeStringify(config));
-
- // ʾÀý5: ¶ÔÏó±âƽ»¯ - ±ãÓÚ´æ´¢ºÍ´«Êä
- printl("\n5. ¶ÔÏó±âƽ»¯:");
- var nestedData = {
- user: {
- info: {
- name: "ÀîËÄ",
- age: 25
- },
- contact: {
- email: "lisi@example.com",
- phone: "13800138000"
- }
- }
- };
- var flatData = flattenObject(nestedData);
- printl(" ±âƽ»¯Êý¾Ý: " + safeStringify(flatData));
-
- // ʾÀý6: ¶ÔÏóÑéÖ¤ - È·±£Êý¾ÝÍêÕûÐÔ
- printl("\n6. ¶ÔÏóÑéÖ¤:");
- var formData = {
- username: "testuser",
- password: "123",
- email: "invalid-email"
- };
- var validationRules = {
- username: {required: true, type: "string"},
- password: function(value) {
- return value.length >= 6;
- },
- email: function(value) {
- return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
- }
- };
- var validationResult = validateObject(formData, validationRules);
- printl(" ÑéÖ¤½á¹û: " + (validationResult.isValid ? "ͨ¹ý" : "ʧ°Ü"));
- if (!validationResult.isValid) {
- printl(" ´íÎóÐÅÏ¢: " + safeStringify(validationResult.errors));
- }
-
- // ʾÀý7: ¶ÔÏóÀàÐÍת»» - ͳһÊý¾Ý¸ñʽ
- printl("\n7. ¶ÔÏóÀàÐÍת»»:");
- var apiData = {
- id: "123",
- price: "99.99",
- quantity: "10",
- isActive: "true"
- };
- var typeConversions = {
- id: parseInt,
- price: parseFloat,
- quantity: parseInt,
- isActive: function(value) { return value === "true"; }
- };
- var convertedData = convertPropertyTypes(apiData, typeConversions);
- printl(" ת»»Ç°: " + safeStringify(apiData));
- printl(" ת»»ºó: " + safeStringify(convertedData));
- printl(" ¼Û¸ñÀàÐÍ: " + (typeof convertedData.price));
- printl(" ÊýÁ¿ÀàÐÍ: " + (typeof convertedData.quantity));
- printl(" ÊÇ·ñ¼¤»î: " + convertedData.isActive);
- }
- // ¿ÉÒÔͨ¹ýµ÷ÓÃÕâ¸öº¯ÊýÀ´ÔËÐÐʾÀý
- print.log("×¼±¸Ö´ÐÐʾÀýº¯Êý");
- objectUtilsExample();
- print.log("ʾÀýº¯ÊýÖ´ÐÐÍê³É");
- // ʵ¼ÊÏîĿӦÓÃʾÀý - Ä£ÄâAIWROKÏîÄ¿ÖеÄÊý¾Ý´¦Àí³¡¾°
- function aiwrokProjectExample() {
- print.log("Ö´ÐÐaiwrokProjectExampleº¯Êý - AIWROKÏîĿӦÓÃʾÀý");
- printl("\n=== AIWROKÏîĿʵ¼ÊÓ¦ÓÃʾÀý ===");
-
- // Ä£Äâ´Ó·þÎñÆ÷»ñÈ¡µÄÊý¾Ý
- var serverResponse = {
- "code": 200,
- "message": "success",
- "data": {
- "userInfo": {
- "userId": "user_123456",
- "userName": "AIWROKÓû§",
- "userType": "normal",
- "createTime": "2023-12-25T10:30:00Z",
- "preferences": {
- "theme": "dark",
- "language": "zh-CN",
- "notifications": true
- }
- },
- "deviceList": [
- {
- "deviceId": "device_789",
- "deviceName": "²âÊÔÉ豸",
- "status": "online",
- "battery": "85",
- "temperature": "25.5"
- }
- ]
- }
- };
-
- // 1. °²È«»ñÈ¡Óû§ÐÅÏ¢
- var userInfo = getSafe(serverResponse, "data.userInfo", {});
- var userName = getSafe(userInfo, "userName", "δ֪Óû§");
- var userTheme = getSafe(userInfo, "preferences.theme", "light");
- printl("1. Óû§ÐÅÏ¢»ñÈ¡:");
- printl(" Óû§Ãû: " + userName);
- printl(" Ö÷ÌâÉèÖÃ: " + userTheme);
-
- // 2. ´¦ÀíÉ豸Êý¾Ý
- var deviceList = getSafe(serverResponse, "data.deviceList", []);
- if (deviceList.length > 0) {
- var firstDevice = deviceList[0];
-
- // ת»»É豸ÊôÐÔÀàÐÍ
- var deviceData = convertPropertyTypes(firstDevice, {
- battery: parseInt,
- temperature: parseFloat
- });
-
- printl("\n2. É豸Êý¾Ý´¦Àí:");
- printl(" É豸Ãû³Æ: " + deviceData.deviceName);
- printl(" É豸״̬: " + deviceData.status);
- printl(" µç³ØµçÁ¿: " + deviceData.battery + "%");
- printl(" É豸ζÈ: " + deviceData.temperature + "¡ãC");
-
- // ¼ì²éÉ豸״̬
- if (deviceData.battery < 20) {
- printl(" ¾¯¸æ: µç³ØµçÁ¿µÍ£¬ÐèÒª³äµç£¡");
- }
- if (deviceData.temperature > 30) {
- printl(" ¾¯¸æ: É豸ζȹý¸ß£¡");
- }
- }
-
- // 3. ×¼±¸·¢Ë͵½·þÎñÆ÷µÄÇëÇóÊý¾Ý
- var requestData = {
- userId: userInfo.userId,
- action: "update_preferences",
- timestamp: new Date().getTime(),
- data: {
- theme: "light",
- notifications: false
- }
- };
-
- // ÑéÖ¤ÇëÇóÊý¾Ý
- var requestValidation = validateObject(requestData, {
- userId: {required: true, type: "string"},
- action: {required: true, type: "string"},
- timestamp: function(value) {
- return typeof value === "number" && value > 0;
- }
- });
-
- printl("\n3. ÇëÇóÊý¾ÝÑéÖ¤:");
- printl(" ÑéÖ¤½á¹û: " + (requestValidation.isValid ? "ͨ¹ý" : "ʧ°Ü"));
- if (requestValidation.isValid) {
- printl(" ÇëÇóÊý¾Ý: " + safeStringify(requestData));
- // ÔÚʵ¼ÊÏîÄ¿ÖУ¬ÕâÀï»áµ÷ÓÃokHttpµÈÍøÂç¿â·¢ËÍÇëÇó
- // printl(" ÕýÔÚ·¢ËÍÇëÇó...");
- }
- }
- // ÔËÐÐAIWROKÏîĿӦÓÃʾÀý
- aiwrokProjectExample();
¸´ÖÆ´úÂë
|
|