diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..beffa30 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d6727f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +node_modules +bower_components +coverage +npm-debug.log +yarn.lock +Gemfile.lock +yarn-error.log +package-lock.json +.DS_Store +.idea +.vscode + +# vscode localhistory +.history diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..9a964ff --- /dev/null +++ b/.npmignore @@ -0,0 +1,23 @@ +.idea +.DS_Store +**/npm-debug.log +**/node_modules +test +__tests__ +src +build +gulpfile.js + +.editorconfig +.prettierrc +Gemfile +jest.config.js +LICENSE.txt +Rakefile +.vscode +.release-it.json +docs + +# vscode localhistory +.history +.release-it.json diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..62d7190 --- /dev/null +++ b/.npmrc @@ -0,0 +1,2 @@ +sass_binary_site=https://npm.taobao.org/mirrors/node-sass/ +package-lock=false diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..8849e87 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,40 @@ +{ + "trailingComma": "none", + "arrowParens": "always", + "printWidth": 100, + "bracketSpacing": true, + "jsxBracketSameLine": true, + "tabWidth": 2, + "semi": true, + "singleQuote": true, + "overrides": [ + { + "files": ["*.json", ".eslintrc", ".tslintrc", ".prettierrc", ".tern-project"], + "options": { + "parser": "json", + "tabWidth": 2 + } + }, + { + "files": "*.{css,sass,scss,less}", + "options": { + "parser": "postcss", + "tabWidth": 2 + } + }, + { + "files": "*.ts", + "options": { + "parser": "typescript" + } + }, + { + "files": "*.md", + "options": { + "trailingComma": "none", + "tabWidth": 2, + "parser": "json" + } + } + ] +} diff --git a/.release-it.json b/.release-it.json new file mode 100644 index 0000000..d7341a3 --- /dev/null +++ b/.release-it.json @@ -0,0 +1,17 @@ +{ + "git": { + "requireCleanWorkingDir": false + }, + "hooks": { + "after:init": [ + "npm run test", + "t2k" + ], + "after:bump": [ + "npm run build" + ] + }, + "github": { + "release": true + } +} diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..be1fbb9 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2020 afei <1290657123@qq.com> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..5a573df --- /dev/null +++ b/README.md @@ -0,0 +1,65 @@ +# next-weeks +> Weeks for next. + +[![version][version-image]][version-url] +[![license][license-image]][license-url] +[![size][size-image]][size-url] +[![download][download-image]][download-url] + +## installation +```bash +npm install -S @jswork/next-weeks +``` + +## apis +| api | params | description | +| --- | ------------- | ---------------------------------------------- | +| at | (index, type) | Get en/cn/emoji by index(0-6). | +| day | (day, type) | Get en/cn/emoji by js date day.(1,2,3,4,5,6,0) | + +## usage +```js +import NxWeeks from '@jswork/next-weeks'; + +// at: +NxWeeks.at(0, 'en') // 'Monday' +NxWeeks.at(1, 'en') // 'Tuesday' +NxWeeks.at(2, 'en') // 'Wednesday' +NxWeeks.at(3, 'en') // 'Thursday' +NxWeeks.at(4, 'en') // 'Friday' +NxWeeks.at(5, 'en') // 'Saturday' +NxWeeks.at(6, 'en') // 'Sunday' + + +// day: +const d1 = new Date('2020-11-23'); +const d2 = new Date('2020-11-24'); +const d3 = new Date('2020-11-25'); +const d4 = new Date('2020-11-26'); +const d5 = new Date('2020-11-27'); +const d6 = new Date('2020-11-28'); +const d7 = new Date('2020-11-29'); + +NxWeeks.day(d1.getDay(), 'cn') // '一' +NxWeeks.day(d2.getDay(), 'cn') // '二' +NxWeeks.day(d3.getDay(), 'cn') // '三' +NxWeeks.day(d4.getDay(), 'cn') // '四' +NxWeeks.day(d5.getDay(), 'cn') // '五' +NxWeeks.day(d6.getDay(), 'cn') // '六' +NxWeeks.day(d7.getDay(), 'cn') // '日' +``` + +## license +Code released under [the MIT license](https://github.com/afeiship/next-weeks/blob/master/LICENSE.txt). + +[version-image]: https://img.shields.io/npm/v/@jswork/next-weeks +[version-url]: https://npmjs.org/package/@jswork/next-weeks + +[license-image]: https://img.shields.io/npm/l/@jswork/next-weeks +[license-url]: https://github.com/afeiship/next-weeks/blob/master/LICENSE.txt + +[size-image]: https://img.shields.io/bundlephobia/minzip/@jswork/next-weeks +[size-url]: https://github.com/afeiship/next-weeks/blob/master/dist/next-weeks.min.js + +[download-image]: https://img.shields.io/npm/dm/@jswork/next-weeks +[download-url]: https://www.npmjs.com/package/@jswork/next-weeks diff --git a/__tests__/index.spec.js b/__tests__/index.spec.js new file mode 100644 index 0000000..1ba65f0 --- /dev/null +++ b/__tests__/index.spec.js @@ -0,0 +1,32 @@ +(function () { + const NxWeeks = require('../src'); + + describe('NxWeeks.methods', function () { + test('api:at en should return en', () => { + expect(NxWeeks.at(0, 'en')).toBe('Monday'); + expect(NxWeeks.at(1, 'en')).toBe('Tuesday'); + expect(NxWeeks.at(2, 'en')).toBe('Wednesday'); + expect(NxWeeks.at(3, 'en')).toBe('Thursday'); + expect(NxWeeks.at(4, 'en')).toBe('Friday'); + expect(NxWeeks.at(5, 'en')).toBe('Saturday'); + expect(NxWeeks.at(6, 'en')).toBe('Sunday'); + }); + + test('api:day en should get by date.getDay()', () => { + const d1 = new Date('2020-11-23'); + const d2 = new Date('2020-11-24'); + const d3 = new Date('2020-11-25'); + const d4 = new Date('2020-11-26'); + const d5 = new Date('2020-11-27'); + const d6 = new Date('2020-11-28'); + const d7 = new Date('2020-11-29'); + expect(NxWeeks.day(d1.getDay(), 'cn')).toBe('一'); + expect(NxWeeks.day(d2.getDay(), 'cn')).toBe('二'); + expect(NxWeeks.day(d3.getDay(), 'cn')).toBe('三'); + expect(NxWeeks.day(d4.getDay(), 'cn')).toBe('四'); + expect(NxWeeks.day(d5.getDay(), 'cn')).toBe('五'); + expect(NxWeeks.day(d6.getDay(), 'cn')).toBe('六'); + expect(NxWeeks.day(d7.getDay(), 'cn')).toBe('日'); + }); + }); +})(); diff --git a/build/clean.js b/build/clean.js new file mode 100644 index 0000000..2b0766a --- /dev/null +++ b/build/clean.js @@ -0,0 +1,13 @@ +(function() { + 'use strict'; + + const gulp = require('gulp'); + const $ = require('gulp-load-plugins')({ + pattern: ['gulp-*', 'gulp.*', 'del'] + }); + + //clean + gulp.task('clean', function() { + return $.del('dist'); + }); +})(); diff --git a/build/scripts.js b/build/scripts.js new file mode 100644 index 0000000..61801d0 --- /dev/null +++ b/build/scripts.js @@ -0,0 +1,21 @@ +(function() { + 'use strict'; + + const gulp = require('gulp'); + const saveLicense = require('uglify-save-license'); + const $ = require('gulp-load-plugins')({ + pattern: ['gulp-*', 'gulp.*', 'del', '@jswork/gulp-*'] + }); + + gulp.task('scripts', function() { + return gulp + .src('src/*.js') + .pipe($.jswork.pkgHeader()) + .pipe(gulp.dest('dist')) + .pipe($.size({ title: '[ default size ]:' })) + .pipe($.uglify({ output: { comments: saveLicense } })) + .pipe($.rename({ extname: '.min.js' })) + .pipe(gulp.dest('dist')) + .pipe($.size({ title: '[ minimize size ]:' })); + }); +})(); diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..7a44688 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,36 @@ +/*! + * name: @jswork/next-weeks + * description: Weeks for next. + * homepage: https://github.com/afeiship/next-weeks + * version: 1.0.0 + * date: 2020-11-29 21:21:00 + * license: MIT + */ + +(function () { + var global = global || this || window || Function('return this')(); + var nx = global.nx || require('@jswork/next'); + var DATABASE = { + en: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], + shorty: ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'], + emoji: ['😝', '😪', '😭', '🤪', '😎', '😂'], + cn: ['一', '二', '三', '四', '五', '六', '日'] + }; + + var NxWeeks = nx.declare('nx.Weeks', { + statics: { + at: function (inIndex, inType) { + return DATABASE[inType][inIndex]; + }, + day: function (inDay, inType) { + var idx = inDay - 1; + idx = idx >= 0 ? idx : 6; + return this.at(idx, inType); + } + } + }); + + if (typeof module !== 'undefined' && module.exports) { + module.exports = NxWeeks; + } +})(); diff --git a/dist/index.min.js b/dist/index.min.js new file mode 100644 index 0000000..0835a5c --- /dev/null +++ b/dist/index.min.js @@ -0,0 +1,9 @@ +/*! + * name: @jswork/next-weeks + * description: Weeks for next. + * homepage: https://github.com/afeiship/next-weeks + * version: 1.0.0 + * date: 2020-11-29 21:21:00 + * license: MIT + */ +!function(){var e=(this||window||Function("return this")()).nx||require("@jswork/next"),t={en:["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],shorty:["Mon","Tue","Wed","Thur","Fri","Sat","Sun"],emoji:["😝","😪","😭","🤪","😎","😂"],cn:["一","二","三","四","五","六","日"]},e=e.declare("nx.Weeks",{statics:{at:function(e,n){return t[n][e]},day:function(e,n){e=0<=(e=e-1)?e:6;return this.at(e,n)}}});"undefined"!=typeof module&&module.exports&&(module.exports=e)}(); \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..0513561 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,13 @@ +(function() { + 'use strict'; + + const gulp = require('gulp'); + const fs = require('fs'); + + //import + fs.readdirSync('./build').map(function(file) { + require('./build/' + file); + }); + + gulp.task('default', gulp.series(['clean', 'scripts'])); +})(); diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..399cb7b --- /dev/null +++ b/jest.config.js @@ -0,0 +1,9 @@ +// https://jestjs.io/docs/en/configuration +module.exports = { + verbose: true, + testRegex: [/\.spec.js/], + //preset: "jest-puppeteer", + moduleNameMapper: { + '^@/(.*)$': '/src/$1' + } +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..3622777 --- /dev/null +++ b/package.json @@ -0,0 +1,38 @@ +{ + "name": "@jswork/next-weeks", + "version": "1.0.0", + "description": "Weeks for next.", + "homepage": "https://github.com/afeiship/next-weeks", + "author": { + "name": "afei", + "email": "1290657123@qq.com" + }, + "scripts": { + "build": "gulp", + "test": "jest", + "release": "release-it" + }, + "main": "dist/index.js", + "license": "MIT", + "devDependencies": { + "@jswork/gulp-pkg-header": "^1.0.2", + "@jswork/next": "^1.0.2", + "del": "^6.0.0", + "gulp": "^4.0.2", + "gulp-concat": "^2.6.1", + "gulp-debug": "^4.0.0", + "gulp-ignore": "^3.0.0", + "gulp-load-plugins": "^2.0.5", + "gulp-rename": "^2.0.0", + "gulp-size": "^3.0.0", + "gulp-uglify": "^3.0.2", + "jest": "^26.6.3", + "release-it": "^14.2.1", + "uglify-js": "3.11.6", + "uglify-save-license": "^0.4.1" + }, + "publishConfig": { + "access": "public", + "registry": "https://registry.npmjs.org" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..45ebfbd --- /dev/null +++ b/src/index.js @@ -0,0 +1,27 @@ +(function () { + var global = global || this || window || Function('return this')(); + var nx = global.nx || require('@jswork/next'); + var DATABASE = { + en: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], + shorty: ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'], + emoji: ['😝', '😪', '😭', '🤪', '😎', '😂'], + cn: ['一', '二', '三', '四', '五', '六', '日'] + }; + + var NxWeeks = nx.declare('nx.Weeks', { + statics: { + at: function (inIndex, inType) { + return DATABASE[inType][inIndex]; + }, + day: function (inDay, inType) { + var idx = inDay - 1; + idx = idx >= 0 ? idx : 6; + return this.at(idx, inType); + } + } + }); + + if (typeof module !== 'undefined' && module.exports) { + module.exports = NxWeeks; + } +})();