Skip to content

Instantly share code, notes, and snippets.

Avatar

Arthur Ronconi webarthur

View GitHub Profile
@webarthur
webarthur / fixOfxData.js
Created Jul 22, 2021
Fix OFX unclosed tags
View fixOfxData.js
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 / validarData.js
Last active Mar 22, 2021
Função em JavaScript para validar uma datas
View validarData.js
// From: https://blog.devarthur.com/javascript/funcao-para-validar-datas-em-javascript
function validarData (s) {
var l = s.length
var j = 0
var dt = { 0: '', 1:'', 2:'' }
// dias de cada mês
var n = [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
// divide a data para o objeto "dt"
@webarthur
webarthur / validaCNPJ.js
Last active Mar 22, 2021
Função JavaScript para validar CNPJ
View validaCNPJ.js
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 Mar 22, 2021
Função para ler os parâmetros da URL em JavaScript
View getQueryParams.js
// 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 Mar 22, 2021
Função para ler os parâmetros GET da URL em JavaScript
View getQueryParam.js
// 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 Mar 21, 2021
Função JavaScript para validar CPF
View validaCPF.js
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 Mar 11, 2021
Load highlight.js asynchronously
View async-highlight-js.html
<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 Mar 10, 2021
Function to replace SVG images to inline SVG code (WordPress)
View wp-svg-inline.php
<?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 Dec 14, 2020
Publish to Github Pages
View publish-gh-pages.sh
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 Sep 11, 2017
Uso de Promise de forma paralela e serial com async/await
View Promise-series-paralel.js
// 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) {