UNPKG

falcor-router

Version:

A router DataSource constructor for falcor that allows you to model all your cloud data sources as a single JSON resource.

45 lines (39 loc) 1.37 kB
var convertPathToRoute = require('./convertPathToRoute'); var isPathValue = require('./../support/isPathValue'); var slice = require('./../support/slice'); var isArray = Array.isArray; /** * Creates the named variables and coerces it into its * virtual type. * * @param {Array} route - The route that produced this action wrapper * @private */ function createNamedVariables(route, action) { return function innerCreateNamedVariables(matchedPath) { var convertedArguments; var len = -1; var restOfArgs = slice(arguments, 1); var isJSONObject = !isArray(matchedPath); // A set uses a json object if (isJSONObject) { restOfArgs = []; convertedArguments = matchedPath; } // Could be an array of pathValues for a set operation. else if (isPathValue(matchedPath[0])) { convertedArguments = []; matchedPath.forEach(function(pV) { pV.path = convertPathToRoute(pV.path, route); convertedArguments[++len] = pV; }); } // else just convert and assign else { convertedArguments = convertPathToRoute(matchedPath, route); } return action.apply(this, [convertedArguments].concat(restOfArgs)); }; } module.exports = createNamedVariables;