Advertisement
indigo0086

convert-regexp-spec.dl

May 22nd, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.85 KB | None | 0 0
  1. (defmacro comment (&rest body) nil)
  2.  
  3. (let ((test-element "<SomeComponent objProp={true} stringProp=\"Testing\" otherObjProp={true} otherStringProp=\"testing\" />")
  4.       (replace-spec '(("<\\w+\\s-" . "{\n")
  5.                       ("\\s-*\\(\\w+\\)=\"\\(\\w+\\)\"" . "\t\\1: \\2,\n")
  6.                       ("\\s-*\\(\\w+\\)={\\(\\w+\\)}" . " \t\\1: \\2,\n")
  7.                       ("\\s-*/>" . "}"))))
  8.   (seq-reduce (lambda (str rgx-rpl)
  9.                 (s-replace-regexp (car rgx-rpl)
  10.                                   (cdr rgx-rpl)
  11.                                   str))
  12.               replace-spec
  13.               test-element))
  14.  
  15. (defun user/-spec-reducer (str spec)
  16.   (s-replace-regexp (car spec) (cdr spec) str))
  17.  
  18. (defun user/el->prop (start end)
  19.   (interactive "r")
  20.   (let* ((element-str (buffer-substring-no-properties start end))
  21.          (replace-spec '(("<\\w+\\s-" . "{\n")
  22.                          ("\\s-*\\(\\w+\\)=\"\\(\\w+\\)\"" . "\t\\1: \\2,\n")
  23.                          ("\\s-*\\(\\w+\\)={\\(\\w+\\)}" . " \t\\1: \\2,\n")
  24.                          ("\\s-*/>" . "}")))
  25.          (obj-str (seq-reduce #'user/-spec-reducer replace-spec element-str)))
  26.     (message element-str)
  27.     (delete-region start end)
  28.     (insert obj-str)))
  29.  
  30. (comment
  31.  (s-replace-regexp "\\s-*\\(\\w+\\)=\"\\(\\w+\\)\""
  32.                    " \\1: \\2,"
  33.                    "<SomeComponent objProp={true} stringProp=\"Testing\" otherObjProp={true} otherStringProp=\"testing\" />"))
  34.  
  35. <SomeComponent objProp={true} stringProp="Testing" otherObjProp={true} otherStringProp="testing" />
  36. {
  37.     objProp: true,
  38.     stringProp: Testing,
  39.     otherObjProp: true,
  40.     otherStringProp: testing,
  41. }
  42. <SomeComponent objProp={true} stringProp="Testing" otherObjProp={true} otherStringProp="testing" />
  43. <SomeComponent objProp={true} stringProp="Testing" otherObjProp={true} otherStringProp="testing" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement