Skip to content

Instantly share code, notes, and snippets.

Avatar

Lawrence Cherone lcherone

View GitHub Profile
View php.js
function abs(mixed_number) {
return Math.abs(mixed_number) || 0;
}
function acos(arg) {
return Math.acos(arg);
}
function acosh(arg) {
return Math.log(arg + Math.sqrt(arg * arg - 1));
@lcherone
lcherone / file.php
Last active Jan 2, 2021
manual wordpress serialize fixer
View file.php
<?php
$str = 'a:1:{s:3:"foo";s:23:"https://beta.domain.com";}';
echo serialize(json_decode(str_replace('beta.domain.com', 'domain.com', json_encode(unserialize($str))), true));
// a:1:{s:3:"foo";s:18:"https://domain.com";}
@lcherone
lcherone / script.js
Last active Dec 12, 2020
Minio Migrate All - Script to migrate all buckets and files from one S3 server to another.
View script.js
/**
* S3 migration script
*
* Use this script to migrate all buckets and files from one S3 server to another.
*
* Author: Lawrence Cherone
*/
const HOSTS = {
from: {
View index.js
const range = (start, end, step) => {
const range = []
typeof step === 'undefined' && (step = 1)
if (end < start) step = -step
while (step > 0 ? end >= start : end <= start) {
range.push(start)
start += step
}
return range
}
@lcherone
lcherone / script.js
Created Jul 11, 2020
js async queue
View script.js
const queue = []
Object.defineProperty(queue, 'push', {
configurable: true,
enumerable: false,
writable: true,
value: function (...args) {
console.log('queue changed')
if (typeof args[0] === 'function') args[0]()
return Array.prototype.push.apply(this, args)
}
@lcherone
lcherone / script.js
Created Jun 27, 2020
asynchronous IIAFE in a for loop, with step timing
View script.js
console.log('asynchronous IIAFE in a for loop, with step timing')
for (let i = 0; i <= 10; i++)
setTimeout(
async () =>
(async function (index) {
console.log(index)
})(i),
i * 250
)
@lcherone
lcherone / script.js
Last active Jun 26, 2020
Javascript merge multiple arrays, de-duped by property name, O(N1) using reduce
View script.js
const array1 = [
{ name: 'a', creationDate: new Date() },
{ name: 'b', creationDate: new Date() },
]
const array2 = [
{ name: 'a', creationDate: new Date() },
{ name: 'c', creationDate: new Date() },
]
const mergeBy = (...arrays) => {
@lcherone
lcherone / script.js
Last active Jun 27, 2020
JavaScript, merge 2 arrays of objects, remove dupes, irrelevant of length, keys or reference
View script.js
/*
You saw it here first, out of the 8k dupes on stackoverflow, none address diff length b array, diff keys or reference.
Leave a comment if you know a better way
*/
const a = [{ a: 'a' }, { b: 'b' }];
const b = [{ a: 'a' }, { c: 'c' }, { x: 'x' }, {}];
const merge = (a, b) => [
...new Set([
@lcherone
lcherone / fnv.js
Last active Jun 9, 2020
FNV hash function, 32/64
View fnv.js
/**
* FNV hash
* - 32 bit FNV-1a hash
* - 64 bit FNV-1a hash
* @link http://isthe.com/chongo/tech/comp/fnv/
*
* Usage:
* fnv.hash('Hello World', 32) // 3012568359
* fnv.hash('Hello World', 64) // 3599595964
*/
@lcherone
lcherone / gist:9450312764a0fb3055165e3825631976
Created May 30, 2020
Browser crypto.subtle (SubtleCrypto) simple encrypt/decrypt
View gist:9450312764a0fb3055165e3825631976
// derive string key
async function deriveKey(password) {
const algo = {
name: 'PBKDF2',
hash: 'SHA-256',
salt: new TextEncoder().encode('a-unique-salt'),
iterations: 1000
}
return crypto.subtle.deriveKey(
algo,