Skip to content

Commit

Permalink
Updated tsconfig to output jsdoc and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chuacw committed Nov 13, 2024
1 parent 822c144 commit 62d3523
Show file tree
Hide file tree
Showing 17 changed files with 1,686 additions and 1,391 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ node_modules/
lib/
dist/
.env

typings/
*.tsbuildinfo
30 changes: 15 additions & 15 deletions examples/example-sysutils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { hasFieldOfType, hasMessageField } from "../src/sysutils";

const obj1 = {
message: "hello world"
}

console.log(hasMessageField(obj1));
console.log(hasFieldOfType<string>(obj1, "message", "string"));

const obj2 = {
message: 1
}
console.log(hasMessageField(obj2));
console.log(hasFieldOfType(obj2, "message", "number"));
console.log(hasFieldOfType(obj2, "message", "string"));
import { hasFieldOfType, hasMessageField } from "../src/sysutils";

const obj1 = {
message: "hello world"
}

console.log("obj1 has a field message of string type: ", hasMessageField(obj1));
console.log("obj1 has a field message of string type: ", hasFieldOfType<string>(obj1, "message", "string"));

const obj2 = {
message: 1
}
console.log(hasMessageField(obj2));
console.log("obj2 has a field message of number type: ", hasFieldOfType(obj2, "message", "number"));
console.log("obj2 has a field message of string type: ", hasFieldOfType(obj2, "message", "string"));
44 changes: 22 additions & 22 deletions src/BaseJsonRpcServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
createJSONRPCErrorResponse, isJSONRPCID, JSONRPC, JSONRPCClient, JSONRPCErrorCode, JSONRPCErrorException,
JSONRPCErrorResponse, JSONRPCParams, JSONRPCRequest, JSONRPCResponsePromise, JSONRPCServer, JSONRPCServerMiddlewareNext,
SimpleJSONRPCMethod
import {
createJSONRPCErrorResponse, isJSONRPCID, JSONRPC, JSONRPCClient, JSONRPCErrorCode, JSONRPCErrorException,
JSONRPCErrorResponse, JSONRPCParams, JSONRPCRequest, JSONRPCResponsePromise, JSONRPCServer, JSONRPCServerMiddlewareNext,
SimpleJSONRPCMethod
} from "json-rpc-2.0";
import express, { Express, NextFunction, response } from 'express';
import * as http from 'http';
Expand All @@ -15,7 +15,7 @@ type ListenAddr = string | net.AddressInfo | null;
type JSONRPCParameters = JSONRPCParams | undefined;

export interface TSendHandler {
send: (body: string|number|boolean|object|Buffer) => void;
send: (body: string | number | boolean | object | Buffer) => void;
}


