diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index 9071748..0000000 --- a/.eslintignore +++ /dev/null @@ -1,3 +0,0 @@ -node_modules -dist/ -tmp/ diff --git a/.eslintrc.json b/.eslintrc.json deleted file mode 100644 index 788c487..0000000 --- a/.eslintrc.json +++ /dev/null @@ -1,122 +0,0 @@ -{ - "parser": "espree", - "parserOptions": { - "sourceType": "module", - "ecmaVersion": 2018 - }, - "env": { - "es6": true, - "shared-node-browser": true - }, - "extends": "eslint:recommended", - "rules": { - "brace-style": [2, "1tbs", { - "allowSingleLine": false - }], - "camelcase": [ - "error", - { - "properties": "never" - } - ], - "comma-dangle": [ - "warn", - { - "arrays": "always-multiline", - "objects": "always-multiline", - "imports": "ignore", - "exports": "ignore", - "functions": "ignore" - } - ], - "comma-spacing": 2, - "consistent-return": 0, - "curly": [2, "multi-line"], - "dot-notation": [2, { - "allowKeywords": true - }], - "eol-last": 2, - "eqeqeq": 2, - "handle-callback-err": 2, - "indent": [2, 2, { - "SwitchCase": 1 - }], - "key-spacing": [2, { - "beforeColon": false, - "afterColon": true - }], - "keyword-spacing": 2, - "max-len": 0, - "new-cap": 0, - "new-parens": 2, - "no-alert": 2, - "no-array-constructor": 2, - "no-caller": 2, - "no-catch-shadow": 2, - "no-console": 1, - "no-labels": 2, - "no-eval": 2, - "no-extend-native": 2, - "no-extra-bind": 2, - "no-extra-parens": [2, "functions"], - "no-fallthrough": 0, - "no-implied-eval": 2, - "no-iterator": 2, - "no-label-var": 2, - "no-lone-blocks": 2, - "no-loop-func": 2, - "no-multi-spaces": 2, - "no-multi-str": 2, - "no-multiple-empty-lines": [ "error", { - "max": 1, - "maxEOF": 0, - "maxBOF": 0 - } ], - "no-native-reassign": 0, - "no-new": 0, - "no-new-func": 2, - "no-new-object": 2, - "no-new-wrappers": 2, - "no-octal-escape": 2, - "no-path-concat": 2, - "no-process-exit": 2, - "no-proto": 2, - "no-prototype-builtins": 2, - "no-return-assign": 2, - "no-script-url": 2, - "no-sequences": 2, - "no-shadow": 2, - "no-shadow-restricted-names": 2, - "no-spaced-func": 2, - "no-trailing-spaces": 2, - "no-undef": 2, - "no-undef-init": 2, - "no-underscore-dangle": 0, - "no-unused-expressions": 2, - "no-use-before-define": 0, - "no-var": 2, - "no-with": 2, - "prefer-arrow-callback": 2, - "prefer-const": ["error", {"destructuring": "all"}], - "quotes": [2, "single"], - "require-atomic-updates": 0, - "require-await": 2, - "semi": [2, "always"], - "semi-spacing": [2, { - "before": false, - "after": true - }], - "space-infix-ops": 2, - "space-unary-ops": [2, { - "words": true, - "nonwords": false - }], - "space-before-blocks": 2, - "space-before-function-paren": [2, { - "anonymous": "never", - "named": "never" - }], - "strict": [2, "global"], - "yoda": [2, "never"] - } -} diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 439bcdf..7d638e2 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -10,18 +10,18 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node-version: [ 14, 16, 18, latest ] + node-version: [18, 20, latest] steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm i - run: npm run test:lcov - name: Coveralls - uses: coverallsapp/github-action@master + uses: coverallsapp/github-action@v2 with: github-token: ${{ secrets.GITHUB_TOKEN }} flag-name: run-${{ matrix.node-version }} @@ -32,7 +32,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Coveralls Finished - uses: coverallsapp/github-action@master + uses: coverallsapp/github-action@v2 with: github-token: ${{ secrets.GITHUB_TOKEN }} parallel-finished: true diff --git a/.mocharc.cjs b/.mocharc.cjs deleted file mode 100644 index 8a0b053..0000000 --- a/.mocharc.cjs +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -process.env.NODE_ENV = 'test'; -global.expect = require('chai').expect; - -module.exports = { - reporter: 'spec', - recursive: true, -}; diff --git a/.mocharc.json b/.mocharc.json new file mode 100644 index 0000000..55092c2 --- /dev/null +++ b/.mocharc.json @@ -0,0 +1,5 @@ +{ + "reporter": "spec", + "recursive": true, + "require": ["chai/register-expect.js"] +} diff --git a/.nvmrc b/.nvmrc index 8351c19..3c03207 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -14 +18 diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..b947077 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +node_modules/ +dist/ diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..ec2b13c --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,5 @@ +{ + "tabWidth": 2, + "printWidth": 140, + "singleQuote": true +} diff --git a/README.md b/README.md index 605960b..d872683 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -chronokinesis -============= +# chronokinesis + [![Build](https://github.com/paed01/chronokinesis/actions/workflows/build.yaml/badge.svg)](https://github.com/paed01/chronokinesis/actions/workflows/build.yaml) [![Coverage Status](https://coveralls.io/repos/github/paed01/chronokinesis/badge.svg?branch=master)](https://coveralls.io/github/paed01/chronokinesis?branch=master) Mock time and date for traveling and freezing. Inspired and borrowed from [timekeeper](https://github.com/vesln/timekeeper). @@ -92,7 +92,7 @@ let date = new Date(2018, 0, 31); ck.travel(date); -setTimeout(function() { +setTimeout(function () { console.log(new Date()); ck.reset(); }, 1500); @@ -110,7 +110,7 @@ ck.freeze(date); ck.travel(moment().add(1, 'year')); -setTimeout(function() { +setTimeout(function () { console.log(`Still frozen but one year ahead ${new Date()}`); ck.reset(); @@ -148,12 +148,12 @@ Resets Date to current glory. import * as ck from 'chronokinesis'; ck.freeze(2060, 0, 1); -console.log(`end of time is reached at ${new Date()} according to Newton`) +console.log(`end of time is reached at ${new Date()} according to Newton`); ck.reset(); // Today -console.log(new Date()) +console.log(new Date()); ``` ## `isKeepingTime()` @@ -183,7 +183,7 @@ import * as ck from 'chronokinesis'; const tz = ck.timezone('Asia/Shanghai'); // Now in Shanghai -console.log(new Date()) +console.log(new Date()); tz.freeze(); ``` @@ -193,7 +193,7 @@ tz.freeze(); Time zone traveller api. ```javascript -import {TimeZoneTraveller} from 'chronokinesis'; +import { TimeZoneTraveller } from 'chronokinesis'; const timezone = new TimeZoneTraveller('Asia/Shanghai'); diff --git a/dist/chronokinesis.cjs b/dist/chronokinesis.cjs index 9a045c1..bb39574 100644 --- a/dist/chronokinesis.cjs +++ b/dist/chronokinesis.cjs @@ -146,11 +146,11 @@ } function useFakeDate() { - Date = FakeDate; // eslint-disable-line no-global-assign + Date = FakeDate; } function useNativeDate() { - Date = NativeDate; // eslint-disable-line no-global-assign + Date = NativeDate; } function time() { @@ -166,7 +166,7 @@ function toUTC(formatter, dt) { let year, month, day, hour, minute, second; - for (const {type, value} of formatter.formatToParts(dt)) { + for (const { type, value } of formatter.formatToParts(dt)) { switch (type) { case 'year': year = parseInt(value); diff --git a/dist/index.cjs b/dist/index.cjs index 5b76a02..9395043 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -142,11 +142,11 @@ function timezone(timeZone, ...args) { } function useFakeDate() { - Date = FakeDate; // eslint-disable-line no-global-assign + Date = FakeDate; } function useNativeDate() { - Date = NativeDate; // eslint-disable-line no-global-assign + Date = NativeDate; } function time() { @@ -162,7 +162,7 @@ function instantiate(type, args) { function toUTC(formatter, dt) { let year, month, day, hour, minute, second; - for (const {type, value} of formatter.formatToParts(dt)) { + for (const { type, value } of formatter.formatToParts(dt)) { switch (type) { case 'year': year = parseInt(value); diff --git a/eslint.config.js b/eslint.config.js new file mode 100644 index 0000000..a44ff59 --- /dev/null +++ b/eslint.config.js @@ -0,0 +1,111 @@ +import js from '@eslint/js'; +import globals from 'globals'; + +const rules = { + 'dot-notation': [2, { allowKeywords: true }], + 'eol-last': 2, + eqeqeq: 2, + 'linebreak-style': ['error', 'unix'], + 'no-alert': 2, + 'no-array-constructor': 2, + 'no-caller': 2, + 'no-catch-shadow': 2, + 'no-console': 1, + 'no-eval': 2, + 'no-extend-native': 2, + 'no-extra-bind': 2, + 'no-fallthrough': 'off', + 'no-implied-eval': 2, + 'no-iterator': 2, + 'no-label-var': 2, + 'no-labels': 2, + 'no-lone-blocks': 2, + 'no-loop-func': 2, + 'no-multi-spaces': 2, + 'no-multi-str': 2, + 'no-multiple-empty-lines': ['error', { max: 1, maxEOF: 0, maxBOF: 0 }], + 'no-new-func': 2, + 'no-new-object': 2, + 'no-new-wrappers': 2, + 'no-octal-escape': 2, + 'no-path-concat': 2, + 'no-process-exit': 2, + 'no-proto': 2, + 'no-prototype-builtins': 2, + 'no-return-assign': 2, + 'no-script-url': 2, + 'no-sequences': 2, + 'no-shadow-restricted-names': 2, + 'no-shadow': 0, + 'no-spaced-func': 2, + 'no-trailing-spaces': 2, + 'no-undef-init': 2, + 'no-undef': 2, + 'no-underscore-dangle': 0, + 'no-unused-expressions': 2, + 'no-use-before-define': 0, + 'no-var': 2, + 'no-with': 2, + 'prefer-const': ['error', { destructuring: 'all' }], + 'require-atomic-updates': 0, + 'require-await': 2, + 'semi-spacing': [2, { before: false, after: true }], + semi: [2, 'always'], + 'space-before-blocks': 2, + 'space-before-function-paren': [2, { anonymous: 'never', named: 'never' }], + 'space-infix-ops': 2, + 'space-unary-ops': [2, { words: true, nonwords: false }], + 'unicode-bom': ['error', 'never'], + yoda: [2, 'never'], +}; + +export default [ + js.configs.recommended, + { + rules, + }, + { + files: ['**/*.js'], + languageOptions: { + parserOptions: { + sourceType: 'module', + ecmaVersion: 2018, + }, + globals: { + ...globals['shared-node-browser'], + ...globals.es6, + }, + }, + rules: { + 'no-global-assign': 0, + }, + }, + { + files: ['test/**/*.js'], + languageOptions: { + parserOptions: { + ecmaVersion: 2020, + }, + globals: { + ...globals.node, + ...globals.mocha, + expect: 'readonly', + beforeEachScenario: 'readonly', + Buffer: 'readonly', + Feature: 'readonly', + Scenario: 'readonly', + Given: 'readonly', + When: 'readonly', + Then: 'readonly', + And: 'readonly', + But: 'readonly', + }, + }, + rules: { + 'no-unused-expressions': 0, + }, + }, + { + ignores: ['coverage/**/*', 'node_modules/**/*', 'tmp/*', 'dist/*'], + }, +]; diff --git a/generate-api-toc.cjs b/generate-api-toc.cjs index 5ba38fb..4c796f3 100644 --- a/generate-api-toc.cjs +++ b/generate-api-toc.cjs @@ -8,20 +8,22 @@ const internals = { filename: './README.md', }; -internals.generate = function() { +internals.generate = function generate() { const api = Fs.readFileSync(internals.filename, 'utf8'); const tocOptions = { bullets: '-', - slugify: function(text) { - - return text.toLowerCase() + slugify(text) { + return text + .toLowerCase() .replace(/\s/g, '-') .replace(/[^\w-]/g, ''); }, }; - const output = Toc.insert(api, tocOptions) - .replace(/(.|\n)*/, '\n# ' + Package.version + ' API Reference\n'); + const output = Toc.insert(api, tocOptions).replace( + /(.|\n)*/, + '\n# ' + Package.version + ' API Reference\n', + ); Fs.writeFileSync(internals.filename, output); }; diff --git a/index.js b/index.js index 9a7712a..d385cd2 100644 --- a/index.js +++ b/index.js @@ -140,11 +140,11 @@ export function timezone(timeZone, ...args) { } function useFakeDate() { - Date = FakeDate; // eslint-disable-line no-global-assign + Date = FakeDate; } function useNativeDate() { - Date = NativeDate; // eslint-disable-line no-global-assign + Date = NativeDate; } function time() { @@ -160,7 +160,7 @@ function instantiate(type, args) { function toUTC(formatter, dt) { let year, month, day, hour, minute, second; - for (const {type, value} of formatter.formatToParts(dt)) { + for (const { type, value } of formatter.formatToParts(dt)) { switch (type) { case 'year': year = parseInt(value); diff --git a/package.json b/package.json index 640ccee..bf3a798 100644 --- a/package.json +++ b/package.json @@ -20,10 +20,10 @@ "scripts": { "toc": "node generate-api-toc.cjs", "test": "mocha", - "lint": "eslint . --cache", + "lint": "eslint . --cache && prettier . --check --cache", "test:lcov": "c8 -r lcov -r text mocha && npm run lint", "cov:html": "c8 mocha -R dot && c8 report --reporter html", - "posttest": "npm run lint && npm run dist", + "posttest": "npm run lint && npm run dist && texample", "prepack": "npm run dist", "dist": "npm run toc && rollup -c" }, @@ -49,14 +49,18 @@ "fake now" ], "devDependencies": { - "@rollup/plugin-commonjs": "^25.0.3", - "c8": "^8.0.1", - "chai": "^4.3.7", - "eslint": "^8.46.0", + "@rollup/plugin-commonjs": "^25.0.7", + "c8": "^9.1.0", + "chai": "^5.1.0", + "eslint": "^9.3.0", "lodash.clonedeep": "^4.5.0", "markdown-toc": "^1.2.0", - "mocha": "^10.1.0", - "moment": "^2.29.4", - "rollup": "^3.27.2" + "mocha": "^10.3.0", + "moment": "^2.30.1", + "prettier": "^3.2.5", + "rollup": "^4.12.1" + }, + "dependencies": { + "texample": "^0.0.5" } } diff --git a/test/chronokinesis-test.js b/test/chronokinesis-test.js index 7c27812..9751ef8 100644 --- a/test/chronokinesis-test.js +++ b/test/chronokinesis-test.js @@ -17,9 +17,7 @@ describe('chronokinesis', () => { const now = new Date(); ck.freeze(now); return postpone(() => { - - expect((new Date()).getTime()).to.equal(now.getTime()); - + expect(new Date().getTime()).to.equal(now.getTime()); }, 10); }); @@ -32,7 +30,7 @@ describe('chronokinesis', () => { ck.freeze(again); return postpone(() => { - expect((new Date()).getTime()).to.equal(again.getTime()); + expect(new Date().getTime()).to.equal(again.getTime()); }, 10); }); @@ -46,7 +44,7 @@ describe('chronokinesis', () => { dateObj.setUTCHours(hour); expect(dateObj.getUTCHours()).to.equal(hour); - expect((new Date()).getTime()).to.equal(now.getTime()); + expect(new Date().getTime()).to.equal(now.getTime()); }, 10); }); @@ -56,7 +54,7 @@ describe('chronokinesis', () => { const dateObj = new Date(); return postpone(() => { - expect((new Date()).getTime()).to.equal(dateObj.getTime()); + expect(new Date().getTime()).to.equal(dateObj.getTime()); }, 10); }); @@ -74,7 +72,9 @@ describe('chronokinesis', () => { return postpone(() => { const tt = traveledTo.getTime(); - expect(freeze.getTime()).to.be.above(tt - 1).and.below(tt + 1000); + expect(freeze.getTime()) + .to.be.above(tt - 1) + .and.below(tt + 1000); }, 10); }); @@ -93,7 +93,9 @@ describe('chronokinesis', () => { return postpone(() => { const tt = traveled.getTime(); - expect(freeze.getTime()).to.be.above(tt - 1).and.below(tt + 1000); + expect(freeze.getTime()) + .to.be.above(tt - 1) + .and.below(tt + 1000); }, 10); }); @@ -116,7 +118,7 @@ describe('chronokinesis', () => { ck.defrost(); return postpone(() => { - expect((new Date()).getTime()).to.be.above(freeze.getTime()); + expect(new Date().getTime()).to.be.above(freeze.getTime()); expect(ck.isKeepingTime()).to.be.true; }, 10); }); @@ -128,8 +130,8 @@ describe('chronokinesis', () => { ck.defrost(); return postpone(() => { - expect((new Date()).getTime()).to.be.above(traveled - 1); - expect((new Date()).getTime()).to.be.below(traveled + 1000); + expect(new Date().getTime()).to.be.above(traveled - 1); + expect(new Date().getTime()).to.be.below(traveled + 1000); }, 10); }); }); @@ -148,7 +150,7 @@ describe('chronokinesis', () => { ck.travel(dateObj); - expect((new Date()).getUTCFullYear()).to.be.above(now.getUTCFullYear()); + expect(new Date().getUTCFullYear()).to.be.above(now.getUTCFullYear()); }); it('and backwards', () => { @@ -158,7 +160,7 @@ describe('chronokinesis', () => { ck.travel(dateObj); - expect((new Date()).getUTCFullYear()).to.be.below(now.getUTCFullYear()); + expect(new Date().getUTCFullYear()).to.be.below(now.getUTCFullYear()); }); it('with arguments', () => { @@ -168,17 +170,17 @@ describe('chronokinesis', () => { ck.travel('1970-01-01T01:01:01Z'); - expect((new Date()).getUTCFullYear()).to.be.equal(1970); + expect(new Date().getUTCFullYear()).to.be.equal(1970); ck.travel(1980, 11, 24); - expect((new Date()).getUTCFullYear()).to.be.equal(1980); + expect(new Date().getUTCFullYear()).to.be.equal(1980); }); it('without arguments does just about nothing', () => { ck.travel(); - expect((new Date()).getUTCFullYear()).to.be.equal(now.getUTCFullYear()); + expect(new Date().getUTCFullYear()).to.be.equal(now.getUTCFullYear()); }); it('Date.now has traveled', () => { @@ -198,7 +200,7 @@ describe('chronokinesis', () => { const traveledTo = ck.travel(1982, 5, 25, 10, 10, 10, 10); return postpone(() => { - expect((new Date()).getTime()).to.equal(traveledTo.getTime()); + expect(new Date().getTime()).to.equal(traveledTo.getTime()); }, 10); }); @@ -258,9 +260,11 @@ describe('chronokinesis', () => { }); it('after reset in combination with lodash cloneDeep returns native Date', () => { - const content = _.assign(_.cloneDeep({ - d: new Date(), - })); + const content = _.assign( + _.cloneDeep({ + d: new Date(), + }), + ); expect(content.d).to.not.be.a('function'); expect(content.d).to.be.instanceOf(NativeDate); }); @@ -272,7 +276,7 @@ describe('chronokinesis', () => { const freeze = ck.freeze(dateObj); ck.reset(); - expect((new Date()).getUTCFullYear()).to.be.below(freeze.getUTCFullYear()); + expect(new Date().getUTCFullYear()).to.be.below(freeze.getUTCFullYear()); expect(Date.now()).to.be.below(freeze.getTime()); }); @@ -283,7 +287,7 @@ describe('chronokinesis', () => { ck.travel(dateObj); ck.reset(); - expect((new Date()).getUTCFullYear()).to.be.below(dateObj.getUTCFullYear()); + expect(new Date().getUTCFullYear()).to.be.below(dateObj.getUTCFullYear()); expect(Date.now()).to.be.below(dateObj.getTime()); }); }); @@ -463,7 +467,6 @@ describe('chronokinesis', () => { let count = 2; return new Promise((resolve) => { - const ptr = setInterval(atInterval, 10); function atInterval() { if (count === 0) { @@ -502,7 +505,7 @@ describe('chronokinesis', () => { ck.travel(momentDate); - expect((new Date()).getTime()).to.be.equal(momentDate.valueOf()); + expect(new Date().getTime()).to.be.equal(momentDate.valueOf()); }); it('freezes when used as argument', () => { @@ -511,7 +514,7 @@ describe('chronokinesis', () => { ck.freeze(momentDate); return postpone(() => { - expect((new Date()).getTime()).to.equal(momentDate.valueOf()); + expect(new Date().getTime()).to.equal(momentDate.valueOf()); }, 10); }); });