Skip to content

Instantly share code, notes, and snippets.

View fedeghe's full-sized avatar
❤️‍🔥
you

Federico Corrado Ghedina Krepo fedeghe

❤️‍🔥
you
View GitHub Profile
@fedeghe
fedeghe / replaceall.js
Last active January 31, 2023 08:04
A useful function to give values to a template full of placeholders
/**
* [ description]
* @param {string} tpl the template
* @param {literal|function} obj a literal for substitution or a function that will
* return the substitution given as parameter a string
* @param {string} start optional- the opening placeholder delimitator (%)
* @param {string} end optional- the closing placeholder delimitator (%)
* @param {string} fb optional- a fallback value in case an element is not found
* @return {string} the resulting string with replaced values
*/
(function (ns){
// this is due, to test all implications see
// http://www.jmvc.org/test_strict
"use strict";
var debugActive = false;
/**
* Creates a namespace
* @param {String} str dot or slash separated path for the namespace
@fedeghe
fedeghe / searchHash.js
Last active July 5, 2018 23:48
A function to search deep for keys and/or values also using regexp
var searchHash = (function () {
// some utility func
function jCompare(obj1, obj2) {
return !isNode(obj1)
&& typeof JSON !== 'undefined' ?
JSON.stringify(obj1) === JSON.stringify(obj2)
:
obj1 == obj2;
}
@fedeghe
fedeghe / check_duplicate_ids.js
Last active May 1, 2019 19:34
spot duplicate ids
(function (idsEls) {
var ids = idsEls.map(e => e.id).sort(),
uniq = [],
dups = {};
ids.forEach(id => {
if (uniq.indexOf(id) === -1) {
uniq.push(id);
} else {
dups[id] = id in dups ? ++dups[id] : 2;
}
@fedeghe
fedeghe / hyperbookmarlet.js
Last active May 9, 2022 10:25
Generate a higher order bookmarklet which allows to generate any bookmarklet really quickly
console.log("javascript:void "+encodeURIComponent(`function _(){var code=prompt('Paste here Your code'),bm="javascript:void(function(){"+encodeURIComponent(code+'')+"}())";if(code){if(bm.length>2e3){alert('The resulting code looks quite long to be printed in a prompt, the output will be written in the console, please copy it from there');console.log(bm)}else{prompt("Here's the bookmarklet code",bm)}}else{alert('No code given')}}()`))
// which gives
/**
javascript:void function%20_()%7Bvar%20code%3Dprompt('Paste%20here%20Your%20code')%2Cbm%3D%22javascript%3Avoid(function()%7B%22%2BencodeURIComponent(code%2B'')%2B%22%7D())%22%3Bif%20(code)%7Bif%20(bm.length%20%3E%202e3)%20%7Balert('The%20resulting%20code%20looks%20quite%20long%20to%20be%20printed%20in%20a%20prompt%2C%20the%20output%20will%20be%20written%20in%20the%20console%2C%20please%20copy%20it%20from%20there')%3Bconsole.log(bm)%7Delse%7Bprompt(%22Here's%20the%20bookmarklet%20code%22%2Cbm)%7D%7Delse%7Balert('No%20code%20given')%7D%7D()
**/
// save a bookmar
@fedeghe
fedeghe / uniqueID.js
Created August 17, 2020 06:34
dummy function to generate uniq ids
var uniqueID = new function () {
var count = 0,
self = this;
this.prefix = 'NS_';
this.toString = function () {
count += 1;
return self.prefix + count;
};
}
@fedeghe
fedeghe / scrollableTable.html
Last active January 14, 2022 22:12
full scrollable table
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {
overflow-y: scroll;
@fedeghe
fedeghe / test.js
Last active April 6, 2022 13:14
Quick benchmarking utility function
/**
// HOW TO USE IT
const test = require('./wherever/it/is/test')
function myFunc1 (a,b) { ... }
function myFunc2 (a,b) { ... }
const benchs = [{
in: [[1,2,3.4], 'test'],
out: true
}, ...]
test(
@fedeghe
fedeghe / findInsertIndex.js
Last active April 4, 2022 09:52
find insert index in a sorted array
/**
* Implements in place binary search. O(log n)
* optional comparator can be passed
*/
function findInsertIndex(a, item, smaller) {
var l = 0,
len = a.length,
r = len - 1,
m;
smaller = smaller || function(a, b, strict){
@fedeghe
fedeghe / getSplitter.js
Created November 26, 2022 20:21
getSplitter
var testone = require('@fedeghe/testone'),
benchs = [
{
in: ['"', ',', `a,,"a;.b,c",1.4.3.2,3,r,"c.,d r;t",s`],
out: [
'a', '', '"a;.b,c"', '1.4.3.2', '3', 'r', '"c.,d r;t"', 's'
]
},{
in: ["'", ';', `a;;'a,b;c';r;'c.,d r;t';s`],
out: [ 'a', '', "'a,b;c'", 'r', "'c.,d r;t'", 's' ]