Skip to content

Commit 2482c20

Browse files
committed
change type checker to using lodash
1 parent e768c3b commit 2482c20

6 files changed

Lines changed: 25 additions & 16 deletions

File tree

‎package-lock.json‎

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
},
1919
"dependencies": {
2020
"@msgpack/msgpack": "2.8.0",
21-
"component-emitter": "2.0.0"
21+
"component-emitter": "2.0.0",
22+
"lodash": "^4.17.21"
2223
},
2324
"scripts": {
2425
"build": "rimraf dist/* && pkgroll",
@@ -43,6 +44,7 @@
4344
"dist"
4445
],
4546
"devDependencies": {
47+
"@types/lodash": "^4.17.4",
4648
"@types/mocha": "^10.0.6",
4749
"mocha": "10.4.0",
4850
"nyc": "15.1.0",

‎src/decoder.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
33
import msgpack from '@msgpack/msgpack';
44
import Emitter from 'component-emitter';
5-
import { isInteger, isObject, isString } from './type-checkers';
5+
import isString from 'lodash/isString';
66
import { PacketType } from './packet-format';
77
import validatePacketData from './validate-packet-data';
8+
import { objectIsInteger, isRecord } from './type-checkers';
89

910
export class DecoderClass extends Emitter {
1011
options: msgpack.DecodeOptions = {};
@@ -28,11 +29,11 @@ const buildDecoder = (options: msgpack.DecodeOptions = {}): typeof DecoderClass
2829
}
2930

3031
validatePacket(decoded: unknown) {
31-
if (!isObject(decoded)) {
32+
if (!isRecord(decoded)) {
3233
throw new Error('invalid packet');
3334
}
3435

35-
const isValidTypeField = isInteger(decoded.type)
36+
const isValidTypeField = objectIsInteger(decoded.type)
3637
&& decoded.type >= PacketType.CONNECT
3738
&& decoded.type <= PacketType.CONNECT_ERROR;
3839

@@ -48,7 +49,7 @@ const buildDecoder = (options: msgpack.DecodeOptions = {}): typeof DecoderClass
4849
throw new Error('invalid packet payload');
4950
}
5051

51-
if (undefined !== decoded.id && !isInteger(decoded.id)) {
52+
if (undefined !== decoded.id && !objectIsInteger(decoded.id)) {
5253
throw new Error('invalid packet id');
5354
}
5455
}

‎src/type-checkers.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
export function isString(object: unknown) {
2-
return 'string' === typeof object;
3-
}
1+
import isInteger from 'lodash/isInteger';
42

5-
export function isObject(object: unknown): object is Record<string, unknown> {
3+
export function isRecord(object: unknown): object is Record<string, unknown> {
64
return null !== object && 'object' === typeof object && !Array.isArray(object);
75
}
86

9-
export function isInteger(object: unknown): object is number {
10-
return Number.isInteger(object);
7+
export function objectIsInteger(object: unknown): object is number {
8+
return isInteger(object);
119
}

‎src/validate-packet-data.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import isObject from 'lodash/isObject';
2+
import isString from 'lodash/isString';
13
import { PacketType } from './packet-format';
2-
import { isObject, isString } from './type-checkers';
34

45
function validatePacketData(packet: Record<string, unknown>) {
56
switch (packet.type) {

‎tsconfig.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
3737
/* JavaScript Support */
3838
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
39-
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
39+
"checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
4040
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
4141
/* Emit */
4242
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */

0 commit comments

Comments
 (0)