Skip to content

Instantly share code, notes, and snippets.

View luan0ap's full-sized avatar
👁️‍🗨️
Looking for a job

Luan AP luan0ap

👁️‍🗨️
Looking for a job
View GitHub Profile
@luan0ap
luan0ap / romanNumeralToNumeral.js
Last active May 25, 2022 21:22
Convert roman numerals to numerals using recursive solution
/**
*
* @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')
}
@luan0ap
luan0ap / cryptoAveragePrice.js
Last active April 6, 2022 16:45
Calculate cryptocurrency average price from Binance API
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
}
@luan0ap
luan0ap / BitcoinButtonClick.js
Last active March 15, 2022 14:33
Auto click script for Binance #BitcoinButton. See: https://www.binance.com/en/activity/bitcoin-button-game
const getRich = async () => {
const p = () => new Promise(resolve => setTimeout(() => resolve(), 100))
// improve it :v
while (true) {
await p()
const $btn = document.querySelector('.css-86uttc')
$btn.click()
}
@luan0ap
luan0ap / javascript-proxy-as-rest-client.js
Created February 8, 2022 18:56 — forked from DavidWells/javascript-proxy-as-rest-client.js
Using a javascript proxy as low code REST client
/* 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}`)
@luan0ap
luan0ap / currencyToFloat.js
Created January 20, 2022 15:59
Convert a currency string to float
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)
@luan0ap
luan0ap / prependHttp.js
Created November 26, 2021 00:50
Prepende http/https to an URL
// 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;
@luan0ap
luan0ap / createUsersKeycloak.js
Last active November 22, 2021 17:39
Script to create several user in Keycloak
(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({
@luan0ap
luan0ap / arrayBufferToBase64.js
Created August 17, 2021 13:21
Asyncronous convert an array buffer to a base64 string
/**
* @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)
@luan0ap
luan0ap / memoizer.js
Created August 10, 2021 14:23
memoizer using es6
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
@luan0ap
luan0ap / deepForIn.js
Last active June 25, 2021 19:54
Deep for in a object
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')
}