Skip to content

Commit 78de640

Browse files
authored
Merge pull request #371 from jwp-consulting/add-prettier
Format code using prettier, update node build target
2 parents a248c9b + 9c8152a commit 78de640

File tree

8 files changed

+109
-20
lines changed

8 files changed

+109
-20
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[src/*.ts]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
charset = utf-8
11+
indent_style = space
12+
indent_size = 2
13+
trim_trailing_whitespace = true
14+
max_line_length = 80

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

1818
strategy:
1919
matrix:
20-
node-version: [14.x]
20+
node-version: [18.x, 20.x]
2121
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2222

2323
steps:

.github/workflows/prettier.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
9+
permissions:
10+
contents: read
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: true
15+
16+
jobs:
17+
lint:
18+
name: Lint
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout
22+
uses: actions/[email protected]
23+
- name: Setup Node.js
24+
uses: actions/[email protected]
25+
- name: Clean NPM install
26+
run: npm clean-install
27+
- name: Check with prettier
28+
run: npm run check-prettier

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Anephenix OÜ
3+
Copyright (c) 2019-2023 The Sarus Authors
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

package-lock.json

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

package.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
"version": "0.4.6",
44
"description": "A WebSocket JavaScript library",
55
"main": "dist/index.js",
6+
"contributors": [
7+
{
8+
"name": "Paul Jensen",
9+
"email": "[email protected]",
10+
"url": "https://paulbjensen.co.uk"
11+
},
12+
{
13+
"name": "Chris Lajoie",
14+
"email": "[email protected]"
15+
},
16+
{
17+
"name": "Justus Perlwitz",
18+
"email": "[email protected]",
19+
"url": "https://www.jwpconsulting.net"
20+
}
21+
],
622
"devDependencies": {
723
"@babel/parser": "^7.22.5",
824
"@babel/types": "^7.22.5",
@@ -17,14 +33,17 @@
1733
"jsdom": "^22.1.0",
1834
"mock-socket": "^9.2.1",
1935
"npm-upgrade": "^3.1.0",
36+
"prettier": "^3.0.3",
2037
"ts-jest": "^29.1.0",
2138
"typescript": "^5.1.3"
2239
},
2340
"scripts": {
2441
"build": "npx tsc --project tsconfig.json",
2542
"watch": "npx tsc --project tsconfig.json --watch",
2643
"cover": "npx jest --coverage --coverageReporters=text-lcov | coveralls",
27-
"test": "npx jest --coverage"
44+
"test": "npx jest --coverage",
45+
"prettier": "prettier src --write",
46+
"check-prettier": "prettier src --check"
2847
},
2948
"repository": {
3049
"type": "git",

src/index.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// File Dependencies
2-
import { WS_EVENT_NAMES, DATA_STORAGE_TYPES, DEFAULT_EVENT_LISTENERS_OBJECT } from "./lib/constants";
2+
import {
3+
WS_EVENT_NAMES,
4+
DATA_STORAGE_TYPES,
5+
DEFAULT_EVENT_LISTENERS_OBJECT,
6+
} from "./lib/constants";
37
import { serialize, deserialize } from "./lib/dataTransformer";
4-
import { PartialEventListenersInterface, EventListenersInterface } from "./lib/validators";
8+
import {
9+
PartialEventListenersInterface,
10+
EventListenersInterface,
11+
} from "./lib/validators";
512

613
interface StorageParams {
714
storageType: string;
@@ -93,7 +100,7 @@ export default class Sarus {
93100
retryProcessTimePeriod, // TODO - write a test case to check this
94101
retryConnectionDelay,
95102
storageType = "memory",
96-
storageKey = "sarus"
103+
storageKey = "sarus",
97104
} = props;
98105

99106
this.eventListeners = this.auditEventListeners(eventListeners);
@@ -246,15 +253,14 @@ export default class Sarus {
246253
error: [],
247254
close: [],
248255
};
249-
256+
250257
const mergedEventListeners: EventListenersInterface = {
251258
...defaultEventListeners,
252259
...eventListeners,
253260
} as EventListenersInterface; // Type assertion added here
254-
261+
255262
return mergedEventListeners;
256263
}
257-
258264

259265
/**
260266
* Connects the WebSocket client, and attaches event listeners
@@ -310,7 +316,7 @@ export default class Sarus {
310316
const eventFunctions = this.eventListeners[eventName];
311317
if (eventFunctions && eventFunctions.indexOf(eventFunc) !== -1) {
312318
throw new Error(
313-
`${eventFunc.name} has already been added to this event Listener`
319+
`${eventFunc.name} has already been added to this event Listener`,
314320
);
315321
}
316322
if (eventFunctions && eventFunctions instanceof Array) {
@@ -347,7 +353,7 @@ export default class Sarus {
347353
| {
348354
doNotThrowError: boolean;
349355
}
350-
| undefined
356+
| undefined,
351357
) {
352358
if (!existingFunc) {
353359
if (!(opts && opts.doNotThrowError)) {
@@ -366,7 +372,7 @@ export default class Sarus {
366372
off(
367373
eventName: string,
368374
eventFuncOrName: Function | string,
369-
opts?: { doNotThrowError: boolean } | undefined
375+
opts?: { doNotThrowError: boolean } | undefined,
370376
) {
371377
const existingFunc = this.findFunction(eventName, eventFuncOrName);
372378
if (existingFunc) {
@@ -421,7 +427,7 @@ export default class Sarus {
421427
*/
422428
attachEventListeners() {
423429
const self: any = this;
424-
WS_EVENT_NAMES.forEach(eventName => {
430+
WS_EVENT_NAMES.forEach((eventName) => {
425431
self.ws[`on${eventName}`] = (e: Function) => {
426432
self.eventListeners[eventName].forEach((f: Function) => f(e));
427433
if (eventName === "close" && self.reconnectAutomatically) {
@@ -433,16 +439,16 @@ export default class Sarus {
433439
}
434440

435441
/**
436-
* Removes the event listeners from a closed WebSocket instance, so that
442+
* Removes the event listeners from a closed WebSocket instance, so that
437443
* they are cleaned up
438444
*/
439445
removeEventListeners() {
440446
const self: any = this;
441-
WS_EVENT_NAMES.forEach(eventName => {
447+
WS_EVENT_NAMES.forEach((eventName) => {
442448
if (self.ws.listeners && self.ws.listeners[eventName]) {
443-
self.ws.listeners[eventName].forEach((iel:Function) => {
449+
self.ws.listeners[eventName].forEach((iel: Function) => {
444450
self.ws.removeEventListener(eventName, iel);
445-
})
451+
});
446452
}
447453
});
448454
}

src/lib/constants.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const WS_EVENT_NAMES: Array<string> = [
1010
"open",
1111
"close",
1212
"message",
13-
"error"
13+
"error",
1414
];
1515

1616
/**
@@ -34,5 +34,5 @@ export const DEFAULT_EVENT_LISTENERS_OBJECT: EventListenersInterface = {
3434
open: [],
3535
message: [],
3636
error: [],
37-
close: []
38-
};
37+
close: [],
38+
};

0 commit comments

Comments
 (0)