Skip to content

Commit

Permalink
Adding locale and moment computeds
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmit committed Jan 3, 2016
1 parent 69df90d commit 477a00d
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 89 deletions.
76 changes: 42 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,54 @@ It's advisable to run `ember g ember-moment` between upgrades as dependencies ma

## Usage

### Helpers

```hbs
{{moment-format date}}
{{moment-from-now date}}
{{moment-to-now date}}
{{moment-duration ms}}
```

### Advanced Usage
### Computed Property Macros

Ships with the following computed property macros: `duration`, `humanize`, `locale`, `format`, `moment`, `toNow`, `fromNow`. They can be used individually or composed together.

[Full API Documentation](https://github.com/stefanpenner/ember-moment/wiki/Computed-Property-Macros)

#### Moment & Format Computed

Behaves like `moment()` and will return a moment object. All arguments of the underlying API are supported.

```js
import momentComputed from 'ember-moment/computeds/moment';
import format from 'ember-moment/computeds/format';

export default Ember.Component.extend({
createdOn: new Date('01/02/2016'),
createdOnFormatted: format(momentComputed('createdOn'), 'MMMM DD, YYYY')
});
```

#### i18n/Locale

Locale takes a moment object and apply a locale to that instance

```js
import momentComputed from 'ember-moment/computeds/moment';
import format from 'ember-moment/computeds/format';
import locale from 'ember-moment/computeds/locale';

export default Ember.Component.extend({
moment: Ember.inject.service(),
createdOn: new Date('01/02/2016'),
createdOnFormatted: format(locale(momentComputed('createdOn'), 'moment.locale'), 'MMMM DD, YYYY')
});
```

## Advanced Usage

### Helpers

```hbs
{{moment-format date outputFormat inputFormat}}
Expand All @@ -48,41 +88,9 @@ Recomputes the time ago every 1-second (1000 milliseconds). This is useful for
## ES6 Moment

This addon provides the ability to import moment as an ES6 module.
```js
import moment from 'moment';
```

## Computed Macro

```js
import momentDuration from 'ember-moment/computeds/duration';
import momentFormat from 'ember-moment/computeds/format';
import momentFromNow from 'ember-moment/computeds/from-now';
import momentToNow from 'ember-moment/computeds/to-now';

export default Ember.Controller.extend({
date: new Date('2013-02-08T09:30:26'),

// Takes on the behavior of moment().format()
// http://momentjs.com/docs/#/displaying/format/
shortDate: momentFormat('date', 'MM/DD/YYYY'),

// first param: date input
// second param: date format http://momentjs.com/docs/#/parsing/string-format/ (optional)
// third param: hide suffix (optional, false by default)
// http://momentjs.com/docs/#/displaying/fromnow/
timeSince: momentFromNow("12-25-1995", "MM-DD-YYYY", false), // -> output: "2 years ago"

// first param: date input
// second param: date format http://momentjs.com/docs/#/parsing/string-format/ (optional)
// third param: hide prefix (optional, false by default)
// http://momentjs.com/docs/#/displaying/tonow
computedNumHours: momentToNow("12-25-1995", "MM-DD-YYYY", false), // -> output: "in 20 years"

// duration units: seconds, minutes, hours, days, weeks, months, years
// http://momentjs.com/docs/#/durations/
computedNumHours: momentDuration(10, 'hours')
});
import moment from 'moment';
```

## Include Moment Timezone
Expand Down
4 changes: 3 additions & 1 deletion addon/computeds/-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Ember from 'ember';
import getValue from '../utils/get-value';
import getDependentKeys from '../utils/get-dependent-keys';

const { computed } = Ember;

export default function computedFactory(fn) {
return function(...args) {
const computedArgs = [].concat(getDependentKeys(args));
Expand All @@ -13,6 +15,6 @@ export default function computedFactory(fn) {
return fn.call(this, params);
});

return Ember.computed(...computedArgs);
return computed(...computedArgs);
};
}
2 changes: 1 addition & 1 deletion addon/computeds/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import moment from 'moment';
import computedFactory from './-base';

export default computedFactory(function durationComputed(params) {
return moment.duration(...params).humanize();
return moment.duration(...params);
});
27 changes: 10 additions & 17 deletions addon/computeds/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,20 @@ import getOwner from 'ember-getowner-polyfill';
import computedFactory from './-base';

const CONFIG_KEY = 'config:environment';
const { get } = Ember;

const { get, assert } = Ember;
export default computedFactory(function formatComtputed([value, optionalFormat]) {
if (!optionalFormat) {
const owner = getOwner(this);

export default computedFactory(function formatComputed(params) {
assert('At least one datetime argument required for moment computed', params.length);
if (owner && owner.hasRegistration && owner.hasRegistration(CONFIG_KEY)) {
const config = owner.resolveRegistration(CONFIG_KEY);

const owner = getOwner(this);
const momentArgs = [params[0]];

let maybeOutputFormat = params[1];

if (params.length > 2) {
momentArgs.push(params[2]);
}
else if (owner && owner.hasRegistration && owner.hasRegistration(CONFIG_KEY)) {
const config = owner.resolveRegistration(CONFIG_KEY);

if (config) {
maybeOutputFormat = get(config, 'moment.outputFormat');
if (config) {
optionalFormat = get(config, 'moment.outputFormat');
}
}
}

return moment.apply(this, momentArgs).format(maybeOutputFormat);
return moment(value).format(optionalFormat);
});
2 changes: 1 addition & 1 deletion addon/computeds/from-now.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import computedFactory from './-base';
export default computedFactory(function fromNowComputed(params) {
let maybeHideSuffix;

if (params.length > 2) {
if (params.length > 1) {
maybeHideSuffix = params.pop();
}

Expand Down
10 changes: 10 additions & 0 deletions addon/computeds/humanize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import moment from 'moment';
import computedFactory from './-base';

export default computedFactory(function humanizeComputed([duration, suffixless]) {
if (!moment.isDuration(duration)) {
duration = moment.duration(duration);
}

return duration.humanize(suffixless);
});
11 changes: 11 additions & 0 deletions addon/computeds/locale.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 localeComputed([value, locale]) {
if (moment.isMoment(value)) {
value = moment(value);
}

return value.locale(locale);
});
7 changes: 7 additions & 0 deletions addon/computeds/moment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import moment from 'moment';

import computedFactory from './-base';

export default computedFactory(function momentComputed(params) {
return moment(...params);
});
2 changes: 1 addition & 1 deletion addon/computeds/to-now.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import computedFactory from './-base';
export default computedFactory(function toNowComputed(params) {
let maybeHidePrefix;

if (params.length > 2) {
if (params.length > 1) {
maybeHidePrefix = params.pop();
}

Expand Down
15 changes: 9 additions & 6 deletions tests/dummy/app/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Ember from 'ember';
import momentDuration from 'ember-moment/computeds/duration';
import momentFormat from 'ember-moment/computeds/format';
import momentFromNow from 'ember-moment/computeds/from-now';
import duration from 'ember-moment/computeds/duration';
import format from 'ember-moment/computeds/format';
import fromNow from 'ember-moment/computeds/from-now';
import locale from 'ember-moment/computeds/locale';
import humanize from 'ember-moment/computeds/humanize';
import momentComputed from 'ember-moment/computeds/moment';

export default Ember.Controller.extend({
moment: Ember.inject.service(),
Expand All @@ -18,8 +21,8 @@ export default Ember.Controller.extend({
lastHour: new Date(new Date().valueOf() - (60*60*1000)),
date: new Date(),
numHours: 822,
computedDate: momentFormat('date'),
computedOneHourAgo: momentFromNow('lastHour'),
computedNumHours: momentDuration('numHours', 'hours'),
computedDate: format(locale(momentComputed('date'), 'moment.locale')),
computedOneHourAgo: fromNow(locale(momentComputed('lastHour'), 'moment.locale')),
computedNumHours: humanize(locale(duration('numHours', 'hours'), 'moment.locale')),
usIndependenceDay: new Date(1776, 6, 4, 12, 0, 0)
});
25 changes: 20 additions & 5 deletions tests/unit/computeds/duration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Ember from 'ember';
import moment from 'moment';
import getOwner from 'ember-getowner-polyfill';
import { moduleFor, test } from 'ember-qunit';
import computedDuration from 'ember-moment/computeds/duration';
import duration from 'ember-moment/computeds/duration';
import humanize from 'ember-moment/computeds/humanize';
import locale from 'ember-moment/computeds/locale';

moduleFor('ember-moment@computed:duration', {
setup() {
Expand All @@ -11,7 +13,7 @@ moduleFor('ember-moment@computed:duration', {
}
});

function createSubject(attrs={}) {
function createSubject(attrs) {
return getOwner(this).resolveRegistration('object:empty').extend(Ember.$.extend(attrs, {
container: this.container,
registry: this.registry
Expand All @@ -23,20 +25,33 @@ test('get and set (ms)', function(assert) {

const subject = createSubject.call(this, {
ms: 5000,
duration: computedDuration('ms')
duration: humanize(duration('ms'))
});

assert.equal(subject.get('duration'), 'a few seconds');
subject.set('ms', 10800000);
assert.equal(subject.get('duration'), '3 hours');
});

test('computed composition using locale and humanize', function(assert) {
assert.expect(2);

const subject = createSubject.call(this, {
ms: 5000,
duration: humanize(locale(duration('ms'), 'es'))
});

assert.equal(subject.get('duration'), 'unos segundos');
subject.set('ms', 10800000);
assert.equal(subject.get('duration'), '3 horas');
});

test('get and set (days)', function(assert) {
assert.expect(2);

const subject = createSubject.call(this, {
numDays: 4,
duration: computedDuration('numDays', 'days')
duration: humanize(duration('numDays', 'days'))
});

assert.equal(subject.get('duration'), '4 days');
Expand All @@ -48,7 +63,7 @@ test('get literal (ms)', function(assert) {
assert.expect(1);

const subject = createSubject.call(this, {
duration: computedDuration(5000)
duration: humanize(duration(5000))
});

assert.equal(subject.get('duration'), 'a few seconds');
Expand Down
Loading

0 comments on commit 477a00d

Please sign in to comment.