Hi all,
The implementation of loading function libraries to authentication script
is changed according to the given suggestion. Even though *require() * is
not part of the standard JavaScript API, but it is in Node.js to load
modules. Since this requirement is also similar to that, using require() is
intuitive.
Here, a separate javascript function is written to define the require()
function. That function is stored in a separate file and it is loaded when
the authentication framework bundle is activating. Then it is evaluated
just before evaluating the authentication script, by the same nashorn
engine which evaluates the authentication script.
In order to avoid namespace clashing issues, each function library needs to
be exported and imported explicitly.
There are three options to write function libraries.
1. Write multiple functions in one file and export them. Following syntax
needs to be used to export the functions.
module.exports.<function_name_for_outside> =
<function_name_in _the_script>
or
exports.<function_name_for_outside> = <function_name_in _the_script>
ex:
function getAge(birthDate) {
var today = new Date();
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
};
var validateDOB = function (dob) {
return dob.match(/^(\d{4})-(\d{2})-(\d{2})$/);
};
module.exports.getAge = getAge;
//or
//exports.getAge = getAge;
module.exports.validateDOB = validateDOB;
//or
//exports.validateDOB = validateDOB;
2. Create an object and write functions inside the object. Then export the
whole object using
*module.exports = <object> *syntax.
ex:
var ageModule = {
getAge : function (birthDate) {
var today = new Date();
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
},
validateDOB : function (dob) {
return dob.match(/^(\d{4})-(\d{2})-(\d{2})$/);
}
};
module.exports = ageModule;
3. Create an object and attach functions to it. Then export the whole
object using , *module.exports = <object> *syntax.
var ageModule = { };
ageModule.getAge = function (birthDate) {
var today = new Date();
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
};
ageModule.validateDOB = function (dob) {
return dob.match(/^(\d{4})-(\d{2})-(\d{2})$/);
};
module.exports = ageModule;
When importing to authentication script, following syntanx needs to be
used. Moreover, multiple function libraries can be imported to a single
auth. script using multiple require functions.
*var <object_name> = require('<function_library_name>');*
Then you can call functions of the imported function library, inside the
authentication script as,
*<object_name>.<function _name>*
ex:
var ageModule = require('age_based.js');
usage:
ageModule.getAge(birthday);
The following flowchart demonstrates the whole function of the loading
function library mechanism.
[image: require.png]
Regards,
*Anuradha Karunarathna*
Intern-Software Engineering | WSO2,inc.
On Thu, Oct 11, 2018 at 6:36 PM Ruwan Abeykoon <[email protected]> wrote:
> Hi Anuradha,
> I think require() [1] function better suited for this.
> Reason is that many other dynamic language based on JS uses it and seems
> intuitive.
>
> [1]
> https://stackoverflow.com/questions/9901082/what-is-this-javascript-require
>
> Cheers,
> Ruwan
>
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev