Last active
September 16, 2022 21:59
-
-
Save dereckmezquita/460ae1f6ffd2d7264be3b135e3ddad4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ------------------------------------------------ | |
// Update elements of an object (key - value) by index number not name | |
const obj = { | |
student1: { | |
country: 'Chile', | |
name: 'Tom' | |
}, | |
student2: { | |
country: 'Argentina', | |
name: 'Julia' | |
} | |
}; | |
console.log(obj); | |
Object.values(obj)[0].country = 'USA'; | |
console.log(obj); | |
// ------------------------------------------------ | |
// Synchronously read a file line by line without loading all of it into memory | |
// Inspired from: https://stackoverflow.com/questions/7545147/nodejs-synchronization-read-large-file-line-by-line | |
import fs from 'fs'; | |
const filename: string = 'gds_result.txt' | |
const fd: number = fs.openSync(filename, 'r'); | |
const bufferSize: number = 1024; | |
const buffer: Buffer = Buffer.alloc(bufferSize); | |
let leftOver: string = ''; | |
let read: number, line: string, idxStart: number, idx: number; | |
// while buffer is not empty/0 get more 1024 more bytes | |
while ((read = fs.readSync(fd, buffer, 0, bufferSize, null)) !== 0) { | |
// line content here | |
leftOver += buffer.toString('utf8', 0, read); | |
idxStart = 0 | |
// Only executes if there is a newline in the buffered content | |
while ((idx = leftOver.indexOf('\n', idxStart)) !== -1) { | |
// remove everything before the newline | |
line = leftOver.substring(idxStart, idx); | |
idxStart = idx + 1; | |
console.log('One line read: ' + line); | |
} | |
// keep the rest of the buffer for the next iteration | |
leftOver = leftOver.substring(idxStart); | |
} | |
// Asynchronous method for reading a file line by line | |
import fs from 'fs'; | |
import readline, { Interface } from 'readline'; | |
let line_counter: number = 0; | |
const rl: Interface = readline.createInterface({ | |
input: fs.createReadStream("./gds_result.txt"), | |
crlfDelay: Infinity | |
}); | |
rl.on('line', (line: string) => { | |
line_counter++; | |
console.log(`${line_counter}: ${line}`); | |
}); | |
// how to add a method to Number base class | |
// allows extension of Number type | |
// must use declare global if in index file; normally people create a separate module: | |
// Number.extensions.ts and import that | |
declare global { | |
interface Number { | |
sum(a: number): number; | |
} | |
} | |
Number.prototype.sum = function (a: number) { | |
return Number(this) + a; | |
}; | |
console.log(typeof (2).sum(3)); | |
// extend base class formally with typescript | |
// https://stackoverflow.com/questions/27050584/is-it-possible-to-have-a-class-extend-number | |
/** | |
* Decimal class | |
* Represents a decimal number with a fixed precision which can be defined in the constructor. | |
* | |
* @export | |
* @class Decimal | |
* @extends { Number } | |
* @implements { Number } | |
*/ | |
export class Decimal extends Number implements Number { | |
public precision: number; | |
/** | |
* Creates an instance of Decimal. | |
* | |
* @param {(number | string)} value | |
* | |
* @memberOf Decimal | |
*/ | |
constructor(value: number | string, precision: number = 2) { | |
if (typeof value === 'string') { | |
value = parseFloat(value); | |
} | |
if (typeof value !== 'number' || isNaN(value)) { | |
throw new Error('Decimal constructor requires a number or the string representation of a number.'); | |
} | |
super(parseFloat((value || 0).toFixed(2))); | |
this.precision = precision; | |
} | |
} | |
const yeet = new Decimal(1.23456789, 4); | |
// buffers and arraybuffers; useful for laying out a raw binary communication protocol | |
const dataBuf = new ArrayBuffer(10) // basically a raw array | |
const dv = new DataView(dataBuf); // a way to get data from the array in tranches | |
console.log(dataBuf); | |
// set the values | |
dv.setUint8(0, 2); //msgType | |
dv.setFloat32(1, 252.55); //x | |
dv.setFloat32(5, 848.32); //y | |
dv.setUint8(9, 4); //player id | |
// console.log(dataBuf); | |
const msgType = dv.getUint8(0); | |
const x = dv.getFloat32(1); | |
const y = dv.getFloat32(5); | |
const playerID = dv.getUint8(9); | |
console.log(`Msg Type: ${msgType}\nx: ${x}\ny: ${y}\nPlayer ID: ${playerID}`); | |
[ | |
2, // msgType (uint8 = 1 byte) | |
_, _, _, _, // x (float32 = 4 bytes) | |
_, _, _, _, // y (float32 = 4 bytes) | |
4 // playerID (uint8 = 1 byte) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment