Relax and have a tasty snack.
I love JavaScript. It’s an often misunderstood language, full of beauty and flexibility. “Minutes to learn, lifetime to master.” Just about everything in JavaScript is an Object. In fact, JavaScript is more object-oriented than most so-called “OO” programming languages. But for some reason its dynamic typing and superior prototypal inheritance confuses those crusty-compiler naysayers, who only believe in strongly-typed, classically-inherited cruft. Dinosaurs. Inspired by Lisp and Scheme, “LiveScript” (One of JavaScript’s early monikers) had first-class functions and lambdas from the start. Given how speedily it was crafted, and all the ways the Web has careened right off the rails… It’s a better language than we perhaps deserve.
So why Snacks, then? Snacks is a small collection of constants, methods, and conveniences that I have needed with such frequency over the past two decades (Yes—I’ve been doing this a long time), that it made sense to bundle this grab-bag of logic together into something more modularly useful. In its first incarnations (before modules and Node even existed), Snacks just blissfully augmented built-in language prototypes which made for incredibly pleasant function-chaining like so:
( 2 + 3 )
.multiply( 4.5 )
.round()
.toString()
.prepend( 'Our magic number:' )
.print()Sadly, augmenting the built-in prototypes has become so frowned upon (and no one has bothered to sandbox / scope it yet), that Snacks can no longer afford this approach. But the individual logic bits are still so useful—so tasty—that once you pop, you can’t stop. And this toolkit is so lightweight; it’s just the goodies. No heavy meals.
The world can be a stabby, insane place. These sanity checks keep your snacks safe.
isUsefulBoolean( b ).
Not null or undefined or NaN or anything else. Strictly true or false. Note that instance of Boolean and typeof b === 'boolean' would not do the trick here. (See the code comments for details.) This is the simplest sanity check and already JavaScript has gotchas.
isNotUsefulBoolean( b ).
Inverts the above logic.
isUsefulNumber( n ). Returns true if n is a numeric, finite number. It’s just a hair trickier than you’d expect. But we’ve got you covered.
isNotUsefulNumber( n ). Inverts the above logic.
isUsefulInteger( n ).
Adds integer-checking to isUsefulNumber.
isNotUsefulInteger( n ).
Inverts the above logic.
isString( s ).
Is it? (Does not include Template literals.)
isEmptyString( s ).
Is it a String with a length of exactly zero?
isNonEmptyString( s ).
Looking for isString and a length greater than zero.
isUsefulString( s ).
Currently this is pegged to isNonEmptyString, though I could see arguments for otherwise.
isNotUsefulString( s ).
Inverts the above logic.
isArray( a ).
isNotArray( a ).
isEmptyArray( a ).
isNotEmptyArray( a ).
isUsefulArray( a ).
arrayCount. What’s the difference between your grandma’s Array.prototype.length and arrayCount? We only count the defined entries—very useful for determining how many elements are actually in a sparse Array.
How about some higher order logic that feels a bit like Lisp / Scheme? Bonus: These functions allow you to juggle relative numeric values in addition to your normal, every day, absolutely-defined values.
OPERATORS. Object containing operator methods (and their common symbols / names) for the usual suspects like addition, subtraction, and so on.
operate( operatorObject, ...args ).
parseRelativeNumber( a ).
applyRelativeNumber( a ).
random
randomBetween
randomInteger
randomIntegerBetween
clamp
round
normalize
normalize01
lerp
mapRange
average
circularAverage
ratioToQuotient
copySign
signedPower
PI. Alias for Math.PI.
TAU. Math.PI × 2.
degreesToRadians
radiansToDegrees
radiansToPointsArray
wrapToRange
normalizeAngle
polarToCartesian
rotateCartesian
findMidpoint
distance2D
floatToHex
hslToRgb
QUETTA
RONNA
YOTTA
ZETTA
EXA
PETA
TERA
GIGA
MEGA
KILO
HECTO
DEKA
UNIT
DECI
CENTA
MILLI
MICRO
NANO
PICO
FEMTO
ATTO
ZEPTO
YOCTO
RONTO
QUECTO
convertMetric
compareArraysByElementProperty
timeAgo
numberToFullWidthChars
toSentenceCase
toCamelCase
css