Skip to content

Commit

Permalink
s-chop-suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
magnars committed Oct 2, 2012
1 parent 9c68e6a commit 1004cd5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
16 changes: 15 additions & 1 deletion README.md
Expand Up @@ -9,19 +9,33 @@ This is so much a work in progress that you should definitely not be using it ye
## Functions

* [s-trim](#s-trim-s) `(s)`
* [s-chop-suffix](#s-chop-suffix-suffix-s) `(suffix s)`

## Documentation and examples

### s-trim `(s)`

Remove whitespace at beginning and end of string.
Remove whitespace at beginning and end of `s`.

```cl
(s-trim "trim ") ;; => "trim"
(s-trim " this") ;; => "this"
(s-trim " only trims beg and end ") ;; => "only trims beg and end"
```

### s-chop-suffix `(suffix s)`

Remove `suffix` if it is at end of `s`.

```cl
(s-chop-suffix "-test.js" "penguin-test.js") ;; => "penguin"
(s-chop-suffix "
" "no newlines
") ;; => "no newlines"
(s-chop-suffix "
" "no newlines") ;; => "no newlines"
```


## Development

Expand Down
8 changes: 6 additions & 2 deletions examples.el
Expand Up @@ -8,5 +8,9 @@
(defexamples s-trim
(s-trim "trim ") => "trim"
(s-trim " this") => "this"
(s-trim " only trims beg and end ") => "only trims beg and end"
)
(s-trim " only trims beg and end ") => "only trims beg and end")

(defexamples s-chop-suffix
(s-chop-suffix "-test.js" "penguin-test.js") => "penguin"
(s-chop-suffix "\n" "no newlines\n") => "no newlines"
(s-chop-suffix "\n" "no newlines") => "no newlines")
10 changes: 9 additions & 1 deletion s.el
Expand Up @@ -25,10 +25,18 @@
;;; Code:

(defun s-trim (s)
"Remove whitespace at beginning and end of string."
"Remove whitespace at beginning and end of S."
(if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
(if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
s)

(defun s-chop-suffix (suffix s)
"Remove SUFFIX if it is at end of S."
(let ((pos (- (length suffix))))
(if (and (>= (length s) (length suffix))
(string= suffix (substring s pos)))
(substring s 0 pos)
s)))

(provide 's)
;;; s.el ends here

0 comments on commit 1004cd5

Please sign in to comment.