Skip to content

Instantly share code, notes, and snippets.

View fedeghe's full-sized avatar
🏡
I'm working from home

Federico Corrado Ghedina Crepo fedeghe

🏡
I'm working from home
View GitHub Profile
@fedeghe
fedeghe / DateFormatValidation.js
Last active May 21, 2024 08:36
date format validation
const testone = require('@fedeghe/testone');
const validDate = value => {
const plainValue = value.replace(/[^\d]/g, ' ');
const elements = plainValue.split(/\s/).map(i => parseInt(i, 10));
// must find month < 13
const foundMonth = elements.findIndex(e => e > 0 && e < 13);
if (foundMonth < 0) return false;
// must find another value in [1,31]
const foundDate = elements.findIndex(
@fedeghe
fedeghe / genPwd.js
Created March 22, 2024 20:08
Password generaator
const genPassword = (i,u,l) => {
function* getPassword(i, u, l) {
function* gen(start, end, len) {
while(len--)
yield start + ~~(Math.random() * (end-start));
}
yield* gen(48, 57, i);// 0..9
yield* gen(65, 90, u);// A..Z
yield* gen(97, 122, l);// a..z
}
@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' ]
@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 / 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 / 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 / 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 / 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 / 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 / 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;
}