This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param {Strig} roman roman numeral like | |
* @throws {RangeError} if roman is not a valid roman numeral | |
* @returns Number | |
*/ | |
function romanNumeralToNumeral (roman = '') { | |
if (!roman.match(/^[IVXLCDM]+$/)) { | |
throw new RangeError('Invalid roman numeral') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cryptoAveragePrice(binanceData) { | |
return binanceData.reduce((sum, { totalQuota, price }) => { | |
const totalBought = sum.totalBought + Number(totalQuota) | |
const totalPrice = sum.totalPrice + Number(totalQuota) * Number(price) | |
return { | |
totalBought, | |
totalPrice, | |
average: totalPrice / totalBought | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Using a JavaScript proxy for a super low code REST client */ | |
// via https://dev.to/dipsaus9/javascript-lets-create-aproxy-19hg | |
// also see https://towardsdatascience.com/why-to-use-javascript-proxy-5cdc69d943e3 | |
// also see https://github.com/fastify/manifetch | |
// also see https://github.com/flash-oss/allserver | |
const createApi = (url) => { | |
return new Proxy({}, { | |
get(target, key) { | |
return async function(id = "") { | |
const response = await fetch(`${url}/${key}/${id}`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const currencyToFloat = currency => { | |
if (typeof currency === 'string') { | |
// remove caracteres inválidos | |
const CURRENCY_REGEXP = /\D*(\d+|\d.*?\d)(?:\D+(\d{2}))?\D*$/ | |
const parts = CURRENCY_REGEXP.exec(currency) | |
return Number(`${parts[1].replace(/\D/, '')}.${parts[2] ? parts[2] : '00'}`) | |
} | |
return Number(currency) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/sindresorhus/prepend-http/blob/main/index.js | |
function prependHttp(url, {https = true} = {}) { | |
if (typeof url !== 'string') { | |
throw new TypeError(`Expected \`url\` to be of type \`string\`, got \`${typeof url}\``); | |
} | |
url = url.trim(); | |
if (/^\.*\/|^(?!localhost)\w+?:/.test(url)) { | |
return url; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
const KcAdminClient = require('@keycloak/keycloak-admin-client').default | |
const { v4: uuidv4 } = require('uuid') | |
const kcAdminClient = new KcAdminClient({ | |
baseUrl: 'http://turbina/auth', | |
realmName: 'master' | |
}) | |
await kcAdminClient.auth({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {ArrayBuffer | Array} arrayBufferToBase64 | |
* @return Promise<string> | |
*/ | |
const arrayBufferToBase64 = (buffer = new ArrayBuffer()) => { | |
return new Promise((resolve, reject) => { | |
try { | |
let binary = '' | |
const bytes = new Uint8Array(buffer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function memoizer (fn, initialState = null) { | |
const _cache = initialState || {} | |
return (...params) => { | |
const _stringified = JSON.stringify(params) | |
if (_cache[_stringified] === undefined) { | |
const result = fn(...params) | |
_cache[_stringified] = result | |
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getTypeOf = (data) => ({}).toString.call(data).match(/\s([a-zA-Z]+)/)[1].toLowerCase() | |
/** | |
@param {Object} obj | |
@param {deepForInCallback} fn | |
*/ | |
function deepForIn (obj = {}, fn = (value, key, obj) => {}) { | |
if (getTypeOf(obj) !== 'object') { | |
throw new Error('First param must be an object') | |
} |