Skip to content

Instantly share code, notes, and snippets.

View peZhmanParsaee's full-sized avatar

Pezhman Parsaee peZhmanParsaee

View GitHub Profile
@peZhmanParsaee
peZhmanParsaee / deconstructPowersOfTwo.js
Created July 2, 2020 21:20
A function in JavaScript that gets a number as a parameter and returns back an array containing powers of two that sum of all equals to the passed number
/**
* @param {number} num - it's a number in radix 10 that indicates summation of power of 2 numbers
* @returns an array containing power of 2 numbers that sum of all is equal to num
*/
export const deconstructPowersOfTwo = (num: number) => {
let decimalArray = [];
const binaryNumberString = num.toString(2);
const reversedBinaryNumber = reverseString(binaryNumberString);
@peZhmanParsaee
peZhmanParsaee / config.js
Created May 30, 2020 14:14
A correct way in connecting and disconnecting to MongoDb in Node.js
const url = require("url");
const parsedUrl = url.parse(
process.env.CONNECTION_STRING || "mongodb://localhost:27017/reactor"
);
module.exports = {
db: {
host: parsedUrl.hostname,
port: parseInt(parsedUrl.port, 10),
name: parsedUrl.pathname.substr(1),
@peZhmanParsaee
peZhmanParsaee / mongoDbConnectionSingleton.js
Created May 30, 2020 14:06
MongoDB singleton connection in Node.js
const MongoClient = require("mongodb").MongoClient;
const config = require("./config");
module.exports = (function () {
let connectionInstance;
let db;
function getInstance() {
return new Promise(function (resolve, reject) {
if (connectionInstance) {