Skip to content

Instantly share code, notes, and snippets.

View wallace7souza's full-sized avatar

wallace souza wallace7souza

View GitHub Profile
@wallace7souza
wallace7souza / gist:5084a3198321f35602616f6f3f1619be
Created April 14, 2023 02:52
javascript function File size to huma readable format
function humanFileSize(bytes, si=false, dp=1) {
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
const units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
@wallace7souza
wallace7souza / FileCreationDate.java
Created February 6, 2021 16:07
Determine File Creation Date in Java
try {
FileTime creationTime = (FileTime) Files.getAttribute(path, "creationTime");
} catch (IOException ex) {
// handle exception
}
try {
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
FileTime fileTime = attr.creationTime();
} catch (IOException ex) {
@wallace7souza
wallace7souza / LocalDateTimeDiff.java
Last active February 6, 2021 16:06
Using java.time.temporal.ChronoUnit to Find the Difference
public void givenTwoDateTimesInJava8_whenDifferentiatingInSeconds_thenWeGetTen() {
LocalDateTime now = LocalDateTime.now();
LocalDateTime tenSecondsLater = now.plusSeconds(10);
long diff = ChronoUnit.SECONDS.between(now, tenSecondsLater);
assertEquals(10, diff);
}
@wallace7souza
wallace7souza / gist:4db1439d732cf49b65b418113a9d72a3
Created February 6, 2021 16:04
Using java.util.Date to Find the Difference in Days
public void givenTwoDatesBeforeJava8_whenDifferentiating_thenWeGetSix()
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date firstDate = sdf.parse("06/24/2017");
Date secondDate = sdf.parse("06/30/2017");
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
@wallace7souza
wallace7souza / gist:b91df8fef3e18b0341597b9c8164dc65
Last active August 19, 2020 16:37
react native build main.jsbundle
https://medium.com/@onexlab.io/main-jsbundle-does-not-exist-fixed-7d92f466ba5a
in package.json add
"build:ios": "react-native bundle --entry-file='index.js' --bundle-output='./ios/main.jsbundle' --dev=false --platform='ios'
"error Unable to resolve module `react-native-webview` from `src/screens/TermoUsoScreenComponent.js`: react-native-webview could not be found within the project.
If you are sure the module exists, try these steps:
1. Clear watchman watches: watchman watch-del-all
@wallace7souza
wallace7souza / index.js
Last active August 15, 2020 21:10
Essa função substitui caracteres acentuados por sua versão sem acento.
function replaceSpecialChars(str=''){
return str.normalize('NFD')
.replace(/[\u0300-\u036f]/g,'');
}
@wallace7souza
wallace7souza / gist:a5ee19a32ed4081808ecdb11c946ed78
Created October 9, 2019 19:41
Replace all line breaks by tag <br>
str = str.replace(/(?:\r\n|\r|\n)/g, '<br>');
@wallace7souza
wallace7souza / gist:1996a88ece84a43a8798d0528ff8c784
Created June 15, 2019 00:15
Limitar tamanho da requisição para uma rota no express
app.post('/api/0.1/people', express.bodyParser({limit: '5mb'}), yourHandler);
var str = 'a b c';
var replaced = str.replace(/\s/g, '+');
@wallace7souza
wallace7souza / validate.js
Created January 23, 2019 19:24
Validação de data em javascript
//Baseado na solução em https://pt.stackoverflow.com/questions/91379/como-validar-data-com-angularjs-ou-jquery
function validateDate(data){
if(!data){
return false;
}
let dataTemp = data;
if(dataTemp.length===8){
dataTemp = data.substr(0,2)+'/'+data.substr(2,2)+'/'+data.substr(4);