Skip to content

Commit c55c5d0

Browse files
committed
fix: update prettier to ^2.3.0
1 parent b91205a commit c55c5d0

File tree

6 files changed

+72
-187
lines changed

6 files changed

+72
-187
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@types/mocha": "^8.2.2",
2727
"@types/node": "^15.3.0",
2828
"mocha": "^8.4.0",
29-
"prettier": "^1.6.1",
29+
"prettier": "^2.3.0",
3030
"ts-mocha": "^8.0.0",
3131
"ts-node": "^9.1.1",
3232
"typescript": "^4.2.4"
@@ -37,5 +37,8 @@
3737
"package.json",
3838
"tsconfig.json"
3939
],
40-
"license": "GPL-3.0"
40+
"license": "GPL-3.0",
41+
"prettier": {
42+
"printWidth": 120
43+
}
4144
}

src/NtpPacketParser.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,38 @@ export class NtpPacketParser {
1616
{
1717
name: "rootDelay",
1818
bits: 32,
19-
converter: NtpPacketParser._fromNtpTimestamp
19+
converter: NtpPacketParser._fromNtpTimestamp,
2020
} as PacketStruct<"rootDelay">,
2121
{
2222
name: "rootDispersion",
2323
bits: 32,
24-
converter: NtpPacketParser._fromNtpTimestamp
24+
converter: NtpPacketParser._fromNtpTimestamp,
2525
} as PacketStruct<"rootDispersion">,
2626
{
2727
name: "referenceId",
2828
bits: 32,
29-
converter: (value, packet) => this._ntpIdentifier(packet.stratum, value)
29+
converter: (value, packet) => this._ntpIdentifier(packet.stratum, value),
3030
} as PacketStruct<"referenceId">,
3131
{
3232
name: "referenceTimestamp",
3333
bits: 64,
34-
converter: NtpPacketParser._fromNtpTimestamp
34+
converter: NtpPacketParser._fromNtpTimestamp,
3535
} as PacketStruct<"referenceTimestamp">,
3636
{
3737
name: "originTimestamp",
3838
bits: 64,
39-
converter: NtpPacketParser._fromNtpTimestamp
39+
converter: NtpPacketParser._fromNtpTimestamp,
4040
} as PacketStruct<"originTimestamp">,
4141
{
4242
name: "receiveTimestamp",
4343
bits: 64,
44-
converter: NtpPacketParser._fromNtpTimestamp
44+
converter: NtpPacketParser._fromNtpTimestamp,
4545
} as PacketStruct<"receiveTimestamp">,
4646
{
4747
name: "transmitTimestamp",
4848
bits: 64,
49-
converter: NtpPacketParser._fromNtpTimestamp
50-
} as PacketStruct<"transmitTimestamp">
49+
converter: NtpPacketParser._fromNtpTimestamp,
50+
} as PacketStruct<"transmitTimestamp">,
5151
];
5252
}
5353

