Skip to content

Commit

Permalink
Adds return types in arrow functions (lib's code)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrypula committed Oct 27, 2019
1 parent 0aed64f commit b159266
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/lib/0-physical-layer/audio-mono-io/audio-mono-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class AudioMonoIo implements AudioMonoIoInterface {
}

if (this.microphoneMediaStream) {
this.microphoneMediaStream.getTracks().forEach((track: MediaStreamTrack) => track.stop());
this.microphoneMediaStream.getTracks().forEach((track: MediaStreamTrack): void => track.stop());
this.microphoneMediaStream = null;
}

Expand Down Expand Up @@ -83,12 +83,12 @@ export class AudioMonoIo implements AudioMonoIoInterface {
// TODO fix known issue: inputEnable/inputDisable doesn't wait for promise
navigator.mediaDevices
.getUserMedia({ audio: true, video: false })
.then((mediaStream: MediaStream) => {
.then((mediaStream: MediaStream): void => {
this.microphoneMediaStream = mediaStream;
this.microphoneRealNode = this.audioContext.createMediaStreamSource(mediaStream);
this.microphoneRealNode.connect(node);
})
.catch((error: any) => {
.catch((error: any): void => {
throw new Error(error);
});
}
Expand Down
13 changes: 8 additions & 5 deletions src/lib/0-physical-layer/config-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { DspConfig, DspConfigInitializer, DspMode } from '@physical-layer/model'
import { isEqual } from '@shared/utils';

export const detectDspMode = (dspConfigInitializer: DspConfigInitializer): DspMode => {
return Object.keys(DspMode).find((dspMode: DspMode) =>
return Object.keys(DspMode).find((dspMode: DspMode): boolean =>
isEqual(fromConfig.DSP_MODE_TO_DSP_CONFIG_INITIALIZER_LOOK_UP[dspMode], dspConfigInitializer)
) as DspMode;
};

export const getClosestBinIndexes = (fftSize: number, sampleRate: number, frequencies: number[]): number[] => {
return frequencies.map((frequency: number) => Math.round((frequency * fftSize) / sampleRate));
return frequencies.map((frequency: number): number => Math.round((frequency * fftSize) / sampleRate));
};

export const getDspConfig = (dspConfigInitializer: DspConfigInitializer, sampleRate: number = null): DspConfig => {
Expand Down Expand Up @@ -48,8 +48,9 @@ export const getDspConfig = (dspConfigInitializer: DspConfigInitializer, sampleR
};

export const getDspConfigsFromAllDspModes = (sampleRate: number = null): DspConfig[] => {
return Object.keys(DspMode).map((dspMode: DspMode) =>
getDspConfig(fromConfig.DSP_MODE_TO_DSP_CONFIG_INITIALIZER_LOOK_UP[dspMode], sampleRate)
return Object.keys(DspMode).map(
(dspMode: DspMode): DspConfig =>
getDspConfig(fromConfig.DSP_MODE_TO_DSP_CONFIG_INITIALIZER_LOOK_UP[dspMode], sampleRate)
);
};

Expand Down Expand Up @@ -111,7 +112,9 @@ export const getUnifiedFrequencies = (
};

export const isInsideForbiddenFrequencies = (frequency: number): boolean => {
return fromConfig.FREQUENCY_FORBIDDEN_RANGE.some((range: number[]) => range[0] < frequency && frequency < range[1]);
return fromConfig.FREQUENCY_FORBIDDEN_RANGE.some(
(range: number[]): boolean => range[0] < frequency && frequency < range[1]
);
};

export const validateDspConfigInitializer = (dspConfigInitializer: DspConfigInitializer): void => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/0-physical-layer/fft-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class FftResult {
let maxValue = -Infinity;
let maxIndex = 0;

this.frequencyDomainData.forEach((value: number, index: number) => {
this.frequencyDomainData.forEach((value: number, index: number): void => {
if (value > maxValue) {
maxValue = value;
maxIndex = index;
Expand All @@ -20,7 +20,7 @@ export class FftResult {
public pick(binIndexes: number[]): FftResult {
const newFrequencyDomainData = new Float32Array(binIndexes.length);

binIndexes.forEach((binIndex: number, index: number) => {
binIndexes.forEach((binIndex: number, index: number): void => {
newFrequencyDomainData[index] = this.frequencyDomainData[binIndex];
});

Expand Down
6 changes: 3 additions & 3 deletions src/lib/1-data-link-layer/config-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isEqual } from '@shared/utils';
/*tslint:disable:no-bitwise*/

export const detectFrameMode = (frameConfigInitializer: FrameConfigInitializer): FrameMode => {
return Object.keys(FrameMode).find((frameMode: FrameMode) =>
return Object.keys(FrameMode).find((frameMode: FrameMode): boolean =>
isEqual(FRAME_MODE_TO_FRAME_CONFIG_INITIALIZER_LOOK_UP[frameMode], frameConfigInitializer)
) as FrameMode;
};
Expand All @@ -32,8 +32,8 @@ export const getFrameConfig = (frameConfigInitializer: FrameConfigInitializer):
};

export const getFrameConfigsFromAllFrameModes = (): FrameConfig[] => {
return Object.keys(FrameMode).map((frameMode: FrameMode) =>
getFrameConfig(FRAME_MODE_TO_FRAME_CONFIG_INITIALIZER_LOOK_UP[frameMode])
return Object.keys(FrameMode).map(
(frameMode: FrameMode): FrameConfig => getFrameConfig(FRAME_MODE_TO_FRAME_CONFIG_INITIALIZER_LOOK_UP[frameMode])
);
};

Expand Down
10 changes: 5 additions & 5 deletions src/lib/1-data-link-layer/data-link-layer-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ export class DataLinkLayerWrapper {
}

protected handleRxInterval(): void {
this.rxInterval = setInterval(() => {
this.rxInterval = setInterval((): void => {
if (this.dataLinkLayer.rxTimeTick(this.getCurrentTime()) === RxTimeTickState.Stopped) {
this.listenStop(INPUT_DISABLE_FALSE);
} else if (this.rxHandlers.next) {
this.dataLinkLayer
.getRxBytesCollection()
.forEach((rxBytes: number[]) => this.rxHandlers.next(rxBytes, ERROR_CORRECTED_FALSE));
.forEach((rxBytes: number[]): void => this.rxHandlers.next(rxBytes, ERROR_CORRECTED_FALSE));
this.dataLinkLayer
.getRxBytesErrorCorrectedCollection()
.forEach((rxBytes: number[]) => this.rxHandlers.next(rxBytes, ERROR_CORRECTED_TRUE));
.forEach((rxBytes: number[]): void => this.rxHandlers.next(rxBytes, ERROR_CORRECTED_TRUE));
}
}, this.dataLinkLayer.physicalLayer.getDspConfig().rxIntervalMilliseconds);
}

protected handleTxInterval(): void {
const getTxGuardTimeout = (): any =>
setTimeout(() => {
setTimeout((): void => {
this.txHandlers.next && this.txHandlers.next(this.dataLinkLayer.getTxProgress());
this.sendStop(OUTPUT_DISABLE_FALSE);
}, this.dataLinkLayer.getTxGuardMilliseconds());
Expand All @@ -102,7 +102,7 @@ export class DataLinkLayerWrapper {
break;

case TxTimeTickState.Symbol:
this.txInterval = setInterval(() => {
this.txInterval = setInterval((): void => {
this.txHandlers.next && this.txHandlers.next(this.dataLinkLayer.getTxProgress());

switch (this.dataLinkLayer.txTimeTick(this.getCurrentTime())) {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/1-data-link-layer/data-link-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export class DataLinkLayer {
}

public getRxBytesCollection(): number[][] {
return this.rxFrames.length ? this.rxFrames.map((frame: Frame) => frame.getPayload()) : [];
return this.rxFrames.length ? this.rxFrames.map((frame: Frame): number[] => frame.getPayload()) : [];
}

public getRxBytesErrorCorrectedCollection(): number[][] {
return this.rxFramesErrorCorrected.length
? this.rxFramesErrorCorrected.map((frame: Frame) => frame.getPayload())
? this.rxFramesErrorCorrected.map((frame: Frame): number[] => frame.getPayload())
: [];
}

Expand Down Expand Up @@ -113,7 +113,7 @@ export class DataLinkLayer {
this.scrambleSequence,
this.frameConfig,
this.rxErrorCorrection,
(frameCandidate: Frame, isErrorCorrected: boolean) => {
(frameCandidate: Frame, isErrorCorrected: boolean): void => {
this.tryToFindValidFrame(frameCandidate, isErrorCorrected) && validFramesCounter++;
}
);
Expand Down Expand Up @@ -170,7 +170,7 @@ export class DataLinkLayer {
if (frameCandidate.isValid()) {
const frame: Frame = isErrorCorrected ? frameCandidate.clone() : frameCandidate;
const equalFramesHalfStepBack: FrameHistory = rxFrameHistoryHalfStepBack.filter(
(frameHistoryEntry: FrameHistoryEntry) =>
(frameHistoryEntry: FrameHistoryEntry): boolean =>
frameHistoryEntry.rawBytePosition >= this.rxRawBytesCounter - 1 && frameHistoryEntry.frame.isEqualTo(frame)
);

Expand Down
10 changes: 5 additions & 5 deletions src/lib/1-data-link-layer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ export const findFrameCandidates = (
): void => {
const { min } = frameConfig.rawBytesLength;

allPossibleUnScrambledArrays(input, scrambleSequence, (unScrambled: number[]) => {
allPossibleRightAlignedArrays(unScrambled, min, (rawBytes: number[]) => {
allPossibleUnScrambledArrays(input, scrambleSequence, (unScrambled: number[]): void => {
allPossibleRightAlignedArrays(unScrambled, min, (rawBytes: number[]): void => {
callback(new Frame(frameConfig).setRawBytes(rawBytes), ERROR_CORRECTED_FALSE);

errorCorrection === ErrorCorrection.On &&
allPossibleSinglePositionErrors(rawBytes, () =>
callback(new Frame(frameConfig).setRawBytes(rawBytes), ERROR_CORRECTED_TRUE)
);
allPossibleSinglePositionErrors(rawBytes, (): void => {
callback(new Frame(frameConfig).setRawBytes(rawBytes), ERROR_CORRECTED_TRUE);
});
});
});
};
Expand Down
16 changes: 9 additions & 7 deletions src/lib/examples/web/data-link-layer/chat-simple/chat-simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export class DataLinkLayerChatSimpleWebExample {
public listenEnable(): void {
domUtils.getById('listen-wrapper').classList.add('enabled');
this.dataLinkLayerWrapper.listen({
complete: () => domUtils.getById('listen-wrapper').classList.remove('enabled'),
next: (bytes: number[]) => this.logFrame(bytes, true)
complete: (): void => domUtils.getById('listen-wrapper').classList.remove('enabled'),
next: (bytes: number[]): void => this.logFrame(bytes, true)
});
}

Expand All @@ -63,15 +63,17 @@ export class DataLinkLayerChatSimpleWebExample {

this.sendStart();
this.dataLinkLayerWrapper.send(bytes, {
complete: () => this.sendComplete(bytes),
next: (progress: number) => (domUtils.getById('send-progress-bar').style.width = progress * 100 + '%')
complete: (): void => this.sendComplete(bytes),
next: (progress: number): void => {
domUtils.getById('send-progress-bar').style.width = progress * 100 + '%';
}
});
}

protected initializeHtmlElements(): void {
domUtils.getById('send-button').addEventListener('click', () => this.send());
domUtils.getById('listen-enable-button').addEventListener('click', () => this.listenEnable());
domUtils.getById('send-field').addEventListener('keydown', (event: KeyboardEvent) => {
domUtils.getById('send-button').addEventListener('click', (): void => this.send());
domUtils.getById('listen-enable-button').addEventListener('click', (): void => this.listenEnable());
domUtils.getById('send-field').addEventListener('keydown', (event: KeyboardEvent): void => {
if (event.key === 'Enter') {
event.preventDefault();
this.send();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class DataLinkLayerHexBytesSimpleWebExample {

protected handleRxInterval(): void {
this.clearRxInterval();
this.rxInterval = setInterval(() => {
this.rxInterval = setInterval((): void => {
let rxBytesCollection: number[][];

this.dataLinkLayer.rxTimeTick(new Date().getTime());
Expand All @@ -80,7 +80,7 @@ export class DataLinkLayerHexBytesSimpleWebExample {
if (rxBytesCollection.length) {
const div: HTMLElement = domUtils.createElement('div');

div.innerHTML = rxBytesCollection.map((rxBytes: number[]) => getHexFromBytes(rxBytes)).join(' | ');
div.innerHTML = rxBytesCollection.map((rxBytes: number[]): string => getHexFromBytes(rxBytes)).join(' | ');
domUtils.getById('rx-data').appendChild(div);
}
}, this.dataLinkLayer.physicalLayer.getDspConfig().rxIntervalMilliseconds);
Expand All @@ -89,7 +89,7 @@ export class DataLinkLayerHexBytesSimpleWebExample {
protected handleTxInterval(): void {
this.clearTxInterval();
if (this.dataLinkLayer.txTimeTick(new Date().getTime()) === TxTimeTickState.Symbol) {
this.txInterval = setInterval(() => {
this.txInterval = setInterval((): void => {
// Example code is bad - it's not waiting for guard interval to be finished
this.dataLinkLayer.txTimeTick(new Date().getTime()) === TxTimeTickState.Guard && this.clearTxInterval();
}, this.dataLinkLayer.physicalLayer.getDspConfig().txIntervalMilliseconds);
Expand All @@ -108,7 +108,7 @@ export class DataLinkLayerHexBytesSimpleWebExample {
domUtils.getById('dsp-mode-dropdown').innerHTML =
fromTemplate.dropdownOptionEmpty +
getDspConfigsFromAllDspModes()
.map((dspConfig: DspConfig) => fromTemplate.dropdownOption(dspConfig))
.map((dspConfig: DspConfig): string => fromTemplate.dropdownOption(dspConfig))
.join('');
}
}
2 changes: 1 addition & 1 deletion src/lib/shared/check-algorithms/crc-08.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const CRC_08_LOOKUP_TABLE: number[] = `
47,52,55,5c,5b,76,71,78,7f,6a,6d,64,63,3e,39,30,37,22,25,2c,2b,06,01,08,0f,1a,1d,14,13,ae,a9,a0,a7,b2,b5,bc,bb,96,91,
98,9f,8a,8d,84,83,de,d9,d0,d7,c2,c5,cc,cb,e6,e1,e8,ef,fa,fd,f4,f3`
.split(',')
.map((value: string) => parseInt(value, 16));
.map((value: string): number => parseInt(value, 16));

export const getCrc08: CheckAlgorithmImplementation = (bytes: number[]): number[] => {
// Code migrated to TypeScript from vanilla JavaScript implementation:
Expand Down
2 changes: 1 addition & 1 deletion src/lib/shared/check-algorithms/crc-16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const CRC_16_LOOKUP_TABLE: number[] = `
8a81,4a40,4e00,8ec1,8f81,4f40,8d01,4dc0,4c80,8c41,4400,84c1,8581,4540,8701,47c0,4680,8641,8201,42c0,4380,8341,4100,
81c1,8081,4040`
.split(',')
.map((value: string) => parseInt(value, 16));
.map((value: string): number => parseInt(value, 16));

export const getCrc16: CheckAlgorithmImplementation = (bytes: number[]): number[] => {
// Code migrated to TypeScript from vanilla JavaScript implementation:
Expand Down
2 changes: 1 addition & 1 deletion src/lib/shared/check-algorithms/crc-24.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const CRC_24_LOOKUP_TABLE: number[] = `
26359f,a07964,ace092,2aac69,b5d37e,339f85,3f0673,b94a88,87b4a6,01f85d,0d61ab,8b2d50,145247,921ebc,9e874a,18cbb1,
e37b16,6537ed,69ae1b,efe2e0,709df7,f6d10c,fa48fa,7c0401,42fa2f,c4b6d4,c82f22,4e63d9,d11cce,575035,5bc9c3,dd8538`
.split(',')
.map((value: string) => parseInt(value, 16));
.map((value: string): number => parseInt(value, 16));

export const getCrc24: CheckAlgorithmImplementation = (bytes: number[]): number[] => {
// Code migrated to TypeScript from vanilla JavaScript implementation:
Expand Down
2 changes: 1 addition & 1 deletion src/lib/shared/check-algorithms/crc-32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const CRC_32_LOOKUP_TABLE: number[] = `
40df0b66,37d83bf0,a9bcae53,debb9ec5,47b2cf7f,30b5ffe9,bdbdf21c,cabac28a,53b39330,24b4a3a6,bad03605,cdd70693,54de5729,
23d967bf,b3667a2e,c4614ab8,5d681b02,2a6f2b94,b40bbe37,c30c8ea1,5a05df1b,2d02ef8d`
.split(',')
.map((value: string) => parseInt(value, 16));
.map((value: string): number => parseInt(value, 16));

export const getCrc32: CheckAlgorithmImplementation = (bytes: number[]): number[] => {
// Code migrated to TypeScript from vanilla JavaScript implementation:
Expand Down
6 changes: 3 additions & 3 deletions src/lib/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const getBytesFromHex = (hex: string): number[] => {
};

export const getBytesFromText = (text: string): number[] => {
return text.split('').map((char: string) => char.charCodeAt(0) % 256);
return text.split('').map((char: string): number => char.charCodeAt(0) % 256);
};

export const getFilledArray = (length: number, fillWith = 0): number[] => {
Expand All @@ -50,7 +50,7 @@ export const getHexFromBytes = (bytes: number[], joinWith = ' '): string => {

export const getRandomBytes = (length: number): number[] => {
// https://stackoverflow.com/questions/1295584
return getFilledArray(length).map(() => getRandomInt(0, 255));
return getFilledArray(length).map((): number => getRandomInt(0, 255));
};

export const getRandomInt = (min: number, max: number): number => {
Expand Down Expand Up @@ -89,7 +89,7 @@ export const sortKeys = (object: any): void => {
object !== null &&
Object.keys(object)
.sort()
.forEach((key: string) => {
.forEach((key: string): void => {
object[someAlmostRandomValue + key] = object[key];
delete object[key];
object[key] = object[someAlmostRandomValue + key];
Expand Down
4 changes: 2 additions & 2 deletions src/lib/shared/web-utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) 2019 Robert Rypuła - https://github.com/robertrypula

export const fileRead = (file: File): Promise<number[]> => {
return new Promise<number[]>((resolve, reject) => {
return new Promise<number[]>((resolve, reject): void => {
const reader = new FileReader();

if (file) {
reader.onload = () => resolve(Array.from(new Uint8Array(reader.result as ArrayBuffer)));
reader.onload = (): void => resolve(Array.from(new Uint8Array(reader.result as ArrayBuffer)));
reader.readAsArrayBuffer(file);
} else {
reject('no file');
Expand Down
4 changes: 2 additions & 2 deletions src/lib/visualization/visualizers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const visualizeUnifiedFrequencies = (
sampleRates.length * binSpacing
);

sampleRates.forEach((sampleRate: number, index: number) => {
sampleRates.forEach((sampleRate: number, index: number): void => {
const resolution: number = sampleRate / fftSize;

for (let bin = 0; bin < frequencyBinsCount; bin++) {
Expand All @@ -45,7 +45,7 @@ export const visualizeUnifiedFrequencies = (
}
});

unifiedFrequencies.forEach((frequency: number) => {
unifiedFrequencies.forEach((frequency: number): void => {
const y: number = binHeight * 1.5;
const halfHeight: number = binHeight / 3;
simpleCanvas.line(frequency, y - halfHeight, frequency, y + halfHeight);
Expand Down

0 comments on commit b159266

Please sign in to comment.