1 (in-package :parenscript
)
3 (defun string-join (strings separator
)
4 (format nil
"~{~}" (format nil
"~~a~~^~a" separator
) strings
))
6 (defun val-to-string (val)
8 (string-downcase (symbol-name val
))
9 (princ-to-string val
)))
11 (defun string-split (string separators
&key
(keep-separators nil
) (remove-empty-subseqs nil
))
12 (do ((len (length string
))
17 (let ((split (if (> i last
)
18 (cons (subseq string last i
) res
)
20 (nreverse (if remove-empty-subseqs
21 (delete "" split
:test
#'string-equal
)
23 (when (member (char string i
) separators
)
24 (push (subseq string last i
) res
)
25 (when keep-separators
(push (string (char string i
)) res
))
28 (defparameter *special-chars
*
38 ;;; Parenscript-style symbol -> Javascript-style symbol
40 (defun constant-string-p (string)
41 (let ((len (length string
))
42 (constant-chars '(#\
+ #\
*)))
44 (member (char string
0) constant-chars
)
45 (member (char string
(1- len
)) constant-chars
))))
47 (defun first-uppercase-p (string)
48 (and (> (length string
) 1)
49 (member (char string
0) '(#\
+ #\
*))))
51 (defun untouchable-string-p (string)
52 (and (> (length string
) 1)
53 (char= #\
: (char string
0))))
55 (defun symbol-to-js (symbol)
56 "Given a Lisp symbol or string, produces to a valid JavaScript
57 identifier by following transformation heuristics case conversion. For
58 example, paren-script becomes parenScript, *some-global* becomes
60 (when (symbolp symbol
)
61 (setf symbol
(symbol-name symbol
)))
62 (let ((symbols (string-split symbol
'(#\.
#\
[ #\
]) :keep-separators t
:remove-empty-subseqs t
)))
63 (cond ((null symbols
) "")
64 ((= (length symbols
) 1)
69 (cond ((constant-string-p symbol
)
71 symbol
(subseq symbol
1 (1- (length symbol
)))))
72 ((first-uppercase-p symbol
)
74 symbol
(subseq symbol
1)))
75 ((untouchable-string-p symbol
)
77 symbol
(subseq symbol
1))))
81 ((and lowercase
(not all-uppercase
))
86 (dotimes (i (length symbol
))
87 (let ((c (char symbol i
)))
90 (setf lowercase
(not lowercase
)))
91 ((assoc c
*special-chars
*)
92 (dolist (i (coerce (cdr (assoc c
*special-chars
*)) 'list
))
95 (coerce (nreverse res
) 'string
)))
96 (t (string-join (mapcar #'symbol-to-js symbols
) "")))))
98 (defun ordered-set-difference (list1 list2
&key
(test #'eql
)) ;; because the CL set-difference may not preserve order
99 (reduce (lambda (list el
) (remove el list
:test test
))