Skip to content

Instantly share code, notes, and snippets.

//Found on https://stackoverflow.com/a/20097994/5394675
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function(m,key,value) {
vars[key] = value;
});
return vars;
}
//addapted from here: //http://wbruno.com.br/expressao-regular/formatar-em-moeda-reais-expressao-regular-em-javascript/
const convertToBrMoney = (value) => {
value = parseFloat(value);//force value to be a number
var int = parseFloat(value.toFixed(2).toString().replace(/[^\d]+/g, ''));
var tmp = int + '';
tmp = tmp.replace(/([0-9]{2})$/g, ",$1");
if (tmp.length > 6)
tmp = tmp.replace(/([0-9]{3}),([0-9]{2}$)/g, ".$1,$2");
tmp = value < 1 ? value.toString().replace('.', ',') : tmp;
@jhonsore
jhonsore / gist:faa51cab7b9d5e563dae0b6fbc26b5ba
Last active July 7, 2020 15:50
Elementos semânticos html5
<head>
<!-- as três linhas abaixo são uma correção para que elementos semânticos HTML5 funcionem em versões antigas do Internet Explorer-->
<!--[if lt IE 9]>
<script src="https://cdn.es.gov.br/scripts/extensions/html5shiv/3.7.2/html5shiv.min.js"></script>
<![endif]-->
</head>
function validaEmail (email){
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
};
[
{ "label": "Acre", "id": "AC" },
{ "label": "Alagoas", "id": "AL" },
{ "label": "Amapá", "id": "AP" },
{ "label": "Amazonas", "id": "AM" },
{ "label": "Bahia", "id": "BA" },
{ "label": "Ceará", "id": "CE" },
{ "label": "Distrito Federal", "id": "DF" },
{ "label": "Espírito Santo", "id": "ES" },
{ "label": "Goiás", "id": "GO" },
@jhonsore
jhonsore / textTtruncate.js
Last active April 17, 2020 19:09
Truncate a string if it is longer than the specified number of characters
function truncateString(str, len, append)
{
var newLength;
append = append || ""; //Optional: append a string to str after truncating. Defaults to an empty string if no value is given
if (append.length > 0)
{
append = " "+append; //Add a space to the beginning of the appended text
}
if (str.indexOf(' ')+append.length > len)
const strPtBrMoneyToNumber = value => {
if(!value){
console.warn('strPtBrMoneyToNumber needs a value to be converted');
return false;
}
value = String(value);
value = value.replace(/\./g, '#');//remove . e troca por #
value = value.replace(/,/g, '.');//remove a , e muda para .
value = value.replace(/#/g, '');//remove a # e muda para ""
//utils.js
import React from 'react'
export function childrenWithProps (props) {
return React.children.map(props.children, child => {
return React.CloneElement(child, { ...props });
});
}
//------
@jhonsore
jhonsore / date.php
Created June 30, 2019 12:03
Add months, days, years to PHP date()
//found on https://snipplr.com/view.php?codeview&id=10958
$date = date("Y-m-d");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +12 month");
$date = date("Y-m-d",$date);
echo $date;
Other examples
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
@jhonsore
jhonsore / alpha_num_and_-.php
Created June 29, 2019 18:59
Replace non alphanum and - from php string
$str = '123 - *&ˆ%$#@ T+=esteJhon#4255%%';
$newStr = preg_replace('/[^[:alnum:]-]/','',$str);