snacks-js
v1.0.4
Published
Let’s have some snacks!
Downloads
168
Readme
JavaScript snacks
Relax and have a tasty snack.
The need for snacks
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.
Sanity checks
The world can be a stabby, insane place. These sanity checks keep your snacks safe.
Booleans
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.
Numbers
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.
Strings
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.
Arrays
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.
Unitless goodies
Operators
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 ).
Randomness
randomrandomBetweenrandomIntegerrandomIntegerBetween
Misc unitless
clamproundnormalizenormalize01lerpmapRangeaveragecircularAverageratioToQuotientcopySignsignedPower
Geometry (and trigonometry)
PI. Alias for Math.PI.TAU. Math.PI × 2.degreesToRadiansradiansToDegreesradiansToPointsArraywrapToRangenormalizeAnglepolarToCartesianrotateCartesianfindMidpointdistance2D
Color
floatToHexhslToRgb
Metric system
QUETTARONNAYOTTAZETTAEXAPETATERAGIGAMEGAKILOHECTODEKAUNITDECICENTAMILLIMICRONANOPICOFEMTOATTOZEPTOYOCTORONTOQUECTOconvertMetric
Misc
compareArraysByElementPropertytimeAgonumberToFullWidthCharstoSentenceCasetoCamelCasecss
