Skip to content

Commit

Permalink
Adding tz and calendar computed property macros
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmit committed Jan 23, 2016
1 parent 9e1929f commit be87a04
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
13 changes: 13 additions & 0 deletions addon/computeds/calendar.js
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);
});
11 changes: 11 additions & 0 deletions addon/computeds/tz.js
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);
});
45 changes: 45 additions & 0 deletions tests/unit/computeds/calendar-test.js
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 calendarComputed 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: calendarComputed('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: calendarComputed('date', 'referenceDate')
});

assert.equal(subject.get('computedDate'), 'hoy a las 3:30');
});

0 comments on commit be87a04

Please sign in to comment.