diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..09804eb --- /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' + 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..634647d 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", + "time-require": "^0.1.2", "ts-node": "^7.0.1", - "typescript": "^3.2.2" + "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..83f9fd7 100644 --- a/test_ts/date.test.ts +++ b/test_ts/date.test.ts @@ -2,7 +2,6 @@ import test from 'ava'; import * as moment from 'moment'; import * as utility from '../'; - test('YYYYMMDDHHmmss() should return an "YYYY-MM-DD HH:mm:ss" format date string', t => { // !!! TSError @@ -136,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 04b7c38..83db31a 100644 --- a/test_ts/function.test.ts +++ b/test_ts/function.test.ts @@ -2,10 +2,9 @@ import test from 'ava'; import * as 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/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