basic-functions
A Simple module which contains common functions to save you time, code, and memory
Install
npm install basic-functions
or
npm install -g basic-functions
Then import the module into your program:
var just =
An simply reference the stored functions
Default Values
justundefined //undefined just //null just //true//or just.True() just //false//or just.False() just //0//or just[0], just['0'], just.Zero just //1//or just[1], just['1'], just.One just //NaN//or just.nan() just //Infinity//or just.infinity() just //-Infinity//or just._infinity() just //[]//or just.array() just //{}//or just.object() just //{}//or just.string() just //function(){}//or just.function() just //this //more detailed example of how 'this' can be usedvar obj = {} f = justthis; === obj; //true
Handlers
Empty Handler
- noop
- noOp
Does nothing
var fn = justnoop;//or just.noOp //undefined
Calling a Function
- call
- run
Returns the result of invoking the first input argument
- Only if the input argument is a
Function
var fn = justcall;//or just.run { return 'a''b''c';} //['a','b','c']
- call.with( args )
Returns the result of invoking the first input argument with args
as the arguments
var fn = justcall;//or just.run.with( 5, 10 ) { return a + b;} { return a * b;} //15 //50
- call.fn( fn )
Returns the result of invoking fn
with the input arguments as the arguments
var fn = justcall;//or just.run.fn( add ) //15
- call.fn( fn ).with( args )
Returns the result of invoking fn
with args
as the arguments
var fn = justcall;//or just.run.fn( add ).with( 5, 10 ) //15 //15
- call.firstFn
Returns the result of invoking the first input argument that is a Function
var fn = justcallfirstFn;//or just.run.firstFn { return 10;} { return 'hi';} //10
- call.firstFn.with( args )
Returns the result of invoking the first input argument that is a Function
with args
as the arguments
var fn = justcallfirstFn;//or just.run.firstFn.with( 5, 10 ) //15
- call.nth( n )
Returns the result of invoking the nth
input argument
Note: Index starts at 0
var fn = justcall;//or just.run.nth(1) //10
- call.nth( n ).with( args )
Returns the result of invoking the nth
input argument with args
as the arguments
var fn = justcall;//or just.run.nth(1).with( 5, 10 ) //15
- call.nth( n ).fn
Returns the result of invoking the nth
input argument that is a Function
Note: Index starts at 0
var fn = justcallfn;//or just.run.nth(1).fn //'hi'
- call.nth( n ).fn.with( args )
Returns the result of invoking the nth
input argument that is a Function
with args
as the arguments
var fn = justcallfn;//or just.run.nth(1).fn.with( 5, 10 ) //50
- call.key( key )
Returns the result of invoking the property key
in the first input argument
var numObj = fn : numFn strObj = fn : strFn fn = justcall; //or just.run.key('fn') //10 //'hi'
- call.key( key ).with( args )
Returns the result of invoking the property key
in the first input argument with args
as the arguments
var multObj = fn : multiply addObj = fn : add fn = justcall; //or just.run.key('fn').with( 5, 10 ) //50 //15
- call.key( key ).inThis
Returns the result of invoking the property key
in this
with the input arguments as the arguments
var fn = justcallinThis;//or just.run.key('fn').inThis.bind( addObj ) //15
- call.key( key ).inThis.with( args )
Returns the result of invoking the property key
in this
with args
as the arguments
var fn = justcallinThis;//or just.run.key('fn').inThis.with( 5, 10 ).bind( addObj ) //15 //15
- call.key( key ).inNth( n )
Returns the result of invoking the property key
in the nth
input argument
var fn = justcall;//or just.run.key('fn').inNth(1) //10
- call.key( key ).inNth( n ).with( args )
Returns the result of invoking the property key
in the nth
input argument with args
as the arguments
var fn = justcall;//or just.run.key('fn').inNth(1).with( 5, 10 ) //15
- call.key( key ).inNth( n ).ofType( type )
Returns the result of invoking the property key
in the nth
input argument of type type
var fn = justcall;//or just.run.key('fn').inNth(1).ofType( Object ) //'hi'
- call.key( key ).inNth( n ).ofType( type ).with( args )
Returns the result of invoking the property key
in the nth
input argument of type type
with args
as the arguments
//can also use strings for the typevar fn = justcall;//or just.run.key('fn').inNth(1).ofType( 'object' ).with( 5, 10 ) //50
Instantiating a Constructor
- instantiate
- new
Returns a new
instance of the first input argument
- Only if the input argument is a
Function
var fn = justinstantiate;//or just.new //[]
- instantiate.with( args )
Returns a new
instance of the first input argument with args
as the arguments
var fn = justinstantiate;//or just.new.with( 'abc' ) //['abc']
- instantiate.fn( fn )
Returns a new
instance of fn
with the input arguments as the arguments
var fn = justinstantiate;//or just.new.fn( Array ) //[5]
- instantiate.fn( fn ).with( args )
Returns a new
instance of fn
with args
as the arguments
var fn = justinstantiate;//or just.new.fn( Array ).with( 5, 10 ) //[5, 10] //[5, 10]
- instantiate.firstFn
Returns a new
instance of the first input argument that is a Function
var fn = justinstantiatefirstFn;//or just.new.firstFn //[]
- instantiate.firstFn.with( args )
Returns a new
instance of the first input argument that is a Function
with args
as the arguments
var fn = justinstantiatefirstFn;//or just.new.firstFn.with( true ) //[ true ]
- instantiate.nth( n )
Returns a new
instance of the nth
input argument
Note: Index starts at 0
var fn = justinstantiate;//or just.new.nth(1) //[]
- instantiate.nth( n ).with( args )
Returns a new
instance of the nth
input argument with args
as the arguments
var fn = justinstantiate;//or just.new.nth(1).with( true ) //[ true ]
- instantiate.nth( n ).fn
Returns a new
instance of the nth
input argument that is a Function
Note: Index starts at 0
var fn = justinstantiatefn;//or just.new.nth(1).fn //''
- instantiate.nth( n ).fn.with( args )
Returns a new
instance of the nth
input argument that is a Function
with args
as the arguments
var fn = justinstantiatefn;//or just.new.nth(1).fn.with( true ) //'true'
- instantiate.key( key )
Returns a new
instance of the property key
in the first input argument
var arrObj = fn : Array strObj = fn : String fn = justinstantiate; //or just.new.key('fn') //[] //''
- instantiate.key( key ).with( args )
Returns a new
instance of the property key
in the first input argument with args
as the arguments
var fn = justinstantiate;//or just.new.key('fn').with( true ) //[ true ]
- instantiate.key( key ).inThis
Returns a new
instance of the property key
in this
with the input arguments as the arguments
var fn = justinstantiateinThis;//or just.new.key('fn').inThis.bind( arrObj ) //[] //[ 'hi' ]
- instantiate.key( key ).inThis.with( args )
Returns a new
instance of the property key
in this
with args
as the arguments
var fn = justinstantiateinThis;//or just.new.key('fn').inThis.with( true ).bind( arrObj ) //[ true ] //[ true ]
- instantiate.key( key ).inNth( n )
Returns a new
instance of the property key
in the nth
input argument
var fn = justinstantiate;//or just.new.key('fn').inNth(1) //[]
- instantiate.key( key ).inNth( n ).with( args )
Returns a new
instance of the property key
in the nth
input argument with args
as the arguments
var fn = justinstantiate;//or just.new.key('fn').inNth(1).with( true ) //[ true ]
- instantiate.key( key ).inNth( n ).ofType( type )
Returns a new
instance of the property key
in the nth
input argument of type type
var fn = justinstantiate;//or just.new.key('fn').inNth(1).ofType( Object ) //''
- instantiate.key( key ).inNth( n ).ofType( type ).with( args )
Returns a new
instance of the property key
in the nth
input argument of type type
with args
as the arguments
var fn = justinstantiate;//or just.new.key('fn').inNth(1).ofType( 'object' ).with( true ); //'true'
Throwing an Error
- throw
- error
Throws the first input argument
var fn = justthrow;//or just.error //throws Error
- throw.error( err )
Throws fn
var fn = justthrow;//or just.error.error( new TypeError ) //throws TypeError
- throw.firstError
Throws the first input argument that is an instanceof Error
var fn = justthrowfirstError;//or just.error.firstError //throws Error
- throw.nth( n )
Throws the nth
input argument
Note: Index starts at 0
var fn = justthrow;//or just.error.nth(1) //throws Error
- throw.nth( n ).error
Throws the nth
input argument that is an instanceof Error
Note: Index starts at 0
var fn = justthrowerror;//or just.error.nth(1).error //throws TypeError
- throw.key( key )
Throws the property key
in the first input argument
var errObj = err : typeErrObj = err : fn = justthrow; //or just.error.key('err') //throws Error //throws TypeError
- throw.key( key ).inThis
Throws the property key
in this
var fn = justthrowinThis;//or just.error.key('err').inThis.bind( errObj ) //throws Error
- throw.key( key ).inNth( n )
Throws the property key
in the nth
input argument
var fn = justthrow;//or just.error.key('err').inNth(1) //throws Error
- throw.key( key ).inNth( n ).ofType( type )
Throws the property key
in the nth
input argument of type type
var fn = justthrow;//or just.error.key('err').inNth(1).ofType( Object ) //throws TypeError
Echoing a Value
- echo
- return
Returns the first input argument
var fn = justecho;//or just.return //10
- echo.value( v )
Returns v
var fn = justechovalue 10 ;//or just.return.value( 10 ) //10 //10
- echo.nth( n )
Returns the nth
input argument
Note: Index starts at 0
var fn = justecho;//or just.return.nth(1) //100
- echo.nth( n ).ofType( type )
Returns the nth
input argument of type type
Note: Index starts at 0
var fn = justecho;//or just.return.nth(1).ofType('string') //'there'
- echo.key( key )
Returns the property key
in the first input argument
var obj = str : 'hi' num : 100 fn = justecho; //or just.return.key('str') //'hi'
- echo.key( key ).inThis
Returns the property key
in this
var fn = justechoinThis;//or just.return.key('str').inThis.bind( obj ) //'hi'
- echo.key( key ).inNth( n )
Returns the property key
in the nth
input argument
var fn = justecho;//or just.return.key('num').inNth(1) //100
- echo.key( key ).inNth( n ).ofType( type )
Returns the property key
in the nth
input argument of type type
var fn = justecho;//or just.return.key('str').inNth(1).ofType( Object ) //'hi'