UNPKG

1.14 kBJavaScriptView Raw
1'use strict';
2
3var Options = require('./options');
4
5module.exports = normalizeArgs;
6
7/**
8 * Normalizes the given arguments, accounting for optional args.
9 *
10 * @param {Arguments} args
11 * @returns {object}
12 */
13function normalizeArgs (args) {
14 var path, schema, options, callback;
15 args = Array.prototype.slice.call(args);
16
17 if (typeof args[args.length - 1] === 'function') {
18 // The last parameter is a callback function
19 callback = args.pop();
20 }
21
22 if (typeof args[0] === 'string') {
23 // The first parameter is the path
24 path = args[0];
25 if (typeof args[2] === 'object') {
26 // The second parameter is the schema, and the third parameter is the options
27 schema = args[1];
28 options = args[2];
29 }
30 else {
31 // The second parameter is the options
32 schema = undefined;
33 options = args[1];
34 }
35 }
36 else {
37 // The first parameter is the schema
38 path = '';
39 schema = args[0];
40 options = args[1];
41 }
42
43 if (!(options instanceof Options)) {
44 options = new Options(options);
45 }
46
47 return {
48 path: path,
49 schema: schema,
50 options: options,
51 callback: callback
52 };
53}