@@ -74,14 +74,9 @@ export class NtpPacketParser {
7474
if (stratum != 1) {
7575
return parseInt(value, 2).toString();
7676
}
77-
let chars = [
78-
value.slice(0, 8),
79-
value.slice(8, 16),
80-
value.slice(16, 24),
81-
value.slice(24, 32)
82-
];
77+
let chars = [value.slice(0, 8), value.slice(8, 16), value.slice(16, 24), value.slice(24, 32)];
8378

84-
chars = chars.map(function(v) {
79+
chars = chars.map(function (v) {
8580
return String.fromCharCode(parseInt(v, 2));
8681
});
8782

@@ -94,9 +89,7 @@ export class NtpPacketParser {
9489
*/
9590
public static _fromNtpTimestamp(value: string): Date {
9691
if (value.length % 2 !== 0) {
97-
throw new Error(
98-
"Invalid timestamp format, expected even number of characters"
99-
);
92+
throw new Error("Invalid timestamp format, expected even number of characters");
10093
}
10194

10295
const seconds = parseInt(value, 2) / Math.pow(2, value.length / 2),
@@ -114,12 +107,8 @@ export class NtpPacketParser {
114107
let data: Partial<NtpPacket> = {};
115108
let offset = 0;
116109

117-
NtpPacketParser.packetStruct.forEach(struct => {
118-
const baseRepresentation = NtpPacketParser._getBits(
119-
udpPacket,
120-
offset,
121-
struct.bits
122-
);
110+
NtpPacketParser.packetStruct.forEach((struct) => {
111+
const baseRepresentation = NtpPacketParser._getBits(udpPacket, offset, struct.bits);
123112
if (struct.converter) {
124113
// @ts-ignore
125114
data[struct.name] = struct.converter(baseRepresentation, data);

test/packets.valid.js

Lines changed: 9 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,8 @@
11
module.exports = [
22
{
33
buffer: [
4-
28,
5-
1,
6-
48,
7-
234,
8-
0,
9-
0,
10-
0,
11-
0,
12-
0,
13-
0,
14-
0,
15-
68,
16-
80,
17-
84,
18-
66,
19-
0,
20-
221,
21-
82,
22-
40,
23-
120,
24-
122,
25-
239,
26-
46,
27-
145,
28-
48,
29-
48,
30-
48,
31-
48,
32-
48,
33-
48,
34-
48,
35-
10,
36-
221,
37-
82,
38-
40,
39-
124,
40-
17,
41-
15,
42-
55,
43-
20,
44-
221,
45-
82,
46-
40,
47-
124,
48-
17,
49-
25,
50-
203,
51-
213
4+
28, 1, 48, 234, 0, 0, 0, 0, 0, 0, 0, 68, 80, 84, 66, 0, 221, 82, 40, 120, 122, 239, 46, 145, 48, 48, 48, 48, 48,
5+
48, 48, 10, 221, 82, 40, 124, 17, 15, 55, 20, 221, 82, 40, 124, 17, 25, 203, 213,
526
],
537
expected: {
548
leapIndicator: 0,
@@ -63,59 +17,13 @@ module.exports = [
6317
referenceTimestamp: new Date("2017-08-31T06:17:28.480Z"),
6418
originTimestamp: new Date("1925-08-15T05:27:12.188Z"),
6519
receiveTimestamp: new Date("2017-08-31T06:17:32.066Z"),
66-
transmitTimestamp: new Date("2017-08-31T06:17:32.066Z")
67-
}
20+
transmitTimestamp: new Date("2017-08-31T06:17:32.066Z"),
21+
},
6822
},
6923
{
7024
buffer: [
71-
28,
72-
1,
73-
48,
74-
234,
75-
0,
76-
0,
77-
0,
78-
0,
79-
0,
80-
0,
81-
0,
82-
70,
83-
80,
84-
84,
85-
66,
86-
0,
87-
221,
88-
81,
89-
98,
90-
144,
91-
122,
92-
240,
93-
131,
94-
160,
95-
48,
96-
48,
97-
48,
98-
48,
99-
48,
100-
48,
101-
48,
102-
10,
103-
221,
104-
81,
105-
98,
106-
150,
107-
72,
108-
91,
109-
119,
110-
135,
111-
221,
112-
81,
113-
98,
114-
150,
115-
72,
116-
95,
117-
235,
118-
26
25+
28, 1, 48, 234, 0, 0, 0, 0, 0, 0, 0, 70, 80, 84, 66, 0, 221, 81, 98, 144, 122, 240, 131, 160, 48, 48, 48, 48, 48,
26+
48, 48, 10, 221, 81, 98, 150, 72, 91, 119, 135, 221, 81, 98, 150, 72, 95, 235, 26,
11927
],
12028
expected: {
12129
leapIndicator: 0,
@@ -130,7 +38,7 @@ module.exports = [
13038
referenceTimestamp: new Date("2017-08-30T16:13:04.480Z"),
13139
originTimestamp: new Date("1925-08-15T05:27:12.188Z"),
13240
receiveTimestamp: new Date("2017-08-30T16:13:10.282Z"),
133-
transmitTimestamp: new Date("2017-08-30T16:13:10.282Z")
134-
}
135-
}
41+
transmitTimestamp: new Date("2017-08-30T16:13:10.282Z"),
42+
},
43+
},
13644
];

test/parser.spec.ts

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { NtpPacketParser } from "../src";
33

44
const assert = require("assert");
55

6-
describe("NTP packet parser", function() {
7-
describe("Structure", function() {
6+
describe("NTP packet parser", function () {
7+
describe("Structure", function () {
88
const validPacket = require("./packets.valid")[0].buffer;
99

10-
it("should parse without errors", function() {
10+
it("should parse without errors", function () {
1111
NtpPacketParser.parse(validPacket);
1212
});
1313

14-
it("should return a full struct", function() {
14+
it("should return a full struct", function () {
1515
const struct = NtpPacketParser.parse(validPacket);
1616

1717
assert.ok("leapIndicator" in struct);
@@ -29,106 +29,100 @@ describe("NTP packet parser", function() {
2929
assert.ok("transmitTimestamp" in struct);
3030
});
3131

32-
it("should return an integer for leapIndicator", function() {
32+
it("should return an integer for leapIndicator", function () {
3333
const struct = NtpPacketParser.parse(validPacket);
3434

3535
assert.ok(Number.isInteger(struct.leapIndicator));
3636
});
3737

38-
it("should return an integer for mode", function() {
38+
it("should return an integer for mode", function () {
3939
const struct = NtpPacketParser.parse(validPacket);
4040

4141
assert.ok(Number.isInteger(struct.mode));
4242
});
4343

44-
it("should return an integer for stratum", function() {
44+
it("should return an integer for stratum", function () {
4545
const struct = NtpPacketParser.parse(validPacket);
4646

4747
assert.ok(Number.isInteger(struct.stratum));
4848
});
4949

50-
it("should return an integer for poll", function() {
50+
it("should return an integer for poll", function () {
5151
const struct = NtpPacketParser.parse(validPacket);
5252

5353
assert.ok(Number.isInteger(struct.poll));
5454
});
5555

56-
it("should return an integer for precision", function() {
56+
it("should return an integer for precision", function () {
5757
const struct = NtpPacketParser.parse(validPacket);
5858

5959
assert.ok(Number.isInteger(struct.precision));
6060
});
6161

62-
it("should return a date for rootDelay", function() {
62+
it("should return a date for rootDelay", function () {
6363
const struct = NtpPacketParser.parse(validPacket);
6464

6565
assert.ok(struct.rootDelay instanceof Date);
6666
});
6767

68-
it("should return a date for rootDispersion", function() {
68+
it("should return a date for rootDispersion", function () {
6969
const struct = NtpPacketParser.parse(validPacket);
7070

7171
assert.ok(struct.rootDispersion instanceof Date);
7272
});
7373

74-
it("should return a string for referenceId", function() {
74+
it("should return a string for referenceId", function () {
7575
const struct = NtpPacketParser.parse(validPacket);
7676

7777
assert.ok(typeof struct.referenceId === "string");
7878
});
7979

80-
it("should return a date for referenceTimestamp", function() {
80+
it("should return a date for referenceTimestamp", function () {
8181
const struct = NtpPacketParser.parse(validPacket);
8282

8383
assert.ok(struct.referenceTimestamp instanceof Date);
8484
});
8585

86-
it("should return a date for originTimestamp", function() {
86+
it("should return a date for originTimestamp", function () {
8787
const struct = NtpPacketParser.parse(validPacket);
8888

8989
assert.ok(struct.originTimestamp instanceof Date);
9090
});
9191

92-
it("should return a date for receiveTimestamp", function() {
92+
it("should return a date for receiveTimestamp", function () {
9393
const struct = NtpPacketParser.parse(validPacket);
9494

9595
assert.ok(struct.receiveTimestamp instanceof Date);
9696
});
9797

98-
it("should return a date for transmitTimestamp", function() {
98+
it("should return a date for transmitTimestamp", function () {
9999
const struct = NtpPacketParser.parse(validPacket);
100100

101101
assert.ok(struct.transmitTimestamp instanceof Date);
102102
});
103103
});
104104

105105
const validPackets = require("./packets.valid.js");
106-
validPackets.forEach(function(packet) {
107-
describe("Parsing", function() {
106+
validPackets.forEach(function (packet) {
107+
describe("Parsing", function () {
108108
const struct = NtpPacketParser.parse(packet.buffer);
109109

110110
for (let key in packet.expected) {
111111
if (!packet.expected.hasOwnProperty(key)) {
112112
continue;
113113
}
114114

115-
it("should have the property " + key, function() {
115+
it("should have the property " + key, function () {
116116
assert.ok(key in struct);
117117
});
118118

119-
it(
120-
"should return " + packet.expected[key] + " for property " + key,
121-
function() {
122-
if (packet.expected[key] instanceof Date) {
123-
assert.equal(
124-
struct[key].getTime(),
125-
packet.expected[key].getTime()
126-
);
127-
} else {
128-
assert.equal(struct[key], packet.expected[key]);
129-
}
119+
it("should return " + packet.expected[key] + " for property " + key, function () {
120+
if (packet.expected[key] instanceof Date) {
121+
assert.equal(struct[key].getTime(), packet.expected[key].getTime());
122+
} else {
123+
assert.equal(struct[key], packet.expected[key]);
130124
}
131-
);
125+
});
132126
}
133127
});
134128
});

0 commit comments

Comments
 (0)