/*! * Hyundai U&I JavaScript Library v3.0.0 * http://www.hyundai-uni.com/ * * Copyright 2007-2015 Hyundai Ubiquitous & Information Technology CO.LTD. * * 시스템에 공통으로 사용하는 유틸리티 함수를 정의한 js * * @author 권중규 */ var ArrayUtils = { contains: function(array, valueToFind) { return ArrayUtils.indexOf(array, valueToFind) != -1; }, indexOf: function(array, valueToFind, startIndex) { if(ArrayUtils.isEmpty(array)) { return -1; } startIndex = startIndex != null ? startIndex : 0; for(var i = startIndex; i < array.length; i++) { if(valueToFind == array[i]) { return i; } } return -1; }, isEmpty: function(array) { return ArrayUtils.getLength(array) == 0; }, isNotEmpty: function(array) { return !ArrayUtils.isEmpty(array); }, getLength: function(array) { if(jQuery.isArray(array)) { return array.length; } return 0; } }; var DateUtils = { SECOND_IN_MILLISECONDS: 1000, MINUTE_IN_MILLISECONDS: 60 * 1000, HOUR_IN_MILLISECONDS: 60 * 60 * 1000, DAY_IN_MILLISECONDS: 24 * 60 * 60 * 1000, MONTH_IN_MILLISECONDS: 30 * 24 * 60 * 60 * 1000, YEAR_IN_MILLISECONDS: 12 * 30 * 24 * 60 * 60 * 1000, addTime: function(date, amount) { if(date instanceof Date) { return new Date(date.getTime() + amount); } return date; }, addYears: function(date, amount) { if(date instanceof Date) { return new Date(new Date(date).setFullYear(date.getFullYear() + amount)); } return date; }, addMonths: function(date, amount) { if(date instanceof Date) { return new Date(new Date(date).setMonth(date.getMonth() + amount)); } return date; }, addDays: function(date, amount) { if(date instanceof Date) { return new Date(new Date(date).setDate(date.getDate() + amount)); } return date; }, addHours: function(date, amount) { if(date instanceof Date) { return new Date(new Date(date).setHours(date.getHours() + amount)); } return date; }, addMinutes: function(date, amount) { if(date instanceof Date) { return new Date(new Date(date).setMinutes(date.getMinutes() + amount)); } return date; }, addSeconds: function(date, amount) { if(date instanceof Date) { return new Date(new Date(date).setSeconds(date.getSeconds() + amount)); } return date; }, addMilliseconds: function(date, amount) { return DateUtils.addTime(date, amount); }, parseDate: function(str, pattern, defaultDate) { return defaultDate; } }; var NumberUtils = { toString: function(number, length) { var numberString = (number || 0).toString(); var pads = (length || 0) - numberString.length; var padString = ""; if(pads > 0) { for(var i = 0; i < pads; i++) { padString += "0"; } } return padString + numberString; } }; var RandomStringUtils = { randomAlphanumeric: function(count) { return RandomStringUtils.random(count, 0, 0, true, true); }, random: function(count, start, end, letters, numbers, chars) { if(count == 0) { return ""; } else if(count < 0) { throw new Error("Requested random string length " + count + " is less than 0."); } if(chars && chars.length == 0) { throw new Error("The chars array must not be empty"); } if(start == 0 && end == 0) { if(chars) { end = chars.length; } else { if(!letters && !numbers) { end = Number.MAX_VALUE; } else { end = "z".charCodeAt(0) + 1; start = " ".charCodeAt(0); } } } else { if(end <= start) { throw new Error("Parameter end (" + end + ") must be greater than start (" + start + ")"); } } var buffer = new Array(count); var gap = end - start; while(count-- != 0) { var ch = null; if(chars == null) { ch = (Math.floor((Math.random() * gap) + 1) + start); } else { ch = chars[Math.floor((Math.random() * gap) + 1) + start]; } var str = String.fromCharCode(ch); if(letters && str.match(/[a-zA-Z]/) || numbers && str.match(/\d/) || !letters && !numbers) { if(ch >= 56320 && ch <= 57343) { if(count == 0) { count++; } else { // low surrogate, insert high surrogate after putting it in buffer[count] = str; count--; buffer[count] = (55296 + Math.floor((Math.random() * 128) + 1)); } } else if(ch >= 55296 && ch <= 56191) { if(count == 0) { count++; } else { // high surrogate, insert low surrogate before putting it in buffer[count] = (56320 + Math.floor((Math.random() * 128) + 1)); count--; buffer[count] = str; } } else if(ch >= 56192 && ch <= 56319) { // private high surrogate, no effing clue, so skip it count++; } else { buffer[count] = str; } } else { count++; } } return buffer.join(""); } }; var ObjectUtils = { isArray: function(object) { return jQuery.isArray(object); }, isString: function(object) { return typeof object === "string"; }, isFunction: function(object) { return jQuery.isFunction(object); }, isEmptyObject: function(object) { return jQuery.isEmptyObject(object); }, isPlainObject: function(object) { return jQuery.isPlainObject(object); }, setValue: function(object, name, value) { if(object && name in object == false) { object[name] = value; } }, transform: function(object, handler) { if(jQuery.isPlainObject(object) || jQuery.isArray(object)) { for( var key in object) { var value = object[key]; if(jQuery.isFunction(handler)) { handler(object, key, value); } ObjectUtils.transform(value, handler); } } }, find: function(object, handler) { if(jQuery.isPlainObject(object) || jQuery.isArray(object)) { for( var key in object) { var value = object[key]; if(jQuery.isFunction(handler) && handler(object, key, value)) { return value; } var result = ObjectUtils.find(value, handler); if(result != null) { return result; } } } return null; }, clone: function(object) { if(object != null) { // return jQuery.extend({}, object); return JSON.parse(JSON.stringify(object)); } }, toString: function(object) { return object == null ? "" : object.toString(); } }; var StringUtils = { startsWith: function(str, prefix, offset) { if(!str || !prefix) { return !str && !prefix; } if(prefix.length > str.length) { return false; } var to = (offset || 0); var po = 0; var pc = prefix.length; if((to < 0) || (to > str.length - pc)) { return false; } while(--pc >= 0) { if(str.charAt(to++) != prefix.charAt(po++)) { return false; } } return true; }, endsWith: function(str, suffix) { if(!str || !suffix) { return !str && !suffix; } if(suffix.length > str.length) { return false; } return StringUtils.startsWith(str, suffix, str.length - suffix.length); }, isEmpty: function(object) { if(object == null || object == "") { return true; } return false; }, isNotEmpty: function(object) { return !StringUtils.isEmpty(object); }, defaultIfEmpty: function(str, defaultStr) { return !StringUtils.isEmpty(str) ? str : (defaultStr || ""); }, removeStart: function(str, remove) { if(StringUtils.isEmpty(str) || StringUtils.isEmpty(remove)) { return str; } if(StringUtils.startsWith(str, remove)) { return str.substring(remove.length); } return str; }, removeEnd: function(str, remove) { if(StringUtils.isEmpty(str) || StringUtils.isEmpty(remove)) { return str; } if(StringUtils.endsWith(str, remove)) { return str.substring(0, str.length - remove.length); } return str; }, strip: function(str, stripChars) { if(StringUtils.isEmpty(str)) { return str; } str = StringUtils.stripStart(str, stripChars); return StringUtils.stripEnd(str, stripChars); }, stripStart: function(str, stripChars) { var strLen = 0; if(str == null || (strLen = str.length) == 0) { return str; } var start = 0; if(!stripChars) { while(start != strLen && (str.charAt(start) == "")) { start++; } } else if(stripChars.length == 0) { return str; } else { while(start != strLen && stripChars.indexOf(str.charAt(start)) != -1) { start++; } } return str.substring(start); }, stripEnd: function(str, stripChars) { var end = 0; if(str == null || (end = str.length) == 0) { return str; } if(!stripChars) { while(end != 0 && (str.charAt(end - 1) == "")) { end--; } } else if(stripChars.length == 0) { return str; } else { while(end != 0 && stripChars.indexOf(str.charAt(end - 1)) != -1) { end--; } } return str.substring(0, end); }, substringBefore: function(str, separator) { if(StringUtils.isEmpty(str) || separator == null) { return str; } if(StringUtils.isEmpty(separator)) { return ""; } var pos = str.indexOf(separator); if(pos == -1) { return str; } return str.substring(0, pos); }, substringAfter: function(str, separator) { if(StringUtils.isEmpty(str)) { return str; } if(separator == null) { return ""; } var pos = str.indexOf(separator); if(pos == -1) { return ""; } return str.substring(pos + separator.length); }, substringBeforeLast: function(str, separator) { if(StringUtils.isEmpty(str) || StringUtils.isEmpty(separator)) { return str; } var pos = str.lastIndexOf(separator); if(pos == -1) { return str; } return str.substring(0, pos); }, substringAfterLast: function(str, separator) { if(StringUtils.isEmpty(str)) { return str; } if(StringUtils.isEmpty(separator)) { return ""; } var pos = str.lastIndexOf(separator); if(pos == -1 || pos == str.length - separator.length) { return ""; } return str.substring(pos + separator.length); }, substringBetween: function(str, open, close) { if(str == null || open == null || close == null) { return null; } var start = str.indexOf(open); if(start != -1) { var end = str.indexOf(close, start + open.length); if(end != -1) { return str.substring(start + open.length, end); } } return null; } }; var StringEscapeUtils = { unescapeHtml: function(html) { var text = document.createElement("textarea"); text.innerHTML = html; return text.value; } }; var UUID = { randomUUID: function() { var id = ""; for(var i = 0; i < 32; i++) { var random = Math.random() * 16 | 0; if(i == 8 || i == 12 || i == 16 || i == 20) { id += "-"; } id += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16); } return id; } };