Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -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'
17 changes: 17 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 23 additions & 10 deletions lib/json.js → json.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
},
Expand All @@ -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": "*",
Expand All @@ -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": {
Expand All @@ -63,6 +62,6 @@
"engines": {
"node": ">= 0.12.0"
},
"author": "fengmk2 <fengmk2@gmail.com> (https://fengmk2.com)",
"author": "fengmk2 <fengmk2@gmail.com> (https://github.com/fengmk2)",
"license": "MIT"
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}');
Expand Down
3 changes: 1 addition & 2 deletions test_ts/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions test_ts/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
2 changes: 1 addition & 1 deletion test_ts/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {} },
Expand Down
2 changes: 1 addition & 1 deletion test_ts/optimize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
3 changes: 1 addition & 2 deletions test_ts/string.test.ts
Original file line number Diff line number Diff line change
@@ -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}$/);
});
Expand All @@ -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(',,,,'), []);
Expand Down
File renamed without changes.
File renamed without changes.