helpers.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // helpers.js
  2. // This file contains some low level helper functions for the VWF nodeJS server.
  3. var libpath = require( 'path' ),
  4. fs = require( 'fs' );
  5. // List of valid ID characters for use in an instance.
  6. var ValidIDChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  7. // IsInstanceID tests if the passed in potential Instance ID
  8. // is a valid instance id.
  9. function IsInstanceID( potentialInstanceID ) {
  10. if ( potentialInstanceID.match(/^[0-9A-Za-z]{16}$/) ) {
  11. return true;
  12. }
  13. return false;
  14. }
  15. // GenerateInstanceID function creates a randomly generated instance ID.
  16. function GenerateInstanceID( ) {
  17. var text = "";
  18. for( var i=0; i < 16; i++ )
  19. text += ValidIDChars.charAt( Math.floor( Math.random( ) * ValidIDChars.length ) );
  20. return text;
  21. }
  22. // JoinPath
  23. // Takes multiple arguments, joins them together into one path.
  24. function JoinPath( /* arguments */ ) {
  25. var result = "";
  26. if ( arguments.length > 0 ) {
  27. if ( arguments[ 0 ] ) {
  28. result = arguments[ 0 ];
  29. }
  30. for ( var index = 1; index < arguments.length; index++ ) {
  31. var newSegment = arguments[ index ];
  32. if ( newSegment == undefined ) {
  33. newSegment = "";
  34. }
  35. if ( ( newSegment[ 0 ] == "/" ) && ( result[ result.length - 1 ] == "/" ) ) {
  36. result = result + newSegment.slice( 1 );
  37. } else if ( ( newSegment[ 0 ] == "/" ) || ( result[ result.length - 1 ] == "/" ) ) {
  38. result = result + newSegment;
  39. } else {
  40. result = result + "/" + newSegment;
  41. }
  42. //result = libpath.join( result, newSegment );
  43. }
  44. }
  45. return result;
  46. }
  47. // IsDirectory tests if the passed in path exists, and if it is a directory.
  48. function IsDirectory( path ) {
  49. // var seperatorFixedPath = path.replace( /\//g, libpath.sep );
  50. // if ( ! fs.existsSync( seperatorFixedPath ) ) {
  51. // return false;
  52. // }
  53. // return fs.statSync( seperatorFixedPath ).isDirectory();
  54. return true
  55. }
  56. // IsFile tests if the passed in path exists, and if it is a file.
  57. function IsFile( path ) {
  58. // var seperatorFixedPath = path.replace( /\//g, libpath.sep );
  59. // if ( ! fs.existsSync( seperatorFixedPath ) ) {
  60. // return false;
  61. // }
  62. // return fs.statSync( seperatorFixedPath ).isFile();
  63. return true
  64. }
  65. // GenerateSegments takes a string, breaks it into
  66. // '/' separated segments, and removes potential
  67. // blank first and last segments.
  68. function GenerateSegments( argument ) {
  69. var result = argument.split("/");
  70. if ( result.length > 0 ) {
  71. if ( result[ 0 ] == "" ) {
  72. result.shift();
  73. }
  74. }
  75. if ( result.length > 0 ) {
  76. if ( result[ result.length - 1 ] == "" ) {
  77. result.pop();
  78. }
  79. }
  80. return result;
  81. }
  82. exports.JoinPath = JoinPath;
  83. exports.IsDirectory = IsDirectory;
  84. exports.IsFile = IsFile;
  85. exports.IsInstanceID = IsInstanceID;
  86. exports.GenerateSegments = GenerateSegments;
  87. exports.GenerateInstanceID = GenerateInstanceID;