util.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Generated by IcedCoffeeScript 1.7.1-c
  2. (function() {
  3. var ProgressBar, constants, date_to_unixtime, fork, is_dict, make_email, make_full_username, path, purge,
  4. __slice = [].slice;
  5. fork = require('child_process').fork;
  6. path = require('path');
  7. constants = require('./constants').constants;
  8. ProgressBar = require('progress');
  9. exports.dict_union = function() {
  10. var args, d, k, out, v, _i, _len;
  11. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  12. out = {};
  13. for (_i = 0, _len = args.length; _i < _len; _i++) {
  14. d = args[_i];
  15. for (k in d) {
  16. v = d[k];
  17. out[k] = v;
  18. }
  19. }
  20. return out;
  21. };
  22. exports.rmkey = function(obj, key) {
  23. var ret;
  24. ret = obj[key];
  25. delete obj[key];
  26. return ret;
  27. };
  28. exports.daemon = function(args) {
  29. var icmd;
  30. icmd = path.join(__dirname, "..", "node_modules", ".bin", "iced");
  31. return fork(process.argv[1], args, {
  32. execPath: icmd,
  33. detatched: true
  34. });
  35. };
  36. exports.js2unix = function(t) {
  37. return Math.floor(t / 1000);
  38. };
  39. is_dict = function(d) {
  40. return (typeof d === 'object') && !(Array.isArray(d));
  41. };
  42. exports.purge = purge = function(d) {
  43. var k, out, v;
  44. out = {};
  45. for (k in d) {
  46. v = d[k];
  47. if (v != null) {
  48. out[k] = is_dict(v) ? purge(v) : v;
  49. }
  50. }
  51. return out;
  52. };
  53. exports.make_scrypt_progress_hook = function() {
  54. var bar, prev, progress_hook;
  55. bar = null;
  56. prev = 0;
  57. progress_hook = function(obj) {
  58. if (obj.what !== "scrypt") {
  59. } else {
  60. bar || (bar = new ProgressBar("- run scrypt [:bar] :percent", {
  61. width: 35,
  62. total: obj.total
  63. }));
  64. bar.tick(obj.i - prev);
  65. return prev = obj.i;
  66. }
  67. };
  68. return progress_hook;
  69. };
  70. exports.make_email = make_email = function(un) {
  71. return un + "@" + constants.canonical_host;
  72. };
  73. exports.make_full_username = make_full_username = function(un) {
  74. return constants.canonical_host + "/" + un;
  75. };
  76. exports.date_to_unixtime = date_to_unixtime = function(s) {
  77. return ~~(s.getTime() / 1000);
  78. };
  79. exports.date_to_unix = function(o) {
  80. switch (typeof o) {
  81. case 'string':
  82. return date_to_unixtime(new Date(o));
  83. case 'number':
  84. return o;
  85. case 'object':
  86. if (o instanceof Date) {
  87. return date_to_unixtime(o);
  88. } else {
  89. return null;
  90. }
  91. break;
  92. default:
  93. return null;
  94. }
  95. };
  96. }).call(this);