Skip to content

Instantly share code, notes, and snippets.

@bcalmac
bcalmac / FilmsOfTatooine.js
Last active May 14, 2021
Axios with concurrent requests
View FilmsOfTatooine.js
// What films does planet Tatooine shows up in?
// http://swapi.dev/api/planets/1/
// Functional-style friendly functions
function get(url) {
return axios.get(url).then(r => r.data)
}
function getAll(urls) {
return Promise.all(urls.map(get))
@bcalmac
bcalmac / sleep.js
Created Sep 4, 2020
sleep function that can be used as "await sleep(1000)"
View sleep.js
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
@bcalmac
bcalmac / OvertimeTestUtils.java
Created Feb 10, 2020
Load resource to string
View OvertimeTestUtils.java
package io.github.bcalmac.overtime.server.utils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
public class OvertimeTestUtils {
@bcalmac
bcalmac / LocalDateJacksonSerializationTest.java
Created Jan 13, 2020
Test LocalDate JSON serialization / deserialization with Jackson
View LocalDateJacksonSerializationTest.java
package io.github.bcalmac.scratch;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.Data;
import org.junit.jupiter.api.Test;
@bcalmac
bcalmac / DateTimeFormatConfigurer.java
Created Jan 13, 2020
Cofigure Spring type conversion to use ISO-8601 format for dates
View DateTimeFormatConfigurer.java
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Override date/time formatters with ones using ISO-8601.
*/
@Component
public class DateTimeFormatConfigurer implements WebMvcConfigurer {
@bcalmac
bcalmac / RemaindersOfPowers.hs
Created Dec 14, 2019
Determine the list of remainders of powers of a divided by b
View RemaindersOfPowers.hs
-- a^0 mod b is 1. Once we find the next power with a remainder of 1, the remainders will repeat.
-- Take from a list until the first element repeats
takeUntilFirstRepeats (x:xs) = x : takeWhile (/= x) xs
remaindersOfPowers a b = takeUntilFirstRepeats [a^i `mod` b | i <- [0..]]
-- Example
remaindersOfPowers 9 7
@bcalmac
bcalmac / IntervalMixIn.java
Last active Aug 18, 2021
Jackson serialization for Interval
View IntervalMixIn.java
import java.time.Instant;
import org.threeten.extra.Interval;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
// Exclude JavaBean properties that are not part of the state
@bcalmac
bcalmac / WireMockTransformerExample.java
Created Aug 2, 2017
Simple WireMock transformer that converts the content to upper case
View WireMockTransformerExample.java
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static org.junit.Assert.assertEquals;
import wiremock.org.apache.http.client.utils.URIBuilder;
import java.net.URI;
import java.net.URISyntaxException;
@bcalmac
bcalmac / DuplicateLogging.java
Last active Jul 28, 2017
What happens if stdout is redirected to the same file as a FileAppender?
View DuplicateLogging.java
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DuplicateLogging {
private static final Logger logger = LoggerFactory.getLogger(DuplicateLogging.class);
public static void main(String[] args) throws IOException, InterruptedException {
@bcalmac
bcalmac / StackOverflowSearchExample.java
Last active Jul 11, 2017
Minimal Selenium example that performs a search on stackoverflow.com
View StackOverflowSearchExample.java
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class StackOverflowSearchExample {
@Test