Skip to content

Instantly share code, notes, and snippets.

View jmakeig's full-sized avatar

Justin Makeig jmakeig

View GitHub Profile
@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
*/
@jmakeig
jmakeig / _README.md
Last active March 15, 2019 06:47
Single pass annotation rendering

There are libraries that can add highlights to existing markup. Because HTML does not allow for overlapping elements, for example V<a>W<b>X</a>Y</b>Z, these libraries split inline elements across boundaries, nesting part of overlapping portion under a sibling element. The previous example would be split as V<a>W</a><b><a>X</a>Y</b>Z. Note the <b><a>X</a>… designates the leftover portion of a that overlaps with b.

The objective here is to be able to render the content and the higlights in one pass. Rather than modifying the rendered content in situ in with highlights in a second pass, render the content and the highlights in a single pass. This is how you’d do it in React, for example.

@jmakeig
jmakeig / optic-random.sjs
Last active December 18, 2018 19:45
Get lexicon values from a random sample of documents based on a query in MarkLogic
'use strict';
const op = require('/MarkLogic/optic');
function* randomURIs(count = 1) {
yield* op
.fromLexicons({
uri: cts.uriReference()
})
.select(['uri', op.as('rand', op.xdmp.random())])
@jmakeig
jmakeig / xpath.sjs
Last active December 13, 2018 21:11
Defensive MarkLogic XPath generator function
function* xpath(doc, path = '.', bindings = {}) {
// Only if you want path to be relative to the root element.
// Probably not what you want.
// if (doc instanceof Document) {
// yield* xpath(doc.root, path, bindings);
// } else
if (doc instanceof Node) {
yield* doc.xpath(path, bindings);
}
// Becuase this is a Generator, it’s probably better just to
@jmakeig
jmakeig / documentPermissions.sjs
Last active November 19, 2018 20:40
Get the permissions of a MarkLogic document by URI, including role names, instead of IDs
'use strict';
const sec = require('/MarkLogic/security');
function role(id) {
return fn
.head(
xdmp.invokeFunction(() => sec.getRoleNames(id), {
database: xdmp.database('Security')
})
)
@jmakeig
jmakeig / xml-ttl.js
Last active December 14, 2018 18:29
Tagged template literal to build XML Nodes in MarkLogic.
'use strict';
function xml(strings, ...values) {
let str = '';
for (let i = 0; i < strings.length; i++) {
str += strings[i] + (values[i] || '');
}
return fn.head(
xdmp.unquote(str, null, 'format-xml') // Sequence
); // Document
@jmakeig
jmakeig / class-factory.js
Created September 8, 2018 16:58
Factory for classes using a closure to set initial state.
'use strict';
class Component {
constructor(props) {
this._props = props;
}
get props() {
return this._props;
}
set props(p) {
this._props = p;