Skip to content

Instantly share code, notes, and snippets.

Avatar
🤓
I'm an explorer not a producer.

Audi Nugraha audinue

🤓
I'm an explorer not a producer.
  • Surabaya, Jawa Timur, Indonesia
View GitHub Profile
View textBBox.js
function textBBox (text, attributes) {
var ns = 'http://www.w3.org/2000/svg'
var svg = document.createElementNS(ns, 'svg')
var svgText = document.createElementNS(ns, 'text')
for (var name in attributes) {
svgText.setAttribute(name, attributes[name])
}
svgText.textContent = text
svg.appendChild(svgText)
svg.style.position = 'fixed'
@audinue
audinue / render.html
Created Dec 12, 2021
Simple DOM diff :P
View render.html
<script src="render.js"></script>
<script>
var a = 0
var b = 0
var c = function () {
return a + b
}
function render (e) {
if (e.type === 'input') {
View data-logic-presentation.html
<script>
// Data abstracts storage
let Data = {
_count: 0,
get count () {
return Data._count
},
set count (value) {
Data._count = value
}
View basic-lisp.js
function parseTokens (code) {
return code
.split(/([()]|"(?:\\.|[^"])*"|;[^\n]*|\s+)/)
.filter(token => !/^;|^\s|^$/.test(token))
}
function parseArray (tokens) {
const array = []
if (tokens.shift() !== '(') {
throw new Error(`Expected '('`)
View tokenizer-example.js
var foo = function (match, offset) {
return { tag: 'foo', offset: offset }
}
var bar = function (match, offset) {
return { tag: 'bar', offset: offset }
}
var baz = function (match, offset) {
return { tag: 'baz', offset: offset }
@audinue
audinue / tiny-curry.min.mjs
Last active Dec 9, 2020
Tiny curry with placeholder support.
View tiny-curry.min.mjs
let __={},curry=_=>{let r=l=>(...e)=>{let t=l.map(_=>_===__?e.length?e.shift():__:_);return t.some(_=>_===__)?r(t):_.apply(null,t)};return r(Array(_.length).fill(__))};export{__,curry}
@audinue
audinue / flow-example.js
Created Dec 5, 2020
Simple flow based programming.
View flow-example.js
import { Flow } from '/path/to/flow.js'
let num = new Flow(1)
.map(x => x + 1)
.map(x => x * 2)
.map(x => x / 8)
console.log(num.get())
@audinue
audinue / example.js
Created Nov 8, 2020
Modify deeply nested immutable object easily.
View example.js
let foo = {
bar: {
baz: 2
}
}
let foo2 = modify(foo, () => {})
console.assert(foo === foo2)
@audinue
audinue / object-map.js
Last active Oct 30, 2020
Iterable safe plain JavaScript object as map.
View object-map.js
function objectMap () {
return Object.defineProperty(Object.create(null), Symbol.iterator, {
configurable: true, // This descriptor is inspired by Map.prototype[Symbol.iterator]
writable: true,
value: function* () {
for (const key in this) {
yield [key, this[key]]
}
}
})
View uid.js
const uid = () => [...crypto.getRandomValues(new Uint8Array(16))]
.map(x => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.charAt(x % 62))
.join('')