Skip to content

Commit

Permalink
Merge pull request #347 from spira/feature/moment-date
Browse files Browse the repository at this point in the history
Adding momentDate type. Updating userProfile.
  • Loading branch information
zakhenry committed Jan 10, 2016
2 parents 55b6e05 + e230cca commit da26ff3
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 49 deletions.
5 changes: 3 additions & 2 deletions app/bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"jquery": "~2.1.3",
"lil-uuid": "~0.1.0",
"lodash": "~3.10",
"moment": "~2.10",
"moment": "~2.11.1",
"moment-timezone": "~0.4.0",
"ng-file-upload": "~7.0.15",
"ngInfiniteScroll": "~1.2.1",
Expand All @@ -58,7 +58,8 @@
"angular": "^1.4.8",
"ng-file-upload": "~7.0.15",
"angular-material": "~1.0.0",
"angular-animate": "^1.4.8"
"angular-animate": "^1.4.8",
"moment": "~2.11.1"
},
"overrides": {
"simplemde": {
Expand Down
14 changes: 1 addition & 13 deletions app/src/app/_abstract/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,7 @@ namespace app.abstract.profile {

export const namespace = 'app.abstract.profile';

export class AbstractProfileController {

static $inject = [
'userService',
'notificationService',
'authService',
'countries',
'timezones',
'genderOptions',
'regions',
'providerTypes',
'fullUserInfo',
];
export abstract class AbstractProfileController {

protected showEditPassword:boolean = false;

Expand Down
9 changes: 4 additions & 5 deletions app/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,19 @@ namespace app {

// Configure MD-Datepicker to work with moment objects
// Refer to moment.ts for further moment hacks to get this working
// Refer to datePickerDecorator.ts for overridden functions in the date picker source
$mdDateLocaleProvider.parseDate = (date:string):moment.Moment => {
return moment(date, DATEPICKER_FORMAT);
$mdDateLocaleProvider.parseDate = (date:string):moment.MomentDate => {
return momentDate(date, DATEPICKER_FORMAT);
};

$mdDateLocaleProvider.formatDate = (date:Object):string => {
// Unhelpful, but date is always of type Object. It comes in 2 forms:
// 1. A moment instance - This occurs when the date picker is set up
// 2. A date time string - This occurs when a date is picked from the picker window
try {
return (<moment.Moment>date).format(DATEPICKER_FORMAT);
return (<moment.MomentDate>date).format(DATEPICKER_FORMAT);
}
catch(e) {
return moment(date).format(DATEPICKER_FORMAT);
return momentDate(date).format(DATEPICKER_FORMAT);
}
};

Expand Down
27 changes: 0 additions & 27 deletions app/src/common/libs/moment.ts

This file was deleted.

40 changes: 40 additions & 0 deletions app/src/common/libs/moment/moment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Moment overrides and additions

declare module moment {
interface Duration {
toJSON():string;
}

interface Moment {
getFullYear():number;
getMonth():number;
getDate():number;
getTime():string;
setHours(...args):Moment;
}
}


(<any>moment).duration.fn.toString = function() {
return (<any>moment)(this.hours() + ':' + this.minutes() + ':' + this.seconds(), 'HH:mm:ss').format('HH:mm:ss');
};

(<any>moment).duration.fn.toJSON = (<any>moment).duration.fn.toString;

// Replace javascript Date object functions with moment ones.
// This allows moment object to be used with date pickers.
// See: DateLocaleProvider.prototype.$get:defaultFormatDate() in Angular Material source.
// Refer to app.ts for further datepicker configuration.

// These functions are required when the datepicker is opened
(<any>moment).fn.getFullYear = (<any>moment).fn.year;

(<any>moment).fn.getMonth = (<any>moment).fn.month;

(<any>moment).fn.getDate = (<any>moment).fn.date;

(<any>moment).fn.getTime = function() {
return this.format('x'); // Returning time in milliseconds
};

(<any>moment).fn.setHours = (<any>moment).fn.hours;
92 changes: 92 additions & 0 deletions app/src/common/libs/moment/momentDate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
describe('Moment', () => {

describe('Moment overrides and additions', () => {

it('should be able to get the full year', () => {

expect(moment('1988-05-14').getFullYear()).to.equal(1988);

});

it('should be able to get the month', () => {

expect(moment('1988-05-14').getMonth()).to.equal(4);

});

it('should be able to get the date', () => {

expect(moment('1988-05-14').getDate()).to.equal(14);

});

it('should be able to get the time', () => {

expect(moment(111222333444555).getTime()).to.equal('111222333444555');

});

it('should be able to set hours', () => {

expect(moment('1988-05-14 13:00:05').setHours(14).hours()).to.equal(14);

});

});

describe('Moment duration overrides and additions', () => {

it('should be able to format into a string', () => {

expect(moment.duration(11111111).toString()).to.equal('03:05:11');

});

it('should be able to format to JSON', () => {

expect(moment.duration(11111111).toJSON()).to.equal('03:05:11');

});

});

describe('MomentDate function', () => {

it('should be able to create a new moment date object', () => {

let mDate = momentDate('1988-05-14');

expect(mDate).to.be.instanceOf(moment);

});

it('should have standard moment functions', () => {

let mDate = momentDate('1988-05-14');
let m = moment('1988-05-14');

expect(mDate.hours()).to.equal(m.hours());
expect(mDate.isBefore('2010-10-21')).to.equal(m.isBefore('2010-10-21'));

});

it('should have a different toString function', () => {

let mDate = momentDate('1988-05-14');
let m = moment('1988-05-14');

expect(mDate.toISOString()).to.not.equal(m.toISOString());
expect(mDate.toISOString()).to.equal('1988-05-14');
expect(JSON.stringify(mDate)).to.equal('"1988-05-14"');
expect(mDate.toJSON()).to.equal('1988-05-14');

// Timezone is included in this output
expect(mDate.toString()).to.equal('Sat May 14 1988');
expect(m.toString()).to.have.string('Sat May 14 1988 00:00:00');
});


});


});
26 changes: 26 additions & 0 deletions app/src/common/libs/moment/momentDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
declare module moment {
interface MomentDate extends Moment {
}
}

function momentDate(...args) {
var self = (<any>moment)(...args);

(<any>self).__proto__ = momentDate.prototype;

return self;
}

momentDate.prototype.__proto__ = (<any>moment).prototype;

momentDate.prototype.toString = function () {
return this.format('ddd MMM DD YYYY');
};

momentDate.prototype.toISOString = function () {
return this.format('YYYY-MM-DD');
};

momentDate.prototype.toJSON = function () {
return this.format('YYYY-MM-DD');
};
9 changes: 9 additions & 0 deletions app/src/common/models/abstractModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ namespace common.models {
return moment(value);
}

/**
* Converts a momentDate object
* @param value
* @returns {MomentDate}
*/
protected castMomentDate(value:string):moment.MomentDate {
return momentDate(value);
}

/**
* Converts a time string to a moment duration object
* @param value
Expand Down
4 changes: 2 additions & 2 deletions app/src/common/models/user/userProfileModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module common.models {
export class UserProfile extends AbstractModel {

protected __attributeCastMap:IAttributeCastMap = {
dob: this.castMoment,
dob: this.castMomentDate,
};

public userId:string;
public dob:moment.Moment;
public dob:moment.MomentDate;
public mobile:string;
public phone:string;
public gender:string;
Expand Down

0 comments on commit da26ff3

Please sign in to comment.