-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding tz and calendar computed property macros
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import moment from 'moment'; | ||
|
||
import computedFactory from './-base'; | ||
|
||
export default computedFactory(function calendarComputed(params) { | ||
if (!params || params && params.length > 2) { | ||
throw new TypeError('ember-moment: Invalid Number of arguments, at most 2'); | ||
} | ||
|
||
const [date, referenceTime] = params; | ||
|
||
return moment(date).calendar(referenceTime); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import moment from 'moment'; | ||
|
||
import computedFactory from './-base'; | ||
|
||
export default computedFactory(function tzComputed([value, tz]) { | ||
if (moment.isMoment(value)) { | ||
value = moment(value); | ||
} | ||
|
||
return value.tz(tz); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Ember from 'ember'; | ||
import moment from 'moment'; | ||
import getOwner from 'ember-getowner-polyfill'; | ||
import { moduleFor, test } from 'ember-qunit'; | ||
import calendar from 'ember-moment/computeds/calendar'; | ||
import tz from 'ember-moment/computeds/tz'; | ||
import locale from 'ember-moment/computeds/locale'; | ||
|
||
moduleFor('ember-moment@computed:moment', { | ||
setup() { | ||
this.register('object:empty', Ember.Object.extend({})); | ||
moment.locale('en'); | ||
} | ||
}); | ||
|
||
function createSubject(attrs) { | ||
return getOwner(this).resolveRegistration('object:empty').extend(Ember.$.extend(attrs, { | ||
container: this.container, | ||
registry: this.registry | ||
})).create(); | ||
} | ||
|
||
test('two args (date, referenceDate)', function(assert) { | ||
assert.expect(1); | ||
|
||
const subject = createSubject.call(this, { | ||
date: tz(moment('2013-01-01T02:30:26Z'), 'America/New_York'), | ||
referenceDate: moment('2013-01-01T12:00:00Z'), | ||
computedDate: calendar('date', 'referenceDate') | ||
}); | ||
|
||
assert.equal(subject.get('computedDate'), 'Yesterday at 9:30 PM'); | ||
}); | ||
|
||
test('with es locale', function(assert) { | ||
assert.expect(1); | ||
|
||
const subject = createSubject.call(this, { | ||
date: tz(locale(moment('2013-01-01T08:30:26Z'), 'es'), 'America/New_York'), | ||
referenceDate: locale(moment('2013-01-01T12:00:00Z'), 'es'), | ||
computedDate: calendar('date', 'referenceDate') | ||
}); | ||
|
||
assert.equal(subject.get('computedDate'), 'hoy a las 3:30'); | ||
}); |