Expand Down Expand Up @@ -230,16 +230,16 @@ class BaseJsonRpcServer {
let handled = false;
// creates a custom response with a send function
// if the send function is called, sets the handled flag
const response: TSendHandler = {
send: (body: string|number|boolean|object|Buffer) => {
const response: TSendHandler = {
send: (body: string | number | boolean | object | Buffer) => {
handled = true;
res.send(body);
}
};
this.mResponse = undefined; // prevents mResponse from being used by overridden onParseError methods
this.onParseError(err, req, response);
// if this is not set, we'll know that onParseError didn't call it, so we should handle it
if(!handled) {
if (!handled) {
// this entity.parse.failed constant is used/sent by Express
if (err.type === SEntityParseFailed) {
const obj = createJSONRPCErrorResponse(null, JSONRPCErrorCode.ParseError, SParseError);
Expand All @@ -256,7 +256,7 @@ class BaseJsonRpcServer {
* @returns {void}
*/
protected async handleRequest(request: express.Request, response: express.Response) {
try {
try {
const temp = request.body;
// checks that the request body fits a JSON RPC call
const isBatch = Array.isArray(temp);
Expand All @@ -268,13 +268,13 @@ class BaseJsonRpcServer {
if (jsonRpcResponse) {
response.send(jsonRpcResponse);
} else {
switch(isBatch) {
switch (isBatch) {
case false: {
if (typeof request.body.method != CString) {
response.send(this.createInvalidRequestResponse(request));
break;
}
response.sendStatus(CNoContent);
response.sendStatus(CNoContent);
break;
}
case true: {
Expand All @@ -283,7 +283,7 @@ class BaseJsonRpcServer {
}
}
} catch (e) {
const error = createJSONRPCErrorResponse(isJSONRPCID(request.body.id) ? request.body.id : null,
const error = createJSONRPCErrorResponse(isJSONRPCID(request.body.id) ? request.body.id : null,
JSONRPCErrorCode.InvalidRequest, SInvalidRequest);
response.send(error);
}
Expand All @@ -310,10 +310,10 @@ class BaseJsonRpcServer {
result.use(express.json());
this.initParseErrorHandler(result);

result.post(this.listeningPath, async(request: express.Request, response: express.Response) => {
result.post(this.listeningPath, async (request: express.Request, response: express.Response) => {
this.mResponse = response;
this.mRequest = request;
try {
try {
await this.handleRequest(request, response);
} finally {
// @ts-ignore
Expand All @@ -324,18 +324,18 @@ class BaseJsonRpcServer {
});
return result;
}

/**
* Initializes the JSON RPC server and returns it
*
* @returns {JSONRPCServer}
*/
protected initJsonRpcServer() {
const result = new JSONRPCServer({ errorListener: this.JsonRpcErrorListener.bind(this) });
const checkRequest =
const checkRequest =
(next: JSONRPCServerMiddlewareNext<void>, request: JSONRPCRequest, serverParams: void | undefined): JSONRPCResponsePromise => {
return this.onBeforeDispatchRequest(next, request, serverParams);
};
};
result.applyMiddleware(checkRequest);
return result;
}
Expand Down Expand Up @@ -367,7 +367,7 @@ class BaseJsonRpcServer {
*/
async listen(port?: number) {
this.addRPCMethods();
switch(typeof port) {
switch (typeof port) {
case "undefined": {
port = this.mListeningPort;
break;
Expand Down Expand Up @@ -400,7 +400,7 @@ class BaseJsonRpcServer {
*/
protected logListeningMethods(methods: SimpleJSONRPCMethod<void>[]) {
this.log("Listening for these JSON RPC methods:")
for(const method of methods) {
for (const method of methods) {
this.log(method.name);
}
}
Expand All @@ -413,7 +413,7 @@ class BaseJsonRpcServer {
* @returns {JSONRPCResponsePromise}
*/
protected onBeforeDispatchRequest(next: JSONRPCServerMiddlewareNext<void>, request: JSONRPCRequest, serverParams: void | undefined): JSONRPCResponsePromise {
switch(typeof request.method) {
switch (typeof request.method) {
case CString: {
return next(request, serverParams);
}
Expand Down Expand Up @@ -507,9 +507,9 @@ export {
SimpleJSONRPCMethod,
Request,
Response,
createJSONRPCErrorResponse, isJSONRPCID,
createJSONRPCErrorResponse, isJSONRPCID,
JSONRPCErrorCode, JSONRPCErrorException, JSONRPC, JSONRPCClient, JSONRPCParams,
JSONRPCRequest, JSONRPCResponsePromise, JSONRPCServer, JSONRPCServerMiddlewareNext,
JSONRPCRequest, JSONRPCResponsePromise, JSONRPCServer, JSONRPCServerMiddlewareNext,
JSONRPCParameters,
SEntityParseFailed,
SInvalidParams,
Expand Down
36 changes: 18 additions & 18 deletions src/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Queue<T> {
return result;
}


/**
* The number of values in the store
*
Expand Down Expand Up @@ -73,7 +73,7 @@ class Stack<T> {
return result;
}


/**
* The number of items in the store
*
Expand Down Expand Up @@ -123,7 +123,7 @@ class Dictionary<K, V> extends Map<K, V> {
this.clear();
}


/**
* Gets the number of keys and values stored.
*
Expand All @@ -145,7 +145,7 @@ class Dictionary<K, V> extends Map<K, V> {
*/
public get count(): number { return this.Count; }


/**
* Gets the keys and values stored in the Dictionary
*
Expand All @@ -154,7 +154,7 @@ class Dictionary<K, V> extends Map<K, V> {
* @type {{ [key: string]: V }}
*/
public get Items(): { [key: string]: V } {
const items: { } = {};
const items: {} = {};
this.forEach((value: V, key: K) => {
(items as any)[key as any] = value;
});
Expand All @@ -166,7 +166,7 @@ class Dictionary<K, V> extends Map<K, V> {
// so, there's the repeated definition of { [key...]: V }
return new Proxy(items, {
// Intercept 'get' operation
get: (map: { [key: string | number | symbol ]: V }, key: any) => {
get: (map: { [key: string | number | symbol]: V }, key: any) => {
let k = key.toString();
if (!this.has(k)) {
throw new Error(`Key "${key}" not found.`);
Expand Down Expand Up @@ -210,7 +210,7 @@ class Dictionary<K, V> extends Map<K, V> {
} {
const value = this.get(key);
const found = value !== undefined;
const result = {found, value};
const result = { found, value };
return result;
}

Expand All @@ -232,7 +232,7 @@ class Dictionary<K, V> extends Map<K, V> {
* @returns {boolean}
*/
ContainsValue(value: V): boolean {
for(const v of this.values()) {
for (const v of this.values()) {
if (value === v) {
return true;
}
Expand Down Expand Up @@ -276,7 +276,7 @@ class TreeNode<T> {
left: PTreeNode<T>;
right: PTreeNode<T>;
data: T;

constructor(value: T) {
this.data = value;
this.left = null;
Expand All @@ -287,7 +287,7 @@ class TreeNode<T> {

// ensure iterables have at least 1 value
let count = 0;
for(const nodeValue of nodeValues) {
for (const nodeValue of nodeValues) {
count++;
break;
}
Expand Down Expand Up @@ -328,7 +328,7 @@ function createTree<T>(nodeValues: Iterable<T>): TreeNode<T> {

// ensure iterables have at least 1 value
let count = 0;
for(const nodeValue of nodeValues) {
for (const nodeValue of nodeValues) {
count++;
break;
}
Expand All @@ -345,7 +345,7 @@ function height<T>(root: PTreeNode<T>): number {
if (root === null) {
return -1;
}
const result = 1+Math.max(height(root.left), height(root.right));
const result = 1 + Math.max(height(root.left), height(root.right));
return result;
}

Expand Down Expand Up @@ -380,7 +380,7 @@ class List<T> {
return this.items.length;
}


/**
* Returns the length of this list
*
Expand Down Expand Up @@ -415,12 +415,12 @@ class List<T> {
* @type {*}
*/
delete(value: T): boolean {
const index = this.items.indexOf(value);
if (index !== -1) {
this.items.splice(index, 1);
return true;
const index = this.items.indexOf(value);
if (index !== -1) {
this.items.splice(index, 1);
return true;
}
return false;
return false;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/dateutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers } from "ethers";
declare global {
interface Date {
ToBlockchainTimestamp(): number;

/**
* Adds the given number of seconds to a new instance of Date, with the starting value from this instance
*
Expand Down Expand Up @@ -146,7 +146,7 @@ Date.prototype.addYears = function (years: number) {
return result;
};

Date.prototype.isValidDate = function(): boolean {
Date.prototype.isValidDate = function (): boolean {
const result = this instanceof Date && !isNaN(this.valueOf());
return result;
}
Expand Down Expand Up @@ -267,22 +267,22 @@ export interface TDiffDuration {
*/
function DiffDuration(d1: Date, d2: Date): TDiffDuration {
const msDiff = Math.abs(d1.getTime() - d2.getTime());

const totalSeconds = Math.floor(msDiff / 1000);
const totalMinutes = Math.floor(totalSeconds / 60);
const totalHours = Math.floor(totalMinutes / 60);
const totalDays = Math.floor(totalHours / 24);

let years = Math.floor(totalDays / 365);
let remainingDays = totalDays % 365;

let months = Math.floor(remainingDays / 30);
remainingDays = remainingDays % 30;

const hours = totalHours % 24;
const minutes = totalMinutes % 60;
const seconds = totalSeconds % 60;

return { years, months, days: remainingDays, hours, minutes, seconds };
}

Expand Down
8 changes: 4 additions & 4 deletions src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ type SubscriptionIndex = number
export type MessageType = Date | boolean | number | string | {}

/**
* Description placeholder
* TMessageManager enables messaging between different code points.
*
* @class TMessageManager
* @typedef {TMessageManager}
* @category Messaging
*/
class TMessageManager {
private fListeners: Map<any, Array<any>> = new Map();
private fDisabledTypes: string[] = [];
private fListeners!: Map<any, Array<any>>;
private fDisabledTypes!: string[];
private static fDefaultManager: TMessageManager;

constructor() {
Expand All @@ -127,7 +127,7 @@ class TMessageManager {

reset() {
// Enums works similarly to number during runtime, so, disable Number as a native type from working
this.fDisabledTypes = []
this.fDisabledTypes = [];
this.fDisabledTypes.push(Number.name);
this.fListeners = new Map();
}
Expand Down
Loading

0 comments on commit 62d3523

Please sign in to comment.