Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arcollector/93d9cd99057851e0437c9614a56e4f97 to your computer and use it in GitHub Desktop.
Save arcollector/93d9cd99057851e0437c9614a56e4f97 to your computer and use it in GitHub Desktop.
import R from 'ramda';
const createElement = (tag: String): HTMLElement =>
document.createElement(tag);
const createTextElement = (text: String): HTMLElement =>
document.createTextNode(text);
const setElementText = (text: String, elem: HTMLElement): HTMLElement => {
R.compose(
R.curry(appendTo)(elem),
createTextElement,
)(text);
return elem;
};
const setElementAttrs = (attrs: Object, elem: HTMLElement): HTMLElement => {
for(let [key, value] of Object.entries(attrs)) {
elem.setAttribute(key, value);
}
return elem;
};
const getBodyElement = (): HTMLElement =>
document.body
const appendTo = (parent: HTMLElement, child: HTMLElement) =>
parent.appendChild(child);
const createArrayBuffer = (size: Number) =>
new Uint8Array(size);
const arrayBufferToBlob = (type: String, arrayBuffer) =>
new Blob([arrayBuffer], {type});
const blobToURLObject = (blob) =>
URL.createObjectURL(blob);
document.addEventListener('DOMContentLoaded', function() {
/* @flow */
let trolo: Number = 'false';
R.compose(
R.curry(appendTo)(getBodyElement()),
R.curry(setElementAttrs)({
download: 'image.bmp',
href: R.compose(
blobToURLObject,
R.curry(arrayBufferToBlob)('application/octet-binary'),
)(createArrayBuffer(10)),
}),
R.curry(setElementText)('Download BMP file!'),
createElement,
)('a');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment