Skip to content

Instantly share code, notes, and snippets.

View jmakeig's full-sized avatar

Justin Makeig jmakeig

View GitHub Profile
@jmakeig
jmakeig / .csscomb.json
Last active March 29, 2020 19:15
Default IDE formatting options
{
"exclude": [
".git/**",
"node_modules/**",
"bower_components/**"
],
"always-semicolon": true,
"block-indent": "\t",
"color-case": "lower",
"color-shorthand": true,
@jmakeig
jmakeig / nvm-current-link.sh
Created November 27, 2019 02:40
Create soft link to current version of Node as managed by nvm
@jmakeig
jmakeig / arraynode-iterable.js
Created November 20, 2019 00:04
Are ArrayNode instances in MarkLogic iterable? (Yes, by duck typing.)
const letters = xdmp.toJSON(['a', 'b', 'c']).root; // ArrayNode
Array.from(letters); // [Text, Text, Text]
ArrayNode.prototype[Symbol.iterator] = function*() {
for (let i = 0; i < this.length; i++) {
yield this[String(i)];
}
};
@jmakeig
jmakeig / test.config.js
Last active October 7, 2019 18:16
Bundle TypeScript tests for the browser and Node using Rollup, including resolving reported stacktraces with sourcemaps
import multiEntry from 'rollup-plugin-multi-entry';
import nodeResolve from 'rollup-plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
export default {
input: 'src/**/*.test.ts',
output: [
/* Command-line in Node
```shell
rollup -c test.config.js && \
@jmakeig
jmakeig / promise-timeout.js
Created July 9, 2019 21:24
Promise timeout
// https://github.com/github/fetch/issues/175#issuecomment-216791333
function timeoutPromise(ms, promise) {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error('promise timeout'));
}, ms);
promise.then(
res => {
clearTimeout(timeoutId);
resolve(res);
@jmakeig
jmakeig / function-composition.js
Last active July 9, 2019 20:51
Function composition in JavaScript
// https://www.codementor.io/michelre/use-function-composition-in-javascript-gkmxos5mj
function compose(...functions) {
return args => functions.reduceRight((arg, fn) => fn(arg), args);
}
const cntk = {
c1(input, options = {}) {
// console.log('c1', input, options);
return 'c1: ' + input;
},
@jmakeig
jmakeig / pretty-print-json.java
Created May 21, 2019 06:32
Pretty print a Jackson JsonNode to the console
try {
System.out.println(
new ObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(api)
);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
@jmakeig
jmakeig / takeWhile.js
Created April 10, 2019 19:41
takeWhile implementation for JavaScript Iterables
function* takeWhile(iterable /* Sequence, Array, etc. */, predicate = () => true) {
// TODO: Test that iterable is, well, iterable and not something else
for (const item of iterable) {
if (predicate(item)) {
yield item;
} else {
break;
}
}
}
@jmakeig
jmakeig / rollup.config.js
Last active April 6, 2019 18:07
Rollup configuration to bundle a React app, declaring React as an external dependency
import nodeResolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
export default {
/* https://engineering.mixmax.com/blog/rollup-externals */
external: ['react', 'react-dom'],
/*
* Then in the host HTML page:
* <script crossorigin="anonymous" src="//cdn.jsdelivr.net/combine/npm/react@16.8.6/umd/react.development.min.js,npm/react-dom@16.8.6/umd/react-dom.development.min.js"></script>
@jmakeig
jmakeig / log-advice.js
Created April 2, 2019 20:05
Wrap a generator to log its output
/**
* Logs generated values and re-yields them. Keeps the same
* signature and behavior as the wrapped geneator.
*
* @example const myGenerator = logAdvice(function*(…){…})
*
* @param {GeneratorFunction} generator The generator to wrap
* @param {Function} [filter = () => true] Only log yielded values that evaluate to `true`
* @returns {GeneratorFunction} A generator that can be called exactly like the one passed in
*/