Permalink
Browse files

Add kvplist-merge function, test and readme text

  • Loading branch information...
1 parent 608de1e commit f6e0b5a16631a551469a22e140736dabe44b98d7 @leoc leoc committed Jan 22, 2013
Showing with 23 additions and 3 deletions.
  1. +3 −2 README.creole
  2. +7 −0 kv-tests.el
  3. +13 −1 kv.el
View
@@ -145,9 +145,10 @@ Filter the plist to just those matching //keys//.
[[kvalist->filter-keys]] is actually used to do this work.
+=== kvplist->merge old-plist new-plist ===
+
+Merge the keys from new-plist into old-plist and return the new plist.
=== kvplist2->filter-keys plist2 &rest keys ===
Return the //plist2// (a list of plists) filtered to the //keys//.
-
-
View
@@ -179,4 +179,11 @@
(list :key1 "value1" :key2 t :key3 '(big list of symbols) :key4 10)
'key1 'key4))))
+(ert-deftest kvplist-merge ()
+ (should
+ (equal
+ '(:key1 "value1" :key2 "new value" :key3 "entirely new")
+ (kvplist-merge '(:key1 "value1" :key2 "old value")
+ '(:key2 "new value" :key3 "entirely new")))))
+
;;; kv-tests.el ends here
View
@@ -179,7 +179,7 @@ The keys are expected to be :prefixed and the colons are removed.
The keys in the resulting alist are symbols."
(when plist
(loop for (key value . rest) on plist by 'cddr
- collect (cons (keyword->symbol key) value))))
+ collect (cons (keyword->symbol key) value))))
(defun kvalist2->plist (alist2)
"Convert a list of alists too a list of plists."
@@ -385,6 +385,18 @@ SEXP will describe the structure desired."
(defalias 'map-bind 'kvmap-bind)
+(defun kvplist-merge (old new)
+ "Merges two plists. The keys from NEW will overwrite the ones in OLD."
+ (let ((key (car new))
+ (val (cadr new))
+ (new (cddr new)))
+ (while (and key val)
+ (setq old (plist-put old key val))
+ (setq key (car new))
+ (setq val (cadr new))
+ (setq new (cddr new)))
+ old))
+
(provide 'kv)
(provide 'dotassoc)

0 comments on commit f6e0b5a

Please sign in to comment.