Walking never felt so easy.
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 Shoes, then? Shoes 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), Shoes 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 Shoes can no longer afford this approach. But the individual logic bits are still so useful—so cozy—that once you try them, you won’t want to give them up. And this toolkit is so lightweight; it’s just the goodies.
The world can be a stabby, insane place. Put on your shoes, and keep your sanity with these type helpers.
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.
clamp
,
round
,
normalize
,
normalize01
,
lerp
,
mapRange
,
average
,
circularAverage
,
random
,
randomBetween
,
randomInteger
,
randomIntegerBetween
,
ratioToQuotient
,
copySign
,
signedPower
,
degreesToRadians
,
radiansToDegrees
,
radiansToPointsArray
,
wrapToRange
,
normalizeAngle
,
polarToCartesian
,
rotateCartesian
,
findMidpoint
,
distance2D
,
floatToHex
,
hslToRgb
,
compareArraysByElementProperty
,
timeAgo
,
numberToFullWidthChars
,
toSentenceCase
,
toCamelCase