From 822e3f8563f1d6e246e32f843c8bbfa64e08ceea Mon Sep 17 00:00:00 2001 From: afeiship <1290657123@qq.com> Date: Tue, 18 Apr 2023 09:23:13 +0800 Subject: [PATCH] refactor: use es6 sytanx --- .babelrc | 7 -- .editorconfig | 11 --- .gitignore | 11 +-- .npmignore | 9 ++- .npmrc | 10 --- .prettierrc | 32 +-------- .release-it.json | 13 ++-- LICENSE.txt | 2 +- build/clean.js | 13 ---- build/scripts.js | 22 ------ gulpfile.js | 16 ++--- index.d.ts | 3 + jest.config.js | 1 - jest.setup.js | 1 - package.json | 50 +++++++------- src/index.js | 171 ++++++++++++++++++++++++----------------------- 16 files changed, 139 insertions(+), 233 deletions(-) delete mode 100644 .babelrc delete mode 100644 .editorconfig delete mode 100644 build/clean.js delete mode 100644 build/scripts.js create mode 100644 index.d.ts delete mode 100644 jest.setup.js diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 67fc288..0000000 --- a/.babelrc +++ /dev/null @@ -1,7 +0,0 @@ -{ - "presets": [ - [ - "@babel/preset-env" - ] - ] -} diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index beffa30..0000000 --- a/.editorconfig +++ /dev/null @@ -1,11 +0,0 @@ -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 index 071e717..6a3e3b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,6 @@ node_modules bower_components coverage -npm-debug.log -yarn.lock -Gemfile.lock -yarn-error.log -package-lock.json .DS_Store .idea .vscode @@ -13,3 +8,9 @@ package-lock.json # vscode localhistory .history dist + +# package manager +npm-debug.log +yarn.lock +package-lock.json +pnpm-lock.yaml diff --git a/.npmignore b/.npmignore index 9d6b5bc..407d4d9 100644 --- a/.npmignore +++ b/.npmignore @@ -6,19 +6,22 @@ test __tests__ src build +docs gulpfile.js .editorconfig .prettierrc Gemfile jest.config.js +jest.setup.js LICENSE.txt Rakefile -.vscode +express.js .babelrc +.vscode .release-it.json -docs + # vscode localhistory .history -jest.setup.js +.release-it.json diff --git a/.npmrc b/.npmrc index 3475c59..43c97e7 100644 --- a/.npmrc +++ b/.npmrc @@ -1,11 +1 @@ -registry=https://registry.npm.taobao.org -disturl=https://npm.taobao.org/dist -sass_binary_site=https://npm.taobao.org/mirrors/node-sass/ -phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/ -electron_mirror=https://npm.taobao.org/mirrors/electron/ -chromedriver_cdnurl=https://npm.taobao.org/mirrors/chromedriver -operadriver_cdnurl=https://npm.taobao.org/mirrors/operadriver -selenium_cdnurl=https://npm.taobao.org/mirrors/selenium -node_inspector_cdnurl=https://npm.taobao.org/mirrors/node-inspector -fsevents_binary_host_mirror=http://npm.taobao.org/mirrors/fsevents/ package-lock=false diff --git a/.prettierrc b/.prettierrc index 8849e87..cf847e6 100644 --- a/.prettierrc +++ b/.prettierrc @@ -6,35 +6,5 @@ "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" - } - } - ] + "singleQuote": true } diff --git a/.release-it.json b/.release-it.json index d7341a3..f7f3e83 100644 --- a/.release-it.json +++ b/.release-it.json @@ -3,15 +3,12 @@ "requireCleanWorkingDir": false }, "hooks": { - "after:init": [ - "npm run test", - "t2k" - ], - "after:bump": [ - "npm run build" - ] + "after:init": ["npm run test", "t2k"], + "after:bump": ["npm run build"], + "after:release": ["npm pkg get name | cnpm sync"] }, "github": { - "release": true + "release": true, + "proxy": "http://127.0.0.1:9090" } } diff --git a/LICENSE.txt b/LICENSE.txt index be1fbb9..abce289 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2020 afei <1290657123@qq.com> +Copyright (c) 2016 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 diff --git a/build/clean.js b/build/clean.js deleted file mode 100644 index 2b0766a..0000000 --- a/build/clean.js +++ /dev/null @@ -1,13 +0,0 @@ -(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 deleted file mode 100644 index 043d64f..0000000 --- a/build/scripts.js +++ /dev/null @@ -1,22 +0,0 @@ -(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($.babel()) - .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/gulpfile.js b/gulpfile.js index 0513561..ad00360 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,13 +1,9 @@ -(function() { - 'use strict'; +const gulp = require('gulp'); +const { NxScripts, CleanRegistry } = require('@jswork/gulp-registry'); - const gulp = require('gulp'); - const fs = require('fs'); +const task1 = new CleanRegistry(); +const task2 = new NxScripts({ name: 'qs', classify: true }); - //import - fs.readdirSync('./build').map(function(file) { - require('./build/' + file); - }); +[task1, task2].forEach(gulp.registry); - gulp.task('default', gulp.series(['clean', 'scripts'])); -})(); +gulp.task('default', gulp.series(['clean', 'nx:scripts'])); diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..887fddc --- /dev/null +++ b/index.d.ts @@ -0,0 +1,3 @@ +interface NxStatic { + ActiveState: any; +} diff --git a/jest.config.js b/jest.config.js index a0ca96e..29d96fb 100644 --- a/jest.config.js +++ b/jest.config.js @@ -4,7 +4,6 @@ module.exports = { testRegex: [/\.spec.js/], //preset: "jest-puppeteer", automock: false, - setupFiles: ['./jest.setup.js'], moduleNameMapper: { '^@/(.*)$': '/src/$1' } diff --git a/jest.setup.js b/jest.setup.js deleted file mode 100644 index ec28f0a..0000000 --- a/jest.setup.js +++ /dev/null @@ -1 +0,0 @@ -jest.setTimeout(60 * 1000); diff --git a/package.json b/package.json index eb4946f..5916170 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@jswork/next-active-state", "version": "1.0.13", "description": "Mini state mananger based on proxy.", - "homepage": "https://github.com/afeiship/next-active-state", + "homepage": "https://js.work", "author": { "name": "afei", "email": "1290657123@qq.com" @@ -10,46 +10,46 @@ "scripts": { "build": "gulp", "test": "jest", + "start": "node ./express.js", "release": "release-it" }, "main": "dist/index.js", + "module": "dist/index.esm.js", "license": "MIT", "devDependencies": { - "@babel/core": "^7.13.14", - "@babel/preset-env": "^7.13.12", - "@jswork/gulp-pkg-header": "^1.0.4", - "@jswork/next": "^1.0.5", + "@babel/core": "^7.21.3", + "@babel/preset-env": "^7.20.2", + "@jswork/gulp-pkg-header": "^1.0.8", + "@jswork/gulp-registry": "^1.0.20", + "@jswork/next": "^1.1.6", "del": "^6.0.0", "gulp": "^4.0.2", "gulp-babel": "^8.0.0", - "gulp-concat": "^2.6.1", - "gulp-debug": "^4.0.0", - "gulp-ignore": "^3.0.0", - "gulp-load-plugins": "^2.0.6", + "gulp-prettier": "^4.0.0", "gulp-rename": "^2.0.0", - "gulp-size": "^3.0.0", + "gulp-replace": "^1.1.4", "gulp-uglify": "^3.0.2", - "jest": "^26.6.3", - "release-it": "^14.2.2", - "uglify-js": "3.12.4", + "jest": "^29.5.0", + "jest-location-mock": "^1.0.9", "uglify-save-license": "^0.4.1" }, + "babel": { + "presets": [ + [ + "@babel/preset-env" + ] + ] + }, "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" }, "dependencies": { - "@jswork/event-mitt": "^1.0.2", - "@jswork/next-deep-clone": "^1.0.0", - "@jswork/next-deep-each": "^1.0.0", - "@jswork/next-empty": "^1.0.0", - "@jswork/next-is-empty-object": "^1.0.1", + "@jswork/event-mitt": "^1.0.3", + "@jswork/next-deep-clone": "^1.0.2", + "@jswork/next-deep-each": "^1.0.1", + "@jswork/next-empty": "^1.0.1", + "@jswork/next-is-empty-object": "^1.0.3", "fast-deep-equal": "^3.1.3" - }, - "keywords": [ - "next", - "active", - "state", - "proxy" - ] + } } diff --git a/src/index.js b/src/index.js index 438a5c3..631dd47 100644 --- a/src/index.js +++ b/src/index.js @@ -1,94 +1,95 @@ -(function () { - var global = typeof window !== 'undefined' ? window : this || Function('return this')(); - var nx = global.nx || require('@jswork/next'); - var EventMitt = global.EventMitt || require('@jswork/event-mitt'); - var nxDeepEach = nx.deepEach || require('@jswork/next-deep-each'); - var nxDeepClone = nx.deepClone || require('@jswork/next-deep-clone'); - var nxIsEmptyObject = nx.isEmptyObject || require('@jswork/next-is-empty-object'); - var nxEmpty = nx.empty || require('@jswork/next-empty'); - var deepEqual = require('fast-deep-equal'); - // https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy - // https://github.com/sindresorhus/on-change +import nx from '@jswork/next'; +import EventMitt from '@jswork/event-mitt'; +import deepEqual from 'fast-deep-equal'; - var merge = function (state, initial) { - nxEmpty(state); - var isArray = Array.isArray(initial); - if (isArray) { - initial.forEach((item) => state.push(item)); - } else { - if (!nxIsEmptyObject(initial)) { - nx.mix(state, initial); - } +import '@jswork/next-deep-each'; +import '@jswork/next-deep-clone'; +import '@jswork/next-is-empty-object'; +import '@jswork/next-empty'; + +// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy +// https://github.com/sindresorhus/on-change + +var merge = function (state, initial) { + nx.empty(state); + var isArray = Array.isArray(initial); + if (isArray) { + initial.forEach((item) => state.push(item)); + } else { + if (!nx.isEmptyObject(initial)) { + nx.mix(state, initial); } - }; + } +}; - var NxActiveState = nx.declare('nx.ActiveState', { - statics: { - use: function (inData, inCallback) { - var instance = new this(inData); - instance.one('change', inCallback); - return instance.state; - }, - get: function (inState) { - return nxDeepClone(inState); - } +const NxActiveState = nx.declare('nx.ActiveState', { + statics: { + use: function (inData, inCallback) { + var instance = new this(inData); + instance.one('change', inCallback); + return instance.state; }, - properties: { - touched: function () { - return !deepEqual(this.state, this.cloned); - } - }, - methods: { - __initialized__: false, - __muted__: false, - init: function (inData) { - nx.mix(this, EventMitt); - this.cloned = nxDeepClone(inData); - var handler = (key, args) => { - var res = Reflect[key].apply(null, args); - !this.ignore(key, args) && this.emit('change', { action: key, args: args }); - return res; - }; + get: function (inState) { + return nx.deepClone(inState); + } + }, + properties: { + touched: function () { + return !deepEqual(this.state, this.cloned); + } + }, + methods: { + __initialized__: false, + __muted__: false, + init: function (inData) { + nx.mix(this, EventMitt); + this.cloned = nx.deepClone(inData); + var handler = (key, args) => { + var res = Reflect[key].apply(null, args); + !this.ignore(key, args) && this.emit('change', { action: key, args: args }); + return res; + }; - var proxyer = { - set: function () { - var args = nx.slice(arguments); - var value = args[2]; - if (value && typeof value === 'object') { - args[2] = new Proxy(value, proxyer); - } - return handler('set', args); - }, - deleteProperty: function () { - return handler('deleteProperty', nx.slice(arguments)); + var proxyer = { + set: function () { + var args = nx.slice(arguments); + var value = args[2]; + if (value && typeof value === 'object') { + args[2] = new Proxy(value, proxyer); } - }; + return handler('set', args); + }, + deleteProperty: function () { + return handler('deleteProperty', nx.slice(arguments)); + } + }; - this.state = new Proxy(inData, proxyer); - nxDeepEach(this.state, (key, value, target) => (target[key] = value)); - this.__initialized__ = true; - }, - reset: function () { - var initial = nxDeepClone(this.cloned); - this.__muted__ = true; - merge(this.state, initial); - this.__muted__ = false; - this.emit('change', { action: 'reset', args: null }); - return initial; - }, - get: function () { - return nxDeepClone(this.state); - }, - ignore: function (key, args) { - if (!this.__initialized__) return true; - if (this.__muted__) return true; - if (key === 'set' && args[1] === 'length' && Array.isArray(args[0])) return true; - return false; - } + this.state = new Proxy(inData, proxyer); + nx.deepEach(this.state, (key, value, target) => (target[key] = value)); + this.__initialized__ = true; + }, + reset: function () { + var initial = nx.deepClone(this.cloned); + this.__muted__ = true; + merge(this.state, initial); + this.__muted__ = false; + this.emit('change', { action: 'reset', args: null }); + return initial; + }, + get: function () { + return nx.deepClone(this.state); + }, + ignore: function (key, args) { + if (!this.__initialized__) return true; + if (this.__muted__) return true; + if (key === 'set' && args[1] === 'length' && Array.isArray(args[0])) return true; + return false; } - }); - - if (typeof module !== 'undefined' && module.exports) { - module.exports = NxActiveState; } -})(); +}); + +if (typeof module !== 'undefined' && module.exports) { + module.exports = NxActiveState; +} + +export default NxActiveState;