Skip to content

Instantly share code, notes, and snippets.

View malko's full-sized avatar

Jonathan Gotti malko

View GitHub Profile
@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 / 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 / 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 / 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 / gist:b455030fa408ba9369fd74a704a16f4a
Created May 5, 2022 09:41
Psql imports useful commands
-- deactivates all triggers and constraints
SET session_replication_role = replica;
-- restore triggers and constraints
SET session_replication_role = DEFAULT;
-- treat out-of-sync auto-increment sequence fields (use double quote arround table names to preserve uppercase letters)
SELECT pg_catalog.setval(pg_get_serial_sequence('"table_name"', 'id'), MAX(id)) FROM "table_name";
@malko
malko / urlBase64ToUint8Array.js
Created March 31, 2017 12:58
used in pushManager.Subscribe to correctly encode the key to a Uint8Array
function urlBase64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/')
;
const rawData = window.atob(base64);
return Uint8Array.from([...rawData].map((char) => char.charCodeAt(0)));
}
@malko
malko / makeTemplate.js
Created March 21, 2017 11:03
dynamic es6 template string to template methods
//const tpl = makeTemplate('hello ${name}')
//const name = 'world';
//tpl({name});
const makeTemplate = (templateString) => {
return (templateData) => new Function(`{${Object.keys(templateData).join(',')}}`, 'return `' + templateString + '`')(templateData);
}
@malko
malko / notify.js
Created March 20, 2017 16:51
Notification api
const notificationGrantedPromise = () => {
if (!('Notification' in window)) {
return Promise.reject();
}
return (Notification.permission !== 'default') ? Promise.resolve(Notification.permission) : Notification.requestPermission();
};
const notify = (...args) => notificationGrantedPromise().then((permission) => permission === 'granted' ? new Notification(...args) : null);
@malko
malko / enchainProxifier.js
Created March 8, 2017 13:33
add a fluent interface on object with promise methods
const enchainProxifier = (target, promise = Promise.resolve()) => {
return new Proxy(target, {
get(target, propName) {
if (propName === 'promise') {
return promise;
} else if (propName === 'then') {
return (...args) => promise.then(...args);
}
if (target[propName] instanceof Function) {
return (...args) => enchainProxifier(target, promise.then(() => target[propName](...args)));
@malko
malko / rocketPoll.js
Last active February 17, 2017 14:41
outgoing rocketchat webhook to create polls
/*
EXAMPLE MESSAGE
!poll question?
option 1
option 2
*/
class Script {
/**
* @params {object} request