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
const itemFactory = () => { | |
return { | |
name: makeid(), | |
price: getRandomInt(1, 100), | |
items: Math.random() < 0.5 ? Array.from({ length: getRandomInt(1, 4) }, itemFactory) : [], | |
} | |
} | |
function getRandomInt(min, max) { | |
min = Math.ceil(min); |
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
const validateSchema = (json, schema) => { | |
try { | |
JSON.parse(json) | |
const parsed = JSON.parse(json) | |
for (const key in parsed) { | |
if (!schema.hasOwnProperty(key)) { | |
return false; | |
} |
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
function initTimer() { | |
let timer = 0; | |
let interval = setInterval(() => { | |
timer++; | |
console.log("🚀 ~ file: main.js ~ line 5 ~ interval ~ timer", timer) | |
if (timer === 10) { | |
clearInterval(interval); | |
console.log('Time is up!'); | |
} | |
}, 1000); |
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
const baseArr = [[1, 2], [{ foo: 'bar' }]] | |
const reducer1 = (keepRefs) => (acc, val, i) => { | |
if (keepRefs) { | |
acc[i] = val | |
return acc | |
} | |
try { | |
acc[i] = JSON.parse(JSON.stringify(val)) |
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
/** | |
* Takes a string input | |
* @param {String} str | |
* @returns {String} The first not repeated character or empty string | |
*/ | |
const firstNonRepeatingLetter = (str) => { | |
function isUnique(char) { | |
let charCount = 0; | |
for (let i = 0; i < str.length; i++) { |
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
const main = async () => { | |
try { | |
const resultSet = fetchYearRange(2022, 2052) | |
console.log("🚀 ~ file: main.js ~ line 24 ~ merged ~ resultSet", JSON.stringify(resultSet)) | |
// copy and do stuff. | |
console.log("🚀 ~ file: main.js ~ line 7 ~ main ~ resultSet", resultSet) | |
// save this to file if you're less lazy then me :) |
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
const express = require('express') | |
const app = express() | |
const port = 3000 | |
const catchErrors = (requestHandler) => { | |
return async (req, res, next) => { | |
try { | |
throw 'oops' | |
return await requestHandler(req, res, next); | |
} catch (error) { |
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
const arrayMapper = (array, uniqueID) => array.reduce((acc, obj, index) => { | |
if (!obj?.[uniqueID]) { | |
console.error("🚀 ~ file: doodle.js ~ line 6 ~ arrayMapper ~ ", { obj, uniqueID, index }) | |
throw new Error('uniqueId no found') | |
} | |
acc[obj[uniqueID]] = obj | |
return acc; | |
}, {}) | |
function uuidv4() { |
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
const Joi = require('joi'); | |
const Logger = require('../services/logger.service'); | |
const mysqlEscapeStrings = (value, helper) => { | |
const validMysqlChars = /[0-9,a-z,A-Z$_]/ | |
if (value.match(validMysqlChars)) { | |
return value; | |
} | |
return helper.message(`${value} contains illegal characters`) |
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
// the logger to sql class....based on winston-mysql | |
/** | |
* This is a MySQL transport module for winston. | |
* https://github.com/winstonjs/winston | |
* I made some customizations | |
*/ | |
const Transport = require('winston-transport'); | |
const MySql = require('mysql2'); |