Long time writing JS code I collect reusable code fragments into one file for easely copying. Then this collection grows to sizes to be library.
Function is the same value as other types and can be calculated.
All functions use this
and can be used like methods. So calculated functions must use this
and be methods.
Most of used alhorithms use elementary abilities. So ECMAScript5 abilities are not needed. Library can be used with ECMAScript3.
Library architecture means to use library as it is. Without modification of code but using additions. You can change any function or method by modifed or your own one. Also you can extend library and apply to it the inherit pattern.
The library may be written in pure JavaScript but CoffeeScript is simpler, shorter and faster. Not sense to make problems for myself.
Most of code examples written in CoffeeScript. If you have questions about CoffeeScript code you can convert it to JavaScript by webinterface in coffeescript.org.
It is bad idea to modify Function.prototype
, better to use $F
decorator. It create from function new function doing the same but with extend API. So you able to use original function in other parts of code by same way as you done. It allow you to use other function based types with this library.
Was question how to name the function added other API to functions. Also prototype of function must be function else some object oriented abilities not work. Thus I solved this problems by one feature.
Allows after current function execution to call other function with current function result like first argument. It may looks like change order of execution unary functions.
do $F(->"example").then(alert)
works like
alert "example"
Exceptions is the same result of calculating alhoruthm as returned value. More, it is part of API of standart liberis and meny others. So like result it may be returned.
catch
function attaches expeption processor to current function allowing to use execution in following calculations.
parseJSON = $F(JSON.parse).catch ->null
works identical to
parseJSON = (str)->
try JSON.parse str
catch then null
This ECMAScript5 function returns the function value. So better result function have the same API as original. Also, in ECMAScript3 it would be useful too.
Returns the function were applied passed arguments. Other argument you can pass to result.
The same as bingArgs
but rest arguments are ignored.
Catches exceptions by condition.
Catches exception if it equal to passed value.
Catches exception by type.
Repeat passed function until exception.
Repeat function and return array with results.
First argument is times, second is this
.
$F(->"\t").times(10).join ""
works like
(for x in [0...10] then "\t").join ""
Works like times
but return the same function.
Curry function by one argument per call.
Applies the function to argument list and pass result as arguments to current function.
Changes arguments order.
Applies passed function to argument in the same position.
The same preprocess
but not processed arguments ignored.
Checks result by condition. Throw Error
exception on false.
Checks result by type.
Checks each argument by condition functions.
Checks arguments by type list.
Returns function from array list that apply original function for each index until array not finish.
Zip passed arrays by current function.
The same as zip
but use first argument like this
.
The same as zipper
but for objects. Also you can set object for change fields.
The same as zip
for objects.
The same as zipWith
for objects.
The same as zipObjects
but fields change in first argument using rest arguments.
Creates cell object like in some Clojure liberis. Arguments used as relative cells.
Changes order of call off function returning function.
Useful function used in objectZipper
.
Detect the first argument is a instance of second. Works for types as constucrors and types as strings by typeof
.
Creates the cell for setting value.
Creates decorator with the same behavior as $F
. It is the children of $F
Exception used inside the library.