From 06e41f2b811ecbf7fe127427c1ab4c561317c67c Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 10:19:42 +0800 Subject: [PATCH 01/20] refactor: util is now exported as a plain object --- karma.sauce.conf.js | 1 + src/index.js | 2 +- src/utils.js | 21 +++++++++++++++------ test/utils.test.js | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/karma.sauce.conf.js b/karma.sauce.conf.js index a146e974d..9403eb49f 100644 --- a/karma.sauce.conf.js +++ b/karma.sauce.conf.js @@ -90,6 +90,7 @@ module.exports = function (config) { colors: true, logLevel: config.LOG_DEBUG, sauceLabs: { + // build: 'Manual', testName: 'Day.js' }, captureTimeout: 200000, // try fix ios timeout diff --git a/src/index.js b/src/index.js index a4cfc6d84..7bc099c4a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ import * as C from './constant' -import * as U from './utils' +import U from './utils' import en from './locale/en' let L = 'en' // global locale diff --git a/src/utils.js b/src/utils.js index 4d727d2f6..de18ac760 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,17 +1,17 @@ -export const padStart = (string, length, pad) => { +const padStart = (string, length, pad) => { const s = String(string) if (!s || s.length >= length) return string return `${Array((length + 1) - s.length).join(pad)}${string}` } -export const padZoneStr = (negMinuts) => { +const padZoneStr = (negMinuts) => { const minutes = Math.abs(negMinuts) const hourOffset = Math.floor(minutes / 60) const minuteOffset = minutes % 60 return `${negMinuts <= 0 ? '+' : '-'}${padStart(hourOffset, 2, '0')}:${padStart(minuteOffset, 2, '0')}` } -export const monthDiff = (a, b) => { +const monthDiff = (a, b) => { // function from moment.js in order to keep the same result const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month()) const anchor = a.clone().add(wholeMonthDiff, 'months') @@ -27,8 +27,17 @@ export const monthDiff = (a, b) => { return Number(-(wholeMonthDiff + adjust)) } -export const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n)) +const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n)) -export const prettyUnit = u => (u && String(u).toLowerCase().replace(/s$/, '')) +const prettyUnit = u => (u && String(u).toLowerCase().replace(/s$/, '')) -export const isUndefined = s => s === undefined +const isUndefined = s => s === undefined + +export default { + padStart, + padZoneStr, + monthDiff, + absFloor, + prettyUnit, + isUndefined +} diff --git a/test/utils.test.js b/test/utils.test.js index 5ab21fddc..e70623e79 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -1,4 +1,4 @@ -import * as Utils from '../src/utils' +import Utils from '../src/utils' const { prettyUnit, From 5e5780bc359ffabe84c6f3c3428de3ee898395c1 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 11:21:32 +0800 Subject: [PATCH 02/20] chore: en should be a built-in default locale --- src/constant.js | 9 ++++++--- src/index.js | 3 +-- src/locale/en.js | 7 ------- test/locale.test.js | 3 +-- 4 files changed, 8 insertions(+), 14 deletions(-) delete mode 100644 src/locale/en.js diff --git a/src/constant.js b/src/constant.js index d92932022..fc6c079bc 100644 --- a/src/constant.js +++ b/src/constant.js @@ -21,12 +21,15 @@ export const Q = 'quarter' export const Y = 'year' export const DATE = 'date' -export const WEEKDAYS = 'Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday'.split('.') -export const MONTHS = 'January.February.March.April.May.June.July.August.September.October.November.December'.split('.') - export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ' // regex export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{1,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/ export const REGEX_FORMAT = /\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g +export const en = { + name: 'en', + weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'), + months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_') +} + diff --git a/src/index.js b/src/index.js index 7bc099c4a..37dad9eb9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,9 @@ import * as C from './constant' import U from './utils' -import en from './locale/en' let L = 'en' // global locale const Ls = {} // global loaded locale -Ls[L] = en +Ls[L] = C.en const isDayjs = d => d instanceof Dayjs // eslint-disable-line no-use-before-define diff --git a/src/locale/en.js b/src/locale/en.js deleted file mode 100644 index 9128a9126..000000000 --- a/src/locale/en.js +++ /dev/null @@ -1,7 +0,0 @@ -import * as C from '../constant' - -export default { // may be we just need these two at the present - name: 'en', - weekdays: C.WEEKDAYS, - months: C.MONTHS -} diff --git a/test/locale.test.js b/test/locale.test.js index e90185b9c..88d34e7cf 100644 --- a/test/locale.test.js +++ b/test/locale.test.js @@ -1,7 +1,6 @@ import MockDate from 'mockdate' import dayjs from '../src' import es from '../src/locale/es' -import en from '../src/locale/en' beforeEach(() => { MockDate.set(new Date()) @@ -39,7 +38,7 @@ it('set locale for this line only', () => { }) it('set global locale', () => { - dayjs.locale(en) + dayjs.locale('en') expect(dayjs('2018-4-28').format(format)) .toBe('Saturday 28, April') dayjs.locale(es) From cac9b83f0c8e8e8b50962f7df6b9f59206fb597e Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 13:48:15 +0800 Subject: [PATCH 03/20] chore: add zh-cn zh-hk locale --- src/locale/es.js | 5 +++-- src/locale/zh-cn.js | 6 ++++++ src/locale/zh-hk.js | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/locale/zh-cn.js create mode 100644 src/locale/zh-hk.js diff --git a/src/locale/es.js b/src/locale/es.js index 7470bffcb..aecb9b551 100644 --- a/src/locale/es.js +++ b/src/locale/es.js @@ -1,5 +1,6 @@ export default { name: 'es', - weekdays: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'], - months: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'] + weekdays: 'Domingo_Lunes_Martes_Miércoles_Jueves_Viernes_Sábado'.split('_'), + months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'), + ordinal: n => `${n}º` } diff --git a/src/locale/zh-cn.js b/src/locale/zh-cn.js new file mode 100644 index 000000000..be3ce6360 --- /dev/null +++ b/src/locale/zh-cn.js @@ -0,0 +1,6 @@ +export default { + name: 'cn', + weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'), + months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'), + ordinal: n => n +} diff --git a/src/locale/zh-hk.js b/src/locale/zh-hk.js new file mode 100644 index 000000000..be3ce6360 --- /dev/null +++ b/src/locale/zh-hk.js @@ -0,0 +1,6 @@ +export default { + name: 'cn', + weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'), + months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'), + ordinal: n => n +} From 44d2fb884954cd8169a158559df752070d5574a7 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 14:58:27 +0800 Subject: [PATCH 04/20] test: add locale key test --- test/locale/keys.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 test/locale/keys.test.js diff --git a/test/locale/keys.test.js b/test/locale/keys.test.js new file mode 100644 index 000000000..e5d14bb2d --- /dev/null +++ b/test/locale/keys.test.js @@ -0,0 +1,19 @@ +/* eslint-disable camelcase */ +import es from '../../src/locale/es' +import zh_cn from '../../src/locale/zh-cn' +import zh_hk from '../../src/locale/zh-hk' + +const L = [es, zh_cn, zh_hk] + +it('Locale keys', () => { + L.forEach((l) => { + const { + name, ordinal, weekdays, months + } = l + expect(name).toEqual(expect.any(String)) + expect(weekdays).toEqual(expect.any(Array)) + expect(months).toEqual(expect.any(Array)) + // function pass date return string or number or null + expect(ordinal(1)).toEqual(expect.anything()) + }) +}) From 0177508b31fc5174a0c6b38c02f39d75739fe367 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 17:53:50 +0800 Subject: [PATCH 05/20] test: mock dayjs, to prevent variable pollution, load locales instead of returning object --- .eslintrc.json | 3 +++ package.json | 3 +++ src/locale/es.js | 8 +++++++- src/locale/zh-cn.js | 10 ++++++++-- src/locale/zh-hk.js | 6 ------ test/__mocks__/dayjs.js | 5 +++++ test/locale/keys.test.js | 5 +++-- 7 files changed, 29 insertions(+), 11 deletions(-) delete mode 100644 src/locale/zh-hk.js create mode 100644 test/__mocks__/dayjs.js diff --git a/.eslintrc.json b/.eslintrc.json index a69b5f2cf..6345f358c 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -24,6 +24,9 @@ ], "func-names": [ 0 + ], + "import/no-extraneous-dependencies": [ + 0 ] } } \ No newline at end of file diff --git a/package.json b/package.json index d03402263..0e141e5e7 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,9 @@ "lint" ], "jest": { + "roots": [ + "test" + ], "testRegex": "test/(.*?/)?.*test.js$", "coverageDirectory": "./coverage/", "collectCoverage": true, diff --git a/src/locale/es.js b/src/locale/es.js index aecb9b551..9af582382 100644 --- a/src/locale/es.js +++ b/src/locale/es.js @@ -1,6 +1,12 @@ -export default { +import dayjs from '../index' + +const locale = { name: 'es', weekdays: 'Domingo_Lunes_Martes_Miércoles_Jueves_Viernes_Sábado'.split('_'), months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'), ordinal: n => `${n}º` } + +dayjs.locale(locale, null, true) + +export default locale diff --git a/src/locale/zh-cn.js b/src/locale/zh-cn.js index be3ce6360..6480ebd23 100644 --- a/src/locale/zh-cn.js +++ b/src/locale/zh-cn.js @@ -1,6 +1,12 @@ -export default { - name: 'cn', +import dayjs from 'dayjs' + +const locale = { + name: 'zh-cn', weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'), months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'), ordinal: n => n } + +dayjs.locale(locale, null, true) + +export default locale diff --git a/src/locale/zh-hk.js b/src/locale/zh-hk.js deleted file mode 100644 index be3ce6360..000000000 --- a/src/locale/zh-hk.js +++ /dev/null @@ -1,6 +0,0 @@ -export default { - name: 'cn', - weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'), - months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'), - ordinal: n => n -} diff --git a/test/__mocks__/dayjs.js b/test/__mocks__/dayjs.js new file mode 100644 index 000000000..abbe0fa57 --- /dev/null +++ b/test/__mocks__/dayjs.js @@ -0,0 +1,5 @@ +const dayjs = () => { +} +dayjs.locale = () => { +} +module.exports = dayjs diff --git a/test/locale/keys.test.js b/test/locale/keys.test.js index e5d14bb2d..8a8460b1a 100644 --- a/test/locale/keys.test.js +++ b/test/locale/keys.test.js @@ -1,9 +1,10 @@ /* eslint-disable camelcase */ import es from '../../src/locale/es' import zh_cn from '../../src/locale/zh-cn' -import zh_hk from '../../src/locale/zh-hk' -const L = [es, zh_cn, zh_hk] +jest.mock('dayjs') + +const L = [es, zh_cn] it('Locale keys', () => { L.forEach((l) => { From 09cff021ce7c1a7830dcfeb59c5a3ebd5b80d4e6 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 18:21:10 +0800 Subject: [PATCH 06/20] build: move build code to a separate folder --- .npmignore | 7 ++++++- build/index.js | 19 +++++++++++++++++++ build/rollup.config.js | 22 ++++++++++++++++++++++ package.json | 4 ++-- rollup.config.js | 18 ------------------ 5 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 build/index.js create mode 100644 build/rollup.config.js delete mode 100644 rollup.config.js diff --git a/.npmignore b/.npmignore index b318b779b..7b67ff777 100644 --- a/.npmignore +++ b/.npmignore @@ -10,4 +10,9 @@ yarn.lock package-lock.json # jest -coverage \ No newline at end of file +coverage + +# dev +src +test +build \ No newline at end of file diff --git a/build/index.js b/build/index.js new file mode 100644 index 000000000..219f3d8d8 --- /dev/null +++ b/build/index.js @@ -0,0 +1,19 @@ +const rollup = require('rollup') +const configFactory = require('./rollup.config') + +// load locale + +// load plugin + +// core + +async function build() { + const option = configFactory({ + input: './src/index.js', + fileName: './dayjs.min.js' + }) + const bundle = await rollup.rollup(option.input) + await bundle.write(option.output) +} + +build() diff --git a/build/rollup.config.js b/build/rollup.config.js new file mode 100644 index 000000000..55924ee44 --- /dev/null +++ b/build/rollup.config.js @@ -0,0 +1,22 @@ +const babel = require('rollup-plugin-babel') +const uglify = require('rollup-plugin-uglify') + +module.exports = (config) => { + const { input, fileName } = config + return { + input: { + input + }, + output: { + file: fileName, + format: 'umd', + name: 'dayjs' + }, + plugins: [ + babel({ + exclude: 'node_modules/**' + }), + uglify() + ] + } +} diff --git a/package.json b/package.json index 0e141e5e7..3276b3def 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "types": "index.d.ts", "scripts": { "test": "jest", - "lint": "./node_modules/.bin/eslint src/* test/*", - "build": "BABEL_ENV=build rollup -c", + "lint": "./node_modules/.bin/eslint src/* test/* build/*", + "build": "BABEL_ENV=build node build", "sauce": "npx karma start karma.sauce.conf.js", "test:sauce": "npm run sauce -- 0 && npm run sauce -- 1 && npm run sauce -- 2 && npm run sauce -- 3", "gzip-size": "gzip-size ./dist/*.min.js" diff --git a/rollup.config.js b/rollup.config.js deleted file mode 100644 index 6a1fc4d54..000000000 --- a/rollup.config.js +++ /dev/null @@ -1,18 +0,0 @@ -import babel from 'rollup-plugin-babel' -import uglify from 'rollup-plugin-uglify' -import packageInfo from './package.json'; - -export default { - input: 'src/index.js', - output: { - file: `dist/${packageInfo.name}.min.js`, - format: 'umd', - name: 'dayjs' - }, - plugins: [ - babel({ - exclude: 'node_modules/**' - }), - uglify() - ] -}; \ No newline at end of file From eec272c84a567103bd5e2286524fea0d24e63e2d Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 18:29:55 +0800 Subject: [PATCH 07/20] build: add build plugin --- build/rollup.config.js | 16 ++++++++-------- package.json | 2 +- test/__mocks__/dayjs.js | 5 +---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/build/rollup.config.js b/build/rollup.config.js index 55924ee44..2f6750363 100644 --- a/build/rollup.config.js +++ b/build/rollup.config.js @@ -5,18 +5,18 @@ module.exports = (config) => { const { input, fileName } = config return { input: { - input + input, + plugins: [ + babel({ + exclude: 'node_modules/**' + }), + uglify() + ] }, output: { file: fileName, format: 'umd', name: 'dayjs' - }, - plugins: [ - babel({ - exclude: 'node_modules/**' - }), - uglify() - ] + } } } diff --git a/package.json b/package.json index 3276b3def..f5891a392 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "build": "BABEL_ENV=build node build", "sauce": "npx karma start karma.sauce.conf.js", "test:sauce": "npm run sauce -- 0 && npm run sauce -- 1 && npm run sauce -- 2 && npm run sauce -- 3", - "gzip-size": "gzip-size ./dist/*.min.js" + "gzip-size": "gzip-size dayjs.min.js" }, "pre-commit": [ "lint" diff --git a/test/__mocks__/dayjs.js b/test/__mocks__/dayjs.js index abbe0fa57..4ff8351c0 100644 --- a/test/__mocks__/dayjs.js +++ b/test/__mocks__/dayjs.js @@ -1,5 +1,2 @@ -const dayjs = () => { -} -dayjs.locale = () => { -} +const dayjs = require('../../src') module.exports = dayjs From 39bff8fe2b293ee08b3d9f536b67192a52a23730 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 18:30:10 +0800 Subject: [PATCH 08/20] chore: lint --- test/__mocks__/dayjs.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/__mocks__/dayjs.js b/test/__mocks__/dayjs.js index 4ff8351c0..b696adbea 100644 --- a/test/__mocks__/dayjs.js +++ b/test/__mocks__/dayjs.js @@ -1,2 +1,3 @@ const dayjs = require('../../src') + module.exports = dayjs From 5095fe47fa1565b6e11a025a6957101654f4bf2d Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 23:01:38 +0800 Subject: [PATCH 09/20] chore: lint --- .eslintrc.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.eslintrc.json b/.eslintrc.json index 6345f358c..97a4645f8 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -27,6 +27,18 @@ ], "import/no-extraneous-dependencies": [ 0 + ], + "import/no-unresolved": [ + 2, + { + "ignore": [ + "dayjs" + ] + } + ], + "import/extensions": [ + 2, + "ignorePackages" ] } } \ No newline at end of file From d49a78811536ef121df140681f4f90cfc718ac1b Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 23:13:08 +0800 Subject: [PATCH 10/20] test: auto load locale to test --- test/locale/keys.test.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test/locale/keys.test.js b/test/locale/keys.test.js index 8a8460b1a..d3eeb89ba 100644 --- a/test/locale/keys.test.js +++ b/test/locale/keys.test.js @@ -1,10 +1,17 @@ -/* eslint-disable camelcase */ -import es from '../../src/locale/es' -import zh_cn from '../../src/locale/zh-cn' +import fs from 'fs' +import path from 'path' jest.mock('dayjs') -const L = [es, zh_cn] +const localeDir = '../../src/locale' +const L = [] + +// load all locales from locale dir +fs.readdirSync(path.join(__dirname, localeDir)) + .forEach((file) => { + // eslint-disable-next-line + L.push(require(path.join(__dirname, localeDir, file)).default) + }) it('Locale keys', () => { L.forEach((l) => { From 8c49e438d7b4400c364de40af408a16b55435b2f Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 23:28:03 +0800 Subject: [PATCH 11/20] chore: gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index cf2838a38..5679e2c4a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,7 @@ package-lock.json coverage # dist -dist \ No newline at end of file +dist + +#dev +demo.js \ No newline at end of file From cf78bcf67efe8e6d3712ff28c4c330e4250ce1ec Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 14 May 2018 23:47:40 +0800 Subject: [PATCH 12/20] build: build update --- build/index.js | 42 ++++++++++++++++++++++++++++++++---------- build/rollup.config.js | 3 ++- src/locale/es.js | 2 +- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/build/index.js b/build/index.js index 219f3d8d8..b752cfd79 100644 --- a/build/index.js +++ b/build/index.js @@ -1,19 +1,41 @@ const rollup = require('rollup') const configFactory = require('./rollup.config') +const fs = require('fs') +const util = require('util') +const path = require('path') -// load locale +const { promisify } = util -// load plugin +const promisifyReadDir = promisify(fs.readdir) -// core - -async function build() { - const option = configFactory({ - input: './src/index.js', - fileName: './dayjs.min.js' - }) +async function build(option) { const bundle = await rollup.rollup(option.input) await bundle.write(option.output) } -build() +(async () => { + try { + const locales = await promisifyReadDir(path.join(__dirname, '../src/locale')) + locales.forEach((l) => { + build(configFactory({ + input: `./src/locale/${l}`, + fileName: `./locale/${l}` + })) + }) + + const plugins = await promisifyReadDir(path.join(__dirname, '../src/plugin')) + plugins.forEach((l) => { + build(configFactory({ + input: `./src/plugin/${l}`, + fileName: `./plugin/${l}` + })) + }) + + build(configFactory({ + input: './src/index.js', + fileName: './dayjs.min.js' + })) + } catch (e) { + console.error(e) // eslint-disable-line no-console + } +})() diff --git a/build/rollup.config.js b/build/rollup.config.js index 2f6750363..936ae5509 100644 --- a/build/rollup.config.js +++ b/build/rollup.config.js @@ -16,7 +16,8 @@ module.exports = (config) => { output: { file: fileName, format: 'umd', - name: 'dayjs' + name: 'dayjs', + globals: ['dayjs'] } } } diff --git a/src/locale/es.js b/src/locale/es.js index 9af582382..6fc201439 100644 --- a/src/locale/es.js +++ b/src/locale/es.js @@ -1,4 +1,4 @@ -import dayjs from '../index' +import dayjs from 'dayjs' const locale = { name: 'es', From 6ef3d2fe107332456f26aaf09cfb7c620d4f00c0 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 15 May 2018 00:11:32 +0800 Subject: [PATCH 13/20] chore: use plugin by default --- .gitignore | 6 ++++-- src/plugin/advancedFormat.js | 6 ++++-- test/plugin/advancedFormat.test.js | 8 ++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 5679e2c4a..2ca2c00de 100644 --- a/.gitignore +++ b/.gitignore @@ -12,8 +12,10 @@ package-lock.json # jest coverage -# dist -dist +# build +locale +plugin +dayjs.min.js #dev demo.js \ No newline at end of file diff --git a/src/plugin/advancedFormat.js b/src/plugin/advancedFormat.js index 10200325b..cbdd559c3 100644 --- a/src/plugin/advancedFormat.js +++ b/src/plugin/advancedFormat.js @@ -1,6 +1,7 @@ +import dayjs from 'dayjs' import { FORMAT_DEFAULT } from '../constant' -export default (o, c, d) => { // locale needed later +dayjs.extend((o, c, d) => { // locale needed later const proto = c.prototype const oldFormat = proto.format // eslint-disable-next-line no-nested-ternary @@ -28,4 +29,5 @@ export default (o, c, d) => { // locale needed later }) return oldFormat.bind(this)(result, locale) } -} +}) + diff --git a/test/plugin/advancedFormat.test.js b/test/plugin/advancedFormat.test.js index 6b1f5432e..46f22edd4 100644 --- a/test/plugin/advancedFormat.test.js +++ b/test/plugin/advancedFormat.test.js @@ -1,9 +1,7 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' -import advancedFormat from '../../src/plugin/advancedFormat' - -dayjs.extend(advancedFormat) +import '../../src/plugin/advancedFormat' beforeEach(() => { MockDate.set(new Date()) @@ -41,6 +39,8 @@ it('Format Day of Month Do 1 - 31', () => { it('Format Hour k kk 24-hour 1 - 24', () => { expect(dayjs().format('k')).toBe(moment().format('k')) expect(dayjs().format('kk')).toBe(moment().format('kk')) - const d = '2018-05-02 00:00:00.000' + let d = '2018-05-02 00:00:00.000' + expect(dayjs(d).format('k')).toBe(moment(d).format('k')) + d = '2018-05-02 01:00:00.000' expect(dayjs(d).format('k')).toBe(moment(d).format('k')) }) From 3d2385cd3f8874c75fe2de6e5fd0f753d94d44cd Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 15 May 2018 11:30:47 +0800 Subject: [PATCH 14/20] chore: add rollup external & globals --- build/rollup.config.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/build/rollup.config.js b/build/rollup.config.js index 936ae5509..f1c60cf09 100644 --- a/build/rollup.config.js +++ b/build/rollup.config.js @@ -6,6 +6,9 @@ module.exports = (config) => { return { input: { input, + external: [ + 'dayjs' + ], plugins: [ babel({ exclude: 'node_modules/**' @@ -17,7 +20,9 @@ module.exports = (config) => { file: fileName, format: 'umd', name: 'dayjs', - globals: ['dayjs'] + globals: { + dayjs: 'dayjs' + } } } } From 8baf291d8774d02b615e9e1019b095071831dee9 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 15 May 2018 11:37:06 +0800 Subject: [PATCH 15/20] test: update locale test --- test/locale/keys.test.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/locale/keys.test.js b/test/locale/keys.test.js index d3eeb89ba..e6e4d6fdf 100644 --- a/test/locale/keys.test.js +++ b/test/locale/keys.test.js @@ -1,7 +1,6 @@ import fs from 'fs' import path from 'path' - -jest.mock('dayjs') +import dayjs from '../../src' const localeDir = '../../src/locale' const L = [] @@ -21,7 +20,10 @@ it('Locale keys', () => { expect(name).toEqual(expect.any(String)) expect(weekdays).toEqual(expect.any(Array)) expect(months).toEqual(expect.any(Array)) - // function pass date return string or number or null - expect(ordinal(1)).toEqual(expect.anything()) + if (ordinal) { + // function pass date return string or number or null + expect(ordinal(1)).toEqual(expect.anything()) + } + expect(dayjs().locale(name).$locale().name).toBe(name) }) }) From a09c52de5abba14acfefc37e590741451ee75350 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 15 May 2018 13:52:47 +0800 Subject: [PATCH 16/20] build: build name --- build/index.js | 6 ++++-- build/rollup.config.js | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/build/index.js b/build/index.js index b752cfd79..ee5f84a05 100644 --- a/build/index.js +++ b/build/index.js @@ -19,7 +19,8 @@ async function build(option) { locales.forEach((l) => { build(configFactory({ input: `./src/locale/${l}`, - fileName: `./locale/${l}` + fileName: `./locale/${l}`, + name: `dayjs-locale-${l}` })) }) @@ -27,7 +28,8 @@ async function build(option) { plugins.forEach((l) => { build(configFactory({ input: `./src/plugin/${l}`, - fileName: `./plugin/${l}` + fileName: `./plugin/${l}`, + name: `dayjs-plugin-${l}` })) }) diff --git a/build/rollup.config.js b/build/rollup.config.js index f1c60cf09..4d31b990e 100644 --- a/build/rollup.config.js +++ b/build/rollup.config.js @@ -2,7 +2,7 @@ const babel = require('rollup-plugin-babel') const uglify = require('rollup-plugin-uglify') module.exports = (config) => { - const { input, fileName } = config + const { input, fileName, name } = config return { input: { input, @@ -19,7 +19,7 @@ module.exports = (config) => { output: { file: fileName, format: 'umd', - name: 'dayjs', + name: name || 'dayjs', globals: { dayjs: 'dayjs' } From 08201939a85df9bcf4f345bab9079561577b5f64 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 15 May 2018 14:41:40 +0800 Subject: [PATCH 17/20] build: add umd build name --- build/index.js | 6 ++++-- karma.sauce.conf.js | 2 +- src/plugin/advancedFormat.js | 5 ++--- test/plugin/advancedFormat.test.js | 4 +++- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/build/index.js b/build/index.js index ee5f84a05..65b5bc75e 100644 --- a/build/index.js +++ b/build/index.js @@ -8,6 +8,8 @@ const { promisify } = util const promisifyReadDir = promisify(fs.readdir) +const formatName = n => n.replace(/\.js/, '').replace('-', '_') + async function build(option) { const bundle = await rollup.rollup(option.input) await bundle.write(option.output) @@ -20,7 +22,7 @@ async function build(option) { build(configFactory({ input: `./src/locale/${l}`, fileName: `./locale/${l}`, - name: `dayjs-locale-${l}` + name: `dayjs_locale_${formatName(l)}` })) }) @@ -29,7 +31,7 @@ async function build(option) { build(configFactory({ input: `./src/plugin/${l}`, fileName: `./plugin/${l}`, - name: `dayjs-plugin-${l}` + name: `dayjs_plugin_${formatName(l)}` })) }) diff --git a/karma.sauce.conf.js b/karma.sauce.conf.js index 9403eb49f..4d05721ce 100644 --- a/karma.sauce.conf.js +++ b/karma.sauce.conf.js @@ -82,7 +82,7 @@ module.exports = function (config) { basePath: '', frameworks: ['jasmine'], files: [ - 'dist/*.js', + 'dayjs.min.js', 'test/*spec.js' ], reporters: ['dots', 'saucelabs'], diff --git a/src/plugin/advancedFormat.js b/src/plugin/advancedFormat.js index cbdd559c3..363fe52de 100644 --- a/src/plugin/advancedFormat.js +++ b/src/plugin/advancedFormat.js @@ -1,7 +1,6 @@ -import dayjs from 'dayjs' import { FORMAT_DEFAULT } from '../constant' -dayjs.extend((o, c, d) => { // locale needed later +export default (o, c, d) => { // locale needed later const proto = c.prototype const oldFormat = proto.format // eslint-disable-next-line no-nested-ternary @@ -29,5 +28,5 @@ dayjs.extend((o, c, d) => { // locale needed later }) return oldFormat.bind(this)(result, locale) } -}) +} diff --git a/test/plugin/advancedFormat.test.js b/test/plugin/advancedFormat.test.js index 46f22edd4..f93676d2f 100644 --- a/test/plugin/advancedFormat.test.js +++ b/test/plugin/advancedFormat.test.js @@ -1,7 +1,9 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' -import '../../src/plugin/advancedFormat' +import advancedFormat from '../../src/plugin/advancedFormat' + +dayjs.extend(advancedFormat) beforeEach(() => { MockDate.set(new Date()) From bcdaf71352543d28ca9316ee1c68f822b7441900 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 15 May 2018 15:37:29 +0800 Subject: [PATCH 18/20] chore: readme update --- README.md | 5 +++-- README.zh-CN.md | 2 ++ package.json | 12 ++++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e614ec76e..3cb05a78e 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,10 @@ English | [简体中文](./README.zh-CN.md) src="https://img.shields.io/codecov/c/github/xx45/dayjs/master.svg?style=flat-square" alt="Codecov"> License +
+ Sauce Test Status

- -> Day.js is a minimalist JavaScript library for modern browsers with a largely Moment.js-compatible API. If you use Moment.js, you already know how to use Day.js. +> Day.js is a minimalist JavaScript library that parse, validate, manipulate, and display dates and times for modern browsers with a largely Moment.js-compatible API. If you use Moment.js, you already know how to use Day.js. ```js dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss'); diff --git a/README.zh-CN.md b/README.zh-CN.md index 8fe264c57..ed05641c7 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -16,6 +16,8 @@ src="https://img.shields.io/codecov/c/github/xx45/dayjs/master.svg?style=flat-square" alt="Codecov"> License +
+ Sauce Test Status

> Day.js 是一个轻量的 JavaScript 时间日期处理库,和 Moment.js 的 API 设计保持完全一样. 如果你曾经用过 Moment.js, 那么你已经知道如何使用 Day.js diff --git a/package.json b/package.json index f5891a392..79e789d92 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "dayjs", "version": "0.0.0-development", - "description": "2KB immutable date library alternative to Moment.js with the same modern API ", - "main": "dist/dayjs.min.js", + "description": "2KB immutable date time library alternative to Moment.js with the same modern API ", + "main": "dayjs.min.js", "types": "index.d.ts", "scripts": { "test": "jest", @@ -26,6 +26,14 @@ "src/**/*" ] }, + "release": { + "prepare": [ + { + "path": "@semantic-release/changelog" + }, + "@semantic-release/git" + ] + }, "keywords": [ "dayjs", "date", From 70c4b9d650038db0ece1c0ceddef0de4afbdaa06 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 15 May 2018 15:44:14 +0800 Subject: [PATCH 19/20] chore: lint & style --- README.md | 6 ++++-- README.zh-CN.md | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3cb05a78e..c704cddd5 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,11 @@ English | [简体中文](./README.zh-CN.md) src="https://img.shields.io/codecov/c/github/xx45/dayjs/master.svg?style=flat-square" alt="Codecov"> License -
- Sauce Test Status + + Sauce Test Status +

+ > Day.js is a minimalist JavaScript library that parse, validate, manipulate, and display dates and times for modern browsers with a largely Moment.js-compatible API. If you use Moment.js, you already know how to use Day.js. ```js diff --git a/README.zh-CN.md b/README.zh-CN.md index ed05641c7..ea4b00c5d 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -16,8 +16,9 @@ src="https://img.shields.io/codecov/c/github/xx45/dayjs/master.svg?style=flat-square" alt="Codecov"> License -
- Sauce Test Status + + Sauce Test Status +

> Day.js 是一个轻量的 JavaScript 时间日期处理库,和 Moment.js 的 API 设计保持完全一样. 如果你曾经用过 Moment.js, 那么你已经知道如何使用 Day.js From 9b51dd9157f6ca93c6c3ee0dc539c0cf327a8159 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 15 May 2018 15:47:10 +0800 Subject: [PATCH 20/20] chore: seems not support style --- README.md | 3 ++- README.zh-CN.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c704cddd5..0b74d1a8c 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,9 @@ English | [简体中文](./README.zh-CN.md) src="https://img.shields.io/codecov/c/github/xx45/dayjs/master.svg?style=flat-square" alt="Codecov"> License +
- Sauce Test Status + Sauce Test Status

diff --git a/README.zh-CN.md b/README.zh-CN.md index ea4b00c5d..a9f18193e 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -16,8 +16,9 @@ src="https://img.shields.io/codecov/c/github/xx45/dayjs/master.svg?style=flat-square" alt="Codecov"> License +
- Sauce Test Status + Sauce Test Status