leonelgalan's notebooks

  • Epifany URL Validation - /leonelgalan/url-validation
    Last edited 2 years ago
    const axios = require('axios'); const queryString = require('query-string'); const atob = require('atob') const MAX_REDIRECTS = 10; const landing_regex = /https:\/\/web\.epifany\.com\/([^?\/]*)($|\?(.*))/; const new_regex = /https:\/\/web\.epifany\.com\/([^\/]*)\/new?[^#]*#(.*)/; const redirectUrl = async (url) => { const options = { method: 'GET', maxRedirects: 0 } const result = await axios.request({ url, ...options }).then(_ => undefined).catch((error) => { if (error.response.status === 301 || error.response.status === 302) { return error.response.headers.location; } }); return result; }; const evaluateUrl = async bar => { const urls = [bar] let newMatch = undefined; let landingMatch = undefined; let newUrl = bar let i = 1; do { newUrl = await redirectUrl(newUrl); if (newUrl !== undefined) { urls.push(newUrl) let match = newUrl.match(landing_regex); if (match !== null) { landingMatch = match; } newMatch = newUrl.match(new_regex); i++; } } while (newUrl !== undefined && !newMatch && i < MAX_REDIRECTS); const landingParams = landingMatch && queryString.parse(landingMatch[3]); const newParams = newMatch && queryString.parse(newMatch[2]); const result = { landing: landingParams !== undefined, urls, email: { requested: landingParams && landingParams['email'], served: newParams && atob(newParams['id']), }, major: { requested: landingParams && landingParams['major'], served: newParams && atob(newParams['major']), }, type: { requested: landingParams && landingParams['type'], served: newParams && atob(newParams['type']), }, }; return result; };
  • Tinycolor's monochromatic palette - /leonelgalan/tinycolor-monochromatic-palette
    Last edited 5 years ago
    const _ = require("lodash") const tinycolor = require("tinycolor2"); const color = 'blue'; const number = 10 const offset = Math.round(number / 2); const monochromaticByLuminance = (color, steps) => { const hsl = color.toHsl(); const { s, h } = hsl; let { l } = hsl; const modification = 1 / steps; const ret = []; _.times(steps, () => { ret.push(tinycolor({ h, s, l })); l = (l + modification) % 1; }); return ret; }; _(monochromaticByLuminance(tinycolor(color), number)) .sortBy(color => color.toHsl().l) .reverse() .map((color, index) => { const hsl = color.toHsl(); const { s, l } = hsl; let { h } = color.toHsl(); h = (h + 6 * index - (number / 2)) % 255; //console.log({h, l, color: tinycolor({ h, s, l }).toHexString()}); return tinycolor({ h, s, l }).toHexString(); }).value();
  • Name to Color - /leonelgalan/name-to-color
    Last edited 6 years ago
    const tinycolor = require("tinycolor2") const stringHash = require("string-hash"); const faker = require('faker'); const _ = require('lodash'); const baseColor = tinycolor('#25bcca'); const hsv = baseColor.toHsv(); _.times(40, () => { const name = faker.name.findName(); const hash = stringHash(name); const h = 175 + (hash % 155); const color = tinycolor({ h, s: 82 + (hash % 10), v: 83 + (hash % 10) }); const initial = name[0].toUpperCase(); return({h, initial, color: color.toHexString(), name}); });