Skip to content

Instantly share code, notes, and snippets.

@oliverfoster
oliverfoster / bionic.js
Created May 2, 2023 18:57
bionic reading javascript
(() => {
// function for adding bionic style to a node
function process (n) {
if (n._isBionic) return;
const ns = [...n.childNodes]
.filter(n => n.nodeType === n.TEXT_NODE)
.filter(n => n.nodeValue.trim());
if (!ns.length) return;
ns.forEach(function apply(n) {
const pn = n.parentNode;
function multiplyOut (matrix) {
const partLengths = matrix.map(part => part.length) // how large each part is [3, 3, 2]
const subPartIndices = '0'.repeat(matrix.length).split('').map(Number) // how far we've gone in each part [0, 0, 0]
let isEnded = false
const sumsToPerform = []
while (isEnded === false) {
sumsToPerform.push(subPartIndices.reduce((sum, subPartIndex, partIndex) => {
sum.push(matrix[partIndex][subPartIndex])
return sum
}, []))
function csvToJSON(data) {
var rows = [['']];
for (var i = 0, l = data.length, char = data[i], wasInSpeechMarks = false, isInSpeechMarks = false; i < l; char = data[++i]) {
if (wasInSpeechMarks && char !== "," && char !== "\n" && char !== "\r") { // skip after speech mark waiting for new column or row
} else if (isInSpeechMarks && char === '"' && data[i+1] !== '"') { // at end of speechmark block
isInSpeechMarks = false;
wasInSpeechMarks = true;
} else if (isInSpeechMarks || char !== '"' && char !== "," && char !== "\n" && char !== "\r") { // is in speechmarks or not a new colum or row
if (isInSpeechMarks && char === '"' && data[i+1] === '"') i++; // is in speechmarks at a double quote
rows[rows.length-1][rows[rows.length-1].length-1] += char; // add character to current cell
@oliverfoster
oliverfoster / deferred_promise.js
Last active January 1, 2024 20:10
Deferred promise
class DeferredPromise extends Promise {
constructor(def = (res, rej)=>{}) {
let res, rej;
super((resolve, reject)=>{
def(resolve, reject);
res = resolve;
rej = reject;
});
this.resolve = res;
@oliverfoster
oliverfoster / debounce.js
Created December 13, 2017 12:42
Cancellable debounce
function debounce(cb, time) {
var handle = null;
var func = function() {
func.cancel();
handle = setTimeout(cb, time);
};
func.cancel = function() {
if (!handle) return;
clearTimeout(handle);
handle = null;
@oliverfoster
oliverfoster / waitUntil.js
Last active December 13, 2017 12:42
waitUtil
function waitUntil(options) {
options = options || {};
options.timeout = options.timeout || 1000;
var elapsed = 0;
var handle = setInterval(function() {
if (options.rule()) {
if (typeof options.success === "function") options.success();
if (typeof options.complete === "function") options.complete();
return clearInterval(handle);
}
@oliverfoster
oliverfoster / ios10-pinch-and-zoom.js
Last active October 28, 2016 09:34
stop pinch and zoom ios10
//ios10 stop pinch and zoom
function prevent(e) { e.preventDefault() }
document.body.addEventListener('gesturechange', prevent);
document.body.addEventListener('gesturestart', prevent);
document.body.addEventListener('gestureend', prevent);