pfunction
Libary to manage parameters on functions (including options, typecheck, enumerations)
Motivation
If you build a libary so other user can use the libary to access your data or to use your algorithms (remotly or local) you will need to provide access to this during function calls (and parameters)
Traditionally this will look like
You will end up in problems, cause these names are not managed or typed. It's just the first, second .. parameter.
Most interfaces therefor will follow a pattern where all parameters are named elements of an object.
Mutch better, because now the parameters have names. But there are still a lot of problems
- which options can be used
- which values are allowed
- is the value needed
Some libarys use special functions like assign
or extend
to manage the parameter preparation. But if it comes to type or required parameters...
pfunction is a more complete idea
Note: You will not use pfunction for all your functions, but if these functions are called by external libarys / systems this will build a complete new layer of security to your interfaces
Usage
you create a function by executing the main pFunction with a parameter describing the parameters for this function and the function itself (named '_')
var pFunction = ; var makeFancyStuff = ;
pFunction functions
The pFunction object also contains constructors to describe the parameters.
pFunction.p()
Creates a Parameter. Start here to define a Parameter
pFunction.String(<default>)
Shortcut for pFunction.p().isString()
, set optional value is provided.
pFunction.Number(<default>)
Shortcut for pFunction.p().isNumber()
, set optional value is provided.
pFunction.Enum([<value>,<value>,...])
Shortcut for pFunction.p().enumeration(<default>)
parameter functions (can be piped)
parameter.min(min)
If parameter is number:
- parameter must be greater or equal to If parameter is string:
- parameterstring must be at least characters long
parameter.max(max)
If parameter is number:
- parameter must be less or equal to If parameter is string:
- parameterstring must be shorter than characters
parameter.isString()
Parameter needs to be a string
parameter.isNumber()
Parameter needs to be a number
parameter.enumeration(<array>)
Parameter needs to be ONE from the collection of values provided
parameter.optional(<value>)
Set parameter to optional, set default value to <value>
if parameter is not provided.