Skip to content

Instantly share code, notes, and snippets.

@ahallora
ahallora / hextorgb.js
Created July 27, 2017 18:13
Convert hex value to RGB value
function hexToRGBA (hex, opacity = 1) {
const components = hex
.replace('#', '')
.match(/.{1,2}/g)
.map(hextet => parseInt(hextet, 16))
.concat(opacity)
.join(', ');
return `rgba(${components})`
}
@ahallora
ahallora / tracker.js
Last active October 25, 2017 14:54
Tracker Object to track time - including pause and resume functionality
/*
LICENSE: MIT
Created by: Anders Holm-Jensen
Website: allora.dk
Date: 25-oct-2017
*/
const timestamp = () => ( Math.round( new Date() * 1 / 1000) );
let tracker = {
data: {
@ahallora
ahallora / PwnedPasswords.js
Created February 6, 2019 07:05
A simple ES6 implementation of PwnedPasswords API
// https://cdnjs.cloudflare.com/ajax/libs/js-sha1/0.6.0/sha1.min.js
/*
* [js-sha1]{@link https://github.com/emn178/js-sha1}
*
* @version 0.6.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2017
* @license MIT
*/
!function(){"use strict";function t(t){t?(f[0]=f[16]=f[1]=f[2]=f[3]=f[4]=f[5]=f[6]=f[7]=f[8]=f[9]=f[10]=f[11]=f[12]=f[13]=f[14]=f[15]=0,this.blocks=f):this.blocks=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],this.h0=1732584193,this.h1=4023233417,this.h2=2562383102,this.h3=271733878,this.h4=3285377520,this.block=this.start=this.bytes=this.hBytes=0,this.finalized=this.hashed=!1,this.first=!0}var h="object"==typeof window?window:{},s=!h.JS_SHA1_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;s&&(h=global);var i=!h.JS_SHA1_NO_COMMON_JS&&"object"==typeof module&&module.exports,e="function"==typeof define&&define.amd,r="0123456789abcdef".split(""),o=[-2147483648,8388608,32768,128],n=[24,16,8,0],a=["hex","array","digest","arrayBuffer"],f=[],u=functio
@ahallora
ahallora / promises.js
Last active February 15, 2020 20:27
How to properly nest functions returning Promises (ECMA6) in node.js (Nested Promises example)
/*
How to properly nest functions returning Promises (ECMA6) in node.js (Nested Promises example)
generated output (with 1 second ticks) =>
p1 function
p2 function
p3 function
p3 done
p2 done
@ahallora
ahallora / LambdaCacheController.js
Last active March 29, 2020 19:50
Simple AWS Lambda Container In-Memory Cache Controller for Node.js / ES6
export class LambdaCacheController {
constructor() {
this.value = null;
this.timestamp = null;
this.ttl = 3000000; // default 5 minutes
}
async getValue() {
return new Promise((resolve, reject) => {
try {
@ahallora
ahallora / timestampToExcelDate.js
Last active April 5, 2020 19:01
Epoch / Unix timestamp to Excel Date converter in JavaScript / ES6 / Node
const timestampToExcelDate = timestamp => {
const start = new Date("1899-12-30 00:00:00");
return (timestamp - start) / (1000 * 60 * 60 * 24).toFixed(12);
};
@ahallora
ahallora / CsvToJson.js
Created April 8, 2020 11:29
Javascript function to convert a CSV list into list of JSON objects
const mapCsvToJson = (csv, delimiter) => {
const lines = csv.split("\n");
let dataArray = [];
let keys = [];
for (i = 0; i < lines.length; i++) {
const line = lines[i];
const data = line.split(delimiter);
if (i === 0) {
@ahallora
ahallora / index.html
Created June 19, 2020 12:18
XSS onerror image example
<img src=x onerror="&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041">
@ahallora
ahallora / s1ep1.md
Created July 28, 2020 19:13
Math and JavaScript live problem solving with Anders - episode 1

Math and JavaScript live problem solving - episode 1

Welcome to this mini series of me trying to solve a simple math problem with Javascript. I apologize in advance to everyone more capable in both math and javascript.

Today's objective

  • Divide a number by 100 and limit the result's amount of decimals to 5 tops.
  • Expected outcome: 49.95 becomes 0.4995 and 9.95 becomes 0.0995.

That's easy, you might think.

@ahallora
ahallora / dotPathToObject.js
Last active February 4, 2021 15:01
Convert string in dot notation into an object with JavaScript
const dotPathToObject = (pathStr, value) => pathStr
.split(".")
.reverse()
.reduce((acc, cv, index) => ({
[cv]: index === 1 && value ? {[acc]: value} : acc
}))
console.log("expect:", JSON.stringify({
"a": {