Skip to content

Instantly share code, notes, and snippets.

View darkmavis1980's full-sized avatar
🎯
Focusing

Alessio Michelini darkmavis1980

🎯
Focusing
View GitHub Profile
@darkmavis1980
darkmavis1980 / relative-to-absolute.js
Created November 12, 2019 11:51
Relative links to asbolute regex
const text = `
<p>This is some test with <a href=\"/piesync.html\">a relative link</a> and one with an <a href=\"http://piesync.com\">absolute link</a></p>
<p>This is some test with <a href=\"/piesync.html\" title="test">a relative link</a> and one with an <a href=\"http://www.piesync.com\">absolute link</a></p>
<p>This is some test with <a href=\"/piesync/hello/path\">a relative link</a> and one with an <a href=\"http://piesync.com\">absolute link</a></p>
<p>This is some test with <a href=\"/\">a relative link</a> and one with an <a href=\"http://piesync.com\">absolute link</a></p>
<p>This is some test with <a href=\"/?_ga=2.12323232.131232131.423413123-1231231.123123123\">a relative link</a> and one with an <a href=\"http://piesync.com\">absolute link</a></p>
`;
let replaced = text.replace(/<a href="(\/([\w.\/?=-]+)|\/)/gm, `<a href="https://www.domain.com$1`);
@darkmavis1980
darkmavis1980 / vscode-extensions.sh
Created November 14, 2019 13:45
Show installed VS Code plugins
code --list-extensions | xargs -L 1 echo code --install-extension
@darkmavis1980
darkmavis1980 / format.js
Created March 3, 2020 16:46
Format a string with variables passed as an object
function format(string, keyValues) {
const regex = /{{([a-zA-Z]+)}}/g;
return string.replace(regex, (match, key) => {
return keyValues[key];
});
}
console.log(format('Hello {{name}}', {name: 'Alessio'})); // Hello Alessio
console.log(format('The price is {{currency}} {{price}}', {price: '10.5', currency: 'EUR'})); // The price is EUR 10.5
console.log(format('Il prezzo è {{price}}{{currency}}', {price: '10.5', currency: 'EUR'})); // Il prezzo è 10.5EUR
@darkmavis1980
darkmavis1980 / kebabToCamel.js
Created March 20, 2020 09:25
Kebab case to camel case string conversion
/**
* @description Transform a kebab case string into a Camel Case
* @param {string} string Source string
*
* @example
* // returns thisIsMyString
* kebabToCamelCase('this-is-my-string');
* @returns {string}
*/
export function kebabToCamelCase(string) {
@darkmavis1980
darkmavis1980 / FixLoop.md
Created March 25, 2020 21:29
Fix ubuntu login loop

If you get stuck with the login loop, press ctrl + alt + F3 to access to the shell. Login with your account, then run these commands:

sudo apt purge gdm3
sudo apt install gdm3

Restart and you should be able to login now.

@darkmavis1980
darkmavis1980 / replace.js
Last active September 10, 2022 20:47
Replace require with ESM import in VS Code find/replace
/**
* search for:
* const x = require('x');
* const { a } = require('a');
*/
const ([\w\s\n,{}:]+) = require\('([.\/@\w-]+)'\)
/**
* replace with:
@darkmavis1980
darkmavis1980 / hs-regex.js
Last active December 22, 2022 14:04
Matches either hubspot domain or hs-sites (sandboxes)
// url => https://regex101.com/r/jhw0wy/1
const regex = /https:\/\/([a-z\.]{4})?([0-9]+.hs-sites|hubspot).com[\/]?/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('https:\/\/([a-z\.]{4})?([0-9]+.hs-sites|hubspot).com[\/]?', 'gm')
const str = `https://hubspot.com/
https://123456.hs-sites.com/
https://google.com`;
@darkmavis1980
darkmavis1980 / fix-docker.sh
Created March 10, 2023 23:47
Fix Docker Permission Denied WSL 2
sudo addgroup --system docker
sudo adduser $USER docker
newgrp docker
# And something needs to be done so $USER always runs in group `docker` on the `Ubuntu` WSL
sudo chown root:docker /var/run/docker.sock
sudo chmod g+w /var/run/docker.sock
const functionA = async () => {
try {
// request something that resolves in a promise
} catch (error) {
throw new Error("Function A failed");
}
}
const functionB = async () => {
try {