Skip to content

Commit

Permalink
refactor: migrate to ts and remove chai-as-promised (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Mar 15, 2024
1 parent a3f399a commit c061251
Show file tree
Hide file tree
Showing 40 changed files with 296 additions and 305 deletions.
2 changes: 1 addition & 1 deletion .mocharc.yml
@@ -1,3 +1,3 @@
reporter: spec
opts: false
spec: "test/*.spec.js"
spec: "test/*.spec.ts"
2 changes: 1 addition & 1 deletion lib/camel_case_keys.ts
Expand Up @@ -27,7 +27,7 @@ function camelCaseKeys(obj: object) {
if (typeof obj !== 'object') throw new TypeError('obj must be an object!');

const keys = Object.keys(obj);
const result = {};
const result: Record<string, any> = {};

Check warning on line 30 in lib/camel_case_keys.ts

View workflow job for this annotation

GitHub Actions / linter

Unexpected any. Specify a different type

for (const oldKey of keys) {
const newKey = toCamelCase(oldKey);
Expand Down
2 changes: 1 addition & 1 deletion lib/full_url_for.ts
Expand Up @@ -2,7 +2,7 @@ import { parse } from 'url';
import encodeURL from './encode_url';
import prettyUrls from './pretty_urls';
import Cache from './cache';
const cache = new Cache();
const cache = new Cache<string>();

function fullUrlForHelper(path = '/') {
const { config } = this;
Expand Down
4 changes: 2 additions & 2 deletions lib/html_tag.ts
Expand Up @@ -14,8 +14,8 @@ function encSrcset(str: string) {
return str;
}

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

Expand Down
2 changes: 1 addition & 1 deletion lib/toc_obj.ts
Expand Up @@ -40,7 +40,7 @@ function tocObj(str: string, options = {}) {

if (!headingsLen) return [];

const result = [];
const result: Result[] = [];

for (let i = 0; i < headingsLen; i++) {
const el = headings[i];
Expand Down
2 changes: 1 addition & 1 deletion lib/truncate.ts
Expand Up @@ -4,7 +4,7 @@ interface Options {
separator?: string;
}

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

const length = options.length || 30;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -34,12 +34,14 @@
],
"license": "MIT",
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/cross-spawn": "^6.0.2",
"@types/mocha": "^10.0.6",
"@types/node": "^18.11.8",
"@types/prismjs": "^1.26.0",
"@types/rewire": "^2.5.30",
"c8": "^9.1.0",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"domhandler": "^5.0.3",
"eslint": "^8.23.0",
"eslint-config-hexo": "^5.0.0",
Expand Down
10 changes: 8 additions & 2 deletions test/.eslintrc.json
@@ -1,7 +1,13 @@
{
"extends": "hexo/test",
"extends": "hexo/ts-test",
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/no-non-null-assertion": 0,
"node/no-unsupported-features/es-syntax": 0,
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-empty-function": 0
"@typescript-eslint/no-empty-function": 0,
"@typescript-eslint/no-unused-vars": 0,
"node/no-missing-require": 0
}
}
11 changes: 5 additions & 6 deletions test/cache.spec.js → test/cache.spec.ts
@@ -1,14 +1,13 @@
'use strict';

require('chai').should();
import chai from 'chai';
import Cache from '../lib/cache';
chai.should();

describe('Cache', () => {
const Cache = require('../dist/cache');
const cache = new Cache();
const cache = new Cache<number>();

it('get & set', () => {
cache.set('foo', 123);
cache.get('foo').should.eql(123);
cache.get('foo')!.should.eql(123);
});

it('size', () => {
Expand Down
10 changes: 4 additions & 6 deletions test/cache_stream.spec.js → test/cache_stream.spec.ts
@@ -1,12 +1,10 @@
'use strict';
import chai from 'chai';
import { Readable } from 'stream';
import CacheStream from '../lib/cache_stream';

require('chai').should();

const { Readable } = require('stream');
chai.should();

describe('CacheStream', () => {
const CacheStream = require('../dist/cache_stream');

it('default', () => {
const src = new Readable();
const cacheStream = new CacheStream();
Expand Down
8 changes: 3 additions & 5 deletions test/camel_case_keys.spec.js → test/camel_case_keys.spec.ts
@@ -1,10 +1,8 @@
'use strict';

require('chai').should();
import chai from 'chai';
import camelCaseKeys from '../lib/camel_case_keys';
chai.should();

describe('camelCaseKeys', () => {
const camelCaseKeys = require('../dist/camel_case_keys');

it('default', () => {
const result = camelCaseKeys({
foo_bar: 'test'
Expand Down
9 changes: 4 additions & 5 deletions test/color.spec.js → test/color.spec.ts
@@ -1,10 +1,8 @@
'use strict';

require('chai').should();
import chai from 'chai';
import Color from '../lib/color';
chai.should();

describe('color', () => {
const Color = require('../dist/color');

it('name', () => {
const red = new Color('red');
const pink = new Color('pink');
Expand Down Expand Up @@ -67,6 +65,7 @@ describe('color', () => {
it('invalid color', () => {
let color;
try {
// @ts-ignore
color = new Color(200);
} catch (e) {
e.message.should.eql('color is required!');
Expand Down
8 changes: 3 additions & 5 deletions test/decode_url.spec.js → test/decode_url.spec.ts
@@ -1,10 +1,8 @@
'use strict';

require('chai').should();
import chai from 'chai';
import decodeURL from '../lib/decode_url';
chai.should();

describe('decodeURL', () => {
const decodeURL = require('../dist/decode_url');

it('regular', () => {
const content = 'http://foo.com/';
decodeURL(content).should.eql(content);
Expand Down
45 changes: 22 additions & 23 deletions test/deep_merge.spec.js → test/deep_merge.spec.ts
@@ -1,36 +1,34 @@
'use strict';

require('chai').should();
import chai from 'chai';
import deepMerge from '../lib/deep_merge';
chai.should();

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

describe('deepMerge()', () => {
const deepMerge = require('../dist/deep_merge');

it('should act as lodash.merge', () => {
const obj1 = { 'a': [{ 'b': 2 }, { 'd': 4 }] };
const obj2 = { 'a': [{ 'c': 3 }, { 'e': 5 }] };
const obj1: any = { 'a': [{ 'b': 2 }, { 'd': 4 }] };
const obj2: any = { 'a': [{ 'c': 3 }, { 'e': 5 }] };

const expected = { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] };

deepMerge(obj1, obj2).should.eql(expected);
const result: any = deepMerge(obj1, obj2);
result.should.eql(expected);
});

it('should do a deep merge', () => {
const obj1 = {a: {b: 1, c: 1, d: {e: 1, f: 1}}};
const obj2 = {a: {b: 2, d: {f: 'f'} }};
const obj1: any = {a: {b: 1, c: 1, d: {e: 1, f: 1}}};
const obj2: any = {a: {b: 2, d: {f: 'f'} }};

const expected = {a: {b: 2, c: 1, d: {e: 1, f: 'f'} }};

deepMerge(obj1, obj2).should.eql(expected);
const result: any = deepMerge(obj1, obj2);
result.should.eql(expected);
});

it('should not merge strings', () => {
const obj1 = {a: 'fooo'};
const obj2 = {a: {b: 2, d: {f: 'f'} }};
const obj3 = {a: 'bar'};
const obj1: any = {a: 'fooo'};
const obj2: any = {a: {b: 2, d: {f: 'f'} }};
const obj3: any = {a: 'bar'};

const result = deepMerge(deepMerge(obj1, obj2), obj3);
const result: any = deepMerge(deepMerge(obj1, obj2), obj3);
result.a.should.eql('bar');
});

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

it('should not merge an objects into an array', () => {
const obj1 = {a: {b: 1}};
const obj2 = {a: ['foo', 'bar']};
const obj1: any = {a: {b: 1}};
const obj2: any = {a: ['foo', 'bar']};

deepMerge(obj1, obj2).should.eql({a: ['foo', 'bar']});
const result: any = deepMerge(obj1, obj2);
result.should.eql({a: ['foo', 'bar']});
});

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

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

const result = deepMerge(obj1, obj2);
const result: any = deepMerge(obj1, obj2);

result.a.should.eql([1, 2, [3, 4]]);
result.a[2].should.eql([3, 4]);
Expand Down
8 changes: 3 additions & 5 deletions test/encode_url.spec.js → test/encode_url.spec.ts
@@ -1,10 +1,8 @@
'use strict';

require('chai').should();
import chai from 'chai';
import encodeURL from '../lib/encode_url';
chai.should();

describe('encodeURL', () => {
const encodeURL = require('../dist/encode_url');

it('regular', () => {
const content = 'http://foo.com/';
encodeURL(content).should.eql(content);
Expand Down
@@ -1,10 +1,8 @@
'use strict';

require('chai').should();
import chai from 'chai';
import escapeDiacritic from '../lib/escape_diacritic';
chai.should();

describe('escapeDiacritic', () => {
const escapeDiacritic = require('../dist/escape_diacritic');

it('default', () => {
escapeDiacritic('Hell\u00F2 w\u00F2rld').should.eql('Hello world');
});
Expand Down
8 changes: 3 additions & 5 deletions test/escape_html.spec.js → test/escape_html.spec.ts
@@ -1,10 +1,8 @@
'use strict';

require('chai').should();
import chai from 'chai';
import escapeHTML from '../lib/escape_html';
chai.should();

describe('escapeHTML', () => {
const escapeHTML = require('../dist/escape_html');

it('default', () => {
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;');
});
Expand Down
8 changes: 3 additions & 5 deletions test/escape_regexp.spec.js → test/escape_regexp.spec.ts
@@ -1,10 +1,8 @@
'use strict';

require('chai').should();
import chai from 'chai';
import escapeRegExp from '../lib/escape_regexp';
chai.should();

describe('escapeRegExp', () => {
const escapeRegExp = require('../dist/escape_regexp');

it('default', () => {
escapeRegExp('hello*world').should.eql('hello\\*world');
});
Expand Down
8 changes: 5 additions & 3 deletions test/full_url_for.spec.js → test/full_url_for.spec.ts 100755 → 100644
@@ -1,13 +1,15 @@
'use strict';
import chai from 'chai';
import fullUrlForHelper from '../lib/full_url_for';
chai.should();

describe('full_url_for', () => {
const ctx = {
const ctx: any = {
config: {
url: 'http://example.com'
}
};

const fullUrlFor = require('../dist/full_url_for').bind(ctx);
const fullUrlFor: typeof fullUrlForHelper = fullUrlForHelper.bind(ctx);

it('internal url - root directory', () => {
ctx.config.url = 'https://example.com';
Expand Down
7 changes: 2 additions & 5 deletions test/gravatar.spec.js → test/gravatar.spec.ts
@@ -1,10 +1,7 @@
'use strict';

const { createHash } = require('crypto');
import { createHash } from 'crypto';
import gravatar from '../lib/gravatar';

describe('gravatar', () => {
const gravatar = require('../dist/gravatar');

function md5(str) {
return createHash('md5').update(str).digest('hex');
}
Expand Down
16 changes: 6 additions & 10 deletions test/hash.spec.js → test/hash.spec.ts
@@ -1,25 +1,21 @@
'use strict';

require('chai').should();
const crypto = require('crypto');
import { createHash } from 'crypto';
import { hash, createSha1Hash } from '../lib/hash';

function sha1(content) {
const hash = crypto.createHash('sha1');
const hash = createHash('sha1');
hash.update(content);

return hash.digest();
}

describe('hash', () => {
const hash = require('../dist/hash');

it('hash', () => {
const content = '123456';
hash.hash(content).should.eql(sha1(content));
hash(content).should.eql(sha1(content));
});

it('createSha1Hash', () => {
const _sha1 = hash.createSha1Hash();
const _sha1 = createSha1Hash();
const content = '123456';
_sha1.update(content);
_sha1.digest().should.eql(sha1(content));
Expand All @@ -28,7 +24,7 @@ describe('hash', () => {
it('createSha1Hash - streamMode', () => {
const content1 = '123456';
const content2 = '654321';
const stream = hash.createSha1Hash();
const stream = createSha1Hash();
// explicit convert
stream.write(Buffer.from(content1));
// implicit convert
Expand Down

0 comments on commit c061251

Please sign in to comment.