Skip to content

Instantly share code, notes, and snippets.

View avisek's full-sized avatar

Avisek Das avisek

  • Planet Earth
View GitHub Profile
@avisek
avisek / ResizeObserverPolyfill.js
Created April 19, 2022 19:53
Tiny ResizeObserver polyfill
// https://codepen.io/dgca/pen/WoJoNB
export const ResizeObserver = window.ResizeObserver || class ResizeObserver {
constructor(callback) {
this.observables = [];
// Array of observed elements that looks like this:
// [{
// el: domNode,
// size: {height: x, width: y}
// }]
this.boundCheck = this.check.bind(this);
@avisek
avisek / colorConversions.js
Created May 21, 2022 12:11
RGB, HSL, XYZ, LAB, LCH color conversion algorithms in JavaScript
export function rgbToHsl(rgb) {
const r = rgb.r / 255
const g = rgb.g / 255
const b = rgb.b / 255
const max = Math.max(r, g, b), min = Math.min(r, g, b)
let h, s, l = (max + min) / 2
if (max == min) {
h = s = 0 // achromatic
@avisek
avisek / copyOrMoveRecursively.ts
Created November 7, 2023 16:05
Recursively copy and move files & directories using Node.js
function copyRecursively(sourceDir: string, destinationDir: string): void {
const files = fs.readdirSync(sourceDir)
if (!fs.existsSync(destinationDir))
fs.mkdirSync(destinationDir, { recursive: true })
files.forEach(file => {
const sourceFile = path.join(sourceDir, file)
const destinationFile = path.join(destinationDir, file)
@avisek
avisek / constrain.js
Created November 13, 2023 17:38
Constrains a number between a minimum and maximum value.
function constrain(value, min, max) {
return Math.min(Math.max(value, min), max)
}