Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add default format #86

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -187,6 +187,22 @@ export default {
}
```

### Set default format

You can set a [default format](https://momentjs.com/docs/#/displaying/format/) via the `defaultFormat` option.

```js
export default {
buildModules: [
'@nuxtjs/moment'
],
moment: {
// Default is 'YYYY-MM-DDTHH:mm:ssZ'
defaultFormat: 'dddd, MMMM Do YYYY, h:mm:ss a' // "Sunday, February 14th 2010, 3:25:50 pm"
}
}
```

### Disable plugin

This module also registers a plugin to include all needed locales as well as injecting moment as `$moment` to Vue context. You can disable this behaviour using `plugin: false`.
Expand Down
1 change: 1 addition & 0 deletions lib/module.js
Expand Up @@ -4,6 +4,7 @@ const defaults = {
locales: [],
defaultLocale: null,
defaultTimezone: null,
defaultFormat: null,
plugin: true,
plugins: [],
timezone: false
Expand Down
2 changes: 2 additions & 0 deletions lib/plugin.js
Expand Up @@ -8,6 +8,8 @@ import moment from 'moment'

<% if(options.defaultTimezone) { %>moment.tz.setDefault('<%= options.defaultTimezone %>')<% } %>

<% if(options.defaultFormat) { %>moment.defaultFormat = '<%= options.defaultFormat %>'<% } %>

export default (ctx, inject) => {
ctx.$moment = moment
inject('moment', moment)
Expand Down
18 changes: 18 additions & 0 deletions test/default-format.test.js
@@ -0,0 +1,18 @@
const { setup, loadConfig, get } = require('@nuxtjs/module-test-utils')

describe('format', () => {
let nuxt

beforeAll(async () => {
({ nuxt } = (await setup(loadConfig(__dirname, 'default-format'))))
}, 60000)

afterAll(async () => {
await nuxt.close()
})

test('render', async () => {
const html = await get('/')
expect(html).toContain('test 2014-06-01')
})
})
9 changes: 9 additions & 0 deletions test/fixture/default-format/nuxt.config.js
@@ -0,0 +1,9 @@
module.exports = {
rootDir: __dirname,
buildModules: [
{ handler: require('../../../') }
],
moment: {
defaultFormat: '[test] YYYY-MM-DD'
}
}
5 changes: 5 additions & 0 deletions test/fixture/default-format/pages/index.vue
@@ -0,0 +1,5 @@
<template>
<div>
{{ $moment('2014-06-01T12:00:00Z').format() }}
</div>
</template>