From 2c41bd881ced83e09bb3da47bcc86e7b3ee62562 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 26 Apr 2023 17:05:00 +0800 Subject: [PATCH 1/3] feat: require speedup closes https://github.com/node-modules/utility/issues/58 --- .github/workflows/nodejs.yml | 18 ++++++++++++++++++ .github/workflows/release.yml | 17 +++++++++++++++++ .travis.yml | 18 ------------------ lib/array.js => array.js | 0 lib/crypto.js => crypto.js | 0 lib/date.js => date.js | 0 lib/function.js => function.js | 0 lib/json.js => json.js | 33 +++++++++++++++++++++++---------- lib/number.js => number.js | 0 lib/object.js => object.js | 0 lib/optimize.js => optimize.js | 0 package.json | 13 ++++++------- lib/polyfill.js => polyfill.js | 0 lib/string.js => string.js | 0 test/json.test.js | 2 +- test_ts/date.test.ts | 5 ++--- test_ts/function.test.ts | 7 +++---- test_ts/json.test.ts | 2 +- test_ts/object.test.ts | 2 +- test_ts/optimize.test.ts | 2 +- test_ts/string.test.ts | 3 +-- lib/utility.js => utility.js | 0 lib/web.js => web.js | 0 23 files changed, 74 insertions(+), 48 deletions(-) create mode 100644 .github/workflows/nodejs.yml create mode 100644 .github/workflows/release.yml delete mode 100644 .travis.yml rename lib/array.js => array.js (100%) rename lib/crypto.js => crypto.js (100%) rename lib/date.js => date.js (100%) rename lib/function.js => function.js (100%) rename lib/json.js => json.js (70%) rename lib/number.js => number.js (100%) rename lib/object.js => object.js (100%) rename lib/optimize.js => optimize.js (100%) rename lib/polyfill.js => polyfill.js (100%) rename lib/string.js => string.js (100%) rename lib/utility.js => utility.js (100%) rename lib/web.js => web.js (100%) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..6068404 --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,18 @@ +name: CI + +on: + push: + branches: [ master ] + + pull_request: + branches: [ master ] + + workflow_dispatch: {} + +jobs: + Job: + name: Node.js + uses: artusjs/github-actions/.github/workflows/node-test.yml@v1 + with: + os: 'ubuntu-latest, windows-latest' + version: '8, 10, 12, 14, 16, 18, 20' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..1612587 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,17 @@ +name: Release + +on: + push: + branches: [ master ] + + workflow_dispatch: {} + +jobs: + release: + name: Node.js + uses: artusjs/github-actions/.github/workflows/node-release.yml@v1 + secrets: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + GIT_TOKEN: ${{ secrets.GIT_TOKEN }} + with: + checkTest: false diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c687fc7..0000000 --- a/.travis.yml +++ /dev/null @@ -1,18 +0,0 @@ -os: - - linux - - osx - - windows -language: node_js -node_js: - - '8' - - '10' - - '12' - - '14' -install: - - npm install --global npminstall && npminstall -script: - - 'npm run ci' -after_script: - - 'npm i codecov && codecov' - - 'node benchmark/map.js' - - 'node benchmark/array_splice.js' diff --git a/lib/array.js b/array.js similarity index 100% rename from lib/array.js rename to array.js diff --git a/lib/crypto.js b/crypto.js similarity index 100% rename from lib/crypto.js rename to crypto.js diff --git a/lib/date.js b/date.js similarity index 100% rename from lib/date.js rename to date.js diff --git a/lib/function.js b/function.js similarity index 100% rename from lib/function.js rename to function.js diff --git a/lib/json.js b/json.js similarity index 70% rename from lib/json.js rename to json.js index 684dee9..468e55f 100644 --- a/lib/json.js +++ b/json.js @@ -1,8 +1,21 @@ 'use strict'; -var fs = require('mz/fs'); var path = require('path'); -var mkdirp = require('mkdirp'); + +var _mkdirp; +function getMkdirp() { + if (!_mkdirp) { + _mkdirp = require('mkdirp'); + } + return _mkdirp; +} +var _fs; +function getFS() { + if (!_fs) { + _fs = require('mz/fs'); + } + return _fs; +} exports.strictJSONParse = function (str) { var obj = JSON.parse(str); @@ -13,10 +26,10 @@ exports.strictJSONParse = function (str) { }; exports.readJSONSync = function(filepath) { - if (!fs.existsSync(filepath)) { + if (!getFS().existsSync(filepath)) { throw new Error(filepath + ' is not found'); } - return JSON.parse(fs.readFileSync(filepath)); + return JSON.parse(getFS().readFileSync(filepath)); }; exports.writeJSONSync = function(filepath, str, options) { @@ -25,21 +38,21 @@ exports.writeJSONSync = function(filepath, str, options) { options.space = 2; } - mkdirp.sync(path.dirname(filepath)); + getMkdirp().sync(path.dirname(filepath)); if (typeof str === 'object') { str = JSON.stringify(str, options.replacer, options.space) + '\n'; } - fs.writeFileSync(filepath, str); + getFS().writeFileSync(filepath, str); }; exports.readJSON = function(filepath) { - return fs.exists(filepath) + return getFS().exists(filepath) .then(function(exists) { if (!exists) { throw new Error(filepath + ' is not found'); } - return fs.readFile(filepath); + return getFS().readFile(filepath); }) .then(function(buf) { return JSON.parse(buf); @@ -58,13 +71,13 @@ exports.writeJSON = function(filepath, str, options) { return mkdir(path.dirname(filepath)) .then(function() { - return fs.writeFile(filepath, str); + return getFS().writeFile(filepath, str); }); }; function mkdir(dir) { return new Promise(function(resolve, reject) { - mkdirp(dir, function(err) { + getMkdirp()(dir, function(err) { if (err) { return reject(err); } diff --git a/lib/number.js b/number.js similarity index 100% rename from lib/number.js rename to number.js diff --git a/lib/object.js b/object.js similarity index 100% rename from lib/object.js rename to object.js diff --git a/lib/optimize.js b/optimize.js similarity index 100% rename from lib/optimize.js rename to optimize.js diff --git a/package.json b/package.json index d4961b6..d99780a 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,9 @@ "name": "utility", "version": "1.17.0", "description": "A collection of useful utilities.", - "main": "lib/utility.js", + "main": "utility.js", "files": [ - "lib", + "*.js", "index.d.ts" ], "scripts": { @@ -15,7 +15,6 @@ "test-cov": "nyc ava test/**/*.test.js && nyc report --reporter=lcov", "lint": "jshint .", "ci": "npm run lint && npm run test-cov && npm run test-ts", - "autod": "autod -w --prefix '^' -e benchmark", "test-optimized": "node --allow-natives-syntax --trace_opt --trace_deopt test/optimized.js", "contributor": "git-contributor" }, @@ -29,7 +28,6 @@ "devDependencies": { "@types/escape-html": "0.0.20", "@types/node": "^10.12.12", - "autod": "*", "ava": "^0.25.0", "ava-ts": "^0.25.2", "beautify-benchmark": "*", @@ -42,8 +40,9 @@ "object-assign": "^4.1.1", "optimized": "^1.2.0", "rimraf": "^2.6.2", - "ts-node": "^7.0.1", - "typescript": "^3.2.2" + "time-require": "^0.1.2", + "ts-node": "^10.9.1", + "typescript": "^5.0.4" }, "homepage": "https://github.com/node-modules/utility", "repository": { @@ -63,6 +62,6 @@ "engines": { "node": ">= 0.12.0" }, - "author": "fengmk2 (https://fengmk2.com)", + "author": "fengmk2 (https://github.com/fengmk2)", "license": "MIT" } diff --git a/lib/polyfill.js b/polyfill.js similarity index 100% rename from lib/polyfill.js rename to polyfill.js diff --git a/lib/string.js b/string.js similarity index 100% rename from lib/string.js rename to string.js diff --git a/test/json.test.js b/test/json.test.js index 96ebacc..2486a03 100644 --- a/test/json.test.js +++ b/test/json.test.js @@ -5,7 +5,7 @@ import path from 'path'; import rimraf from 'rimraf'; import assert from 'assert'; import test from 'ava'; -import utils from '../'; +import utils from '../json'; test('strictJSONParse() should parse normal json ok', t => { const obj = utils.strictJSONParse('{"foo": "bar"}'); diff --git a/test_ts/date.test.ts b/test_ts/date.test.ts index b03c464..ca8a4d7 100644 --- a/test_ts/date.test.ts +++ b/test_ts/date.test.ts @@ -1,7 +1,6 @@ import test from 'ava'; -import * as moment from 'moment'; -import * as utility from '../'; - +import moment from 'moment'; +import utility from '..'; test('YYYYMMDDHHmmss() should return an "YYYY-MM-DD HH:mm:ss" format date string', t => { diff --git a/test_ts/function.test.ts b/test_ts/function.test.ts index 04b7c38..1b24da7 100644 --- a/test_ts/function.test.ts +++ b/test_ts/function.test.ts @@ -1,11 +1,10 @@ import test from 'ava'; -import * as utility from '../'; - +import utility from '../'; test('getParamNames() should return parameter names', t => { - t.throws(() => utility.getParamNames(null)); - t.throws(() => utility.getParamNames(undefined)); + t.throws(() => utility.getParamNames(null as any)); + t.throws(() => utility.getParamNames(undefined as any)); t.deepEqual(utility.getParamNames(function () {}), []); /* jshint ignore:start */ t.deepEqual(utility.getParamNames(function (key1) {}), ['key1']); diff --git a/test_ts/json.test.ts b/test_ts/json.test.ts index 4025f74..9814edc 100644 --- a/test_ts/json.test.ts +++ b/test_ts/json.test.ts @@ -3,7 +3,7 @@ import * as fs from 'fs'; import * as path from 'path'; const rimraf = require('rimraf'); import test from 'ava'; -import * as utility from '../'; +import utility from '..'; test('strictJSONParse() should parse normal json ok', t => { diff --git a/test_ts/object.test.ts b/test_ts/object.test.ts index 5c5fe7e..0fee802 100644 --- a/test_ts/object.test.ts +++ b/test_ts/object.test.ts @@ -14,7 +14,7 @@ test('has() should has property ok', t => { test('getOwnEnumerables() should return all enumerable and ownership property names', t => { t.deepEqual(utility.getOwnEnumerables({a: 1}), [ 'a' ]); - const a = { a: 1 }; + const a = { a: 1 } as any; Object.defineProperties(a, { one: { enumerable: true, value: 'one' }, two: { enumerable: false, value: function() {} }, diff --git a/test_ts/optimize.test.ts b/test_ts/optimize.test.ts index b370c38..c8896c2 100644 --- a/test_ts/optimize.test.ts +++ b/test_ts/optimize.test.ts @@ -31,7 +31,7 @@ test('try() should work when throw err with string', t => { throw 'string error'; }); t.true(res.error instanceof Error); - t.is(res.error.message, 'string error'); + t.is(res.error!.message, 'string error'); t.falsy(res.value); }); diff --git a/test_ts/string.test.ts b/test_ts/string.test.ts index 976b3d2..dc7de50 100644 --- a/test_ts/string.test.ts +++ b/test_ts/string.test.ts @@ -1,7 +1,6 @@ import test from 'ava'; import * as utility from '../'; - test('randomString() should get random string by default', t => { t.regex(utility.randomString(), /^[0-9a-zA-Z]{16}$/); }); @@ -25,7 +24,7 @@ test('split(), splitAlwaysOptimized() should return []', t => { // !!! TSError // t.deepEqual(utility.split(), []); - t.deepEqual(utility.split(null), []); + t.deepEqual(utility.split(null as any), []); t.deepEqual(utility.split(''), []); t.deepEqual(utility.splitAlwaysOptimized(',,,,'), []); diff --git a/lib/utility.js b/utility.js similarity index 100% rename from lib/utility.js rename to utility.js diff --git a/lib/web.js b/web.js similarity index 100% rename from lib/web.js rename to web.js From 73208f08c9a667fefd6b3f5feac71c225f8f088f Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 26 Apr 2023 17:12:11 +0800 Subject: [PATCH 2/3] f --- package.json | 2 +- test_ts/date.test.ts | 6 +++--- test_ts/function.test.ts | 2 +- test_ts/json.test.ts | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index d99780a..634647d 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "optimized": "^1.2.0", "rimraf": "^2.6.2", "time-require": "^0.1.2", - "ts-node": "^10.9.1", + "ts-node": "^7.0.1", "typescript": "^5.0.4" }, "homepage": "https://github.com/node-modules/utility", diff --git a/test_ts/date.test.ts b/test_ts/date.test.ts index ca8a4d7..83f9fd7 100644 --- a/test_ts/date.test.ts +++ b/test_ts/date.test.ts @@ -1,6 +1,6 @@ import test from 'ava'; -import moment from 'moment'; -import utility from '..'; +import * as moment from 'moment'; +import * as utility from '../'; test('YYYYMMDDHHmmss() should return an "YYYY-MM-DD HH:mm:ss" format date string', t => { @@ -135,7 +135,7 @@ test('datestruct() should return an date struct', t => { test('timestamp() should return a unix timestamp', t => { const ts = utility.timestamp(); t.is(typeof ts, 'number'); - t.true(ts > 1378153366); + t.true(ts as any > 1378153366); t.is(String(ts).length, 10); t.is((utility.timestamp(1385091596) as Date).getTime(), 1385091596000); diff --git a/test_ts/function.test.ts b/test_ts/function.test.ts index 1b24da7..83db31a 100644 --- a/test_ts/function.test.ts +++ b/test_ts/function.test.ts @@ -1,6 +1,6 @@ import test from 'ava'; -import utility from '../'; +import * as utility from '../'; test('getParamNames() should return parameter names', t => { t.throws(() => utility.getParamNames(null as any)); diff --git a/test_ts/json.test.ts b/test_ts/json.test.ts index 9814edc..4025f74 100644 --- a/test_ts/json.test.ts +++ b/test_ts/json.test.ts @@ -3,7 +3,7 @@ import * as fs from 'fs'; import * as path from 'path'; const rimraf = require('rimraf'); import test from 'ava'; -import utility from '..'; +import * as utility from '../'; test('strictJSONParse() should parse normal json ok', t => { From a48540a19ca562d6288d00b0597cf5eb62ff288b Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 26 Apr 2023 17:20:53 +0800 Subject: [PATCH 3/3] f --- .github/workflows/nodejs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 6068404..09804eb 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -14,5 +14,5 @@ jobs: name: Node.js uses: artusjs/github-actions/.github/workflows/node-test.yml@v1 with: - os: 'ubuntu-latest, windows-latest' + os: 'ubuntu-latest' version: '8, 10, 12, 14, 16, 18, 20'