Skip to content

Instantly share code, notes, and snippets.

View jmakeig's full-sized avatar

Justin Makeig jmakeig

View GitHub Profile
@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;

JSX is not JavaScript. It introduces new syntax to JavaScript as sugar for React’s underlying JavaScript APIs. JSX is problematic—to me, at least—for several reasons. It requires,

  • An extra compilation step
  • Specialized support in your IDE
  • Some mind bending in the fuzzy areas that aren’t quite JavaScript and aren’t quite HTML, for example, the awful <> tag
  • Lock-in to Facebook’s view of the web

The creatively named react-helper library, provides a humane API for creating DOM elements using React that abstracts away React altogether. It’s possible to interoperate with React components, but for pure DOM manipulation, the API does not leak the underlying React implementation.

@jmakeig
jmakeig / about.md
Created August 28, 2018 07:10
Hierarchical table headers, both row and column orientations

Converting a hierarchical tree data structure to HTML table headings is really tricky. Becuase of the way that rowspan and colspan work, it’s not striaghtforwad to traverse the tree in one pass. Moreover, the row-oriented apporach is very different than column-oriented.

Demo

@jmakeig
jmakeig / excel-keyboard-shortcuts.md
Last active March 16, 2020 20:50
Excel keyboard shortcuts
  • Select row: Shift + Space
  • Insert a copy above: Ctl + Shift + =
  • Insert empty rows above: ⌘ + Shift + =
  • Re-order rows: Drag + Shift
  • Indent: ?
@jmakeig
jmakeig / several.js
Created March 19, 2018 19:32
Create arrays of random length, sampled from a specified distribution
/**
* Generate an array from random length between `min` and `max`, calling `fct`
* to populate each item.
*
* @example Creates an array of random length sampled from a Poisson distribution of timestamp-text pair objects.
* several(
* () => ({
* timestamp: f.date.recent(),
* text: f.lorem.sentence(),
* }),