Simple Way to Validate CSS Colors
function isColor(col) {
const cache = isColor[col]
if (cache != null) {
console.log('- reading cache')
return cache
}
isColor.el.style = ''
isColor.el.style.color = col
return isColor[col] = !!isColor.el.style.color
}
isColor.el = document.createElement('div')
console.log('is "red" a color?', isColor('red'))
console.log('from the cache: ', isColor('red'))
console.log('is "rgbx(1, 2, 3)" a color?', isColor('rgbx(1, 2, 3)'))
console.log('is "#0f0" a color?', isColor('#0f0'))
console.log('is "hsl(192, 50%, 50%)" a color?', isColor('hsl(192, 50%, 50%)'))
console.log('from the cache: ', isColor('hsl(192, 50%, 50%)'))
console.log('is "lab(2000.1337% -8.6911 -159.131231 / .987189732)" ?',
isColor('lab(2000.1337% -8.6911 -159.131231 / .987189732)'))
console.log('is "snippetZone" ?', isColor('snippetZone'))
I find this technique is usually good enough to validate colors…
Other Gates from NAND
const nand = ([a, b]) => +!(a & b)
const not = ([a]) => nand([a, a])
const and = ([a, b]) => nand([nand([a, b]), nand([a, b])])
const or = ([a, b]) => nand([nand([a, a]), nand([b, b])])
const nor = ([a, b]) =>
nand([
nand([nand([a, a]), nand([b, b])]),
nand([nand([a, a]), nand([b, b])])
])
const xor = ([a, b]) =>
nand([
nand([a, nand([a, b])]),
nand([b, nand([a, b])])
])
const xnor = ([a, b]) =>
nand([
nand([nand([a, a]), nand([b, b])]),
nand([a, b])
])
const inputs = [
[0, 0],
[0, 1],
[1, 0],
[1, 1]
]
const testGate = ({ gate, truth, result }) => console.log(
gate + ' matches truth? ',
truth+'' === result+'' ?
'yes :D' : `nope :( ${truth} ${result}`
)
testGate({
gate: 'NAND',
truth: [1, 1, 1, 0],
result: inputs.map(nand)
})
testGate({
gate: 'NOT',
truth: [0, 1],
result: [[1], [0]].map(not)
})
testGate({
gate: 'AND',
truth: [0, 0, 0, 1],
result: inputs.map(and)
})
testGate({
gate: 'OR',
truth: [0, 1, 1, 1],
result: inputs.map(or)
})
testGate({
gate: 'NOR',
truth: [1, 0, 0, 0],
result: inputs.map(nor)
})
testGate({
gate: 'XOR',
truth: [0, 1, 1, 0],
result: inputs.map(xor)
})
testGate({
gate: 'XNOR',
truth: [1, 0, 0, 1],
result: inputs.map(xnor)
})
Use NAND to create a bunch of other gates đ – I used this wikipedia article for reference
Multiplicative Persistence
const multp = (val, count = 1, res) =>
(res = (val + '').split``
.reduce((a, b) => a * b, 1) + '').length > 1 ?
multp(res, count + 1) : count
console.log('test:', multp(2678789))
Started watching this youtube video from numberphile and instantly made this half-golfed thing
Found this:
f=n=>[n,...n>9?f(eval([...n+''].join`*`)):[]]
By Arnauld over at codegolf.stackexchange
will definitely remember: [...n+'']
Parametric Equation for Rectangle
// from http://math.stackexchange.com/questions/69099/equation-of-a-rectangle
const rect = (px, py, rx, ry, t) => ({
x: px + rx + rx * (Math.abs(Math.cos(t)) * Math.cos(t) + Math.abs(Math.sin(t)) * Math.sin(t)),
y: py + ry + ry * (Math.abs(Math.cos(t)) * Math.cos(t) - Math.abs(Math.sin(t)) * Math.sin(t))
})
const SIZE = 200
const c = document.body.appendChild(
Object.assign(document.createElement`canvas`,
{ width: SIZE, height: SIZE }
)).getContext`2d`
c.fillStyle = 'black'
c.fillRect(0, 0, SIZE, SIZE)
let t = 0
setInterval(() => {
const { x, y } = rect(20, 20, 60, 70, t)
c.fillStyle = 'rgba(255, 0, 0, .1)'
c.fillRect(x, y, 10, 10)
t += .05
}, 16)
Wanted to know how to do this for something back in 2015. Great math stackexchange answer here: http://math.stackexchange.com/questions/69099/equation-of-a-rectangle
Could be optimized but leaving as is to match:
x = p(|cos t|cos t + |sin t| sin t)
y = p(|cos t|cos t - |sin t| sin t)
One small change here is to add the width and height to the offset so that it draws from the upper left hand corner instead of the center…
Sort Local Git Branches by Date
git for-each-ref --sort=-committerdate refs/heads/
# Or using git branch (since version 2.7.0)
git branch --sort=-committerdate # DESC
git branch --sort=committerdate # ASC
From this stackoverflow post: https://stackoverflow.com/a/5188364/502848
The above is very useful (who knows maybe SourceTree can do it too).