Skip to content

Instantly share code, notes, and snippets.

View esroyo's full-sized avatar

carles escrig royo esroyo

View GitHub Profile
@esroyo
esroyo / create-random.js
Created March 21, 2024 09:19
Random hex string
function createRandom(len = 32, crypto = globalThis.crypto) {
const ENCODING = "0123456789abcdef";
const ENCODING_LEN = ENCODING.length;
const buffer = new Uint8Array(len);
crypto.getRandomValues(buffer);
return Array.from(buffer).map((num) => {
let rand = Math.floor(num / 0xff * ENCODING_LEN);
if (rand === ENCODING_LEN) {
rand = ENCODING_LEN - 1;
}
@esroyo
esroyo / minimal-ajax.min.js
Created November 27, 2021 18:20
Minimal cross browser AJAX (minified)
function x(u,c,o){var x,i,m="MSXML2.XmlHttp.",v=["%5.0","%4.0","%3.0","%2.0","Microsoft.XmlHttp"];o=o||{};if(window.XDomainRequest){x=new XDomainRequest;x.onload=function(){c(x.reponseText)}}else{if(window.XMLHttpRequest){x=new XMLHttpRequest}else{for(i=0;i<v.length;i+=1){try{x=new ActiveXObject(v[i].replace("%",m));break}catch(e){}}}x.onreadystatechange=function(){if(x.readyState===4){c(x.responseText)}}}for(i in o){if({}.hasOwnProperty.call(o,i)){x[i]=o[i]}}x.open("GET",u,true);x.send()}
@esroyo
esroyo / minimal-ajax.js
Created November 27, 2021 18:18
Minimal cross browser AJAX
function x(u, c, o) {
var x, i, m = 'MSXML2.XmlHttp.', v = ['%5.0', '%4.0', '%3.0', '%2.0', 'Microsoft.XmlHttp'];
o = o || {};
if (window.XDomainRequest) {
x = new XDomainRequest();
x.onload = function () {
c(x.reponseText);
};
} else {
if (window.XMLHttpRequest) {
@esroyo
esroyo / blog.md
Last active June 18, 2016 16:39
UUIDs generation, Laravel/Lumen vs. MySQL

When I read the Wix Engeenering blog post "MySQL is a better NoSQL" I remained curious about the performance difference between generating GUID values in the application code vs. doing it in the database itself. Although MySQL provides a UUID() function, the CREATE TABLE statement only accepts literals for the DEFAULT values, thus the only way to do the job is using triggers. Will that have much negative impact? Let's see.

I've set up a Lumen installation to use a UUID primary key in the User model as described by Steve Azzopardi, and created a parallel user model called Tuser (T for trigger). The former uses laravel-uuid package to generate a version 1 UUID on the application before the INSE

@esroyo
esroyo / undescores_rename.sh
Last active August 29, 2015 14:14
Getting started with _s: five-step find and replace on the name in all the templates
# change the placeholder to something else (like, say, megatherium)
find . -type f -regextype sed -regex '.*\(css\|php\|js\|pot\)' -exec sed -r -i -e s@\'_s\'@\'placeholder\'@g -e s@_s_@placeholder_@g -e 's@Text Domain: _s@Text domain: placeholder@g' -e 's@ _s@ Placeholder@g' -e 's@_s-@placeholder-@g' {} \;
sed -r -i s/_s/placeholder/g languages/_s.pot
git mv languages/_s.pot languages/placeholder.pot
# Then, update the stylesheet header in style.css and the links in footer.php with your own information.
# Next, update or delete this readme.