Skip to content

Instantly share code, notes, and snippets.

View Avi-E-Koenig's full-sized avatar
🎯
Focusing

Avi E. Koenig Avi-E-Koenig

🎯
Focusing
View GitHub Profile
@Avi-E-Koenig
Avi-E-Koenig / recursiveBillPrint.js
Created May 26, 2022 12:17
Recursive Bill display
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);
@Avi-E-Koenig
Avi-E-Koenig / validateJsonSchema.js
Last active May 26, 2022 11:50
draft concept to validate json structure
const validateSchema = (json, schema) => {
try {
JSON.parse(json)
const parsed = JSON.parse(json)
for (const key in parsed) {
if (!schema.hasOwnProperty(key)) {
return false;
}
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);
@Avi-E-Koenig
Avi-E-Koenig / doodle4231.js
Created March 17, 2022 10:54
a basic reducer doodle
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))
/**
* 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++) {
@Avi-E-Koenig
Avi-E-Koenig / getHebrewEventList.js
Created January 15, 2022 14:15
Get hebrew holidays list from API www.hebcal.com
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 :)
@Avi-E-Koenig
Avi-E-Koenig / concept.js
Created January 7, 2022 09:16
basic all purpose error handling wrapper
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) {
@Avi-E-Koenig
Avi-E-Koenig / mappedUserGen.js
Created December 9, 2021 11:41
quick user db as mapped key->val collection
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() {
@Avi-E-Koenig
Avi-E-Koenig / Joidemo.js
Created November 30, 2021 23:35
Joi demo
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`)
@Avi-E-Koenig
Avi-E-Koenig / sqlLogger.js
Created November 22, 2021 14:18
log to mysql
// 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');