Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. const date = '\\d{4}-\\d{2}-\\d{2}',
  2. contextre = /\B@\w+/g;
  3. projectre = /\B\+\w+/g,
  4. tagre = /[\w\-_]+:[\w\-_]+/g;
  5.  
  6. const pluckAll = (raw, re) => {
  7. let matches = raw.match(re);
  8. if (matches) {
  9. matches.forEach(p => {
  10. let position = raw.indexOf(p);
  11. raw = raw.substr(0, position) + raw.substr(position + p.length);
  12. });
  13. }
  14. return [raw, matches];
  15. }
  16.  
  17. const parse = raw => {
  18. let todo = {};
  19. if (raw.indexOf('x ') === 0) {
  20. todo.done = true;
  21. raw = raw.substr(2);
  22.  
  23. let completed = new RegExp('^' + date + ' ').exec(raw);
  24. if (completed) {
  25. todo.completed = completed[0].trim();
  26. raw = raw.substr(11);
  27. }
  28. }
  29.  
  30. let priority = /^\(([a-z])\) /.exec(raw);
  31. if (priority) {
  32. todo.priority = priority[1].toUpperCase()
  33. raw = raw.substr(4);
  34. }
  35.  
  36. let created = new RegExp('^' + date + ' ').exec(raw);
  37. if (created) {
  38. todo.created = created[0].trim();
  39. raw = raw.substr(11);
  40. }
  41.  
  42. let projects = [];
  43. [raw, projects] = pluckAll(raw, projectre);
  44. if (projects) {
  45. todo.projects = projects;
  46. }
  47.  
  48. let contexts = [];
  49. [raw, contexts] = pluckAll(raw, contextre);
  50. if (contexts) {
  51. todo.contexts = contexts;
  52. }
  53.  
  54. let tags = [];
  55. [raw, tags] = pluckAll(raw, tagre);
  56. if (tags) {
  57. tags.forEach(t => {
  58. let [k, v] = t.split(':');
  59. todo[k] = v;
  60. });
  61. }
  62.  
  63. raw = raw.trim();
  64. raw = raw.replace(/\s{2,}/g, ' ');
  65. todo.text = raw;
  66. return todo;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement