Skip to content

Commit c061251

Browse files
authored
refactor: migrate to ts and remove chai-as-promised (#399)
1 parent a3f399a commit c061251

40 files changed

+296
-305
lines changed

.mocharc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
reporter: spec
22
opts: false
3-
spec: "test/*.spec.js"
3+
spec: "test/*.spec.ts"

lib/camel_case_keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function camelCaseKeys(obj: object) {
2727
if (typeof obj !== 'object') throw new TypeError('obj must be an object!');
2828

2929
const keys = Object.keys(obj);
30-
const result = {};
30+
const result: Record<string, any> = {};
3131

3232
for (const oldKey of keys) {
3333
const newKey = toCamelCase(oldKey);

lib/full_url_for.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { parse } from 'url';
22
import encodeURL from './encode_url';
33
import prettyUrls from './pretty_urls';
44
import Cache from './cache';
5-
const cache = new Cache();
5+
const cache = new Cache<string>();
66

77
function fullUrlForHelper(path = '/') {
88
const { config } = this;

lib/html_tag.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ function encSrcset(str: string) {
1414
return str;
1515
}
1616

17-
function htmlTag(tag: string, attrs: {
18-
[key: string]: string | boolean | null | undefined;
17+
function htmlTag(tag: string, attrs?: {
18+
[key: string]: string | boolean | number | null | undefined;
1919
}, text?: string, escape = true) {
2020
if (!tag) throw new TypeError('tag is required!');
2121

lib/toc_obj.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function tocObj(str: string, options = {}) {
4040

4141
if (!headingsLen) return [];
4242

43-
const result = [];
43+
const result: Result[] = [];
4444

4545
for (let i = 0; i < headingsLen; i++) {
4646
const el = headings[i];

lib/truncate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface Options {
44
separator?: string;
55
}
66

7-
function truncate(str: string, options: Options = {}) {
7+
function truncate(str: string, options: Options = {}): string {
88
if (typeof str !== 'string') throw new TypeError('str must be a string!');
99

1010
const length = options.length || 30;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434
],
3535
"license": "MIT",
3636
"devDependencies": {
37+
"@types/chai": "^4.3.11",
3738
"@types/cross-spawn": "^6.0.2",
39+
"@types/mocha": "^10.0.6",
3840
"@types/node": "^18.11.8",
3941
"@types/prismjs": "^1.26.0",
42+
"@types/rewire": "^2.5.30",
4043
"c8": "^9.1.0",
4144
"chai": "^4.3.6",
42-
"chai-as-promised": "^7.1.1",
4345
"domhandler": "^5.0.3",
4446
"eslint": "^8.23.0",
4547
"eslint-config-hexo": "^5.0.0",

test/.eslintrc.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
{
2-
"extends": "hexo/test",
2+
"extends": "hexo/ts-test",
33
"rules": {
4+
"@typescript-eslint/no-explicit-any": 0,
5+
"@typescript-eslint/ban-ts-comment": 0,
6+
"@typescript-eslint/no-non-null-assertion": 0,
7+
"node/no-unsupported-features/es-syntax": 0,
48
"@typescript-eslint/no-var-requires": 0,
5-
"@typescript-eslint/no-empty-function": 0
9+
"@typescript-eslint/no-empty-function": 0,
10+
"@typescript-eslint/no-unused-vars": 0,
11+
"node/no-missing-require": 0
612
}
713
}

test/cache.spec.js renamed to test/cache.spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
'use strict';
2-
3-
require('chai').should();
1+
import chai from 'chai';
2+
import Cache from '../lib/cache';
3+
chai.should();
44

55
describe('Cache', () => {
6-
const Cache = require('../dist/cache');
7-
const cache = new Cache();
6+
const cache = new Cache<number>();
87

98
it('get & set', () => {
109
cache.set('foo', 123);
11-
cache.get('foo').should.eql(123);
10+
cache.get('foo')!.should.eql(123);
1211
});
1312

1413
it('size', () => {

test/cache_stream.spec.js renamed to test/cache_stream.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict';
1+
import chai from 'chai';
2+
import { Readable } from 'stream';
3+
import CacheStream from '../lib/cache_stream';
24

3-
require('chai').should();
4-
5-
const { Readable } = require('stream');
5+
chai.should();
66

77
describe('CacheStream', () => {
8-
const CacheStream = require('../dist/cache_stream');
9-
108
it('default', () => {
119
const src = new Readable();
1210
const cacheStream = new CacheStream();

test/camel_case_keys.spec.js renamed to test/camel_case_keys.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
require('chai').should();
1+
import chai from 'chai';
2+
import camelCaseKeys from '../lib/camel_case_keys';
3+
chai.should();
44

55
describe('camelCaseKeys', () => {
6-
const camelCaseKeys = require('../dist/camel_case_keys');
7-
86
it('default', () => {
97
const result = camelCaseKeys({
108
foo_bar: 'test'

test/color.spec.js renamed to test/color.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
require('chai').should();
1+
import chai from 'chai';
2+
import Color from '../lib/color';
3+
chai.should();
44

55
describe('color', () => {
6-
const Color = require('../dist/color');
7-
86
it('name', () => {
97
const red = new Color('red');
108
const pink = new Color('pink');
@@ -67,6 +65,7 @@ describe('color', () => {
6765
it('invalid color', () => {
6866
let color;
6967
try {
68+
// @ts-ignore
7069
color = new Color(200);
7170
} catch (e) {
7271
e.message.should.eql('color is required!');

test/decode_url.spec.js renamed to test/decode_url.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
require('chai').should();
1+
import chai from 'chai';
2+
import decodeURL from '../lib/decode_url';
3+
chai.should();
44

55
describe('decodeURL', () => {
6-
const decodeURL = require('../dist/decode_url');
7-
86
it('regular', () => {
97
const content = 'http://foo.com/';
108
decodeURL(content).should.eql(content);

test/deep_merge.spec.js renamed to test/deep_merge.spec.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
'use strict';
2-
3-
require('chai').should();
1+
import chai from 'chai';
2+
import deepMerge from '../lib/deep_merge';
3+
chai.should();
44

55
// The test is modified based on https://github.com/jonschlinkert/merge-deep/blob/master/test.js
66

77
describe('deepMerge()', () => {
8-
const deepMerge = require('../dist/deep_merge');
9-
108
it('should act as lodash.merge', () => {
11-
const obj1 = { 'a': [{ 'b': 2 }, { 'd': 4 }] };
12-
const obj2 = { 'a': [{ 'c': 3 }, { 'e': 5 }] };
9+
const obj1: any = { 'a': [{ 'b': 2 }, { 'd': 4 }] };
10+
const obj2: any = { 'a': [{ 'c': 3 }, { 'e': 5 }] };
1311

1412
const expected = { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] };
15-
16-
deepMerge(obj1, obj2).should.eql(expected);
13+
const result: any = deepMerge(obj1, obj2);
14+
result.should.eql(expected);
1715
});
1816

1917
it('should do a deep merge', () => {
20-
const obj1 = {a: {b: 1, c: 1, d: {e: 1, f: 1}}};
21-
const obj2 = {a: {b: 2, d: {f: 'f'} }};
18+
const obj1: any = {a: {b: 1, c: 1, d: {e: 1, f: 1}}};
19+
const obj2: any = {a: {b: 2, d: {f: 'f'} }};
2220

2321
const expected = {a: {b: 2, c: 1, d: {e: 1, f: 'f'} }};
24-
25-
deepMerge(obj1, obj2).should.eql(expected);
22+
const result: any = deepMerge(obj1, obj2);
23+
result.should.eql(expected);
2624
});
2725

2826
it('should not merge strings', () => {
29-
const obj1 = {a: 'fooo'};
30-
const obj2 = {a: {b: 2, d: {f: 'f'} }};
31-
const obj3 = {a: 'bar'};
27+
const obj1: any = {a: 'fooo'};
28+
const obj2: any = {a: {b: 2, d: {f: 'f'} }};
29+
const obj3: any = {a: 'bar'};
3230

33-
const result = deepMerge(deepMerge(obj1, obj2), obj3);
31+
const result: any = deepMerge(deepMerge(obj1, obj2), obj3);
3432
result.a.should.eql('bar');
3533
});
3634

@@ -45,10 +43,11 @@ describe('deepMerge()', () => {
4543
});
4644

4745
it('should not merge an objects into an array', () => {
48-
const obj1 = {a: {b: 1}};
49-
const obj2 = {a: ['foo', 'bar']};
46+
const obj1: any = {a: {b: 1}};
47+
const obj2: any = {a: ['foo', 'bar']};
5048

51-
deepMerge(obj1, obj2).should.eql({a: ['foo', 'bar']});
49+
const result: any = deepMerge(obj1, obj2);
50+
result.should.eql({a: ['foo', 'bar']});
5251
});
5352

5453
it('should not affect target & source', () => {
@@ -68,10 +67,10 @@ describe('deepMerge()', () => {
6867
});
6968

7069
it('should deep clone arrays during merge', () => {
71-
const obj1 = {a: [1, 2, [3, 4]]};
72-
const obj2 = {b: [5, 6]};
70+
const obj1: any = {a: [1, 2, [3, 4]]};
71+
const obj2: any = {b: [5, 6]};
7372

74-
const result = deepMerge(obj1, obj2);
73+
const result: any = deepMerge(obj1, obj2);
7574

7675
result.a.should.eql([1, 2, [3, 4]]);
7776
result.a[2].should.eql([3, 4]);

test/encode_url.spec.js renamed to test/encode_url.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
require('chai').should();
1+
import chai from 'chai';
2+
import encodeURL from '../lib/encode_url';
3+
chai.should();
44

55
describe('encodeURL', () => {
6-
const encodeURL = require('../dist/encode_url');
7-
86
it('regular', () => {
97
const content = 'http://foo.com/';
108
encodeURL(content).should.eql(content);

test/escape_diacritic.spec.js renamed to test/escape_diacritic.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
require('chai').should();
1+
import chai from 'chai';
2+
import escapeDiacritic from '../lib/escape_diacritic';
3+
chai.should();
44

55
describe('escapeDiacritic', () => {
6-
const escapeDiacritic = require('../dist/escape_diacritic');
7-
86
it('default', () => {
97
escapeDiacritic('Hell\u00F2 w\u00F2rld').should.eql('Hello world');
108
});

test/escape_html.spec.js renamed to test/escape_html.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
require('chai').should();
1+
import chai from 'chai';
2+
import escapeHTML from '../lib/escape_html';
3+
chai.should();
44

55
describe('escapeHTML', () => {
6-
const escapeHTML = require('../dist/escape_html');
7-
86
it('default', () => {
97
escapeHTML('<p class="foo">Hello "world".</p>').should.eql('&lt;p class&#x3D;&quot;foo&quot;&gt;Hello &quot;world&quot;.&lt;&#x2F;p&gt;');
108
});

test/escape_regexp.spec.js renamed to test/escape_regexp.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
3-
require('chai').should();
1+
import chai from 'chai';
2+
import escapeRegExp from '../lib/escape_regexp';
3+
chai.should();
44

55
describe('escapeRegExp', () => {
6-
const escapeRegExp = require('../dist/escape_regexp');
7-
86
it('default', () => {
97
escapeRegExp('hello*world').should.eql('hello\\*world');
108
});

test/full_url_for.spec.js renamed to test/full_url_for.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
'use strict';
1+
import chai from 'chai';
2+
import fullUrlForHelper from '../lib/full_url_for';
3+
chai.should();
24

35
describe('full_url_for', () => {
4-
const ctx = {
6+
const ctx: any = {
57
config: {
68
url: 'http://example.com'
79
}
810
};
911

10-
const fullUrlFor = require('../dist/full_url_for').bind(ctx);
12+
const fullUrlFor: typeof fullUrlForHelper = fullUrlForHelper.bind(ctx);
1113

1214
it('internal url - root directory', () => {
1315
ctx.config.url = 'https://example.com';

test/gravatar.spec.js renamed to test/gravatar.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
'use strict';
2-
3-
const { createHash } = require('crypto');
1+
import { createHash } from 'crypto';
2+
import gravatar from '../lib/gravatar';
43

54
describe('gravatar', () => {
6-
const gravatar = require('../dist/gravatar');
7-
85
function md5(str) {
96
return createHash('md5').update(str).digest('hex');
107
}

test/hash.spec.js renamed to test/hash.spec.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
'use strict';
2-
3-
require('chai').should();
4-
const crypto = require('crypto');
1+
import { createHash } from 'crypto';
2+
import { hash, createSha1Hash } from '../lib/hash';
53

64
function sha1(content) {
7-
const hash = crypto.createHash('sha1');
5+
const hash = createHash('sha1');
86
hash.update(content);
97

108
return hash.digest();
119
}
1210

1311
describe('hash', () => {
14-
const hash = require('../dist/hash');
15-
1612
it('hash', () => {
1713
const content = '123456';
18-
hash.hash(content).should.eql(sha1(content));
14+
hash(content).should.eql(sha1(content));
1915
});
2016

2117
it('createSha1Hash', () => {
22-
const _sha1 = hash.createSha1Hash();
18+
const _sha1 = createSha1Hash();
2319
const content = '123456';
2420
_sha1.update(content);
2521
_sha1.digest().should.eql(sha1(content));
@@ -28,7 +24,7 @@ describe('hash', () => {
2824
it('createSha1Hash - streamMode', () => {
2925
const content1 = '123456';
3026
const content2 = '654321';
31-
const stream = hash.createSha1Hash();
27+
const stream = createSha1Hash();
3228
// explicit convert
3329
stream.write(Buffer.from(content1));
3430
// implicit convert

0 commit comments

Comments
 (0)