From 3a39b10eb61b8656f22529bea39da596bee2da18 Mon Sep 17 00:00:00 2001 From: "rupert.avery" Date: Sat, 30 Nov 2013 21:42:55 +0800 Subject: [PATCH] Tests + Sugarpak --- datejs/datejs-tests.ts | 45 +++++++ datejs/datejs.d.ts | 49 ++++---- datejs/sugarpak.d.ts | 271 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 343 insertions(+), 22 deletions(-) create mode 100644 datejs/datejs-tests.ts create mode 100644 datejs/sugarpak.d.ts diff --git a/datejs/datejs-tests.ts b/datejs/datejs-tests.ts new file mode 100644 index 0000000000..9cb5eabbc5 --- /dev/null +++ b/datejs/datejs-tests.ts @@ -0,0 +1,45 @@ +/// +/// + +function tests() { + // TypeScript alias + var DateJS: IDateJSStatic = Date; + + // What date is next thursday? + DateJS.today().next().thursday(); + + // Add 3 days to Today + DateJS.today().add(3).days(); + + // Is today Friday? + DateJS.today().is().friday(); + + var is = DateJS.today().is(); + + // Number fun + (3).days().ago(); + + // 6 months from now + var n = 6; + n.months().fromNow(); + + // Set to 8:30 AM on the 15th day of the month + DateJS.today().set({ day: 15, hour: 8, minute: 30 }); + + DateJS.today().is().january(); + DateJS.today().is().november(); + DateJS.today().add(1).day().is().saturday(); + + // Convert text into Date + DateJS.parse('today'); + DateJS.parse('t + 5 d'); // today + 5 days + DateJS.parse('next thursday'); + DateJS.parse('February 20th 1973'); + DateJS.parse('Thu, 1 July 2004 22:30:00'); + + var future: IDateJS = DateJS.today().add(2).months(); + var someDate: IDateJS = DateJS.today().next().april().add(2).days(); + someDate.same().week(future); // true|false; + someDate.same().day(); // true|false; + DateJS.today().toObject().day; +} \ No newline at end of file diff --git a/datejs/datejs.d.ts b/datejs/datejs.d.ts index 4b4d5dbae9..6c510def6b 100644 --- a/datejs/datejs.d.ts +++ b/datejs/datejs.d.ts @@ -3,8 +3,8 @@ // Definitions by: David Khristepher Santos // Definitions: https://github.com/borisyankov/DefinitelyTyped -/** Defines a structure to be used in the add and set instance methods */ -interface IDateJSConfig { +/** Defines a structure to be used in the add and set instance methods */ +interface IDateJSLiteral { milliseconds: number; seconds: number; minutes: number; @@ -33,7 +33,7 @@ interface IDateJSStatic extends Date { /** Returns a culture-specific timezone abbreviation based on a given offset and a boolean indicating whether daylight savings time is in effect. */ getTimezoneAbbreviation(timezoneOffset: number, isDayLightSavingsTime: boolean): string; /** Gets the timezone offset if given a culture-specific string which is a valid full or abbreviated timezone name and a boolean indicating whether daylight savings time is in effect. */ - getTimezoneOffset(timezoneAbbreviation?: string, isDayLightSavingsTime?: boolean): number + getTimezoneOffset(timezoneAbbreviation?: string, isDayLightSavingsTime?: boolean): number; /** Converts the specified string value into its JavaScript Date equivalent using culture-specific format information. */ parse(dateString: string): IDateJS; /** Converts the specified string value into its JavaScript Date equivalent using the specified format. The format of the string value must match one of the supplied formats exactly. */ @@ -43,23 +43,23 @@ interface IDateJSStatic extends Date { /** Validates the number is within an acceptable range for the days in a month [0-MaxDaysInMonth]. */ validateDay(day: number, fullYear: number, monthNumber: number): boolean; /** Validates the number is within an acceptable range for hours[0 - 23].Returns true if within range, otherwise false. */ - validateHour(hour: number): boolean + validateHour(hour: number): boolean; /** Validates the number is within an acceptable range for milliseconds[0 - 999].Returns true if within range, otherwise false. */ - validateMillisecond(milliseconds: number): boolean + validateMillisecond(milliseconds: number): boolean; /** Validates the number is within an acceptable range for minutes[0 - 59].Returns true if within range, otherwise false. */ - validateMinute(minutes: number): boolean + validateMinute(minutes: number): boolean; /** Validates the number is within an acceptable range for months[0 - 11]. */ - validateMonth(month: number): boolean + validateMonth(month: number): boolean; /** Validates the number is within an acceptable range for seconds[0 - 59].Returns true if within range, otherwise false. */ - validateSecond(second: number): boolean + validateSecond(second: number): boolean; /** Validates the number is within an acceptable range for years[0 - 9999]. */ - validateYear(year: number): boolean + validateYear(year: number): boolean; } /** DateJS Public Instance Methods */ interface IDateJS extends Date { /** Adds(or subtracts) to the value of the year, month, day, hour, minute, second, millisecond of the date instance using given configuration object. Positive and Negative values allowed. */ - add(config?: IDateJSConfig): IDateJS; + add(config?: IDateJSLiteral): IDateJS; /** Adds the specified number of milliseconds to this instance. */ addMilliseconds(milliseconds: number): IDateJS; /** Adds the specified number of seconds to this instance given the number of seconds to add.The number can be positive or negative. */ @@ -76,7 +76,7 @@ interface IDateJS extends Date { addMonths(months: number): IDateJS; /** Adds the specified number of years to this instance given the number of years to add.The number can be positive or negative. */ addYears(years: number): IDateJS; - /** Resets the time of this DateJS object to 12:00 AM(00:00), which is the start of the day. */ + /** Resets the time of this Date object to 12:00 AM(00:00), which is the start of the day. */ clearTime(): IDateJS; /** Resets the time of this Date object to the current time('now'). */ setTimeToNow(): IDateJS; @@ -92,6 +92,10 @@ interface IDateJS extends Date { isAfter(date: IDateJS): boolean; /** Determines if this date occurs before the date to compare to. */ isBefore(date: IDateJS): boolean; + /** Determines if the current Date instance occurs on the same Date as the supplied 'date'. */ + isToday(date: IDateJS): boolean; + /** Returns the number of milliseconds between this date and date. */ + getElapsed(date: IDateJS): number; /** Get the Ordinal day (numeric day number) of the year, adjusted for leap year. Returns 1 through 365 (366 in leap years) */ getOrdinalNumber(): number; /** Get the timezone abbreviation of the current date. */ @@ -101,7 +105,7 @@ interface IDateJS extends Date { /** Get the week number. Week one (1) is the week which contains the first Thursday of the year. Monday is considered the first day of the week. */ getWeek(): number; /** Get the ISO 8601 week number. Week one ("01") is the week which contains the first Thursday of the year. Monday is considered the first day of the week. */ - getISOWeek(): string + getISOWeek(): string; /** Moves the date to Monday of the week set. Week one (1) is the week which contains the first Thursday of the year. */ setWeek(week: number): IDateJS; /** Indicates whether Daylight Saving Time is observed in the current time zone. */ @@ -109,23 +113,24 @@ interface IDateJS extends Date { /** Indicates whether this Date instance is within the Daylight Saving Time range for the current time zone. */ isDaylightSavingTime(): boolean; /** Move to the next or previous dayOfWeek. Whether to move into the future (+1) or past(-1) is controlled by the optional direction parameter. */ - moveToDayOfWeek(dayOfWeek: number, direction: number): IDateJS + moveToDayOfWeek(dayOfWeek: number, direction: number): IDateJS; /** Moves the date to the first day of the month. */ - moveToFirstDayOfMonth(): IDateJS + moveToFirstDayOfMonth(): IDateJS; /** Moves the date to the last day of the month. */ - moveToLastDayOfMonth(): IDateJS + moveToLastDayOfMonth(): IDateJS; /** Move to the next or previous month.Whether to move into the future(+1) or past(-1) is controlled by the optional direction parameter. */ - moveToMonth(month: number, direction: number): IDateJS + moveToMonth(month: number, direction: number): IDateJS; /** Moves the date to the next nth occurrence of the dayOfWeek starting from the beginning of the month. The number (-1) is a magic number and will return the last occurrence of the dayOfWeek in the month. */ - moveToNthOccurrence(dayOfWeek: number, occurrence: number): IDateJS + moveToNthOccurrence(dayOfWeek: number, occurrence: number): IDateJS; /** Set the value of year, month, day, hour, minute, second, millisecond of date instance using given configuration object. */ - set(config: IDateJSConfig): IDateJS + set(config: IDateJSLiteral): IDateJS; /** Set the timezone for the current date using a culture - specific timezone abbreviation("PST").Note that in most JavaScript implementations, this will appear to change the time since the timezone is always based on the locale. */ - setTimezone(timezoneAbbreviation: string): IDateJS + setTimezone(timezoneAbbreviation: string): IDateJS; /** Set the timezone for the current date using an offset(-0700).Note that in most JavaScript implementations, this will appear to change the time since the timezone is always based on the locale. */ - setTimezoneOffset(timezoneOffset: number): IDateJS + setTimezoneOffset(timezoneOffset: number): IDateJS; /** Converts the current date instance into a string with an ISO 8601 format.The date is converted to it's UTC value. As per the ISO 8601 specification, the string will be wrapped with double quotation marks ("). */ - toISOString(): string - /** Converts the value of the current DateJS object to its equivalent string representation.Use format argument to specify format(optional).See FormatSpecifiers for more info. */ + toISOString(): string; + /** Converts the value of the current Date object to its equivalent string representation.Use format argument to specify format(optional).See FormatSpecifiers for more info. */ toString(format?: string): string; } + diff --git a/datejs/sugarpak.d.ts b/datejs/sugarpak.d.ts new file mode 100644 index 0000000000..b52e6cf313 --- /dev/null +++ b/datejs/sugarpak.d.ts @@ -0,0 +1,271 @@ +// Type definitions for DateJS - SugarPak Extensions +// Project: http://www.datejs.com/ +// Definitions by: David Khristepher Santos +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/** SugarPak.js - Domain Specific Language - Syntactical Sugar */ +declare module sugarpak { + + export interface IAddOrientation { + millisecond(): IDateJS; + second(): IDateJS; + minute(): IDateJS; + hour(): IDateJS; + day(): IDateJS; + week(): IDateJS; + month(): IDateJS; + year(): IDateJS; + + milliseconds(): IDateJS; + seconds(): IDateJS; + minutes(): IDateJS; + hours(): IDateJS; + days(): IDateJS; + weeks(): IDateJS; + months(): IDateJS; + years(): IDateJS; + } + + export interface IOrientation extends IAddOrientation { + monday(): IDateJS; + tuesday(): IDateJS; + wednesday(): IDateJS; + thursday(): IDateJS; + friday(): IDateJS; + saturday(): IDateJS; + sunday(): IDateJS; + + mon(): IDateJS; + tue(): IDateJS; + wed(): IDateJS; + thu(): IDateJS; + fri(): IDateJS; + sat(): IDateJS; + sun(): IDateJS; + + january(): IDateJS; + february(): IDateJS; + march(): IDateJS; + april(): IDateJS; + may(): IDateJS; + june(): IDateJS; + july(): IDateJS; + august(): IDateJS; + september(): IDateJS; + october(): IDateJS; + november(): IDateJS; + december(): IDateJS; + + jan(): IDateJS; + feb(): IDateJS; + mar(): IDateJS; + apr(): IDateJS; + // may + jun(): IDateJS; + jul(): IDateJS; + aug(): IDateJS; + sep(): IDateJS; + oct(): IDateJS; + nov(): IDateJS; + dec(): IDateJS; + } + + export interface IPredicate { + today(): boolean; + + monday(): boolean; + tuesday(): boolean; + wednesday(): boolean; + thursday(): boolean; + friday(): boolean; + saturday(): boolean; + sunday(): boolean; + + mon(): boolean; + tue(): boolean; + wed(): boolean; + thu(): boolean; + fri(): boolean; + sat(): boolean; + sun(): boolean; + + january(): boolean; + february(): boolean; + march(): boolean; + april(): boolean; + may(): boolean; + june(): boolean; + july(): boolean; + august(): boolean; + september(): boolean; + october(): boolean; + november(): boolean; + december(): boolean; + + jan(): boolean; + feb(): boolean; + mar(): boolean; + apr(): boolean; + jun(): boolean; + jul(): boolean; + aug(): boolean; + sep(): boolean; + oct(): boolean; + nov(): boolean; + dec(): boolean; + + weekday(): boolean; + + day(date?: IDateJS): boolean; + week(date?: IDateJS): boolean; + month(date?: IDateJS): boolean; + year(date?: IDateJS): boolean; + + + } + + export interface IDatePartComparer { + millisecond(date?: IDateJS): boolean; + second(date?: IDateJS): boolean; + minute(date?: IDateJS): boolean; + hour(date?: IDateJS): boolean; + day(date?: IDateJS): boolean; + week(date?: IDateJS): boolean; + month(date?: IDateJS): boolean; + year(date?: IDateJS): boolean; + } + + export interface IDateElementEvaluator { + ago(): IDateJS; + before(): IDateJS; + fromNow(): IDateJS; + after(): IDateJS; + } + + export interface IDateElement { + millisecond(): IDateElementEvaluator; + second(): IDateElementEvaluator; + minute(): IDateElementEvaluator; + hour(): IDateElementEvaluator; + day(): IDateElementEvaluator; + week(): IDateElementEvaluator; + month(): IDateElementEvaluator; + year(): IDateElementEvaluator; + + milliseconds(): IDateElementEvaluator; + seconds(): IDateElementEvaluator; + minutes(): IDateElementEvaluator; + hours(): IDateElementEvaluator; + days(): IDateElementEvaluator; + weeks(): IDateElementEvaluator; + months(): IDateElementEvaluator; + years(): IDateElementEvaluator; + } + + export interface IDateLiteral extends ITimeLiteral { + day: number; + week: number; + month: number; + year: number; + } + + export interface ITimeLiteral { + millisecond: number; + second: number; + minute: number; + hour: number; + } + +} + +interface IDateJS { + add(n: number): sugarpak.IAddOrientation; + at(time: string): IDateJS; + at(time: sugarpak.ITimeLiteral): IDateJS; + is(): sugarpak.IPredicate; + next(): sugarpak.IOrientation; + last(): sugarpak.IOrientation; + prev(): sugarpak.IOrientation; + final(): sugarpak.IOrientation; + first(): sugarpak.IOrientation; + second(): sugarpak.IOrientation; + third(): sugarpak.IOrientation; + fourth(): sugarpak.IOrientation; + fifth(): sugarpak.IOrientation; + previous(): sugarpak.IOrientation; + /** Determines if two date objects occur on/in exactly the same instance of the subsequent date part function. Must be followed by a date part function (example: .day(), .month(), .year(), etc) */ + same(): sugarpak.IDatePartComparer; + /** Returns a date literal from a DateJS instance */ + toObject(): sugarpak.IDateLiteral; +} + +interface IDateJSStatic { + /** Contains the day-of-week value for Monday */ + MONDAY: number; + /** Contains the day-of-week value for Tuesday */ + TUESDAY: number; + /** Contains the day-of-week value for Wednesday */ + WEDNESDAY: number; + /** Contains the day-of-week value for Thursday */ + THURSDAY: number; + /** Contains the day-of-week value for Friday */ + FRIDAY: number; + /** Contains the day-of-week value for Saturday */ + SATURDAY: number; + /** Contains the day-of-week value for Sunday */ + SUNDAY: number; + /** Instantiates a DateJS object from a literal */ + fromObject(date: sugarpak.IDateLiteral): IDateJS; + + next(): sugarpak.IOrientation; + last(): sugarpak.IOrientation; + prev(): sugarpak.IOrientation; + previous(): sugarpak.IOrientation; + + january(): IDateJS; + february(): IDateJS; + march(): IDateJS; + april(): IDateJS; + may(): IDateJS; + june(): IDateJS; + july(): IDateJS; + august(): IDateJS; + september(): IDateJS; + october(): IDateJS; + november(): IDateJS; + december(): IDateJS; + + monday(): IDateJS; + tuesday(): IDateJS; + wednesday(): IDateJS; + thursday(): IDateJS; + friday(): IDateJS; + saturday(): IDateJS; + sunday(): IDateJS; + + mon(): IDateJS; + tue(): IDateJS; + wed(): IDateJS; + thu(): IDateJS; + fri(): IDateJS; + sat(): IDateJS; + sun(): IDateJS; + + jan(): IDateJS; + feb(): IDateJS; + mar(): IDateJS; + apr(): IDateJS; + // may + jun(): IDateJS; + jul(): IDateJS; + aug(): IDateJS; + sep(): IDateJS; + oct(): IDateJS; + nov(): IDateJS; + dec(): IDateJS; + +} + +interface Number extends sugarpak.IDateElement { + // extend the Number type with all the IDateJS goodness +} \ No newline at end of file