Skip to content

Instantly share code, notes, and snippets.

View IvanAdmaers's full-sized avatar

IvanAdmaers

View GitHub Profile
@IvanAdmaers
IvanAdmaers / browser.ts
Last active March 29, 2023 14:58
Random Roulette Bet | NodeJS | Using crypto (not unsafety Math.random)
const randomRouletteWinBet = () => {
// The possible win bet numbers on an American roulette wheel
const possibleWinBets = [
'0',
'00',
...Array.from({length: 36}, (_, i) => String(i+1))
];
// Generate a cryptographically secure random index
const randomIndex = window.crypto.getRandomValues(new Uint32Array(1))[0] % possibleWinBets.length;
@IvanAdmaers
IvanAdmaers / app.js
Created March 3, 2023 20:10
Telegraf 4 + Express + Webhook
const express = require('express');
const { Telegraf } = require('telegraf');
const app = express();
const port = process.env.PORT || 3000;
// Set up the webhook URL
const webhookUrl = `https://your-webhook-url.com/telegraf-bot`;
// Create a new instance of the Telegraf class
@IvanAdmaers
IvanAdmaers / index.py
Created February 20, 2023 19:27
Python 3 Encrypt / Decrypt files
import os
from cryptography.fernet import Fernet
def encrypt_or_decrypt_files(dir_path, should_encrypt, key):
f = Fernet(key)
for filename in os.listdir(dir_path):
file_path = os.path.join(dir_path, filename)
if os.path.isfile(file_path):
with open(file_path, 'rb') as f_in:
data = f_in.read()
@IvanAdmaers
IvanAdmaers / index.js
Created February 20, 2023 19:03
NodeJS Encrypt / Decrypt files
const { readdir, readFile, writeFile } = require('fs/promises');
const {
randomBytes,
createCipheriv,
createDecipheriv,
createHash,
} = require('crypto');
const { join } = require('path');
/**
@IvanAdmaers
IvanAdmaers / getTimeDifferenceBetweenTwoDates.test.ts
Created November 24, 2022 18:56
Get time difference between two dates JavaScript | JS | TS
import { getTimeDifferenceBetweenTwoDates } from './getTimeDifferenceBetweenTwoDates';
describe('getTimeDifferenceBetweenTwoDates', () => {
it('should return a correct result', () => {
const date1 = new Date(
'Thu Nov 24 2022 13:50:50 GMT-0500 (Eastern Standard Time)'
);
const date2 = new Date(
'Thu Nov 25 2022 13:50:50 GMT-0500 (Eastern Standard Time)'
);
@IvanAdmaers
IvanAdmaers / checkImageUrl.test.ts
Created November 23, 2022 20:22
Check is an image available by its url
import fs from 'fs/promises';
import path from 'path';
import axios from 'axios';
import { checkImageUrl, allowedFormats, requestWaitingTimeInMs } from '..';
jest.useFakeTimers();
describe('checkImageUrl', () => {
it('should return a correct result for jpeg', async () => {
@IvanAdmaers
IvanAdmaers / example.js
Created November 10, 2022 20:39
FlipDown.js countdown to Christmas
const currentYear = new Date().getFullYear();
const deadlineDate = new Date(`12/25/${currentYear}`);
const between = Math.abs(Date.now() - deadlineDate.getTime()) / 1000;
const timestampInSeconds = Date.now() / 1000 + between;
new FlipDown(timestampInSeconds).start();
import { getTimeGreetings, welcomes } from '.';
describe('getTimeGreetings', () => {
beforeEach(() => {
jest.useFakeTimers();
});
it('should return night greetings', () => {
jest.setSystemTime(
new Date('Thu Oct 06 2022 00:00:54 GMT-0400 (Eastern Daylight Time)')
@IvanAdmaers
IvanAdmaers / example.js
Created September 16, 2022 19:47
Random drop chance JS
const items = [
{
name: 'Apple',
dropChance: 0.7
},
{
name: 'Knife',
dropChance: 0.25
},
{