├── .editorconfig ├── .eslintignore ├── .eslintrc.js ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── commitlint.config.js ├── husky.config.js ├── jest.config.js ├── lib ├── module.js └── plugin.js ├── package.json ├── renovate.json ├── test ├── basic.test.js ├── default-locale.test.js ├── empty-config.test.js ├── fallback-locale.test.js ├── fixture │ ├── basic │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ ├── default-locale │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ ├── empty-config │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ ├── fallback-locale │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ ├── with-array │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ └── with-methods │ │ ├── nuxt.config.js │ │ └── pages │ │ └── index.vue ├── with-array.test.js └── with-methods.test.js ├── types ├── date-fns.d.ts └── index.d.ts └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_size = 2 6 | indent_style = space 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | # Common 2 | node_modules 3 | dist 4 | .nuxt 5 | coverage 6 | 7 | # Plugin 8 | lib/plugin.js 9 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: [ 4 | '@nuxtjs' 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | ci: 13 | runs-on: ${{ matrix.os }} 14 | 15 | strategy: 16 | matrix: 17 | # os: [ubuntu-latest, macos-latest, windows-latest] 18 | os: [ubuntu-latest] 19 | node: [10] 20 | 21 | steps: 22 | - uses: actions/setup-node@v2 23 | with: 24 | node-version: ${{ matrix.node }} 25 | 26 | - name: checkout 27 | uses: actions/checkout@v2 28 | 29 | - name: cache node_modules 30 | uses: actions/cache@v2 31 | with: 32 | path: node_modules 33 | key: ${{ matrix.os }}-node-v${{ matrix.node }}-deps-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }} 34 | 35 | - name: Install dependencies 36 | if: steps.cache.outputs.cache-hit != 'true' 37 | run: yarn 38 | 39 | - name: Lint 40 | run: yarn lint 41 | 42 | - name: Test 43 | run: yarn test 44 | 45 | - name: Coverage 46 | uses: codecov/codecov-action@v1 47 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.iml 3 | .idea 4 | *.log* 5 | .nuxt 6 | .vscode 7 | .DS_Store 8 | coverage 9 | dist 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ## [1.5.0](https://github.com/nuxt-community/date-fns-module/compare/v1.4.3...v1.5.0) (2021-03-29) 6 | 7 | 8 | ### Features 9 | 10 | * add fallback locale ([#51](https://github.com/nuxt-community/date-fns-module/issues/51)) ([bdae7ce](https://github.com/nuxt-community/date-fns-module/commit/bdae7ce67f9226f08f0a10cf93a8a442fd35e993)) 11 | * remove unsed locales ([#57](https://github.com/nuxt-community/date-fns-module/issues/57)) ([8a1673b](https://github.com/nuxt-community/date-fns-module/commit/8a1673b713b154580b06dae909dc6da4de2c0f28)) 12 | 13 | 14 | ### Bug Fixes 15 | 16 | * types ([#56](https://github.com/nuxt-community/date-fns-module/issues/56)) ([6f2f648](https://github.com/nuxt-community/date-fns-module/commit/6f2f6484522e7912067d3ba8022d0969df340d8f)) 17 | 18 | ### [1.4.3](https://github.com/nuxt-community/date-fns-module/compare/v1.4.2...v1.4.3) (2020-11-11) 19 | 20 | 21 | ### Bug Fixes 22 | 23 | * when locales is not array ([#47](https://github.com/nuxt-community/date-fns-module/issues/47)) ([f379418](https://github.com/nuxt-community/date-fns-module/commit/f379418e397809556b96680b3fdb4e12b6a17b5e)) 24 | 25 | ### [1.4.2](https://github.com/nuxt-community/date-fns-module/compare/v1.4.1...v1.4.2) (2020-11-09) 26 | 27 | 28 | ### Bug Fixes 29 | 30 | * type Locale ([#46](https://github.com/nuxt-community/date-fns-module/issues/46)) ([3f45c49](https://github.com/nuxt-community/date-fns-module/commit/3f45c4939671b74e1617da01b3e36c9a4d503495)) 31 | 32 | ### [1.4.1](https://github.com/nuxt-community/date-fns-module/compare/v1.4.0...v1.4.1) (2020-10-21) 33 | 34 | 35 | ### Bug Fixes 36 | 37 | * add formatDuration typescript support ([#43](https://github.com/nuxt-community/date-fns-module/issues/43)) ([806d37e](https://github.com/nuxt-community/date-fns-module/commit/806d37e7bc7c48b5dc5ce4280e45c157459a17cf)) 38 | 39 | ## [1.4.0](https://github.com/nuxt-community/date-fns-module/compare/v1.3.1...v1.4.0) (2020-09-08) 40 | 41 | 42 | ### Bug Fixes 43 | 44 | * add 'formatDuration' to fix [#37](https://github.com/nuxt-community/date-fns-module/issues/37) ([#38](https://github.com/nuxt-community/date-fns-module/issues/38)) ([10ea44d](https://github.com/nuxt-community/date-fns-module/commit/10ea44dff1ca7608beab194a50d56e3aee3e602f)) 45 | 46 | ### [1.3.1](https://github.com/nuxt-community/date-fns-module/compare/v1.3.0...v1.3.1) (2020-07-20) 47 | 48 | ## [1.3.0](https://github.com/nuxt-community/date-fns-module/compare/v1.2.0...v1.3.0) (2020-05-05) 49 | 50 | 51 | ### Features 52 | 53 | * add `formatDistanceToNowStrict` ([86b2601](https://github.com/nuxt-community/date-fns-module/commit/86b260175f21555f27326d47cc030277bdc6b8bd)) 54 | 55 | 56 | ### Bug Fixes 57 | 58 | * types ([ca48455](https://github.com/nuxt-community/date-fns-module/commit/ca484551fd9528f201494e299ebcb06ca783bbac)) 59 | 60 | ## [1.2.0](https://github.com/nuxt-community/date-fns-module/compare/v1.1.0...v1.2.0) (2020-01-27) 61 | 62 | 63 | ### Features 64 | 65 | * make injected date-fns treeshakeable ([#25](https://github.com/nuxt-community/date-fns-module/issues/25)) ([f6517cf](https://github.com/nuxt-community/date-fns-module/commit/f6517cf2976757a1305485649f6023ab6aa1c095)) 66 | 67 | ## [1.1.0](https://github.com/nuxt-community/date-fns-module/compare/v1.0.0...v1.1.0) (2019-11-18) 68 | 69 | 70 | ### Features 71 | 72 | * improve types ([#22](https://github.com/nuxt-community/date-fns-module/issues/22)) ([a901a18](https://github.com/nuxt-community/date-fns-module/commit/a901a18)) 73 | 74 | ## [1.0.0](https://github.com/nuxt-community/date-fns-module/compare/v0.2.0...v1.0.0) (2019-09-05) 75 | 76 | 77 | ### ⚠ BREAKING CHANGES 78 | 79 | * use date-fns v2 80 | 81 | * update module ([3688ed7](https://github.com/nuxt-community/date-fns-module/commit/3688ed7)) 82 | 83 | # [0.2.0](https://github.com/nuxt-community/date-fns-module/compare/v0.1.2...v0.2.0) (2019-08-22) 84 | 85 | 86 | 87 | ## [0.1.2](https://github.com/nuxt-community/date-fns-module/compare/v0.1.1...v0.1.2) (2019-08-22) 88 | 89 | 90 | ### Features 91 | 92 | * provide nuxt 2.9 compatible types ([#8](https://github.com/nuxt-community/date-fns-module/issues/8)) ([69ea718](https://github.com/nuxt-community/date-fns-module/commit/69ea718)) 93 | * support `date-fns` in `nuxt.config` ([6898cf7](https://github.com/nuxt-community/date-fns-module/commit/6898cf7)) 94 | 95 | 96 | ## [0.1.1](https://github.com/nuxt-community/date-fns-module/compare/v0.1.0...v0.1.1) (2019-06-24) 97 | 98 | 99 | ### Bug Fixes 100 | 101 | * types ([4e4c889](https://github.com/nuxt-community/date-fns-module/commit/4e4c889)) 102 | 103 | 104 | 105 | 106 | # [0.1.0](https://github.com/nuxt-community/date-fns-module/compare/v0.0.3...v0.1.0) (2019-06-24) 107 | 108 | 109 | ### Features 110 | 111 | * add typing ([7aa7dfe](https://github.com/nuxt-community/date-fns-module/commit/7aa7dfe)) 112 | 113 | 114 | 115 | 116 | ## [0.0.3](https://github.com/nuxt-community/date-fns-module/compare/v0.0.2...v0.0.3) (2019-03-21) 117 | 118 | 119 | ### Bug Fixes 120 | 121 | * **plugin:** override function per function ([148c96d](https://github.com/nuxt-community/date-fns-module/commit/148c96d)) 122 | 123 | 124 | 125 | 126 | ## [0.0.2](https://github.com/nuxt-community/date-fns-module/compare/v0.0.1...v0.0.2) (2019-02-17) 127 | 128 | 129 | ### Bug Fixes 130 | 131 | * **module:** `moduleOptions` should have priority ([c6381c4](https://github.com/nuxt-community/date-fns-module/commit/c6381c4)) 132 | 133 | 134 | 135 | 136 | ## 0.0.1 (2019-02-14) 137 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Nuxt Community 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @nuxtjs/date-fns 2 | 3 | [![npm version][npm-version-src]][npm-version-href] 4 | [![npm downloads][npm-downloads-src]][npm-downloads-href] 5 | [![Github Actions CI][github-actions-ci-src]][github-actions-ci-href] 6 | [![Codecov][codecov-src]][codecov-href] 7 | [![License][license-src]][license-href] 8 | 9 | > Modern JavaScript date utility library - date-fns for Nuxt.js 10 | 11 | [📖 **Release Notes**](./CHANGELOG.md) 12 | 13 | ## Setup 14 | 15 | 1. Add `@nuxtjs/date-fns` dependency to your project 16 | 17 | ```bash 18 | yarn add @nuxtjs/date-fns # or npm install @nuxtjs/date-fns 19 | ``` 20 | 21 | 2. Add `@nuxtjs/date-fns` to the `buildModules` section of `nuxt.config.js` 22 | 23 | ```js 24 | export default { 25 | buildModules: [ 26 | // Simple usage 27 | '@nuxtjs/date-fns', 28 | 29 | // With options 30 | ['@nuxtjs/date-fns', { /* module options */ }] 31 | ] 32 | } 33 | ``` 34 | 35 | :warning: If you are using Nuxt **< v2.9** you have to use `modules` section in `nuxt.config.js` instead of `buildModules`. 36 | 37 | ### Using top level options 38 | 39 | ```js 40 | export default { 41 | buildModules: [ 42 | '@nuxtjs/date-fns' 43 | ], 44 | dateFns: { 45 | /* module options */ 46 | } 47 | } 48 | ``` 49 | 50 | ### Typescript setup 51 | 52 | Add the types to your `"types"` array in `tsconfig.json` after the `@nuxt/types` entry. 53 | 54 | :warning: Use `@nuxt/vue-app` instead of `@nuxt/types` for nuxt < 2.9. 55 | 56 | #### tsconfig.json 57 | 58 | ```json 59 | { 60 | "compilerOptions": { 61 | "types": [ 62 | "@nuxt/types", 63 | "@nuxtjs/date-fns" 64 | ] 65 | } 66 | } 67 | ``` 68 | 69 | > **Why?** 70 | > 71 | > For typescript to be aware of the additions to the `nuxt Context`, the `vue instance` and the `vuex store`, the types need to be merged via [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) by adding `@nuxtjs/date-fns` to your types. 72 | 73 | ## Options 74 | 75 | ### `locales` 76 | 77 | - Type: `Array[String]` 78 | - Default: `[]` 79 | 80 | Locales to be imported. 81 | 82 | ### `defaultLocale` 83 | 84 | - Type: `String` 85 | - Default: `null` 86 | 87 | You can preset default locale. 88 | 89 | ### `fallbackLocale` 90 | 91 | - Type: `String` 92 | - Default: `null` 93 | 94 | You can preset a fallback locale for when a method is called with an unsupported locale. 95 | 96 | ### `format` 97 | 98 | - Type: `String` 99 | - Default: `null` 100 | 101 | You can preset default format. 102 | 103 | ### `methods` 104 | 105 | - Type: `Array` 106 | - Default: `null` 107 | 108 | Methods to be imported. If not defined all methods are imported. 109 | 110 | ## Usage 111 | 112 | This module inject `$dateFns` to your project: 113 | 114 | ```html 115 | 130 | 131 | 140 | ``` 141 | 142 | ## Development 143 | 144 | 1. Clone this repository 145 | 2. Install dependencies using `yarn install` or `npm install` 146 | 3. Start development server using `npm run dev` 147 | 148 | ## License 149 | 150 | [MIT License](./LICENSE) 151 | 152 | Copyright (c) Nuxt Community 153 | 154 | 155 | [npm-version-src]: https://img.shields.io/npm/v/@nuxtjs/date-fns/latest.svg 156 | [npm-version-href]: https://npmjs.com/package/@nuxtjs/date-fns 157 | 158 | [npm-downloads-src]: https://img.shields.io/npm/dt/@nuxtjs/date-fns.svg 159 | [npm-downloads-href]: https://npmjs.com/package/@nuxtjs/date-fns 160 | 161 | [github-actions-ci-src]: https://github.com/nuxt-community/date-fns-module/workflows/ci/badge.svg 162 | [github-actions-ci-href]: https://github.com/nuxt-community/date-fns-module/actions?query=workflow%3Aci 163 | 164 | [codecov-src]: https://img.shields.io/codecov/c/github/nuxt-community/date-fns-module.svg 165 | [codecov-href]: https://codecov.io/gh/nuxt-community/date-fns-module 166 | 167 | [license-src]: https://img.shields.io/npm/l/@nuxtjs/date-fns.svg 168 | [license-href]: https://npmjs.com/package/@nuxtjs/date-fns 169 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [ 3 | '@commitlint/config-conventional' 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /husky.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | hooks: { 3 | 'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS', 4 | 'pre-commit': 'yarn lint', 5 | 'pre-push': 'yarn lint' 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | collectCoverage: true, 3 | collectCoverageFrom: ['lib/**/*.js', '!lib/plugin.js'], 4 | testEnvironment: 'node' 5 | } 6 | -------------------------------------------------------------------------------- /lib/module.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | 3 | module.exports = function (moduleOptions) { 4 | if (Array.isArray(moduleOptions)) { 5 | moduleOptions = { locales: moduleOptions } 6 | } 7 | 8 | const options = { 9 | locales: [], 10 | defaultLocale: null, 11 | fallbackLocale: null, 12 | format: null, 13 | methods: null, 14 | ...this.options['date-fns'], 15 | ...this.options.dateFns, 16 | ...moduleOptions 17 | } 18 | 19 | if (!Array.isArray(options.locales)) { 20 | options.locales = [options.locales] 21 | } 22 | 23 | if (options.defaultLocale && !options.locales.includes(options.defaultLocale)) { 24 | options.locales.push(options.defaultLocale) 25 | } 26 | 27 | if (options.fallbackLocale && !options.locales.includes(options.fallbackLocale)) { 28 | options.locales.push(options.fallbackLocale) 29 | } 30 | 31 | this.addPlugin({ 32 | src: resolve(__dirname, 'plugin.js'), 33 | fileName: 'date-fns.js', 34 | options 35 | }) 36 | 37 | this.extendBuild((config) => { 38 | const { ContextReplacementPlugin } = require('webpack') 39 | 40 | config.plugins.push(new ContextReplacementPlugin( 41 | /date-fns[/\\]/, 42 | new RegExp(`[/\\\\](${options.locales.join('|')})[/\\\\]index.js$`) 43 | )) 44 | }) 45 | } 46 | 47 | module.exports.meta = require('../package.json') 48 | -------------------------------------------------------------------------------- /lib/plugin.js: -------------------------------------------------------------------------------- 1 | <% if (options.methods && options.methods.length) { %>import { <%= options.methods.join(', ') %> } from 'date-fns' 2 | <% } else { %>import * as dateFns from 'date-fns'<% } %> 3 | 4 | const locales = [] 5 | 6 | const importLocale = async (locale) => { 7 | try { 8 | locales[locale] = await import(`date-fns/locale/${locale}/index.js`) 9 | } catch (e) { 10 | // 11 | } 12 | } 13 | 14 | export default async (ctx, inject) => { 15 | <% if (options.methods && options.methods.length) { %> 16 | const dateFns = { <%= options.methods.join(', ') %> } 17 | <% } %> 18 | const dateObj = {...dateFns} 19 | 20 | <% if (options.locales && options.locales.length) { %> 21 | <%= options.locales.map(l => `await importLocale('${l}')`).join('\n') %> 22 | <% } %> 23 | 24 | function parsedDate(date) { 25 | if (dateFns.parseISO) { 26 | return typeof date !== 'string' ? date : dateFns.parseISO(date) 27 | } 28 | return date 29 | } 30 | 31 | if (dateFns.format) { 32 | dateObj.format = (date, format, options) => { 33 | return dateFns.format(parsedDate(date), format || '<%= options.format %>', mergeOptions(options)) 34 | } 35 | } 36 | 37 | if (dateFns.parse) { 38 | dateObj.parse = (dateString, formatString, backupDate, options) => { 39 | return dateFns.parse(dateString, formatString, backupDate, mergeOptions(options)) 40 | } 41 | } 42 | 43 | const threeParams = [ 44 | 'differenceInCalendarWeeks', 45 | 'formatDistance', 46 | 'formatDistanceStrict', 47 | 'formatRelative', 48 | 'isMatch', 49 | 'isSameWeek', 50 | 'setDay', 51 | 'setWeek', 52 | 'setWeekYear' 53 | ].filter(value => Object.keys(dateFns).includes(value)) 54 | 55 | for (const fn of threeParams) { 56 | dateObj[fn] = (param1, param2, options) => { 57 | return dateFns[fn](param1, param2, mergeOptions(options)) 58 | } 59 | } 60 | 61 | const twoParams = [ 62 | 'eachWeekOfInterval', 63 | 'endOfWeek', 64 | 'formatDistanceToNow', 65 | 'formatDistanceToNowStrict', 66 | 'formatDuration', 67 | 'getWeek', 68 | 'getWeekOfMonth', 69 | 'getWeeksInMonth', 70 | 'getWeekYear', 71 | 'isThisWeek', 72 | 'lastDayOfWeek', 73 | 'startOfWeek', 74 | 'startOfWeekYear' 75 | ].filter(value => Object.keys(dateFns).includes(value)) 76 | 77 | for (const fn of twoParams) { 78 | dateObj[fn] = (param, options) => { 79 | return dateFns[fn](param, mergeOptions(options)) 80 | } 81 | } 82 | 83 | ctx.$dateFns = dateObj 84 | inject('dateFns', dateObj) 85 | } 86 | 87 | function mergeOptions(options) { 88 | <% if (options.defaultLocale) { %> 89 | options = { locale: '<%= options.defaultLocale %>', ...options } 90 | <% } %> 91 | 92 | <% if (options.fallbackLocale) { %> 93 | options = { fallbackLocale: '<%= options.fallbackLocale %>', ...options } 94 | <% } %> 95 | 96 | if (options && typeof options.locale === 'string') { 97 | if (locales[options.locale]) { 98 | options.locale = locales[options.locale] 99 | } else if (options.fallbackLocale && locales[options.fallbackLocale]) { 100 | console.warn(`[date-fns] locale '${options.locale}' not found, using fallback locale '${options.fallbackLocale}'`) 101 | options.locale = locales[options.fallbackLocale] 102 | } else { 103 | console.warn(`[date-fns] locale '${options.locale}' not found.`) 104 | } 105 | } 106 | 107 | return options 108 | } 109 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@nuxtjs/date-fns", 3 | "version": "1.5.0", 4 | "description": "Modern JavaScript date utility library - date-fns for Nuxt.js", 5 | "repository": "nuxt-community/date-fns-module", 6 | "license": "MIT", 7 | "contributors": [ 8 | "Ricardo Gobbo de Souza " 9 | ], 10 | "main": "lib/module.js", 11 | "types": "types/index.d.ts", 12 | "files": [ 13 | "lib", 14 | "types/*.d.ts" 15 | ], 16 | "scripts": { 17 | "dev": "nuxt test/fixture/basic", 18 | "lint": "eslint --ext .js,.vue .", 19 | "release": "yarn test && standard-version && git push --follow-tags && npm publish", 20 | "test": "yarn lint && jest" 21 | }, 22 | "dependencies": { 23 | "date-fns": "^2.19.0" 24 | }, 25 | "devDependencies": { 26 | "@commitlint/cli": "latest", 27 | "@commitlint/config-conventional": "latest", 28 | "@nuxtjs/eslint-config": "latest", 29 | "@nuxtjs/module-test-utils": "latest", 30 | "eslint": "latest", 31 | "husky": "latest", 32 | "jest": "latest", 33 | "nuxt-edge": "latest", 34 | "standard-version": "latest" 35 | }, 36 | "publishConfig": { 37 | "access": "public" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "@nuxtjs" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /test/basic.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('basic', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'basic')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('dateFns should be defined', async () => { 15 | const window = await nuxt.renderAndGetWindow(url('/')) 16 | expect(window.$nuxt.$dateFns).toBeDefined() 17 | }) 18 | 19 | test('render monday with es locale', async () => { 20 | const html = await get('/') 21 | expect(html).toContain('lunes') 22 | }) 23 | 24 | test('render month', async () => { 25 | const window = await nuxt.renderAndGetWindow(url('/')) 26 | expect(window.document.querySelector('p').textContent).toBe('December') 27 | }) 28 | 29 | test('render year', async () => { 30 | const html = await get('/') 31 | expect(html).toContain('1995') 32 | }) 33 | }) 34 | -------------------------------------------------------------------------------- /test/default-locale.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('default locale', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'default-locale')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('dateFns should be defined', async () => { 15 | const window = await nuxt.renderAndGetWindow(url('/')) 16 | expect(window.$nuxt.$dateFns).toBeDefined() 17 | }) 18 | 19 | test('render monday with es locale', async () => { 20 | const html = await get('/') 21 | expect(html).toContain('lunes') 22 | }) 23 | 24 | test('render month', async () => { 25 | const window = await nuxt.renderAndGetWindow(url('/')) 26 | expect(window.document.querySelector('p').textContent).toBe('December') 27 | }) 28 | 29 | test('render year', async () => { 30 | const html = await get('/') 31 | expect(html).toContain('1995') 32 | }) 33 | }) 34 | -------------------------------------------------------------------------------- /test/empty-config.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('empty-config', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'empty-config')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('dateFns should be defined', async () => { 15 | const window = await nuxt.renderAndGetWindow(url('/')) 16 | expect(window.$nuxt.$dateFns).toBeDefined() 17 | }) 18 | 19 | test('render month', async () => { 20 | const window = await nuxt.renderAndGetWindow(url('/')) 21 | expect(window.document.querySelector('p').textContent).toBe('December') 22 | }) 23 | 24 | test('render year', async () => { 25 | const html = await get('/') 26 | expect(html).toContain('1995') 27 | }) 28 | }) 29 | -------------------------------------------------------------------------------- /test/fallback-locale.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('fallback locale', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'fallback-locale')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('dateFns should be defined', async () => { 15 | const window = await nuxt.renderAndGetWindow(url('/')) 16 | expect(window.$nuxt.$dateFns).toBeDefined() 17 | }) 18 | 19 | test('render monday with es fallback locale', async () => { 20 | const html = await get('/') 21 | expect(html).toContain('lunes') 22 | }) 23 | 24 | test('render month', async () => { 25 | const window = await nuxt.renderAndGetWindow(url('/')) 26 | expect(window.document.querySelector('p').textContent).toBe('December') 27 | }) 28 | 29 | test('render year', async () => { 30 | const html = await get('/') 31 | expect(html).toContain('1995') 32 | }) 33 | }) 34 | -------------------------------------------------------------------------------- /test/fixture/basic/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | { handler: require('../../../') } 5 | ], 6 | dateFns: { 7 | locales: ['es', 'ru', 'pt-BR'], 8 | defaultLocale: 'en-US', 9 | format: 'yyyy-MM-dd' 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixture/basic/pages/index.vue: -------------------------------------------------------------------------------- 1 | 8 | 9 | 18 | -------------------------------------------------------------------------------- /test/fixture/default-locale/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | { handler: require('../../../') } 5 | ], 6 | dateFns: { 7 | locales: 'es' 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/fixture/default-locale/pages/index.vue: -------------------------------------------------------------------------------- 1 | 8 | 9 | 18 | -------------------------------------------------------------------------------- /test/fixture/empty-config/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | { handler: require('../../../') } 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /test/fixture/empty-config/pages/index.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 17 | -------------------------------------------------------------------------------- /test/fixture/fallback-locale/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | { handler: require('../../../') } 5 | ], 6 | dateFns: { 7 | locales: 'unsupportedLocale', 8 | fallbackLocale: 'es' 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/fixture/fallback-locale/pages/index.vue: -------------------------------------------------------------------------------- 1 | 8 | 9 | 18 | -------------------------------------------------------------------------------- /test/fixture/with-array/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | [require('../../../'), ['es', 'ru']] 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /test/fixture/with-array/pages/index.vue: -------------------------------------------------------------------------------- 1 | 8 | 9 | 18 | -------------------------------------------------------------------------------- /test/fixture/with-methods/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | { handler: require('../../../') } 5 | ], 6 | dateFns: { 7 | locales: ['es', 'ru', 'pt-BR'], 8 | defaultLocale: 'en-US', 9 | format: 'yyyy-MM-dd', 10 | methods: ['format', 'parseISO'] 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/fixture/with-methods/pages/index.vue: -------------------------------------------------------------------------------- 1 | 8 | 9 | 18 | -------------------------------------------------------------------------------- /test/with-array.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('with array', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'with-array')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('dateFns should be defined', async () => { 15 | const window = await nuxt.renderAndGetWindow(url('/')) 16 | expect(window.$nuxt.$dateFns).toBeDefined() 17 | }) 18 | 19 | test('render monday with es locale', async () => { 20 | const html = await get('/') 21 | expect(html).toContain('lunes') 22 | }) 23 | 24 | test('render month', async () => { 25 | const window = await nuxt.renderAndGetWindow(url('/')) 26 | expect(window.document.querySelector('p').textContent).toBe('December') 27 | }) 28 | 29 | test('render year', async () => { 30 | const html = await get('/') 31 | expect(html).toContain('1995') 32 | }) 33 | }) 34 | -------------------------------------------------------------------------------- /test/with-methods.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, url } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('with methods', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'with-methods')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('dateFns should be defined', async () => { 15 | const window = await nuxt.renderAndGetWindow(url('/')) 16 | expect(window.$nuxt.$dateFns).toBeDefined() 17 | }) 18 | 19 | test('dateFns format should be defined', async () => { 20 | const window = await nuxt.renderAndGetWindow(url('/')) 21 | expect(window.$nuxt.$dateFns.format).toBeDefined() 22 | }) 23 | 24 | test('dateFns parseISO should be defined', async () => { 25 | const window = await nuxt.renderAndGetWindow(url('/')) 26 | expect(window.$nuxt.$dateFns.parseISO).toBeDefined() 27 | }) 28 | 29 | test('dateFns parse should NOT be defined', async () => { 30 | const window = await nuxt.renderAndGetWindow(url('/')) 31 | expect(window.$nuxt.$dateFns.parse).toBeUndefined() 32 | }) 33 | 34 | test('dateFns formatDistanceToNow should NOT be defined', async () => { 35 | const window = await nuxt.renderAndGetWindow(url('/')) 36 | expect(window.$nuxt.$dateFns.formatDistanceToNow).toBeUndefined() 37 | }) 38 | 39 | test('dateFns setWeekYear should NOT be defined', async () => { 40 | const window = await nuxt.renderAndGetWindow(url('/')) 41 | expect(window.$nuxt.$dateFns.setWeekYear).toBeUndefined() 42 | }) 43 | 44 | test('dateFns formatISO should NOT be defined', async () => { 45 | const window = await nuxt.renderAndGetWindow(url('/')) 46 | expect(window.$nuxt.$dateFns.formatISO).toBeUndefined() 47 | }) 48 | }) 49 | -------------------------------------------------------------------------------- /types/date-fns.d.ts: -------------------------------------------------------------------------------- 1 | import dateFns, { Interval, Locale as LocaleDateFns, Duration } from 'date-fns' 2 | 3 | type Locale = LocaleDateFns | string 4 | 5 | export interface NuxtDateFnsInstance extends dateFns { 6 | differenceInCalendarWeeks( 7 | dateLeft: Date | number, 8 | dateRight: Date | number, 9 | options?: { 10 | locale?: Locale 11 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 12 | } 13 | ): number 14 | 15 | eachWeekOfInterval( 16 | interval: Interval, 17 | options?: { 18 | locale?: Locale 19 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 20 | } 21 | ): Date[] 22 | 23 | endOfWeek( 24 | date: Date | number, 25 | options?: { 26 | locale?: Locale 27 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 28 | } 29 | ): Date 30 | 31 | format( 32 | date: Date | number, 33 | format?: string, 34 | options?: { 35 | locale?: Locale 36 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 37 | firstWeekContainsDate?: number 38 | useAdditionalWeekYearTokens?: boolean 39 | useAdditionalDayOfYearTokens?: boolean 40 | } 41 | ): string 42 | 43 | formatDistance( 44 | date: Date | number, 45 | baseDate: Date | number, 46 | options?: { 47 | includeSeconds?: boolean 48 | addSuffix?: boolean 49 | locale?: Locale 50 | } 51 | ): string 52 | 53 | formatDistanceStrict( 54 | date: Date | number, 55 | baseDate: Date | number, 56 | options?: { 57 | addSuffix?: boolean 58 | unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' 59 | roundingMethod?: 'floor' | 'ceil' | 'round' 60 | locale?: Locale 61 | } 62 | ): string 63 | 64 | formatDistanceToNow( 65 | date: Date | number, 66 | options?: { 67 | includeSeconds?: boolean 68 | addSuffix?: boolean 69 | locale?: Locale 70 | } 71 | ): string 72 | 73 | formatDistanceToNowStrict( 74 | date: Date | number, 75 | options?: { 76 | addSuffix?: boolean 77 | unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' 78 | roundingMethod?: 'floor' | 'ceil' | 'round' 79 | locale?: Locale 80 | } 81 | ): string 82 | 83 | formatDuration( 84 | duration: Duration, 85 | options?: { 86 | format?: string[] 87 | zero?: boolean 88 | delimiter?: string 89 | locale?: Locale 90 | } 91 | ): string 92 | 93 | formatRelative( 94 | date: Date | number, 95 | baseDate: Date | number, 96 | options?: { 97 | locale?: Locale 98 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 99 | } 100 | ): string 101 | 102 | getWeek( 103 | date: Date | number, 104 | options?: { 105 | locale?: Locale 106 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 107 | firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 108 | } 109 | ): number 110 | 111 | getWeekOfMonth( 112 | date: Date | number, 113 | options?: { 114 | locale?: Locale 115 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 116 | } 117 | ): number 118 | 119 | getWeeksInMonth( 120 | date: Date | number, 121 | options?: { 122 | locale?: Locale 123 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 124 | } 125 | ): number 126 | 127 | getWeekYear( 128 | date: Date | number, 129 | options?: { 130 | locale?: Locale 131 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 132 | firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 133 | } 134 | ): number 135 | 136 | isSameWeek( 137 | dateLeft: Date | number, 138 | dateRight: Date | number, 139 | options?: { 140 | locale?: Locale 141 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 142 | } 143 | ): boolean 144 | 145 | lastDayOfWeek( 146 | date: Date | number, 147 | options?: { 148 | locale?: Locale 149 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 150 | } 151 | ): Date 152 | 153 | parse( 154 | dateString: string, 155 | formatString: string, 156 | referenceDate: Date | number, 157 | options?: { 158 | locale?: Locale 159 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 160 | firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 161 | useAdditionalWeekYearTokens?: boolean 162 | useAdditionalDayOfYearTokens?: boolean 163 | } 164 | ): Date 165 | 166 | setDay( 167 | date: Date | number, 168 | day: number, 169 | options?: { 170 | locale?: Locale 171 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 172 | } 173 | ): Date 174 | 175 | setWeek( 176 | date: Date | number, 177 | week: number, 178 | options?: { 179 | locale?: Locale 180 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 181 | firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 182 | } 183 | ): Date 184 | 185 | setWeekYear( 186 | date: Date | number, 187 | weekYear: number, 188 | options?: { 189 | locale?: Locale 190 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 191 | firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 192 | } 193 | ): Date 194 | 195 | startOfWeek( 196 | date: Date | number, 197 | options?: { 198 | locale?: Locale 199 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 200 | } 201 | ): Date 202 | 203 | startOfWeekYear( 204 | date: Date | number, 205 | options?: { 206 | locale?: Locale 207 | weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 208 | firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 209 | } 210 | ): Date 211 | } 212 | -------------------------------------------------------------------------------- /types/index.d.ts: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import { NuxtDateFnsInstance } from './date-fns' 3 | 4 | declare module '@nuxt/vue-app' { 5 | interface Context { 6 | $dateFns: NuxtDateFnsInstance 7 | } 8 | 9 | interface NuxtAppOptions { 10 | $dateFns: NuxtDateFnsInstance 11 | } 12 | } 13 | 14 | // Nuxt 2.9+ 15 | declare module '@nuxt/types' { 16 | interface Context { 17 | $dateFns: NuxtDateFnsInstance 18 | } 19 | 20 | interface NuxtAppOptions { 21 | $dateFns: NuxtDateFnsInstance 22 | } 23 | } 24 | 25 | declare module 'vue/types/vue' { 26 | interface Vue { 27 | $dateFns: NuxtDateFnsInstance 28 | } 29 | } 30 | 31 | declare module 'vuex/types/index' { 32 | interface Store { 33 | $dateFns: NuxtDateFnsInstance 34 | } 35 | } 36 | --------------------------------------------------------------------------------