tisnthings.js
Smallish set of things I often use. Favors brevity.
Example: type checking with tis(a,b)
(as in "type is"):
//is x a string?//is x an array?//is x a function?//is x a promise?//is x regex?//is x a CustomThing?
tis
works by constructor comparisons, with some quick checks first, so:
//false//true!
type(x)
will give you a string of x a bit more distinctly than typeof
. ([]
is 'array'
!)
Or a quick ways to g
uess random numbers:
//plain Math.random()// 50 / 50 chance truthy//randomly pick an integer between 0 & 100//randomly pick an integer between 50 & 60//pick a floaty decimal between 50 & 60//randomly return a value from the array
array2obj()
, or shorthand a2o(array[,justBeTruthy[,specificValueKey]])
, allowing easy preprocessing of []
s into O(1)-accessible {}
properties instead of using [].indexOf
:
//{1: 1, 2: 1, 3: 1, 4: 1} //values all a truthy 1 //{1: 0, 2: 1, 3: 2, 4: 3} //values ascending //{1:{k:1},5:{k:5},undefined:{z:12}} //key by k, if value has it
and for symmetry's sake (albeit less used), obj2array()
, or o2a(object[,callKey])
:
//[{val:3,key:"f"},{val:9,key:"k"},{val:{Z:"Q"},key:"e"}] //[{val:3,W:"f"},{val:9,W:"k"},{val:{Z:"Q"},W:"e"}]
Some string functions:
//what a human might like seeing:"query_select ColumnName"//"Query Select Column Name"//machine a string to something simpler:"a Goofy nglés sentence!!(#&---"//"a_goofy_ngl_s_sentence" //cookie + smiley face characters"doc brown"//"Doc Brown""Something Else"//"something Else""convert into method name"//"convertIntoMethodName""somethingCamelCased"//["something", "Camel", "Cased"]"pass"//"bass"x//true if x=='a' || x=='b' || x=='c' (case insensitive)
Some constants & simple functions for easy reading:
var aDay=24*3600*1000 aPromise={} {} {} {return a<b?1:-1} {return a>b?1:-1} {return a>b?1:a==b?0:-1}
Sometimes it's worth insisting you have an array:
//x might not be an array//but it definitely is now
Give a function some inputs, but don't execute just yet:
var gp=g//...later//g(1,10)
And all Math.*
functions are brought into the global scope.
///etc