Skip to content

Instantly share code, notes, and snippets.

View jmakeig's full-sized avatar

Justin Makeig jmakeig

View GitHub Profile
@jmakeig
jmakeig / machine.js
Last active January 4, 2020 03:39
Generated by XState Viz: https://xstate.js.org/viz
function clone(...rest) {
return Object.assign({}, ...rest);
}
/**
* Builder for confirmation states.
*
* @param {String} onConfirm State to transition to on confirmation
* @param {String} onCancel State to transition to on cancel
*/
@jmakeig
jmakeig / machine.js
Created December 27, 2019 07:56
Generated by XState Viz: https://xstate.js.org/viz
const config = {
id: 'annotation',
initial: 'unselected',
context: {
id: null,
annotation: null,
errorMessage: null
},
states: {
unselected: {
@jmakeig
jmakeig / machine.js
Last active December 24, 2019 08:58
Generated by XState Viz: https://xstate.js.org/viz
function fetchAnnotation(id) {
//return Promise.reject('oops!');
return Promise.resolve({
id,
comment: 'Dummy resolved',
user: 'jmakeig'
});
}
function confirmCancel(message = 'You sure?') {
@jmakeig
jmakeig / machine.js
Last active December 18, 2019 07:38
Generated by XState Viz: https://xstate.js.org/viz
// Actions are on transitions
// Entry/exit are on states
// Mocks for testing services: https://medium.com/@tahini/how-to-effortlessly-model-async-react-with-xstates-invoke-4c36dc8547b3
const dirty = {
initial: 'clean',
states: {
clean: {
@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;
},