Skip to content

Instantly share code, notes, and snippets.

View malko's full-sized avatar

Jonathan Gotti malko

View GitHub Profile
@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 / 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 / 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 / jira-rocketchat-hook.js
Created May 10, 2016 15:26
Jira / Rocketchat integration
/*jshint esnext:true*/
const DESC_MAX_LENGTH = 140;
const JIRA_LOGO = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAACRElEQVRYhbWXsUscQRTGf4iIyHHIISIWIsHisMgfkNIiBJFwiKQIkipVqpA/wEZEggSxEkmZwiKI5A84REKKkIMQrINYBQmHBDmEHJdNMW+42dk3d3O76wcDu2/e973vZvfN7EF+PAfaMjYL6AzFJFBRYh0gkdEBpryciuQVwjPgFugCu068CvQcAz1g2pnfEc6taOTGL6dIAjxw5nad+FsnvuhxrosYuPbElrz5Rc8Ucu9yfhcxsAncYZZ4fwTeO+HcUcILWgFqOXg1si9vFBrAXB7iEMySfYQZzGCeWxdoAq+Bh8BYjoJjwn0jWrYrqsOIbdIvUQLseTmPgHXgiYx1ibnYU3RuYpyfKMQ/mNWx+KzkfHHmZ4Tj55zGGNhQiAlw5OQ8VeYbzvxRQCNqUxoHLgMCa07eRyd+4sTXAtwrYCLGAJje1URugLrkVIHvMuyLVZccjfsitrhFMyD0k36bTtA/cOZkTuOckaOTFtA7IgEuSG9ONeBHILctWrnwGNO/mvA3zAk4LddaThfTpoXwKiBuVyL0yxPhloLtAUVCY7us4hb7IxQ/KLu4xWFE8cP7Kg6mld4PKH5BvoNrZBMfBphohKnFMAusyvU48ClgoA3M34eBUynwUu6ngK8BE1Gn3ihYccR79Jd5nuyXsx0rZRo498Q7mK8dMDudZuC8rOLLgQI7Ts5xIGe5DANbinCP9AfmEul/SnZslWHgTBFuKnna8a3lpRCzadSVWMiAj6GPIMbAX+/+H9BS8loyN4ibwX9j/jIXDkk+pgAAAABJRU5ErkJggg==';
function stripDesc(str) {
return str.length > DESC_MAX_LENGTH ? str.slice(
@malko
malko / gitlab-rocketchat.hooks.js
Last active May 25, 2018 08:07
Gitlab / Rocketchat intégration
/*jshint esnext:true*/
// see https://gitlab.com/help/web_hooks/web_hooks for full json posted by GitLab
const NOTIF_COLOR = '#6498CC';
const refParser = (ref) => ref.replace(/^.*?([^\/]+)$/,'$1');
class Script {
process_incoming_request({request}) {
try {
switch(request.headers['x-gitlab-event']){
case 'Push Hook':
@malko
malko / cookies.js
Created May 4, 2012 14:15
javascript cookies
cookies={
get:function(name){
var re=new RegExp(name+'=([^;]*);?','gi'), result=re.exec(document.cookie)||[];
return (result.length>1? unescape(result[1]): false);
},
set:function(name, value, expirationTime, path, domain, secure){
var time=new Date();
if(expirationTime){
time.setTime(time.getTime()+(expirationTime*1000));
}
@malko
malko / propertyComparator.js
Last active July 19, 2017 13:01
make a multiple fields comparator for sort functions
/**
* return a comparator to use in Array.sort method context.
* take a list of properties (array or string delimited by pipe or comma) which you want to use for sort
* @param {(string|string[])) fields list of properties names (array or string delimited by pipe or comma) which you want to use for sort
* each field name may be prefixed by an < or > to sort on ascending or descending order
* you can use n< or n> as prefix for a natural order sorting
* @returns {function(object, object)}
*/
function propertyComparator(compareConfig) {
if (typeof compareConfig === 'string') {