Skip to content

Instantly share code, notes, and snippets.

@fpg1503
Created February 21, 2018 17:20
Show Gist options
  • Save fpg1503/5c1c0b8b7e4fa049547a850a87e5153e to your computer and use it in GitHub Desktop.
Save fpg1503/5c1c0b8b7e4fa049547a850a87e5153e to your computer and use it in GitHub Desktop.
enum Year {
noPadding = "y",
twoDigits = "yy",
fourDigits = "yyyy"
}
enum Quarter {
number = "Q",
zeroPaddedNumber = "QQ",
qAndNumber = "QQQ",
spelledOut = "QQQQ"
}
enum Month {
number = "M",
zeroPaddedNumber = "MM",
shortName = "MMM",
fullName = "MMMM",
narrowName = "MMMMM"
}
enum Day {
number = "d",
zeroPaddedNumber = "dd",
dayOfWeekInMonth = "F",
shortDayOfWeek = "E",
fullDayOfWeek = "EEEE",
narrowDayOfWeek = "EEEEE"
}
enum Hour {
twelveHour = "h",
zeroPaddedTwelveHour = "hh",
twentyFourHour = "H",
zeroPaddedTwentyFourHour = "HH",
AMorPM = "a"
}
enum Minute {
number = "m",
zeroPaddedNumber = "mm"
}
enum Second {
number = "s",
zeroPaddedNumber = "ss"
}
enum TimeZone {
threeLetterName = "zzz",
expandedName = "zzzz",
RFC822 = "Z",
ISO8601 = "ZZZZZ"
}
enum Separator {
dash = "-",
slash = "/",
colon = ":",
space = " "
}
class Caterpillar {
dateFormat: string = ""
constructor(dateFormat: string) {
this.dateFormat = dateFormat
}
private static with(formats: string[]) {
return new Caterpillar(formats.reduce((a, b) => a + b, ''))
}
public static year(style: Year): Caterpillar {
return new Caterpillar(style)
}
public static quarter(style: Quarter): Caterpillar {
return new Caterpillar(style)
}
public static month(style: Month): Caterpillar {
return new Caterpillar(style)
}
public static day(style: Day): Caterpillar {
return new Caterpillar(style)
}
public static hour(style: Hour): Caterpillar {
return new Caterpillar(style)
}
public static minute(style: Minute): Caterpillar {
return new Caterpillar(style)
}
public static second(style: Second): Caterpillar {
return new Caterpillar(style)
}
public static timeZone(style: TimeZone) {
return new Caterpillar(style)
}
public static separator(separator: Separator) {
return new Caterpillar(separator)
}
public static string(string: string): Caterpillar {
return new Caterpillar(`'${string}'`)
}
public year(style: Year): Caterpillar {
return Caterpillar.with([this.dateFormat, style])
}
public quarter(style: Quarter): Caterpillar {
return Caterpillar.with([this.dateFormat, style])
}
public month(style: Month): Caterpillar {
return Caterpillar.with([this.dateFormat, style])
}
public day(style: Day): Caterpillar {
return Caterpillar.with([this.dateFormat, style])
}
public hour(style: Hour): Caterpillar {
return Caterpillar.with([this.dateFormat, style])
}
public minute(style: Minute): Caterpillar {
return Caterpillar.with([this.dateFormat, style])
}
public second(style: Second): Caterpillar {
return Caterpillar.with([this.dateFormat, style])
}
public timeZone(style: TimeZone) {
return Caterpillar.with([this.dateFormat, style])
}
public separator(separator: Separator) {
return Caterpillar.with([this.dateFormat, separator])
}
public string(string: string): Caterpillar {
return Caterpillar.with([this.dateFormat, `'${string}'`])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment