Skip to content

Commit 2342c55

Browse files
authored
feat: Locale && Plugin
Merge pull request #141 from xx45/feature/iamkun Locale && Plugin update
2 parents 0d9a01e + 9b51dd9 commit 2342c55

22 files changed

+211
-54
lines changed

.eslintrc.json

+15
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424
],
2525
"func-names": [
2626
0
27+
],
28+
"import/no-extraneous-dependencies": [
29+
0
30+
],
31+
"import/no-unresolved": [
32+
2,
33+
{
34+
"ignore": [
35+
"dayjs"
36+
]
37+
}
38+
],
39+
"import/extensions": [
40+
2,
41+
"ignorePackages"
2742
]
2843
}
2944
}

.gitignore

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@ package-lock.json
1212
# jest
1313
coverage
1414

15-
# dist
16-
dist
15+
# build
16+
locale
17+
plugin
18+
dayjs.min.js
19+
20+
#dev
21+
demo.js

.npmignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ yarn.lock
1010
package-lock.json
1111

1212
# jest
13-
coverage
13+
coverage
14+
15+
# dev
16+
src
17+
test
18+
build

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ English | [简体中文](./README.zh-CN.md)
1717
src="https://img.shields.io/codecov/c/github/xx45/dayjs/master.svg?style=flat-square" alt="Codecov"></a>
1818
<a href="https://github.com/xx45/dayjs/blob/master/LICENSE"><img
1919
src="https://img.shields.io/npm/l/dayjs.svg?style=flat-square" alt="License"></a>
20+
<br>
21+
<a href="https://saucelabs.com/u/dayjs">
22+
<img width="750" src="https://user-images.githubusercontent.com/17680888/40040137-8e3323a6-584b-11e8-9dba-bbe577ee8a7b.png" alt="Sauce Test Status">
23+
</a>
2024
</p>
2125

22-
> 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.
26+
> 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.
2327
2428
```js
2529
dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss');

README.zh-CN.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
src="https://img.shields.io/codecov/c/github/xx45/dayjs/master.svg?style=flat-square" alt="Codecov"></a>
1717
<a href="https://github.com/xx45/dayjs/blob/master/LICENSE"><img
1818
src="https://img.shields.io/npm/l/dayjs.svg?style=flat-square" alt="License"></a>
19+
<br>
20+
<a href="https://saucelabs.com/u/dayjs">
21+
<img width="750" src="https://user-images.githubusercontent.com/17680888/40040137-8e3323a6-584b-11e8-9dba-bbe577ee8a7b.png" alt="Sauce Test Status">
22+
</a>
1923
</p>
2024

2125
> Day.js 是一个轻量的 JavaScript 时间日期处理库,和 Moment.js 的 API 设计保持完全一样. 如果你曾经用过 Moment.js, 那么你已经知道如何使用 Day.js

build/index.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const rollup = require('rollup')
2+
const configFactory = require('./rollup.config')
3+
const fs = require('fs')
4+
const util = require('util')
5+
const path = require('path')
6+
7+
const { promisify } = util
8+
9+
const promisifyReadDir = promisify(fs.readdir)
10+
11+
const formatName = n => n.replace(/\.js/, '').replace('-', '_')
12+
13+
async function build(option) {
14+
const bundle = await rollup.rollup(option.input)
15+
await bundle.write(option.output)
16+
}
17+
18+
(async () => {
19+
try {
20+
const locales = await promisifyReadDir(path.join(__dirname, '../src/locale'))
21+
locales.forEach((l) => {
22+
build(configFactory({
23+
input: `./src/locale/${l}`,
24+
fileName: `./locale/${l}`,
25+
name: `dayjs_locale_${formatName(l)}`
26+
}))
27+
})
28+
29+
const plugins = await promisifyReadDir(path.join(__dirname, '../src/plugin'))
30+
plugins.forEach((l) => {
31+
build(configFactory({
32+
input: `./src/plugin/${l}`,
33+
fileName: `./plugin/${l}`,
34+
name: `dayjs_plugin_${formatName(l)}`
35+
}))
36+
})
37+
38+
build(configFactory({
39+
input: './src/index.js',
40+
fileName: './dayjs.min.js'
41+
}))
42+
} catch (e) {
43+
console.error(e) // eslint-disable-line no-console
44+
}
45+
})()

build/rollup.config.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const babel = require('rollup-plugin-babel')
2+
const uglify = require('rollup-plugin-uglify')
3+
4+
module.exports = (config) => {
5+
const { input, fileName, name } = config
6+
return {
7+
input: {
8+
input,
9+
external: [
10+
'dayjs'
11+
],
12+
plugins: [
13+
babel({
14+
exclude: 'node_modules/**'
15+
}),
16+
uglify()
17+
]
18+
},
19+
output: {
20+
file: fileName,
21+
format: 'umd',
22+
name: name || 'dayjs',
23+
globals: {
24+
dayjs: 'dayjs'
25+
}
26+
}
27+
}
28+
}

karma.sauce.conf.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ module.exports = function (config) {
8282
basePath: '',
8383
frameworks: ['jasmine'],
8484
files: [
85-
'dist/*.js',
85+
'dayjs.min.js',
8686
'test/*spec.js'
8787
],
8888
reporters: ['dots', 'saucelabs'],
8989
port: 9876,
9090
colors: true,
9191
logLevel: config.LOG_DEBUG,
9292
sauceLabs: {
93+
// build: 'Manual',
9394
testName: 'Day.js'
9495
},
9596
captureTimeout: 200000, // try fix ios timeout

package.json

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
{
22
"name": "dayjs",
33
"version": "0.0.0-development",
4-
"description": "2KB immutable date library alternative to Moment.js with the same modern API ",
5-
"main": "dist/dayjs.min.js",
4+
"description": "2KB immutable date time library alternative to Moment.js with the same modern API ",
5+
"main": "dayjs.min.js",
66
"types": "index.d.ts",
77
"scripts": {
88
"test": "jest",
9-
"lint": "./node_modules/.bin/eslint src/* test/*",
10-
"build": "BABEL_ENV=build rollup -c",
9+
"lint": "./node_modules/.bin/eslint src/* test/* build/*",
10+
"build": "BABEL_ENV=build node build",
1111
"sauce": "npx karma start karma.sauce.conf.js",
1212
"test:sauce": "npm run sauce -- 0 && npm run sauce -- 1 && npm run sauce -- 2 && npm run sauce -- 3",
13-
"gzip-size": "gzip-size ./dist/*.min.js"
13+
"gzip-size": "gzip-size dayjs.min.js"
1414
},
1515
"pre-commit": [
1616
"lint"
1717
],
1818
"jest": {
19+
"roots": [
20+
"test"
21+
],
1922
"testRegex": "test/(.*?/)?.*test.js$",
2023
"coverageDirectory": "./coverage/",
2124
"collectCoverage": true,
2225
"collectCoverageFrom": [
2326
"src/**/*"
2427
]
2528
},
29+
"release": {
30+
"prepare": [
31+
{
32+
"path": "@semantic-release/changelog"
33+
},
34+
"@semantic-release/git"
35+
]
36+
},
2637
"keywords": [
2738
"dayjs",
2839
"date",

rollup.config.js

-18
This file was deleted.

src/constant.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ export const Q = 'quarter'
2121
export const Y = 'year'
2222
export const DATE = 'date'
2323

24-
export const WEEKDAYS = 'Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday'.split('.')
25-
export const MONTHS = 'January.February.March.April.May.June.July.August.September.October.November.December'.split('.')
26-
2724
export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
2825

2926
// regex
3027
export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{1,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/
3128
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
3229

30+
export const en = {
31+
name: 'en',
32+
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
33+
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_')
34+
}
35+

src/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as C from './constant'
2-
import * as U from './utils'
3-
import en from './locale/en'
2+
import U from './utils'
43

54
let L = 'en' // global locale
65
const Ls = {} // global loaded locale
7-
Ls[L] = en
6+
Ls[L] = C.en
87

98
const isDayjs = d => d instanceof Dayjs // eslint-disable-line no-use-before-define
109

src/locale/en.js

-7
This file was deleted.

src/locale/es.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
export default {
1+
import dayjs from 'dayjs'
2+
3+
const locale = {
24
name: 'es',
3-
weekdays: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
4-
months: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre']
5+
weekdays: 'Domingo_Lunes_Martes_Miércoles_Jueves_Viernes_Sábado'.split('_'),
6+
months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'),
7+
ordinal: n => `${n}º`
58
}
9+
10+
dayjs.locale(locale, null, true)
11+
12+
export default locale

src/locale/zh-cn.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import dayjs from 'dayjs'
2+
3+
const locale = {
4+
name: 'zh-cn',
5+
weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'),
6+
months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'),
7+
ordinal: n => n
8+
}
9+
10+
dayjs.locale(locale, null, true)
11+
12+
export default locale

src/plugin/advancedFormat.js

+1
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ export default (o, c, d) => { // locale needed later
2929
return oldFormat.bind(this)(result, locale)
3030
}
3131
}
32+

src/utils.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
export const padStart = (string, length, pad) => {
1+
const padStart = (string, length, pad) => {
22
const s = String(string)
33
if (!s || s.length >= length) return string
44
return `${Array((length + 1) - s.length).join(pad)}${string}`
55
}
66

7-
export const padZoneStr = (negMinuts) => {
7+
const padZoneStr = (negMinuts) => {
88
const minutes = Math.abs(negMinuts)
99
const hourOffset = Math.floor(minutes / 60)
1010
const minuteOffset = minutes % 60
1111
return `${negMinuts <= 0 ? '+' : '-'}${padStart(hourOffset, 2, '0')}:${padStart(minuteOffset, 2, '0')}`
1212
}
1313

14-
export const monthDiff = (a, b) => {
14+
const monthDiff = (a, b) => {
1515
// function from moment.js in order to keep the same result
1616
const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())
1717
const anchor = a.clone().add(wholeMonthDiff, 'months')
@@ -27,8 +27,17 @@ export const monthDiff = (a, b) => {
2727
return Number(-(wholeMonthDiff + adjust))
2828
}
2929

30-
export const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n))
30+
const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n))
3131

32-
export const prettyUnit = u => (u && String(u).toLowerCase().replace(/s$/, ''))
32+
const prettyUnit = u => (u && String(u).toLowerCase().replace(/s$/, ''))
3333

34-
export const isUndefined = s => s === undefined
34+
const isUndefined = s => s === undefined
35+
36+
export default {
37+
padStart,
38+
padZoneStr,
39+
monthDiff,
40+
absFloor,
41+
prettyUnit,
42+
isUndefined
43+
}

test/__mocks__/dayjs.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const dayjs = require('../../src')
2+
3+
module.exports = dayjs

test/locale.test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import MockDate from 'mockdate'
22
import dayjs from '../src'
33
import es from '../src/locale/es'
4-
import en from '../src/locale/en'
54

65
beforeEach(() => {
76
MockDate.set(new Date())
@@ -39,7 +38,7 @@ it('set locale for this line only', () => {
3938
})
4039

4140
it('set global locale', () => {
42-
dayjs.locale(en)
41+
dayjs.locale('en')
4342
expect(dayjs('2018-4-28').format(format))
4443
.toBe('Saturday 28, April')
4544
dayjs.locale(es)

0 commit comments

Comments
 (0)