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

Locale && Plugin update #141

Merged
merged 20 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@
],
"func-names": [
0
],
"import/no-extraneous-dependencies": [
0
],
"import/no-unresolved": [
2,
{
"ignore": [
"dayjs"
]
}
],
"import/extensions": [
2,
"ignorePackages"
]
}
}
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ package-lock.json
# jest
coverage

# dist
dist
# build
locale
plugin
dayjs.min.js

#dev
demo.js
7 changes: 6 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ yarn.lock
package-lock.json

# jest
coverage
coverage

# dev
src
test
build
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ English | [简体中文](./README.zh-CN.md)
src="https://img.shields.io/codecov/c/github/xx45/dayjs/master.svg?style=flat-square" alt="Codecov"></a>
<a href="https://github.com/xx45/dayjs/blob/master/LICENSE"><img
src="https://img.shields.io/npm/l/dayjs.svg?style=flat-square" alt="License"></a>
<br>
<a href="https://saucelabs.com/u/dayjs">
<img width="750" src="https://user-images.githubusercontent.com/17680888/40040137-8e3323a6-584b-11e8-9dba-bbe577ee8a7b.png" alt="Sauce Test Status">
</a>
</p>

> 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');
Expand Down
4 changes: 4 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
src="https://img.shields.io/codecov/c/github/xx45/dayjs/master.svg?style=flat-square" alt="Codecov"></a>
<a href="https://github.com/xx45/dayjs/blob/master/LICENSE"><img
src="https://img.shields.io/npm/l/dayjs.svg?style=flat-square" alt="License"></a>
<br>
<a href="https://saucelabs.com/u/dayjs">
<img width="750" src="https://user-images.githubusercontent.com/17680888/40040137-8e3323a6-584b-11e8-9dba-bbe577ee8a7b.png" alt="Sauce Test Status">
</a>
</p>

> Day.js 是一个轻量的 JavaScript 时间日期处理库,和 Moment.js 的 API 设计保持完全一样. 如果你曾经用过 Moment.js, 那么你已经知道如何使用 Day.js
Expand Down
45 changes: 45 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const rollup = require('rollup')
const configFactory = require('./rollup.config')
const fs = require('fs')
const util = require('util')
const path = require('path')

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)
}

(async () => {
try {
const locales = await promisifyReadDir(path.join(__dirname, '../src/locale'))
locales.forEach((l) => {
build(configFactory({
input: `./src/locale/${l}`,
fileName: `./locale/${l}`,
name: `dayjs_locale_${formatName(l)}`
}))
})

const plugins = await promisifyReadDir(path.join(__dirname, '../src/plugin'))
plugins.forEach((l) => {
build(configFactory({
input: `./src/plugin/${l}`,
fileName: `./plugin/${l}`,
name: `dayjs_plugin_${formatName(l)}`
}))
})

build(configFactory({
input: './src/index.js',
fileName: './dayjs.min.js'
}))
} catch (e) {
console.error(e) // eslint-disable-line no-console
}
})()
28 changes: 28 additions & 0 deletions build/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const babel = require('rollup-plugin-babel')
const uglify = require('rollup-plugin-uglify')

module.exports = (config) => {
const { input, fileName, name } = config
return {
input: {
input,
external: [
'dayjs'
],
plugins: [
babel({
exclude: 'node_modules/**'
}),
uglify()
]
},
output: {
file: fileName,
format: 'umd',
name: name || 'dayjs',
globals: {
dayjs: 'dayjs'
}
}
}
}
3 changes: 2 additions & 1 deletion karma.sauce.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ module.exports = function (config) {
basePath: '',
frameworks: ['jasmine'],
files: [
'dist/*.js',
'dayjs.min.js',
'test/*spec.js'
],
reporters: ['dots', 'saucelabs'],
port: 9876,
colors: true,
logLevel: config.LOG_DEBUG,
sauceLabs: {
// build: 'Manual',
testName: 'Day.js'
},
captureTimeout: 200000, // try fix ios timeout
Expand Down
21 changes: 16 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
{
"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",
"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"
"gzip-size": "gzip-size dayjs.min.js"
},
"pre-commit": [
"lint"
],
"jest": {
"roots": [
"test"
],
"testRegex": "test/(.*?/)?.*test.js$",
"coverageDirectory": "./coverage/",
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*"
]
},
"release": {
"prepare": [
{
"path": "@semantic-release/changelog"
},
"@semantic-release/git"
]
},
"keywords": [
"dayjs",
"date",
Expand Down
18 changes: 0 additions & 18 deletions rollup.config.js

This file was deleted.

9 changes: 6 additions & 3 deletions src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('_')
}

5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as C from './constant'
import * as U from './utils'
import en from './locale/en'
import U from './utils'

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

Expand Down
7 changes: 0 additions & 7 deletions src/locale/en.js

This file was deleted.

13 changes: 10 additions & 3 deletions src/locale/es.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export default {
import dayjs from 'dayjs'

const locale = {
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}º`
}

dayjs.locale(locale, null, true)

export default locale
12 changes: 12 additions & 0 deletions src/locale/zh-cn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import dayjs from 'dayjs'

const locale = {
name: 'zh-cn',
weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'),
months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'),
ordinal: n => n
}

dayjs.locale(locale, null, true)

export default locale
1 change: 1 addition & 0 deletions src/plugin/advancedFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export default (o, c, d) => { // locale needed later
return oldFormat.bind(this)(result, locale)
}
}

21 changes: 15 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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
}
3 changes: 3 additions & 0 deletions test/__mocks__/dayjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const dayjs = require('../../src')

module.exports = dayjs
3 changes: 1 addition & 2 deletions test/locale.test.js
Original file line number Diff line number Diff line change
@@ -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())
Expand Down Expand Up @@ -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)
Expand Down
Loading