Skip to content

Instantly share code, notes, and snippets.

@bcalmac
bcalmac / FilmsOfTatooine.js
Last active May 14, 2021 22:32
Axios with concurrent requests
// 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 September 4, 2020 23:38
sleep function that can be used as "await sleep(1000)"
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
@bcalmac
bcalmac / OvertimeTestUtils.java
Created February 10, 2020 16:06
Load resource to string
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 January 13, 2020 01:56
Test LocalDate JSON serialization / deserialization with Jackson
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 January 13, 2020 01:21
Cofigure Spring type conversion to use ISO-8601 format for dates
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 December 14, 2019 20:25
Determine the list of remainders of powers of a divided by b
-- 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 August 18, 2021 21:28
Jackson serialization for Interval
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 August 2, 2017 21:47
Simple WireMock transformer that converts the content to upper case
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 July 28, 2017 05:40
What happens if stdout is redirected to the same file as a FileAppender?
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 July 11, 2017 22:06
Minimal Selenium example that performs a search on stackoverflow.com
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