Skip to content

Instantly share code, notes, and snippets.

View MelodicCrypter's full-sized avatar

Hugh Caluscusin MelodicCrypter

View GitHub Profile
@MelodicCrypter
MelodicCrypter / proper-forking-repo.md
Last active January 24, 2019 15:04
Guide to properly fork a repository and how to pull latest changes from its original repository

Guide to properly fork a repository and how to pull latest changes from its original repository


{ This gist will help the ones that are still confused about forking a repository properly.
 Well, you might say that this is just an 'easy peasy' stuff for you, but not all are like you. 
 The purpose for this gist is share some guidance to how properly fork and then how to properly pull 
 the latest updates from the original repo. I hope this gist can help. }

@MelodicCrypter
MelodicCrypter / clean-fizzbuzz.js
Created April 25, 2019 15:17
Clean FizzBuzz code.
for (let i = 1; i < 100; i++) {
let res = '';
if (i % 3 === 0) { res += 'Fizz'; }
if (i % 5 === 0) { res += 'Buzz'; }
if (res === '') { res = i; }
console.log(res);
}
@MelodicCrypter
MelodicCrypter / uniqueGenerator.js
Last active August 8, 2019 13:04
A small javascript utility that will generate unique (hashed-type) strings.
// Your unique variables here
const allCapsAlpha = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
const allLowerAlpha = [..."abcdefghijklmnopqrstuvwxyz"];
const allUniqueChars = [...'~!@#$%^&*()_+`-=[]\\{}|;:\'",./<>?'];
const allNumbers = [..."0123456789"];
// This is the 'pattern', alternate the order if you want.
// The final output (order of pattern) will not be the same though,
// but you can improve this utility to achieve that.
const base = [...allCapsAlpha, ...allNumbers, ...allLowerAlpha, ...allUniqueChars];
@MelodicCrypter
MelodicCrypter / readline-sample.js
Created August 19, 2019 06:11
A simple NodeJS readline sample.
const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);
const aPerson = {
name: '',
sayings: []
};
// ask a question, then use the callback to use the user's answer
rl.question('What is your name? ', (nameAnswer) => {
@MelodicCrypter
MelodicCrypter / readline-with-stream.js
Created August 19, 2019 06:13
A NodeJS sample for readline with stream.
const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);
const fs = require('fs');
const aPerson = {
name: '',
sayings: []
};
// ask a question, then use the callback to use the user's answer
@MelodicCrypter
MelodicCrypter / advanced-emitter.js
Created August 19, 2019 06:16
NodeJS advanced EventEmitter.
const EventEmitter = require('events').EventEmitter;
const util = require('util');
// constructor function
const Person = function(name) {
this.name = name;
};
// let Person inherit all the EventEmitter features
util.inherits(Person, EventEmitter);
@MelodicCrypter
MelodicCrypter / backpressure-stream.js
Created August 19, 2019 06:20
NodeJS sample for copying files using streams with backpressure.
const { createReadStream, createWriteStream} = require('fs');
// You should include a real file live video, image, etc
const readStream = createReadStream(__dirname+'/powder-day.mp4');
const writeStream = createWriteStream(__dirname+'/copy.mp4', { highWaterMark: 1628920 });
// => flowing stream
readStream.on('data', (chunk) => {
// if this will be false, meaning the stream is full
const result = writeStream.write(chunk);
@MelodicCrypter
MelodicCrypter / piping-video.js
Created August 19, 2019 06:22
A sample of NodeJS Pipe to copy a video file.
const { createReadStream, createWriteStream } = require('fs');
const readStream = createReadStream(__dirname+'/powder-day.mp4');
const writeStream = createWriteStream(__dirname+'/copy.mp4' );
// Pipe, will handle backpressure automatically
readStream
.pipe(writeStream)
.on('error', console.error);
@MelodicCrypter
MelodicCrypter / piping-text.js
Created August 19, 2019 06:26
A NodeJS sample using Pipe that automatically logs whatever the user types in the console.
const { createWriteStream } = require('fs');
const writeStream = createWriteStream('./file-test.txt');
// using stdin, stream and pipe, this will automatically log all the text you will type
process.stdin.pipe(writeStream);
process.stdin.on('data', (data) => {
if (data.toString().toLowerCase().trim() === 'exit>this') {
process.exit();
@MelodicCrypter
MelodicCrypter / promisify-filesystem.js
Created August 19, 2019 06:30
A NodeJS sample of using Util's promisify to write a file.
const fs = require('fs');
const { promisify } = require('util');
const writeFile = promisify(fs.writeFile);
writeFile('sample.txt', 'this is a sample phrase')
.then(() => console.log('done!'))
.catch(e => console.log(e));