[jslint] Enable js lint and fix the errors.
[platform/framework/web/tizen-extensions-crosswalk.git] / utils / utils_api.js
1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var signature_to_type = { 'n': 'number',
6                           'f': 'function',
7                           'b': 'boolean',
8                           's': 'string',
9                           'o': 'object'
10                         };
11
12 function Utils() {
13 }
14
15 Utils.prototype.validateArguments = function(signature, args) {
16   var full_args = Array.prototype.slice.call(args);
17
18   // After '?' everything is optional.
19   var mandatory_len = signature.indexOf('?') === -1 ? signature.length : signature.indexOf('?');
20
21   if (full_args.length < mandatory_len)
22     return false;
23
24   // Mandatory arguments.
25   for (var i = 0; i < mandatory_len; i++) {
26     if (typeof full_args[i] !== signature_to_type[signature[i]] || full_args[i] === null)
27       return false;
28   }
29
30   // Optional args may be null.
31   for (var i = mandatory_len; i < full_args.length && i < signature.length - 1; i++) {
32     if (full_args[i] !== null && typeof full_args[i] !== signature_to_type[signature[i + 1]])
33       return false;
34   }
35
36   return true;
37 };
38
39 Utils.prototype.validateObject = function(object, signature, attributes) {
40   for (var i = 0; i < signature.length; i++) {
41     if (object.hasOwnProperty(attributes[i]) &&
42         typeof object[attributes[i]] !== signature_to_type[signature[i]]) {
43       return false;
44     }
45   }
46
47   return true;
48 };
49
50 exports = new Utils();