aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--stack-core.el42
-rw-r--r--test/tests.el75
2 files changed, 86 insertions, 31 deletions
diff --git a/stack-core.el b/stack-core.el
index ef0dc9f..37ae2af 100644
--- a/stack-core.el
+++ b/stack-core.el
@@ -212,21 +212,41 @@ entire response as a complex alist."
(defun stack-core-filter-data (data desired-tree)
"Filters DATA and returns the DESIRED-TREE"
(if (vectorp data)
- (mapcar (lambda (entry)
- (stack-core-filter-data
- entry desired-tree))
- data)
+ (apply #'vector
+ (mapcar (lambda (entry)
+ (stack-core-filter-data
+ entry desired-tree))
+ data))
+ (mapcar #'identity '(1 2 3))
(delq
nil
(mapcar (lambda (cons-cell)
- (when (member (car cons-cell) desired-tree)
- (if (and (sequencep (cdr cons-cell))
- (sequencep (elt (cdr cons-cell) 0)))
- (stack-core-filter-data
- (cdr cons-cell)
- (cdr (assoc (car cons-cell) desired-tree))))
- cons-cell))
+ (let ((f (stack-core-filter-data--item-in-tree
+ (car cons-cell) desired-tree)))
+ (when f
+ (if (and (sequencep (cdr cons-cell))
+ (sequencep (elt (cdr cons-cell) 0)))
+ (cons (car cons-cell)
+ (stack-core-filter-data
+ (cdr cons-cell) (cdr f)))
+ cons-cell))))
data))))
+(defun stack-core-filter-data--item-in-tree (item tree)
+ "Check if ITEM is in the direct leaves of TREE
+
+For example, when called with (f 'item '(some item here)), the
+return would be `t'. When called as (f 'item '(some (item here)
+in a (deep structure))), `(item here)' would be returned.
+"
+ (when tree
+ (if (equal item (car tree)) tree
+ (if (and (listp (car tree))
+ (equal item (caar tree)))
+ (car tree)
+ (stack-core-filter-data--item-in-tree item (cdr tree))))))
+
+(stack-core-filter-data--item-in-tree 'a '(b (a 1 b c) c))
+
(provide 'stack-core)
;;; stack-core.el ends here
diff --git a/test/tests.el b/test/tests.el
index 59c3413..01d501c 100644
--- a/test/tests.el
+++ b/test/tests.el
@@ -41,8 +41,9 @@
(should-error
(stack-core-make-request "questions" '(()))))
-(ert-deftest test-data-filter-1 ()
- "Test the meta-convenience function -- flat structure"
+(ert-deftest test-tree-filter ()
+ "`stack-core-filter-data'"
+ ;; flat
(should
(equal
'((1 . t) (2 . [1 2]) (3))
@@ -53,28 +54,62 @@
("5" . bop)
(3)
(p . 4))
- '(1 2 3)))))
-
-(ert-deftest test-data-filter-2 ()
- "Test the meta-convenience function -- complex structure"
+ '(1 2 3))))
+ ;; complex
(should
(equal
- '((1 . [a b c]) (2 . [(a . 1)]) (3 . peach))
+ '((1 . [a b c]) (2 . [((a . 1) (c . 3)) ((a . 4) (c . 6))]) (3 . peach))
(stack-core-filter-data '((1 . [a b c])
- (2 . [(a . 1)
- (b . 2)])
+ (2 . [((a . 1)
+ (b . 2)
+ (c . 3))
+ ((a . 4)
+ (b . 5)
+ (c . 6))])
(3 . peach)
(4 . banana))
- '(1 (2 a) 3)))))
+ '(1 (2 a c) 3))))
-(ert-deftest test-data-filter-3 ()
- "Test the meta-convenience function -- vector structure"
- (equal
- '(((1 . 2) (2 . 3) (3 . 4))
+ ;; vector
+ (should
+ (equal
+ [((1 . 2) (2 . 3) (3 . 4))
((1 . a) (2 . b) (3 . c))
- nil ((1 . alpha) (2 . beta)))
- (stack-core-filter-data [((1 . 2) (2 . 3) (3 . 4))
- ((1 . a) (2 . b) (3 . c) (5 . seven))
- ((should-not-go))
- ((1 . alpha) (2 . beta))]
- '(1 2 3))))
+ nil ((1 . alpha) (2 . beta))]
+ (stack-core-filter-data [((1 . 2) (2 . 3) (3 . 4))
+ ((1 . a) (2 . b) (3 . c) (5 . seven))
+ ((should-not-go))
+ ((1 . alpha) (2 . beta))]
+ '(1 2 3)))))
+
+(ert-deftest test-tree-member ()
+ "`stack-core-filter-data--item-in-tree'"
+ (should
+ (equal
+ '(a b c)
+ (stack-core-filter-data--item-in-tree 'a '(a b c))))
+
+ (should
+ (equal
+ '(a b c)
+ (stack-core-filter-data--item-in-tree 'a '(b a b c))))
+
+ (should
+ (equal
+ '(a b c)
+ (stack-core-filter-data--item-in-tree 'a '((a b c) 1 2))))
+
+ (should
+ (equal
+ '(a b c)
+ (stack-core-filter-data--item-in-tree 'a '(1 (a b c) 2))))
+
+ (should
+ (equal
+ '(a b c)
+ (stack-core-filter-data--item-in-tree 'a '(1 2 (a b c)))))
+
+ (should
+ (equal
+ '(a a b c)
+ (stack-core-filter-data--item-in-tree 'a '(1 (a a b c) 2)))))