Skip to content

Commit

Permalink
rename and add me-alist-to-plist
Browse files Browse the repository at this point in the history
Modified-by: ag91 <agiugliano@live.it>
  • Loading branch information
ag91 committed Feb 10, 2024
1 parent 1cbe4ff commit 8ff5daa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
36 changes: 34 additions & 2 deletions moldable-emacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,47 @@ Make sure table is also indented."
(defun me-insert-org-table (headlines objects)
"Produce org table of OBJECTS formatting with HEADLINES."
(me-insert-string-table (me-make-org-table headlines objects)))
(defun me-alist-to-lists-of-plist (alist)
"Convert ALIST to a `plist'."

(defun me-alist-to-plist (alist)
"Convert ALIST to a `plist'.
>> (me-alist-to-plist '((a . 1) (b . 2)))
=> (:a 1 :b 2)
>> (me-alist-to-plist '((:a . 1) (:b . 2)))
=> (:a 1 :b 2)
>> (me-alist-to-plist '((\"a\" . 1) (\"b\" . 2)))
=> (:a 1 :b 2)"
(when (-every? #'consp alist)
(-flatten
(--map
(list
(intern
(s-replace
"\""
""
(let ((key (prin1-to-string (car it))))

(if (s-starts-with-p ":" key)
key
(concat ":" key)))))
(cdr it))
alist))))

(defun org-table-as-alist-to-plist (alist)
"Convert ALIST to a `plist'.
>> (org-table-as-alist-to-plist '((\"a\" \"1\") (\"b\" \"2\") (\"c\" \"3\")))
=> ((:a \"1\" :b \"2\" :c \"3\"))
"
(let ((keys (ignore-errors
(and (= (length (car alist)) (length (-filter #'stringp (car alist))))
(--map (intern (concat ":" it)) (car alist))))))
(if keys
(--map (-flatten (-zip-lists keys it)) (cdr alist))
alist)))

(let ((alist '(("a" "b"))))
(= (length (car alist)) (length (-filter #'stringp (car alist)))))

(defun me-org-table-to-plist (table-string)
"Make TABLE-STRING a plist.
Expand Down
4 changes: 2 additions & 2 deletions molds/core.el
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ This is a more focused view than `CodeToTree.'")
(--all? (equal (-filter #'symbolp (car l)) (-filter #'symbolp it)) l)))))
:then (:fn
(let* ((list (if (ignore-errors (length (car l)))
(me-alist-to-lists-of-plist l)
(me-alist-to-lists-of-plist (-map #'-cons-to-list l)))))
(org-table-as-alist-to-plist l)
(org-table-as-alist-to-plist (-map #'-cons-to-list l)))))
(with-current-buffer buffername
(org-mode)
(erase-buffer)
Expand Down
8 changes: 4 additions & 4 deletions tests/moldable-emacs-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
(require 'tree-sitter)


(ert-deftest me-alist-to-lists-of-plist_convert-alist-to-plist ()
(ert-deftest org-table-as-alist-to-plist_convert-alist-to-plist ()
(should
(equal (me-alist-to-lists-of-plist '(("A" "b") (1 2) (3 4))) '((:A 1 :b 2) (:A 3 :b 4)))))
(equal (org-table-as-alist-to-plist '(("A" "b") (1 2) (3 4))) '((:A 1 :b 2) (:A 3 :b 4)))))

;; (ert-deftest me-alist-to-lists-of-plist_convert-alist-to-plist+1 ()
;; (ert-deftest org-table-as-alist-to-plist_convert-alist-to-plist+1 ()
;; (should
;; (equal (me-alist-to-lists-of-plist '(("A" . "b") (1 . 2) (3 . 4))) '((:A 1 :b 2) (:A 3 :b 4)))))
;; (equal (org-table-as-alist-to-plist '(("A" . "b") (1 . 2) (3 . 4))) '((:A 1 :b 2) (:A 3 :b 4)))))

(ert-deftest me--given_valid-with-buffer ()
(should
Expand Down

0 comments on commit 8ff5daa

Please sign in to comment.