Plato on Github
Report Home
src/require/load.js
Maintainability
81.10
Lines of code
63
Difficulty
12.50
Estimated Errors
0.19
Function weight
By Complexity
By SLOC
/** * @file Require resource loading implementation * @since 0.2.2 */ /*#ifndef(UMD)*/ "use strict"; /*global _gpfIgnore*/ // Helper to remove unused parameter warning /*global _gpfPathExtension*/ // Get the extension of the last name of a path (including dot) /*global _gpfRead*/ // Generic read method /*exported _gpfRequireLoad*/ // Load the resource /*exported _gpfRequireProcessor*/ // Mapping of resource extension to processor function /*#endif*/ /* this is globally used as the current context in this module */ /*jshint -W040*/ /*eslint-disable no-invalid-this*/ /** * Mapping of resource extension to processor function * * @type {Object} * @since 0.2.2 */ var _gpfRequireProcessor = {}; function _gpfLoadOrPreload (context, name) { var preload = context.preload[name]; if (preload) { return Promise.resolve(preload); } return _gpfRead(name); } function _gpfLoadTextProcessor (name, content) { _gpfIgnore(name); return Promise.resolve(content); } function _gpfLoadGetProcessor (resource) { return _gpfRequireProcessor[resource.type] || _gpfLoadTextProcessor; } /** * Load the resource * * @param {String} name Resource name * @return {Promise<*>} Resolved with the resource result * @since 0.2.2 */ function _gpfRequireLoad (name) { var me = this; return _gpfLoadOrPreload(me, name) .then(function (content) { return me.preprocess({ name: name, content: content, type: _gpfPathExtension(name).toLowerCase() }); }) .then(function (resource) { return _gpfLoadGetProcessor(resource).call(me, resource.name, resource.content); }); }