Skip to content

Instantly share code, notes, and snippets.

View Sheraff's full-sized avatar

Flo Sheraff

View GitHub Profile
const privateKey = Symbol('secret')
class SymbolPrivate {
constructor() {
this[privateKey] = 'SECRET'
this.publicKey = 42
}
get privateKey() {
return this[privateKey]
}
@Sheraff
Sheraff / IdlePromise.js
Last active May 27, 2021 03:51
Idle-until-urgent with generators and promises
export default class IdlePromise {
static duration = Symbol('Next yield duration')
static onUrgent = Symbol('Callbacks when finish()')
static padding = 1
promise = new Promise((resolve, reject) => {
this.resolve = resolve
this.reject = reject
})
then = this.promise.then.bind(this.promise)
@Sheraff
Sheraff / circular-data-median.js
Created August 9, 2020 15:19
Implementation of median angle function — A More Efficient Way Of Obtaining A Unique Median Estimate For Circular Data — 2003 / B. Sango Otieno & Christine M. Anderson-Cook
/**
*
* Implementation of median angle function
* A More Efficient Way Of Obtaining A Unique Median Estimate For Circular Data
* 2003 / B. Sango Otieno & Christine M. Anderson-Cook
* from the annexes in https://digitalcommons.wayne.edu/cgi/viewcontent.cgi?referer=&httpsredir=1&article=1738&context=jmasm
*
*/
/**
@Sheraff
Sheraff / isometric_matrix_iteration.js
Created August 20, 2020 13:26
takes a 2D matrix and iterates over it in isometric order (diagonal by diagonal)
const nbColumns = 5
const nbRows = 5
const matrix = new Array(nbColumns)
.fill(null)
.map(() => new Array(nbRows)
.fill(null)
.map(() => 0)
)
export default function (matrix, start, goal) {
return aStar(
matrix,
start,
goal,
(cell) => heuristic(cell, goal),
distance,
)
}
@Sheraff
Sheraff / nothing.js
Created February 2, 2021 20:42 — forked from joakim/nothing.js
The concept of nothing in JavaScript (Crockford's idea of a better undefined)
const nothing = new Proxy(Object.freeze(Object.create(null)), Object.create(null, {
get: { value(target, property) {
return property === Symbol.toPrimitive ? () => null : nothing }
}
}))
nothing.foo === nothing // true
nothing.foo.bar.baz[42].qux // nothing forever
typeof nothing === "object" // true
@Sheraff
Sheraff / sendBeacon.js
Last active July 3, 2021 08:38
send GET request even if user leaves page w/ `sendBeacon`
// last reliably fired event before a user leaves
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
// queues up a request to be sent even if the user leaves
navigator.sendBeacon('/log', analyticsData)
}
})
@Sheraff
Sheraff / stack-overflow-react-snippet.md
Last active June 26, 2021 15:05
Stack Overflow code snippet with React hooks
const App = (props) => {
  var [count, setCount] = React.useState(0)

return (

@Sheraff
Sheraff / codeswing.json
Last active July 2, 2021 06:55
Find all reachable cells
{
"scripts": [],
"styles": [],
"showConsole": true
}
@Sheraff
Sheraff / charsets.js
Last active June 27, 2021 08:18
Visual character count: filter out unicode marks
export const NON_SPACING_MARKS = new Set(['\u0300', '\u0301', '\u0302', '\u0303', '\u0304', '\u0305', '\u0306', '\u0307', '\u0308', '\u0309', '\u030A', '\u030B', '\u030C', '\u030D', '\u030E', '\u030F', '\u0310', '\u0311', '\u0312', '\u0313', '\u0314', '\u0315', '\u0316', '\u0317', '\u0318', '\u0319', '\u031A', '\u031B', '\u031C', '\u031D', '\u031E', '\u031F', '\u0320', '\u0321', '\u0322', '\u0323', '\u0324', '\u0325', '\u0326', '\u0327', '\u0328', '\u0329', '\u032A', '\u032B', '\u032C', '\u032D', '\u032E', '\u032F', '\u0330', '\u0331', '\u0332', '\u0333', '\u0334', '\u0335', '\u0336', '\u0337', '\u0338', '\u0339', '\u033A', '\u033B', '\u033C', '\u033D', '\u033E', '\u033F', '\u0340', '\u0341', '\u0342', '\u0343', '\u0344', '\u0345', '\u0346', '\u0347', '\u0348', '\u0349', '\u034A', '\u034B', '\u034C', '\u034D', '\u034E', '\u034F', '\u0350', '\u0351', '\u0352', '\u0353', '\u0354', '\u0355', '\u0356', '\u0357', '\u0358', '\u0359', '\u035A', '\u035B', '\u035C', '\u035D', '\u035E', '\u035F', '\u0360', '\u0361', '\