Front end of the Slack clone application.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var parse = require('acorn').parse;
  2. var isArray = require('isarray');
  3. var objectKeys = require('object-keys');
  4. var forEach = require('foreach');
  5. module.exports = function (src, opts, fn) {
  6. if (typeof opts === 'function') {
  7. fn = opts;
  8. opts = {};
  9. }
  10. if (src && typeof src === 'object' && src.constructor.name === 'Buffer') {
  11. src = src.toString();
  12. }
  13. else if (src && typeof src === 'object') {
  14. opts = src;
  15. src = opts.source;
  16. delete opts.source;
  17. }
  18. src = src === undefined ? opts.source : src;
  19. if (typeof src !== 'string') src = String(src);
  20. if (opts.parser) parse = opts.parser.parse;
  21. var ast = parse(src, opts);
  22. var result = {
  23. chunks : src.split(''),
  24. toString : function () { return result.chunks.join('') },
  25. inspect : function () { return result.toString() }
  26. };
  27. var index = 0;
  28. (function walk (node, parent) {
  29. insertHelpers(node, parent, result.chunks);
  30. forEach(objectKeys(node), function (key) {
  31. if (key === 'parent') return;
  32. var child = node[key];
  33. if (isArray(child)) {
  34. forEach(child, function (c) {
  35. if (c && typeof c.type === 'string') {
  36. walk(c, node);
  37. }
  38. });
  39. }
  40. else if (child && typeof child.type === 'string') {
  41. walk(child, node);
  42. }
  43. });
  44. fn(node);
  45. })(ast, undefined);
  46. return result;
  47. };
  48. function insertHelpers (node, parent, chunks) {
  49. node.parent = parent;
  50. node.source = function () {
  51. return chunks.slice(node.start, node.end).join('');
  52. };
  53. if (node.update && typeof node.update === 'object') {
  54. var prev = node.update;
  55. forEach(objectKeys(prev), function (key) {
  56. update[key] = prev[key];
  57. });
  58. node.update = update;
  59. }
  60. else {
  61. node.update = update;
  62. }
  63. function update (s) {
  64. chunks[node.start] = s;
  65. for (var i = node.start + 1; i < node.end; i++) {
  66. chunks[i] = '';
  67. }
  68. }
  69. }