Permalink
Browse files

Moved formatting from stilts to babbla and normalized package structures

  • Loading branch information...
1 parent 449cde6 commit aadd026c658c3093dc3d2eb4fa7e0b6dd0058a38 @mstade committed Aug 28, 2011
@@ -1,7 +1,7 @@
package se.stade.babbla
{
- import se.stade.babbla.formats.DateFormat;
- import se.stade.babbla.formats.TimeFormat;
+ import se.stade.babbla.formatting.DateFormat;
+ import se.stade.babbla.formatting.TimeFormat;
import se.stade.stilts.time.DateTime;
import se.stade.stilts.time.TimeSpan;
@@ -6,6 +6,6 @@ package se.stade.babbla
function get comparator():StringComparator;
function get calendar():Calendar;
- function get numericalSystem():NumericalSystem;
+ function get numericalSystem():NumberSystem;
}
}
@@ -0,0 +1,13 @@
+package se.stade.babbla
+{
+ import se.stade.babbla.formatting.CurrencyFormat;
+ import se.stade.babbla.formatting.NumberFormat;
+ import se.stade.babbla.formatting.PercentageFormat;
+
+ public interface NumberSystem
+ {
+ function get number():NumberFormat;
+ function get currency():CurrencyFormat;
+ function get percentage():PercentageFormat;
+ }
+}
@@ -1,13 +0,0 @@
-package se.stade.babbla
-{
- import se.stade.babbla.formats.CurrencyFormat;
- import se.stade.babbla.formats.NumberFormat;
- import se.stade.babbla.formats.PercentageFormat;
-
- public interface NumericalSystem
- {
- function get number():NumberFormat;
- function get currency():CurrencyFormat;
- function get percentage():PercentageFormat;
- }
-}
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface CurrencyFormat extends NumberFormat
{
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface DateFormat
{
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface DayFormat
{
@@ -0,0 +1,7 @@
+package se.stade.babbla.formatting
+{
+ public interface Formatter
+ {
+ function format(template:String, ... parameters):String;
+ }
+}
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface MonthFormat
{
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface NumberFormat
{
@@ -0,0 +1,24 @@
+package se.stade.babbla.formatting
+{
+ public class NumberFormatter implements TypeFormatter
+ {
+ public var numberFormat:NumberFormat;
+
+ public function get recognizedTypes():Vector.<Class>
+ {
+ return new <Class>[Number];
+ }
+
+ public function format(subject:*, template:String):String
+ {
+ if (subject is Number)
+ {
+ var num:Number = Number(subject);
+ return String(num);
+ }
+
+ return template;
+ }
+
+ }
+}
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public final class NumberSymbol
{
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface PercentageFormat extends NumberFormat
{
@@ -0,0 +1,15 @@
+package se.stade.babbla.formatting
+{
+ public class RegExpFormatter implements TypeFormatter
+ {
+ public function get recognizedTypes():Vector.<Class>
+ {
+ return new <Class>[RegExp];
+ }
+
+ public function format(subject:*, parameters:String):String
+ {
+ return subject.source;
+ }
+ }
+}
@@ -0,0 +1,73 @@
+package se.stade.babbla.formatting
+{
+ import se.stade.stilts.string.padLeft;
+ import se.stade.stilts.string.padRight;
+ import se.stade.stilts.time.DateTime;
+
+ public class SimpleDateFormatter implements TypeFormatter
+ {
+ public function get recognizedTypes():Vector.<Class>
+ {
+ return new <Class>[Date, DateTime];
+ }
+
+ public var shortDayname:Vector.<String> = new <String>["Sun", "Mon", "Tue", "Wed", "Fri", "Sat"];
+ public var fullDayname:Vector.<String> = new <String>["Sunday", "Monday", "Tuesday", "Wednesday", "Friday", "Saturday"];
+
+ public var shortMonthname:Vector.<String> = new <String>["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+ public var fullMonthname:Vector.<String> = new <String>["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
+
+ protected function formatDateParts(input:String, year:Number, month:Number, dayOfWeek:Number):String
+ {
+ var shortYear:String = padLeft("00", String(year).substr(-2));
+ var monthNum:String = padLeft("00", String(month).slice(-2));
+ var dayNum:String = padLeft("00", dayOfWeek);
+
+ return input.replace(/YYYY/g, padLeft("0000", year))
+ .replace( /YY/g, padLeft("00", shortYear))
+ .replace(/MMMM/g, fullMonthname[month])
+ .replace( /MMM/g, shortMonthname[month])
+ .replace( /MM/g, monthNum)
+ .replace(/DDDD/g, fullDayname[dayOfWeek])
+ .replace( /DDD/g, shortDayname[dayOfWeek])
+ .replace( /DD/g, dayNum);
+ }
+
+ public var amDesignator:String = "AM";
+ public var pmDesignator:String = "PM";
+
+ protected function formatTime(input:String, hour:uint, minute:uint, second:uint, millisecond:uint):String
+ {
+ var designator:String = hour > 12 ? amDesignator : pmDesignator;
+
+ var tenths:String = String(millisecond).slice(0, 1);
+ var hundreths:String = padRight(String(millisecond).slice(0, 2), "00");
+
+ return input.replace( /hh/g, padLeft("00", hour % 13))
+ .replace( /HH/g, padLeft("00", hour))
+ .replace( /mm/g, padLeft("00", minute))
+ .replace( /ss/g, padLeft("00", second))
+ .replace( /t/g, designator.charAt(0))
+ .replace( /tt/g, designator)
+ .replace(/fff/g, padRight(millisecond, "000"))
+ .replace( /ff/g, padRight(hundreths, "00"))
+ .replace( /f/g, padRight(tenths, "0"));
+ }
+
+ public function format(subject:*, template:String):String
+ {
+ if (subject is Date)
+ {
+ var asDate:Date = subject as Date;
+ return formatDateParts(template, asDate.fullYear, asDate.month, asDate.day);
+ }
+ else if (subject is DateTime)
+ {
+ var myDate:DateTime = subject as DateTime;
+ return formatDateParts(template, myDate.year, myDate.month, myDate.dayOfWeek.value);
+ }
+
+ return template;
+ }
+ }
+}
@@ -0,0 +1,62 @@
+package se.stade.babbla.formatting
+{
+ import flash.utils.Dictionary;
+
+ import se.stade.daffodil.define;
+
+ public class StringFormatter implements Formatter
+ {
+ public function StringFormatter(formatters:Vector.<TypeFormatter>)
+ {
+ for each (var formatter:TypeFormatter in formatters)
+ {
+ registerTypeFormatter(formatter);
+ }
+ }
+
+ private var getTypeFormatter:Dictionary = new Dictionary();
+
+ public function registerTypeFormatter(formatter:TypeFormatter):void
+ {
+ for each (var type:Class in formatter.recognizedTypes)
+ {
+ getTypeFormatter[type] = formatter;
+ }
+ }
+
+ public function format(template:String, ... substitutions):String
+ {
+ var subs:Object = substitutions;
+
+ if (substitutions.length == 1 &&
+ (
+ substitutions[0].constructor == Object ||
+ substitutions[0].constructor == Dictionary
+ ))
+ subs = substitutions[0];
+
+ for (var token:String in subs)
+ {
+ var pattern:RegExp = new RegExp("\\{" + token + "(?::([^\\}]+))?\\}", "g");
+ var match:Object;
+
+ while (match = pattern.exec(template))
+ {
+ var parameters:String = match[1];
+ var start:String = template.slice(0, match.index);
+ var end:String = template.slice(match.index + match[0].length);
+
+ var substitute:* = subs[token];
+ var type:TypeFormatter = getTypeFormatter[define(substitute)];
+
+ var substitution:String = type ? type.format(substitute, parameters) : String(substitute);
+ template = start + substitution + end;
+
+ pattern.lastIndex -= match[0].length - substitution.length;
+ }
+ }
+
+ return template;
+ }
+ }
+}
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public final class SymbolName
{
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface TimeFormat
{
@@ -0,0 +1,9 @@
+package se.stade.babbla.formatting
+{
+ public interface TypeFormatter
+ {
+ function get recognizedTypes():Vector.<Class>;
+
+ function format(subject:*, template:String):String;
+ }
+}
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
import se.stade.stilts.time.DayOfWeek;
@@ -0,0 +1,19 @@
+package se.stade.babbla.formatting
+{
+ public function format(template:String, ... substitutions):String
+ {
+ return formatter.format.apply(null, [template].concat(substitutions));
+ }
+}
+import se.stade.babbla.formatting.Formatter;
+import se.stade.babbla.formatting.RegExpFormatter;
+import se.stade.babbla.formatting.SimpleDateFormatter;
+import se.stade.babbla.formatting.StringFormatter;
+import se.stade.babbla.formatting.TypeFormatter;
+
+
+const formatter:Formatter = new StringFormatter(new <TypeFormatter>
+[
+ new SimpleDateFormatter,
+ new RegExpFormatter
+]);

0 comments on commit aadd026

Please sign in to comment.