Skip to content

Instantly share code, notes, and snippets.

View webarthur's full-sized avatar
✌️

Arthur Ronconi webarthur

✌️
View GitHub Profile
@webarthur
webarthur / fixOfxData.js
Created July 22, 2021 16:04
Fix OFX unclosed tags
function fixOfxData (ofx) {
const lines = ofxData.split(/[\n\r]/)
const reUnclosedTag = /(^[^<]*<(.+)>[^>]+$)/
// Fix unclosed tags line by line
for (var i=0; i<lines.length; i++) {
if (reUnclosedTag.test(lines[i].trim())) {
lines[i] = lines[i].replace(reUnclosedTag, '$1</$2>')
}
}
@webarthur
webarthur / validaData.js
Last active July 31, 2023 12:21
Função JavaScript para validar datas
// From: https://devarthur.com/blog/funcao-para-validar-data-em-javascript
// Codepen: https://codepen.io/devarthur/pen/vYgBWmv
function validaData (valor) {
// Verifica se a entrada é uma string
if (typeof valor !== 'string') {
return false
}
// Verifica formado da data
@webarthur
webarthur / validaCNPJ.js
Last active December 8, 2023 18:39
Função JavaScript para validar CNPJ
function validaCNPJ (cnpj) {
var b = [ 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 ]
var c = String(cnpj).replace(/[^\d]/g, '')
if(c.length !== 14)
return false
if(/0{14}/.test(c))
return false
@webarthur
webarthur / getQueryParams.js
Created March 22, 2021 10:52
Função para ler os parâmetros da URL em JavaScript
// from https://blog.devarthur.com/javascript/como-ler-os-parametros-da-url-em-javascript/
function getQueryParams (name, query) {
var query = location.search.slice(1)
var partes = query.split('&')
var data = {}
partes.forEach(function (parte) {
var chaveValor = parte.split('=')
data[chaveValor[0]] = chaveValor[1]
})
@webarthur
webarthur / getQueryParam.js
Created March 22, 2021 10:41
Função para ler os parâmetros GET da URL em JavaScript
// from https://blog.devarthur.com/javascript/como-ler-os-parametros-da-url-em-javascript/
function getQueryParam (name, query) {
// se o parâmetro query não foi definido pega o parâmetro da URL
if (!query)
query = window.location.search
var l = query.length
var n = '' // nome
var v = '' // valor
@webarthur
webarthur / validaCPF.js
Last active March 21, 2021 20:41
Função JavaScript para validar CPF
function validaCPF(cpf) {
var Soma = 0
var Resto
var strCPF = String(cpf).replace(/[^\d]/g, '')
if (strCPF.length !== 11)
return false
if ([
@webarthur
webarthur / async-highlight-js.html
Created March 11, 2021 22:06
Load highlight.js asynchronously
<script async src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
<script>
const awaitFor = function (cond, cb, timeout = 100) {
!cond() ? setTimeout(() => awaitFor(cond, cb, timeout), timeout) : cb()
}
awaitFor(() => typeof hljs !== 'undefined', function () {
const blocks = document.querySelectorAll('pre code')
blocks.forEach(hljs.highlightBlock)
})
</script>
@webarthur
webarthur / wp-svg-inline.php
Created March 10, 2021 12:36
Function to replace SVG images to inline SVG code (WordPress)
<?php
function wp_svg_inline_replacer($matches) {
$src = $matches[1];
$parts = explode('/wp-content/', $src);
$svg = file_get_contents(WP_CONTENT_DIR . '/' . $parts[1]);
$svg = preg_replace('/<\?xml.*\?>/', '', $svg);
$svg = preg_replace('/<\!DOCTYPE[^>]*>/', '', $svg);
return trim($svg);
@webarthur
webarthur / publish-gh-pages.sh
Last active December 14, 2020 13:56
Publish to Github Pages
git branch gh-pages
git push -u origin gh-pages
# Go to the project page
# https://[username].github.io/[projectname]/
@webarthur
webarthur / Promise-series-paralel.js
Last active September 11, 2017 14:39
Uso de Promise de forma paralela e serial com async/await
// exemplo de como seria com VanillaJS
// comportamento muda qndo há um await dentro da função
// com await funciona de forma assíncrona/paralela, sem await o loop se comporta de maneira síncrona/serial
var dados = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
// paralelo
// equivalente a async.map()
var i = 0
Promise.all(dados.map(async function (item) {