util.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var crypto = require('crypto');
  2. var formatFactory = {json:'json',xml:'xml'};
  3. Number.isInteger = Number.isInteger || function(value) {
  4. return typeof value === "number" &&
  5. isFinite(value) &&
  6. Math.floor(value) === value;
  7. };
  8. function validateId( id ) {
  9. if( Number.isInteger( id ) )
  10. id = id.toString()
  11. return ( id.indexOf( ' ' ) == -1 && id.indexOf( '\t' ) == -1 && id.indexOf( '\n' ) == -1 && id.indexOf( '\r' ) == -1 && id.indexOf( '\r\n' ) == -1 );
  12. }
  13. exports.validateFormat = function(format){
  14. return format in formatFactory;
  15. }
  16. exports.check = function(opts,vals,position){
  17. for (var i = 0,len = opts.length; i < len; i++) {
  18. if (opts[i] != typeof vals[i]) {
  19. throw new Error("The index of " + i + " parameter was wrong type " + opts[i] +" [" + typeof vals[i] + "],position -> " + position);
  20. }
  21. }
  22. }
  23. exports.validateIDs = function( IDs ) {
  24. var valid = true;
  25. for( var i=0; i<IDs.length; ++i ) {
  26. if( ! validateId( IDs[i] ) ) {
  27. valid = false;
  28. break;
  29. }
  30. }
  31. return valid;
  32. }
  33. exports.validateId = validateId;
  34. exports.sha1 = function(input){
  35. var shasum = crypto.createHash('sha1');
  36. shasum.update(input);
  37. return shasum.digest('hex');
  38. }