Skip to content

Commit

Permalink
[Refactor] Minor Typescript Improvements (#355)
Browse files Browse the repository at this point in the history
* perf: use export from syntax for proper tree-shaking

Signed-off-by: Jonah Snider <jonah@jonah.pw>

* refactor: use actual ShardClientUtil class for Discord.js extension

Signed-off-by: Jonah Snider <jonah@jonah.pw>

* refactor(Util): use type guards in `isPrimitive` function

Signed-off-by: Jonah Snider <jonah@jonah.pw>

* refactor(Util): use built-in TypeScript Record generic type instead of UnknownObject custom type

Signed-off-by: Jonah Snider <jonah@jonah.pw>

* build: fix reference to @types/node

Signed-off-by: Jonah Snider <jonah@jonah.pw>

* refactor(Util): check if value is strictly equal to undefined rather than using typeof

Signed-off-by: Jonah Snider <jonah@jonah.pw>
  • Loading branch information
DevYukine committed Mar 6, 2020
1 parent 94ba133 commit 76cc0ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 54 deletions.
12 changes: 4 additions & 8 deletions src/Util/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { Constructable } from 'discord.js';
import { promisify } from 'util';
import { ShardingManager, BaseCluster } from '..';

export interface UnkownObject {
[key: string]: any;
}

export const PRIMITIVE_TYPES = ['string', 'bigint', 'number', 'boolean'];

export function chunk<T>(entries: T[], chunkSize: number) {
Expand All @@ -31,7 +27,7 @@ export function deepClone(source: any): any {
return output;
}
if (isObject(source)) {
const output = {} as UnkownObject;
const output = {} as Record<string, any>;
for (const [key, value] of Object.entries(source)) output[key] = deepClone(value);
return output;
}
Expand All @@ -48,14 +44,14 @@ export function deepClone(source: any): any {
return source;
}

export function isPrimitive(value: any) {
export function isPrimitive(value: any): value is string | bigint | number | boolean {
return PRIMITIVE_TYPES.includes(typeof value);
}

export function mergeDefault<T>(def: UnkownObject, given: UnkownObject): T {
export function mergeDefault<T>(def: Record<string, any>, given: Record<string, any>): T {
if (!given) return deepClone(def);
for (const key in def) {
if (typeof given[key] === 'undefined') given[key] = deepClone(def[key]);
if (given[key] === undefined) given[key] = deepClone(def[key]);
else if (isObject(given[key])) given[key] = mergeDefault(def[key], given[key]);
}

Expand Down
55 changes: 10 additions & 45 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,14 @@
import { ShardingManager, SessionObject, SharderOptions, CloseEvent } from './Sharding/ShardingManager';
import { ShardClientUtil, IPCResult } from './Sharding/ShardClientUtil';
import { BaseCluster } from './Cluster/BaseCluster';
import { version, http, IPCEvents, SharderEvents } from './Util/Constants';
import { Cluster, ClusterOptions } from './Cluster/Cluster';
import { ClusterIPC } from './IPC/ClusterIPC';
import { MasterIPC } from './IPC/MasterIPC';
import * as Util from './Util/Util';
import { SendOptions } from 'veza';
import { ShardClientUtil as KurasutaShardClientUtil } from './Sharding/ShardClientUtil';

declare module 'discord.js' {
interface ShardClientUtil {
ipcSocket: string | number;
readonly clusterCount: number;
readonly shardCount: number;
readonly id: number;
readonly ipc: ClusterIPC;
broadcastEval(script: string | Function): Promise<unknown[]>;
masterEval(script: string | Function): Promise<unknown>;
fetchClientValues(prop: string): Promise<unknown[]>;
fetchGuild(id: string): Promise<object>;
fetchUser(id: string): Promise<object>;
fetchChannel(id: string): Promise<object>;
restartAll(): Promise<void>;
restart(clusterID: number): Promise<void>;
respawnAll(): Promise<void>;
send(data: any, options?: SendOptions): Promise<unknown>;
init(): Promise<void>;
}
interface ShardClientUtil extends KurasutaShardClientUtil {}
}

export {
BaseCluster,
ShardingManager,
Util,
ShardClientUtil,
SessionObject,
SharderOptions,
IPCResult,
CloseEvent,
http,
IPCEvents,
Cluster,
ClusterOptions,
ClusterIPC,
MasterIPC,
SharderEvents,
version
};
export { BaseCluster } from './Cluster/BaseCluster';
export { Cluster, ClusterOptions } from './Cluster/Cluster';
export { ClusterIPC } from './IPC/ClusterIPC';
export { MasterIPC } from './IPC/MasterIPC';
export { IPCResult, ShardClientUtil } from './Sharding/ShardClientUtil';
export { CloseEvent, SessionObject, SharderOptions, ShardingManager } from './Sharding/ShardingManager';
export { http, IPCEvents, SharderEvents, version } from './Util/Constants';
export * from './Util/Util';
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"declarationDir": "./dist/typings",
"lib": ["es2017"],
"resolveJsonModule": true,
"types": ["node"]
"types": ["@types/node"]
},
"include": [
"./src/**/*"
Expand Down

0 comments on commit 76cc0ac

Please sign in to comment.