Skip to content

Commit

Permalink
[NODEJS-1767] Make type dectection way better
Browse files Browse the repository at this point in the history
- Make type dectection way better
- Apply JSHint into practice
  • Loading branch information
realpaul committed Oct 29, 2014
1 parent 32c0dc3 commit f39d779
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 17 deletions.
54 changes: 52 additions & 2 deletions Gruntfile.js
@@ -1,17 +1,67 @@
var spawn = require('child_process').spawn,
BIN = './node_modules/.bin/';

module.exports = function(grunt) {
// Configure Grunt
grunt.initConfig({
jshint: {
options: {
jshintrc: true
},
src: ['lib/**/*.js']
src: ['lib/**/*.js', '*.js']
},
clean: {
pre: ['*.log'],
post: ['tmp']
}
});

// Register task of mocha test
grunt.registerTask('mocha', 'mocha test', function() {
var done = this.async();
// var proc = spawn(BIN + 'mocha', ['--bail']);
var proc = spawn(BIN + 'mocha');
proc.stdout.on('data', function(data) {
process.stdout.write(data);
});
proc.stderr.on('data', function(data) {
process.stderr.write(data);
});
proc.on('close', function(code) {
if (code !== 0) {
grunt.fail.fatal('fail');
} else {
grunt.log.ok('done');
}
done();
});
});

// Register task of test coverage
grunt.registerTask('coverage', 'generate test coverage report', function() {
var done = this.async();
var proc = spawn(BIN + 'istanbul', ['cover', '--root', 'lib', '_mocha', '--', '-R', 'spec']);
proc.stdout.on('data', function(data) {
process.stdout.write(data);
});
proc.stderr.on('data', function(data) {
process.stderr.write(data);
});
proc.on('close', function(code) {
if (code !== 0) {
grunt.fail.fatal('fail');
} else {
grunt.log.ok('done');
}
done();
});
});

// Load grunt plugins
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');

// Register tasks
grunt.registerTask('default', ['jshint']);
grunt.registerTask('default', ['clean:pre', 'mocha', 'jshint', 'clean:post']);
grunt.registerTask('cover', ['clean:pre', 'coverage', 'clean:post']);
};
2 changes: 1 addition & 1 deletion lib/collection.js
Expand Up @@ -6,7 +6,7 @@ var acsCollection = {
created_at: 'date',
custom_field: 'string',
email: 'string',
external_account: 'string',
external_account: 'array',
first_name: 'string',
id: 'object',
last_name: 'string',
Expand Down
18 changes: 13 additions & 5 deletions lib/rest.js
@@ -1,5 +1,6 @@
var _ = require('lodash'),
request = require('request'),
u = require('./util'),
ACSError = require('./acsError'),
messages = require('./messages');

Expand Down Expand Up @@ -88,18 +89,23 @@ var methodCall = function(options, callback) {
// console.log('appOptions: %j', options.appOptions);
// console.log('restOptions: %j', options.restOptions);

var reqBody = {};
var reqBody = {},
requiredType,
actualType;
for (var i = 0; i < options.requiredParams.length; i++) {
var requiredParam = options.requiredParams[i];
if (!options.restOptions[requiredParam.key]) {
return callback(new ACSError(messages.ERR_MISS_REQUIRED_PARAMETER, {
parameter: requiredParam
}));
} else if (typeof options.restOptions[requiredParam.key] !== requiredParam.type) {
}
requiredType = requiredParam.type;
actualType = u.typeOf(options.restOptions[requiredParam.key]);
if (requiredType !== actualType) {
return callback(new ACSError(messages.ERR_WRONG_PARAMETER_TYPE, {
typeName: requiredParam.key,
requiredType: requiredParam.type,
actualType: typeof options.restOptions[requiredParam.key]
actualType: actualType
}));
} else {
reqBody[requiredParam.key] = options.restOptions[requiredParam.key];
Expand All @@ -108,11 +114,13 @@ var methodCall = function(options, callback) {
for (var j = 0; j < options.optionalParams.length; j++) {
var optionalParam = options.optionalParams[j];
if (options.restOptions[optionalParam.key]) {
if (typeof options.restOptions[optionalParam.key] !== optionalParam.type) {
requiredType = optionalParam.type;
actualType = u.typeOf(options.restOptions[optionalParam.key]);
if (requiredType !== actualType) {
return callback(new ACSError(messages.ERR_WRONG_PARAMETER_TYPE, {
typeName: optionalParam.key,
requiredType: optionalParam.type,
actualType: typeof options.restOptions[optionalParam.key]
actualType: actualType
}));
} else {
reqBody[optionalParam.key] = options.restOptions[optionalParam.key];
Expand Down
Empty file removed lib/test.js
Empty file.
30 changes: 30 additions & 0 deletions lib/util.js
Expand Up @@ -2,4 +2,34 @@ var capitalizedString = function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};

var typeOf = function(value) {
if (null === value) {
return 'null';
}
var type = typeof value;
if ('object' === type) {
var typeString = Object.prototype.toString.call(value);
switch (typeString) {
case '[object Array]':
return 'array';
case '[object Date]':
return 'date';
case '[object Boolean]':
return 'boolean';
case '[object Number]':
return 'number';
case '[object Function]':
return 'function';
case '[object RegExp]':
return 'regexp';
case '[object Object]':
return 'object';
default:
return 'unknown';
}
}
return type;
};

module.exports.capitalizedString = capitalizedString;
module.exports.typeOf = typeOf;
19 changes: 10 additions & 9 deletions package.json
Expand Up @@ -27,19 +27,20 @@
"url": "https://github.com/appcelerator/acs-node-sdk"
},
"scripts": {
"test": "NODE_PATH=lib:test istanbul cover --root lib _mocha -- -R spec"
"test": "grunt"
},
"dependencies": {
"request": "~2.40.0",
"tough-cookie": "~0.12.1",
"lodash": "~2.4.1"
"lodash": "^2.4.1",
"request": "^2.47.0",
"tough-cookie": "^0.12.1"
},
"devDependencies": {
"mocha": "~1.21.4",
"istanbul": "~0.3.0",
"colors": "~0.6.2",
"grunt": "~0.4.5",
"grunt-contrib-jshint": "~0.10.0"
"colors": "^1.0.3",
"grunt": "^0.4.5",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-jshint": "^0.10.0",
"istanbul": "^0.3.2",
"mocha": "^2.0.1"
},
"license": "Apache Public License v2",
"main": "./lib/index",
Expand Down

0 comments on commit f39d779

Please sign in to comment.