Skip to content

Instantly share code, notes, and snippets.

View malko's full-sized avatar

Jonathan Gotti malko

View GitHub Profile
@malko
malko / getMapMaxValueKey.js
Created May 24, 2022 13:23
Get the key in a map that hold the max value
const getMapMaxValueKey = (map) => {
return map.size ? [...map.entries()]
.reduce((a, b) => a[1] >= b[1] ? a : b)[0]
: null
}
@malko
malko / git-subtree-split-reminder.txt
Created March 9, 2023 16:38
Extract a git repo from another one using git subtree
# first create a branch in the parent repo
git subtree split -P path/to/folder/toExtract --branch branchName
# then create the directory for the external repo
mkdir myNewRepo
cd myNewRepo
git init
git remote add tempRemote path/to/parent/repo
# only getch what is needed no more to avoid cluttered git reflog in the new repo
git fetch --no-tags tempRemote branchName
@malko
malko / transformApply.js
Created September 24, 2024 08:14
matrix transform apply
/** @typedef {[number,number,number,number,number,number]} Matrix*/
const transformApply = (/**@type {Matrix} */matrix, point=[0,0]) => {
// extract the transformation matrix values
const [scaleX, skewY, skewX, scaleY, translateX, translateY] = matrix
const [x, y] = point
// apply the transformation matrix to the point
return [
x * scaleX + y * skewX + translateX,
x * skewY + y * scaleY + translateY
@malko
malko / rollup-banner-plugin.js
Last active September 26, 2024 15:56
Rollup plugin to enable banner/footer when using vite
/* Include the following code inside your viteconfig then build.rollupOptions.output[banner|footer] will work as intended */
const RollupBannerPlugin = {
name: 'banner',
enforce: 'post',
generateBundle(options, bundle) {
const banner = options.banner() || ''
const footer = options.footer() || ''
for (const module of Object.values(bundle)) {
if (module.type === 'chunk') {
const shebang = module.code.match(/^#![^\n]*\n/)
@malko
malko / periodOverlap.spec.ts
Last active January 5, 2025 16:28
periodOverlap.ts
import { describe, expect, it } from 'vitest'
import { periodOverlap, type Period } from './date'
describe('periodOverlap', () => {
it('should return true if periods overlap', () => {
const period1: Period = { start: "2023-04-01", end: "2023-04-10" }
const period2: Period = { start: "2023-04-05", end: "2023-04-15" }
expect(periodOverlap(period1, period2)).toBe(true)
expect(periodOverlap(period2, period1)).toBe(true)
})
@malko
malko / gist:4f4e6e8891148a0c48decc1052def7ba
Created March 24, 2025 14:14
initDateTimeWithTimezone
/**
* Return a date object with the specified date and timezone respecting DST
* @param {string} dateStr - Date string in 'YYYY-MM-DD' or 'YYYY-MM-DD[T| ]HH:MM:SS' format
* @param {string} timezone - IANA timezone string (e.g., 'Europe/Paris')
* @returns {Date} - Date object adjusted for the specified timezone
*/
function initDateTimeWithTimezone(dateStr, timezone) {
let year, month, day, hours = '00', minutes = '00', seconds = '00';
dateStr = dateStr.trim();