Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved formatting from stilts to babbla and normalized package structures

  • Loading branch information...
commit aadd026c658c3093dc3d2eb4fa7e0b6dd0058a38 1 parent 449cde6
@mstade authored
Showing with 235 additions and 26 deletions.
  1. +2 −2 src/se/stade/babbla/Calendar.as
  2. +1 −1  src/se/stade/babbla/Culture.as
  3. +13 −0 src/se/stade/babbla/NumberSystem.as
  4. +0 −13 src/se/stade/babbla/NumericalSystem.as
  5. +1 −1  src/se/stade/babbla/{formats → formatting}/CurrencyFormat.as
  6. +1 −1  src/se/stade/babbla/{formats → formatting}/DateFormat.as
  7. +1 −1  src/se/stade/babbla/{formats → formatting}/DayFormat.as
  8. +7 −0 src/se/stade/babbla/formatting/Formatter.as
  9. +1 −1  src/se/stade/babbla/{formats → formatting}/MonthFormat.as
  10. +1 −1  src/se/stade/babbla/{formats → formatting}/NumberFormat.as
  11. +24 −0 src/se/stade/babbla/formatting/NumberFormatter.as
  12. +1 −1  src/se/stade/babbla/{formats → formatting}/NumberSymbol.as
  13. +1 −1  src/se/stade/babbla/{formats → formatting}/PercentageFormat.as
  14. +15 −0 src/se/stade/babbla/formatting/RegExpFormatter.as
  15. +73 −0 src/se/stade/babbla/formatting/SimpleDateFormatter.as
  16. +62 −0 src/se/stade/babbla/formatting/StringFormatter.as
  17. +1 −1  src/se/stade/babbla/{formats → formatting}/SymbolName.as
  18. +1 −1  src/se/stade/babbla/{formats → formatting}/TimeFormat.as
  19. +9 −0 src/se/stade/babbla/formatting/TypeFormatter.as
  20. +1 −1  src/se/stade/babbla/{formats → formatting}/WeekFormat.as
  21. +19 −0 src/se/stade/babbla/formatting/format.as
View
4 src/se/stade/babbla/Calendar.as
@@ -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;
View
2  src/se/stade/babbla/Culture.as
@@ -6,6 +6,6 @@ package se.stade.babbla
function get comparator():StringComparator;
function get calendar():Calendar;
- function get numericalSystem():NumericalSystem;
+ function get numericalSystem():NumberSystem;
}
}
View
13 src/se/stade/babbla/NumberSystem.as
@@ -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;
+ }
+}
View
13 src/se/stade/babbla/NumericalSystem.as
@@ -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;
- }
-}
View
2  src/se/stade/babbla/formats/CurrencyFormat.as → src/se/stade/babbla/formatting/CurrencyFormat.as
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface CurrencyFormat extends NumberFormat
{
View
2  src/se/stade/babbla/formats/DateFormat.as → src/se/stade/babbla/formatting/DateFormat.as
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface DateFormat
{
View
2  src/se/stade/babbla/formats/DayFormat.as → src/se/stade/babbla/formatting/DayFormat.as
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface DayFormat
{
View
7 src/se/stade/babbla/formatting/Formatter.as
@@ -0,0 +1,7 @@
+package se.stade.babbla.formatting
+{
+ public interface Formatter
+ {
+ function format(template:String, ... parameters):String;
+ }
+}
View
2  src/se/stade/babbla/formats/MonthFormat.as → src/se/stade/babbla/formatting/MonthFormat.as
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface MonthFormat
{
View
2  src/se/stade/babbla/formats/NumberFormat.as → src/se/stade/babbla/formatting/NumberFormat.as
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface NumberFormat
{
View
24 src/se/stade/babbla/formatting/NumberFormatter.as
@@ -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;
+ }
+
+ }
+}
View
2  src/se/stade/babbla/formats/NumberSymbol.as → src/se/stade/babbla/formatting/NumberSymbol.as
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public final class NumberSymbol
{
View
2  src/se/stade/babbla/formats/PercentageFormat.as → src/se/stade/babbla/formatting/PercentageFormat.as
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface PercentageFormat extends NumberFormat
{
View
15 src/se/stade/babbla/formatting/RegExpFormatter.as
@@ -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;
+ }
+ }
+}
View
73 src/se/stade/babbla/formatting/SimpleDateFormatter.as
@@ -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;
+ }
+ }
+}
View
62 src/se/stade/babbla/formatting/StringFormatter.as
@@ -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;
+ }
+ }
+}
View
2  src/se/stade/babbla/formats/SymbolName.as → src/se/stade/babbla/formatting/SymbolName.as
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public final class SymbolName
{
View
2  src/se/stade/babbla/formats/TimeFormat.as → src/se/stade/babbla/formatting/TimeFormat.as
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
public interface TimeFormat
{
View
9 src/se/stade/babbla/formatting/TypeFormatter.as
@@ -0,0 +1,9 @@
+package se.stade.babbla.formatting
+{
+ public interface TypeFormatter
+ {
+ function get recognizedTypes():Vector.<Class>;
+
+ function format(subject:*, template:String):String;
+ }
+}
View
2  src/se/stade/babbla/formats/WeekFormat.as → src/se/stade/babbla/formatting/WeekFormat.as
@@ -1,4 +1,4 @@
-package se.stade.babbla.formats
+package se.stade.babbla.formatting
{
import se.stade.stilts.time.DayOfWeek;
View
19 src/se/stade/babbla/formatting/format.as
@@ -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
+]);
Please sign in to comment.
Something went wrong with that request. Please try again.