From 8ff5daafb58a5d00091bd4889c3b755627a69f09 Mon Sep 17 00:00:00 2001 From: ag91 Date: Sat, 10 Feb 2024 16:25:19 +0000 Subject: [PATCH] rename and add me-alist-to-plist Modified-by: ag91 --- moldable-emacs.el | 36 ++++++++++++++++++++++++++++++++++-- molds/core.el | 4 ++-- tests/moldable-emacs-test.el | 8 ++++---- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/moldable-emacs.el b/moldable-emacs.el index d52f3eb..c4345be 100644 --- a/moldable-emacs.el +++ b/moldable-emacs.el @@ -220,8 +220,38 @@ 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)))))) @@ -229,6 +259,8 @@ Make sure table is also indented." (--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. diff --git a/molds/core.el b/molds/core.el index 0e59114..3f48a11 100644 --- a/molds/core.el +++ b/molds/core.el @@ -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) diff --git a/tests/moldable-emacs-test.el b/tests/moldable-emacs-test.el index 69d8a93..4db6160 100644 --- a/tests/moldable-emacs-test.el +++ b/tests/moldable-emacs-test.el @@ -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