Commits

Kamyar Inanloo committed 4687dbd

Made the format.js neat and removed code inspector warnings.

Comments (0)

Files changed (10)

.idea/encodings.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
+</project>
+

.idea/format.js.iml

+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>
+

.idea/jsLibraryMappings.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="JavaScriptLibraryMappings">
+    <excludedPredefinedLibrary name="HTML" />
+    <excludedPredefinedLibrary name="HTML5 / EcmaScript 5" />
+  </component>
+</project>
+
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectRootManager" version="2" />
+</project>
+

.idea/modules.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/format.js.iml" filepath="$PROJECT_DIR$/.idea/format.js.iml" />
+    </modules>
+  </component>
+</project>
+

.idea/scopes/scope_settings.xml

+<component name="DependencyValidationManager">
+  <state>
+    <option name="SKIP_IMPORT_STATEMENTS" value="false" />
+  </state>
+</component>
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="hg4idea" />
+  </component>
+</project>
+
-var cultures = new Object();
+var cultures = {};
 
 cultures.invariant = {
-numbers : '0123456789',
-decimal : '.',
-firstDay : 'Monday',
-currency : '$',
-weekDays : ['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thurseday', 'Friday'],
-dayParts : ['AM', 'PM'],
-monthDays : [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
-directtion: 'ltr',
-defaultDateFormat : 'MM/dd/yy hh:mm:ss'
+    numbers: '0123456789',
+    decimal: '.',
+    firstDay: 'Monday',
+    currency: '$',
+    weekDays: ['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
+    dayParts: ['AM', 'PM'],
+    monthDays: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
+    direction: 'ltr',
+    defaultDateFormat: 'MM/dd/yy hh:mm:ss'
+};
+
+cultures['fa-IR'] = {
+    numbers: '۰۱۲۳۴۵۶۷۸۹',
+    months: ['فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'],
+    decimal: '/',
+    firstDay: 'Saturday',
+    currency: 'ریال',
+    weekDays: ['شنبه', 'یک شنبه', 'دوشنبه', 'سه شنبه', 'چهار شنبه', 'پنج شنبه', 'آدینه'],
+    dayParts: ['ق.ظ', 'ب.ظ'],
+    monthDays: [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29],
+    direction: 'rtl',
+    defaultDateFormat: 'yy/MM/dd ساعت hh:mm:ss'
+};
+
+function GetWeekDayName(code, cultureName) {
+    return cultures[cultureName].weekDays[code];
 }
 
-cultures['fa-IR'] = {
-numbers : '۰۱۲۳۴۵۶۷۸۹',
-months : ['فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'],
-decimal : '/',
-firstDay : 'Saturday',
-currency : 'ریال',
-weekDays : ['شنبه', 'یک شنبه', 'دوشنبه', 'سه شنبه', 'چهار شنبه', 'پنج شنبه', 'آدینه'],
-dayParts : ['ق.ظ','ب.ظ'],
-monthDays : [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29],
-direction: 'rtl',
-defaultDateFormat : 'yy/MM/dd ساعت hh:mm:ss'
-}
 
-function GetWeekDayName(code, cultureName) {
-	return cultures[cultureName].weekDays[code];
-}
+cultures['fa-IR'].fromDate = function (date) {
+    var result = {};
+    var invDayNo, calDayNo;
+    var j_np;
+    var i;
 
+    var invDate = {
+        year: date.getFullYear() - 1600,
+        month: date.getMonth(),
+        day: date.getDate() - 1
+    };
 
-cultures['fa-IR'].fromDate = function(date) {
-	var result = new Object();
-	var invDayNo, calDayNo;
-	var j_np;
+    invDayNo = 365 * invDate.year + Math.floor((invDate.year + 3) / 4) - Math.floor((invDate.year + 99) / 100) + Math.floor((invDate.year + 399) / 400);
 
-	var invDate = {
-		year: date.getFullYear() - 1600,
-		month: date.getMonth(),
-		day: date.getDate() - 1
-	}
-	
-   invDayNo = 365 * invDate.year + Math.floor((invDate.year + 3) / 4) - Math.floor((invDate.year + 99) / 100) + Math.floor((invDate.year + 399) / 400);
-   
-   for (var i = 0; i < invDate.month; ++i)
-	  invDayNo += cultures.invariant.monthDays[i];
-   if (invDate.month > 1 && ((invDate.year % 4 == 0 && invDate.year % 100 != 0) || (invDate.year % 400 == 0)))
-	  /* leap and after Feb */
-	  invDayNo++;
-   invDayNo += invDate.day;   
-	
-   calDayNo = invDayNo - 79;
- 
-   j_np = Math.floor(calDayNo / 12053);
-   calDayNo %= 12053;
- 
-   result.year = 979 + 33 * j_np + 4 * Math.floor(calDayNo / 1461);
-   calDayNo %= 1461;
- 
-   if (calDayNo >= 366) {
-	  result.year += Math.floor((calDayNo - 1) / 365);
-	  calDayNo = (calDayNo - 1) % 365;
-   }
- 
-   for (var i = 0; i < 11 && calDayNo >= cultures['fa-IR'].monthDays[i]; i++) {
-	  calDayNo -= cultures['fa-IR'].monthDays[i];
-   }
-   result.month = i + 1;
-   result.day = calDayNo + 1;
+    for (i = 0; i < invDate.month; ++i)
+        invDayNo += cultures.invariant.monthDays[i];
+    if (invDate.month > 1 && ((invDate.year % 4 == 0 && invDate.year % 100 != 0) || (invDate.year % 400 == 0)))
+    /* leap and after Feb */
+        invDayNo++;
+    invDayNo += invDate.day;
 
-   return result;
-}
+    calDayNo = invDayNo - 79;
+
+    j_np = Math.floor(calDayNo / 12053);
+    calDayNo %= 12053;
+
+    result.year = 979 + 33 * j_np + 4 * Math.floor(calDayNo / 1461);
+    calDayNo %= 1461;
+
+    if (calDayNo >= 366) {
+        result.year += Math.floor((calDayNo - 1) / 365);
+        calDayNo = (calDayNo - 1) % 365;
+    }
+
+    for (i = 0; i < 11 && calDayNo >= cultures['fa-IR'].monthDays[i]; i++) {
+        calDayNo -= cultures['fa-IR'].monthDays[i];
+    }
+    result.month = i + 1;
+    result.day = calDayNo + 1;
+
+    return result;
+};
-var defaultCultureName = new String();
-
-function setCulture(name){
-	defaultCultureName = name;
-}
-
-String.prototype.format = function () {
-	var formatted = unescape(this).replace('&amp;', '&');
-	for (var i = 0; i < arguments.length; i++) {
-		var regexp = new RegExp('\\{' + i + '\\}', 'gi');
-		formatted = formatted.replace(regexp, arguments[i] == null ? '' : arguments[i].toString());
-	}
-	return formatted;
-}
-
-String.map = function(str, src, dest) {
-	var result = new String();
-	for(var i = 0; i < str.length; i++) {
-		var index = src.indexOf(str.charAt(i));
-		if(index != -1) {
-			result += dest.charAt(index);
-		}
-		else {
-			result += str.charAt(i);
-		}
-	}
-	return result;
-}
-
-invariantFormatProvider = function (obj, format) {	
-	if (format) {		
-		var result = new String();
-		var raw = new String();
-		switch (typeof obj) {
-			case 'number':
-				raw = obj.toString();				
-				var numParts = raw.split('.');				
-				var re = /(0*)(#*)\.?(#*)(0*)/g;
-				var parts = re.exec(format);
-				
-				if(parts[1] != '' && (parts[1].length + parts[2].length > numParts[0].length)) {
-					result += Array(parts[1].length + parts[2].length - numParts[0].length + 1).join('0');
-				}
-				
-				result += numParts[0];
-												
-								
-				if(numParts.length == 1) {
-					numParts[1] = '';
-				}
-				
-				if(parts[3] != '' && parts[3].length < numParts[1].length) {
-					numParts[1] = numParts[1].substr(0, parts[3].length);
-				}
-				if((parts[4] != '') || (numParts[1] != '')) {
-					result += '.' + numParts[1];
-				}				
-				
-				if((parts[4] != '') && (parts[3].length + parts[4].length > numParts[1].length)) {				
-					result += Array(parts[3].length + parts[4].length - numParts[1].length + 1).join('0');
-				}
-				
-				return result;
-			case 'object':
-				if(obj instanceof Date){
-					var re = /y{2,4}/;					
-					var result = format;
-					var match = format.match(re);
-					if(match) {					
-						if(match[0].length == 4) {
-							result = result.replace(re, obj.getFullYear());
-						}
-						else{
-							result = result.replace(re, obj.getFullYear().toString().substr(2, 2));
-						}
-					}
-					re = /M{1,2}/;
-					match = format.match(re);
-					if(match) {
-						if(match[0].length == 2) {
-							result = result.replace(re, invariantFormatProvider(obj.getMonth(),'00'));
-						}
-						else{
-							result = result.replace(re, obj.getMonth());
-						}
-					}
-					re = /d{1,2}/;
-					match = format.match(re);
-					if(match) {
-						if(match[0].length == 2) {
-							result = result.replace(re, invariantFormatProvider(obj.getDay(),'00'));
-						}
-						else{
-							result = result.replace(re, obj.getDay());
-						}
-					}
-					re = /[hH]{1,2}/;
-					match = format.match(re);
-					if(match) {
-						var hour = obj.getHours();
-						if(match[0].indexOf('h') != -1) {
-							if(hour > 12) hour -= 12;
-						}
-						if(match[0].length == 2) {
-							result = result.replace(re, invariantFormatProvider(hour,'00'));
-						}
-						else{
-							result = result.replace(re, hour);
-						}
-					}
-					re = /m{1,2}/;
-					match = format.match(re);
-					if(match) {
-						if(match[0].length == 2) {
-							result = result.replace(re, invariantFormatProvider(obj.getMinutes(),'00'));
-						}
-						else{
-							result = result.replace(re, obj.getMinutes());
-						}
-					}
-					re = /s{1,2}/;
-					match = format.match(re);
-					if(match) {
-						if(match[0].length == 2) {
-							result = result.replace(re, invariantFormatProvider(obj.getSeconds(),'00'));
-						}
-						else{
-							result = result.replace(re, obj.getSeconds());
-						}
-					}					
-					return result;
-				}
-			default:
-				return obj.toString();
-		}
-	}
-	else {
-		return obj.toString();
-	}	
-}
-
-cultureFormatProvider = function (obj, cultureName, format) {
-	if (obj == null)
-		return '';
-	if (obj instanceof Date && !format) {
-		format = cultures[cultureName].defaultDateFormat;		
-	}
-	if (format) {
-		var result = new String();
-		var raw = new String();
-		switch (typeof obj) {
-			case 'number':
-				raw = obj.toString();				
-				var numParts = raw.split('.');
-				var re = /(0*)(#*)\.?(#*)(0*)/g;
-				var parts = re.exec(format);
-				if (parts[1] != '' && (parts[1].length + parts[2].length > numParts[0].length)) {
-					result += Array(parts[1].length + parts[2].length - numParts[0].length + 1).join(cultures[cultureName].numbers[0]);
-				}
-
-				result += String.map(numParts[0], cultures.invariant.numbers, cultures[cultureName].numbers);
-
-
-				if (numParts.length == 1) {
-					numParts[1] = '';
-				}
-
-				if (parts[3].length < numParts[1].length) {
-					numParts[1] = numParts[1].substr(0, parts[3].length);
-				}
-				if ((parts[4] != '') || (numParts[1] != '')) {					
-					result += cultures[cultureName].decimal + String.map(numParts[1], cultures.invariant.numbers, cultures[cultureName].numbers);
-				}
-
-
-				if ((parts[4] != '') && (parts[3].length + parts[4].length > numParts[1].length)) {
-					result += Array(parts[3].length + parts[4].length - numParts[1].length + 1).join(cultures[cultureName].numbers[0]);
-				}
-
-				return result;
-			case 'object':
-				if (obj instanceof Date) {
-					var re = /y{2,4}/;
-					var result = format;
-					var match = format.match(re);
-					var localeDate = cultures[cultureName].fromDate(obj);
-					if (match) {
-						if (match[0].length == 4) {
-							result = result.replace(re, cultureFormatProvider(localeDate.year, cultureName));
-						}
-						else {
-							result = result.replace(re, cultureFormatProvider(localeDate.year, cultureName).substr(2, 2));
-						}
-					}
-					re = /M{1,2}/;
-					match = format.match(re);
-					if (match) {
-						if (match[0].length == 2) {
-							result = result.replace(re, cultureFormatProvider(localeDate.month, cultureName, '00'));
-						}
-						else {
-							result = result.replace(re, cultureFormatProvider(localeDate.month, cultureName));
-						}
-					}
-					re = /d{1,2}/;
-					match = format.match(re);
-					if (match) {
-						if (match[0].length == 2) {
-							result = result.replace(re, cultureFormatProvider(localeDate.day, cultureName, '00'));
-						}
-						else {
-							result = result.replace(re, cultureFormatProvider(localeDate.day, cultureName));
-						}
-					}
-					re = /[hH]{1,2}/;
-					match = format.match(re);
-					if (match) {
-						var hour = obj.getHours();
-						if (match[0].indexOf('h') != -1) {
-							if (hour > 12) hour -= 12;
-						}
-						if (match[0].length == 2) {
-							result = result.replace(re, cultureFormatProvider(hour, cultureName, '00'));
-						}
-						else {
-							result = result.replace(re, cultureFormatProvider(hour, cultureName));
-						}
-					}
-					re = /m{1,2}/;
-					match = format.match(re);
-					if (match) {
-						if (match[0].length == 2) {
-							result = result.replace(re, cultureFormatProvider(obj.getMinutes(), cultureName, '00'));
-						}
-						else {
-							result = result.replace(re, cultureFormatProvider(obj.getMinutes(), cultureName));
-						}
-					}
-					re = /s{1,2}/;
-					match = format.match(re);
-					if (match) {
-						if (match[0].length == 2) {
-							result = result.replace(re, cultureFormatProvider(obj.getSeconds(), cultureName, '00'));
-						}
-						else {
-							result = result.replace(re, cultureFormatProvider(obj.getSeconds(), cultureName));
-						}
-					}
-					return result;
-				}
-			default:
-				return String.map(obj.toString(), cultures.invariant.numbers + cultures.invariant.decimal, cultures[cultureName].numbers + cultures[cultureName].decimal);
-		}
-	}
-	else {
-		return String.map(obj.toString(), cultures.invariant.numbers + cultures.invariant.decimal, cultures[cultureName].numbers + cultures[cultureName].decimal);
-	}
-}
-
-String.cultureFormat = function (culture, format) {
-	try {
-		var formatProvider = function (obj, format) {
-			if (culture && (culture != ''))
-				return cultureFormatProvider(obj, culture, format);
-			else
-				return invariantFormatProvider(obj, format);
-		};
-
-		var formatted = unescape(format).replace('&amp;', '&');
-		if (arguments[2] instanceof Array) {
-			var array = arguments[2];
-			for (var i = 0; i < array.length; i++) {
-				var regexp = new RegExp('\\{' + i + ':?(.*?)\\}', 'gi');
-				var match = regexp.exec(formatted);
-				if (match && match.length > 0)
-					formatted = formatted.replace(regexp, array[i] == null ? '' : formatProvider(array[i], match[1]));
-			}
-		}
-		else if ((typeof arguments[2] != 'number') && (typeof arguments[2] != 'string') && !(arguments[2] instanceof Date)) {
-			var obj = arguments[2];
-			for (var i in obj) {
-				var regexp = new RegExp('@\\{' + i + ':?(.*?)\\}', 'gi');
-				var match = regexp.exec(formatted);
-				if (match && match.length > 0)
-					formatted = formatted.replace(regexp, obj[i] == null ? '' : formatProvider(obj[i], match[1]));
-			}
-		}
-		else {
-			for (var i = 2; i < arguments.length; i++) {
-				var regexp = new RegExp('\\{' + (i - 2).toString() + ':?(.*?)\\}', 'gi');
-				var match = regexp.exec(formatted);
-				if (match && match.length > 0)
-					formatted = formatted.replace(regexp, arguments[i] == null ? '' : formatProvider(arguments[i], match[1]));
-			}
-		}
-	}
-	catch (error) {
-		format = error.message;
-	}
-	return formatted;
-}
-
-
-String.format = function (format) {
-	try {
-		var formatProvider = function (obj, format) {
-			if (defaultCultureName && (defaultCultureName != ''))
-				return cultureFormatProvider(obj, defaultCultureName, format);
-			else
-				return invariantFormatProvider(obj, format);
-		};
-		var formatted = unescape(format).replace('&amp;', '&');
-		if (arguments[1] instanceof Array) {
-			var array = arguments[1];
-			for (var i = 0; i < array.length; i++) {
-				var regexp = new RegExp('\\{' + i + ':?(.*?)\\}', 'gi');
-				var match = regexp.exec(formatted);
-				if (match && match.length > 0)
-					formatted = formatted.replace(regexp, array[i] == null ? '' : formatProvider(array[i], match[1]));
-				else
-					formatted = 'Invalid format';
-			}
-		}
-		else if ((typeof arguments[1] != 'number') && (typeof arguments[1] != 'string') && !(arguments[1] instanceof Date)) {
-			var obj = arguments[1];
-			for (var i in obj) {
-				var regexp = new RegExp('@\\{' + i + ':?(.*?)\\}', 'gi');
-				var match = regexp.exec(formatted);
-				if (match && match.length > 0)
-					formatted = formatted.replace(regexp, obj[i] == null ? '' : formatProvider(obj[i], match[1]));
-				else
-					formatted = 'Invalid format';
-			}
-		}
-		else {
-			for (var i = 1; i < arguments.length; i++) {
-				var regexp = new RegExp('\\{' + (i - 1).toString() + ':?(.*?)\\}', 'gi');
-				var match = regexp.exec(formatted);
-				if (match && match.length > 0)
-					formatted = formatted.replace(regexp, arguments[i] == null ? '' : formatProvider(arguments[i], match[1]));
-				else
-					formatted = 'Invalid format';
-			}
-		}
-	}
-	catch (error) {
-		format = error.message;
-	}
-	return formatted;
-}
-
+var defaultCultureName = '';
+
+function setCulture(name) {
+    defaultCultureName = name;
+}
+
+String.prototype.format = function () {
+    var formatted = this.replace('&amp;', '&');
+    for (var i = 0; i < arguments.length; i++) {
+        var regexp = new RegExp('\\{' + i + '\\}', 'gi');
+        formatted = formatted.replace(regexp, arguments[i] === null ? '' : arguments[i].toString());
+    }
+    return formatted;
+};
+
+String.map = function (str, src, dst) {
+    var result = '';
+    for (var i = 0; i < str.length; i++) {
+        var index = src.indexOf(str.charAt(i));
+        if (index !== -1) {
+            result += dst.charAt(index);
+        }
+        else {
+            result += str.charAt(i);
+        }
+    }
+    return result;
+};
+
+invariantFormatProvider = function (obj, format) {
+    var result = '';
+    var raw = '';
+    var regexp;
+    if (format) {
+        switch (typeof obj) {
+            case 'number':
+                raw = obj.toString();
+                var numParts = raw.split('.');
+                var re = /(0*)(#*)\.?(#*)(0*)/g;
+                var parts = re.exec(format);
+
+                if (parts[1] !== '' && (parts[1].length + parts[2].length > numParts[0].length)) {
+                    result += new Array(parts[1].length + parts[2].length - numParts[0].length + 1).join('0');
+                }
+
+                result += numParts[0];
+
+
+                if (numParts.length === 1) {
+                    numParts[1] = '';
+                }
+
+                if (parts[3] !== '' && parts[3].length < numParts[1].length) {
+                    numParts[1] = numParts[1].substr(0, parts[3].length);
+                }
+                if ((parts[4] !== '') || (numParts[1] !== '')) {
+                    result += '.' + numParts[1];
+                }
+
+                if ((parts[4] !== '') && (parts[3].length + parts[4].length > numParts[1].length)) {
+                    result += new Array(parts[3].length + parts[4].length - numParts[1].length + 1).join('0');
+                }
+                break;
+            case 'object':
+                if (obj instanceof Date) {
+                    regexp = /y{2,4}/;
+                    result = format;
+                    var match = format.match(regexp);
+                    if (match) {
+                        if (match[0].length === 4) {
+                            result = result.replace(regexp, obj.getFullYear().toString());
+                        }
+                        else {
+                            result = result.replace(regexp, obj.getFullYear().toString().substr(2, 2));
+                        }
+                    }
+                    regexp = /M{1,2}/;
+                    match = format.match(regexp);
+                    if (match) {
+                        if (match[0].length === 2) {
+                            result = result.replace(regexp, invariantFormatProvider(obj.getMonth(), '00'));
+                        }
+                        else {
+                            result = result.replace(regexp, obj.getMonth().toString());
+                        }
+                    }
+                    regexp = /d{1,2}/;
+                    match = format.match(regexp);
+                    if (match) {
+                        if (match[0].length === 2) {
+                            result = result.replace(regexp, invariantFormatProvider(obj.getDay(), '00'));
+                        }
+                        else {
+                            result = result.replace(regexp, obj.getDay().toString());
+                        }
+                    }
+                    regexp = /[hH]{1,2}/;
+                    match = format.match(regexp);
+                    if (match) {
+                        var hour = obj.getHours();
+                        if (match[0].indexOf('h') !== -1) {
+                            if (hour > 12) hour -= 12;
+                        }
+                        if (match[0].length === 2) {
+                            result = result.replace(regexp, invariantFormatProvider(hour, '00'));
+                        }
+                        else {
+                            result = result.replace(regexp, hour.toString());
+                        }
+                    }
+                    regexp = /m{1,2}/;
+                    match = format.match(regexp);
+                    if (match) {
+                        if (match[0].length === 2) {
+                            result = result.replace(regexp, invariantFormatProvider(obj.getMinutes(), '00'));
+                        }
+                        else {
+                            result = result.replace(regexp, obj.getMinutes().toString());
+                        }
+                    }
+                    regexp = /s{1,2}/;
+                    match = format.match(regexp);
+                    if (match) {
+                        if (match[0].length === 2) {
+                            result = result.replace(regexp, invariantFormatProvider(obj.getSeconds(), '00'));
+                        }
+                        else {
+                            result = result.replace(regexp, obj.getSeconds().toString());
+                        }
+                    }
+                }
+                break;
+            default:
+                result = obj.toString();
+                break;
+        }
+    }
+    else {
+        result = obj.toString();
+    }
+    return result;
+};
+
+cultureFormatProvider = function (obj, cultureName, format) {
+    var result = '';
+    if (obj instanceof Date && !format) {
+        format = cultures[cultureName].defaultDateFormat;
+    }
+    if (obj !== null) {
+        if (format) {
+            var raw = '';
+            var regexp;
+            var match;
+            switch (typeof obj) {
+                case 'number':
+                    raw = obj.toString();
+                    var numParts = raw.split('.');
+                    regexp = /(0*)(#*)\.?(#*)(0*)/g;
+                    var parts = regexp.exec(format);
+                    if (parts[1] !== '' && (parts[1].length + parts[2].length > numParts[0].length)) {
+                        result += new Array(parts[1].length + parts[2].length - numParts[0].length + 1).join(cultures[cultureName].numbers[0]);
+                    }
+
+                    result += String.map(numParts[0], cultures.invariant.numbers, cultures[cultureName].numbers);
+
+
+                    if (numParts.length === 1) {
+                        numParts[1] = '';
+                    }
+
+                    if (parts[3].length < numParts[1].length) {
+                        numParts[1] = numParts[1].substr(0, parts[3].length);
+                    }
+                    if ((parts[4] !== '') || (numParts[1] !== '')) {
+                        result += cultures[cultureName].decimal + String.map(numParts[1], cultures.invariant.numbers, cultures[cultureName].numbers);
+                    }
+
+
+                    if ((parts[4] !== '') && (parts[3].length + parts[4].length > numParts[1].length)) {
+                        result += new Array(parts[3].length + parts[4].length - numParts[1].length + 1).join(cultures[cultureName].numbers[0]);
+                    }
+                    break;
+                case 'object':
+                    if (obj instanceof Date) {
+                        regexp = /y{2,4}/;
+                        result = format;
+                        match = format.match(regexp);
+                        var localeDate = cultures[cultureName].fromDate(obj);
+                        if (match) {
+                            if (match[0].length === 4) {
+                                result = result.replace(regexp, cultureFormatProvider(localeDate.year, cultureName));
+                            }
+                            else {
+                                result = result.replace(regexp, cultureFormatProvider(localeDate.year, cultureName).substr(2, 2));
+                            }
+                        }
+                        regexp = /M{1,2}/;
+                        match = format.match(regexp);
+                        if (match) {
+                            if (match[0].length === 2) {
+                                result = result.replace(regexp, cultureFormatProvider(localeDate.month, cultureName, '00'));
+                            }
+                            else {
+                                result = result.replace(regexp, cultureFormatProvider(localeDate.month, cultureName));
+                            }
+                        }
+                        regexp = /d{1,2}/;
+                        match = format.match(regexp);
+                        if (match) {
+                            if (match[0].length === 2) {
+                                result = result.replace(regexp, cultureFormatProvider(localeDate.day, cultureName, '00'));
+                            }
+                            else {
+                                result = result.replace(regexp, cultureFormatProvider(localeDate.day, cultureName));
+                            }
+                        }
+                        regexp = /[hH]{1,2}/;
+                        match = format.match(regexp);
+                        if (match) {
+                            var hour = obj.getHours();
+                            if (match[0].indexOf('h') !== -1) {
+                                if (hour > 12) hour -= 12;
+                            }
+                            if (match[0].length === 2) {
+                                result = result.replace(regexp, cultureFormatProvider(hour, cultureName, '00'));
+                            }
+                            else {
+                                result = result.replace(regexp, cultureFormatProvider(hour, cultureName));
+                            }
+                        }
+                        regexp = /m{1,2}/;
+                        match = format.match(regexp);
+                        if (match) {
+                            if (match[0].length === 2) {
+                                result = result.replace(regexp, cultureFormatProvider(obj.getMinutes(), cultureName, '00'));
+                            }
+                            else {
+                                result = result.replace(regexp, cultureFormatProvider(obj.getMinutes(), cultureName));
+                            }
+                        }
+                        regexp = /s{1,2}/;
+                        match = format.match(regexp);
+                        if (match) {
+                            if (match[0].length === 2) {
+                                result = result.replace(regexp, cultureFormatProvider(obj.getSeconds(), cultureName, '00'));
+                            }
+                            else {
+                                result = result.replace(regexp, cultureFormatProvider(obj.getSeconds(), cultureName));
+                            }
+                        }
+                        break;
+                    }
+                    break;
+                default:
+                    result = String.map(obj.toString(), cultures.invariant.numbers + cultures.invariant.decimal, cultures[cultureName].numbers + cultures[cultureName].decimal);
+                    break;
+            }
+        }
+        else {
+            result = String.map(obj.toString(), cultures.invariant.numbers + cultures.invariant.decimal, cultures[cultureName].numbers + cultures[cultureName].decimal);
+        }
+    }
+    return result;
+};
+
+String.cultureFormat = function (culture, format) {
+    var formatted;
+    try {
+        var regexp;
+        var match;
+        var i = 0;
+        var formatProvider = function (obj, format) {
+            if (culture && (culture !== ''))
+                return cultureFormatProvider(obj, culture, format);
+            else
+                return invariantFormatProvider(obj, format);
+        };
+
+        formatted = format.replace('&amp;', '&');
+        if (arguments[2] instanceof Array) {
+            var array = arguments[2];
+            for (i = 0; i < array.length; i++) {
+                regexp = new RegExp('\\{' + i + ':?(.*?)\\}', 'gi');
+                match = regexp.exec(formatted);
+                if (match && match.length > 0)
+                    formatted = formatted.replace(regexp, array[i] === null ? '' : formatProvider(array[i], match[1]));
+            }
+        }
+        else if ((typeof arguments[2] !== 'number') && (typeof arguments[2] !== 'string') && !(arguments[2] instanceof Date)) {
+            var obj = arguments[2];
+            for (var item in obj) {
+                regexp = new RegExp('@\\{' + item + ':?(.*?)\\}', 'gi');
+                match = regexp.exec(formatted);
+                if (match && match.length > 0)
+                    if (obj.hasOwnProperty(item))
+                        formatted = formatted.replace(regexp, obj[item] === null ? '' : formatProvider(obj[item], match[1]));
+            }
+        }
+        else {
+            for (i = 2; i < arguments.length; i++) {
+                regexp = new RegExp('\\{' + (i - 2).toString() + ':?(.*?)\\}', 'gi');
+                match = regexp.exec(formatted);
+                if (match && match.length > 0)
+                    formatted = formatted.replace(regexp, arguments[i] === null ? '' : formatProvider(arguments[i], match[1]));
+            }
+        }
+    }
+    catch (error) {
+        formatted = error.message;
+    }
+    return formatted;
+};
+
+
+String.format = function (format) {
+    var formatted;
+    try {
+        var regexp;
+        var match;
+        var i = 0;
+        var formatProvider = function (obj, format) {
+            if (defaultCultureName && (defaultCultureName !== ''))
+                return cultureFormatProvider(obj, defaultCultureName, format);
+            else
+                return invariantFormatProvider(obj, format);
+        };
+        formatted = format.replace('&amp;', '&');
+        if (arguments[1] instanceof Array) {
+            var array = arguments[1];
+            for (i = 0; i < array.length; i++) {
+                regexp = new RegExp('\\{' + i + ':?(.*?)\\}', 'gi');
+                match = regexp.exec(formatted);
+                if (match && match.length > 0)
+                    formatted = formatted.replace(regexp, array[i] === null ? '' : formatProvider(array[i], match[1]));
+                else
+                    formatted = 'Invalid format';
+            }
+        }
+        else if ((typeof arguments[1] !== 'number') && (typeof arguments[1] !== 'string') && !(arguments[1] instanceof Date)) {
+            var obj = arguments[1];
+            for (var item in obj) {
+                regexp = new RegExp('@\\{' + item + ':?(.*?)\\}', 'gi');
+                match = regexp.exec(formatted);
+                if (match && match.length > 0)
+                    if (obj.hasOwnProperty(item))
+                        formatted = formatted.replace(regexp, obj[item] === null ? '' : formatProvider(obj[item], match[1]));
+                    else
+                        formatted = 'Invalid format';
+            }
+        }
+        else {
+            for (i = 1; i < arguments.length; i++) {
+                regexp = new RegExp('\\{' + (i - 1).toString() + ':?(.*?)\\}', 'gi');
+                match = regexp.exec(formatted);
+                if (match && match.length > 0)
+                    formatted = formatted.replace(regexp, arguments[i] === null ? '' : formatProvider(arguments[i], match[1]));
+                else
+                    formatted = 'Invalid format';
+            }
+        }
+    }
+    catch (error) {
+        format = error.message;
+    }
+    return formatted;
+};
+
     //alert(String.format('{0} and {1:00}',5, 6));
     //alert(String.format('{0:yyyy/MM/dd h:mm:ss}', new Date()));
     //alert(readNumber(12.25));
-    alert(String.format('@{a:0.00} and @{b:00.#}', { a: 5.1, b: 6.1 }));
+    //alert(String.format('@{a:0.00} and @{b:00.#}', { a: 5.1, b: 6.1 }));
     </script>
 </head>
 <body>