Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 316 lines (216 sloc) 7.927 kb
2bf06bc Magnar Sveen Initial commit
magnars authored
1 # s.el [![Build Status](https://secure.travis-ci.org/magnars/s.el.png)](http://travis-ci.org/magnars/s.el)
2
3 The long lost Emacs string manipulation library.
4
5aeb516 Magnar Sveen Added notes about installation and contribution.
magnars authored
5 ## Installation
6
7 It's available on [marlamade](http://marmalade-repo.org/):
8
9 M-x package-install s
10
11 Or you can just dump `s.el` in your load path somewhere.
12
2bf06bc Magnar Sveen Initial commit
magnars authored
13 ## Functions
14
15 * [s-trim](#s-trim-s) `(s)`
f520cc0 Magnar Sveen s-trim-left and s-trim-right
magnars authored
16 * [s-trim-left](#s-trim-left-s) `(s)`
17 * [s-trim-right](#s-trim-right-s) `(s)`
c26ba65 Magnar Sveen s-collapse-whitespace
magnars authored
18 * [s-collapse-whitespace](#s-collapse-whitespace-s) `(s)`
5818091 Magnar Sveen s-lines
magnars authored
19 * [s-lines](#s-lines-s) `(s)`
7328a0d Magnar Sveen s-join
magnars authored
20 * [s-join](#s-join-separator-strings) `(separator strings)`
862dc5f Magnar Sveen s-concat
magnars authored
21 * [s-concat](#s-concat-rest-strings) `(&rest strings)`
5b9ec8a Magnar Sveen s-repeat
magnars authored
22 * [s-repeat](#s-repeat-num-s) `(num s)`
881275b Magnar Sveen s-truncate
magnars authored
23 * [s-truncate](#s-truncate-len-s) `(len s)`
e1c8b0b Magnar Sveen s-word-wrap
magnars authored
24 * [s-word-wrap](#s-word-wrap-len-s) `(len s)`
b6a4b93 Magnar Sveen s-left and s-right
magnars authored
25 * [s-left](#s-left-len-s) `(len s)`
26 * [s-right](#s-right-len-s) `(len s)`
1004cd5 Magnar Sveen s-chop-suffix
magnars authored
27 * [s-chop-suffix](#s-chop-suffix-suffix-s) `(suffix s)`
be0c595 Magnar Sveen s-chomp
magnars authored
28 * [s-chomp](#s-chomp-s) `(s)`
2952738 Magnar Sveen s-ends-with-p
magnars authored
29 * [s-ends-with-p](#s-ends-with-p-suffix-s) `(suffix s)`
eae9409 Magnar Sveen Start of string: prefix, not suffix
magnars authored
30 * [s-starts-with-p](#s-starts-with-p-prefix-s) `(prefix s)`
4ac3b06 Magnar Sveen s-contains-p
magnars authored
31 * [s-contains-p](#s-contains-p-needle-s) `(needle s)`
f01d014 Magnar Sveen s-replace
magnars authored
32 * [s-replace](#s-replace-old-new-s) `(old new s)`
02d5abb Magnar Sveen s-split-words
magnars authored
33 * [s-split-words](#s-split-words-s) `(s)`
4556706 Magnar Sveen s-lower-camel-case, s-upper-camel-case, s-snake-case, s-dashed-words and...
magnars authored
34 * [s-lower-camel-case](#s-lower-camel-case-s) `(s)`
35 * [s-upper-camel-case](#s-upper-camel-case-s) `(s)`
36 * [s-snake-case](#s-snake-case-s) `(s)`
37 * [s-dashed-words](#s-dashed-words-s) `(s)`
38 * [s-capitalized-words](#s-capitalized-words-s) `(s)`
2bf06bc Magnar Sveen Initial commit
magnars authored
39
40 ## Documentation and examples
41
42 ### s-trim `(s)`
43
d110b9c Magnar Sveen Improved docs
magnars authored
44 Remove whitespace at the beginning and end of `s`.
2bf06bc Magnar Sveen Initial commit
magnars authored
45
46 ```cl
47 (s-trim "trim ") ;; => "trim"
48 (s-trim " this") ;; => "this"
49 (s-trim " only trims beg and end ") ;; => "only trims beg and end"
50 ```
51
f520cc0 Magnar Sveen s-trim-left and s-trim-right
magnars authored
52 ### s-trim-left `(s)`
53
d110b9c Magnar Sveen Improved docs
magnars authored
54 Remove whitespace at the beginning of `s`.
f520cc0 Magnar Sveen s-trim-left and s-trim-right
magnars authored
55
56 ```cl
57 (s-trim-left "trim ") ;; => "trim "
58 (s-trim-left " this") ;; => "this"
59 ```
60
61 ### s-trim-right `(s)`
62
d110b9c Magnar Sveen Improved docs
magnars authored
63 Remove whitespace at the end of `s`.
f520cc0 Magnar Sveen s-trim-left and s-trim-right
magnars authored
64
65 ```cl
66 (s-trim-right "trim ") ;; => "trim"
67 (s-trim-right " this") ;; => " this"
68 ```
69
c26ba65 Magnar Sveen s-collapse-whitespace
magnars authored
70 ### s-collapse-whitespace `(s)`
71
72 Convert all adjacent whitespace characters to a single space.
73
74 ```cl
75 (s-collapse-whitespace "only one space please") ;; => "only one space please"
76 (s-collapse-whitespace "collapse \n all \t sorts of \r whitespace") ;; => "collapse all sorts of whitespace"
77 ```
78
5818091 Magnar Sveen s-lines
magnars authored
79 ### s-lines `(s)`
80
81 Splits `s` into a list of strings on newline characters.
82
83 ```cl
84 (s-lines "abc\ndef\nghi") ;; => '("abc" "def" "ghi")
85 ```
86
7328a0d Magnar Sveen s-join
magnars authored
87 ### s-join `(separator strings)`
88
89 Join all the strings in `strings` with `separator` in between.
90
91 ```cl
92 (s-join "+" '("abc" "def" "ghi")) ;; => "abc+def+ghi"
93 (s-join "\n" '("abc" "def" "ghi")) ;; => "abc\ndef\nghi"
94 ```
95
862dc5f Magnar Sveen s-concat
magnars authored
96 ### s-concat `(&rest strings)`
97
98 Join all the string arguments into one string.
99
100 ```cl
101 (s-concat "abc" "def" "ghi") ;; => "abcdefghi"
102 ```
103
5b9ec8a Magnar Sveen s-repeat
magnars authored
104 ### s-repeat `(num s)`
105
106 Make a string of `s` repeated `num` times.
107
108 ```cl
109 (s-repeat 10 " ") ;; => " "
110 (s-concat (s-repeat 8 "Na") " Batman!") ;; => "NaNaNaNaNaNaNaNa Batman!"
111 ```
112
881275b Magnar Sveen s-truncate
magnars authored
113 ### s-truncate `(len s)`
114
115 If `s` is longer than `len`, cut it down and add ... at the end.
116
117 ```cl
118 (s-truncate 6 "This is too long") ;; => "Thi..."
119 (s-truncate 16 "This is also too long") ;; => "This is also ..."
120 (s-truncate 16 "But this is not!") ;; => "But this is not!"
121 ```
122
e1c8b0b Magnar Sveen s-word-wrap
magnars authored
123 ### s-word-wrap `(len s)`
124
125 If `s` is longer than `len`, wrap the words with newlines.
126
127 ```cl
128 (s-word-wrap 10 "This is too long") ;; => "This is\ntoo long"
129 (s-word-wrap 10 "This is way way too long") ;; => "This is\nway way\ntoo long"
130 (s-word-wrap 10 "It-wraps-words-but-does-not-break-them") ;; => "It-wraps-words-but-does-not-break-them"
131 ```
132
b6a4b93 Magnar Sveen s-left and s-right
magnars authored
133 ### s-left `(len s)`
134
135 Returns up to the `len` first chars of `s`.
136
137 ```cl
138 (s-left 3 "lib/file.js") ;; => "lib"
139 (s-left 3 "li") ;; => "li"
140 ```
141
142 ### s-right `(len s)`
143
144 Returns up to the `len` last chars of `s`.
145
146 ```cl
147 (s-right 3 "lib/file.js") ;; => ".js"
148 (s-right 3 "li") ;; => "li"
149 ```
150
1004cd5 Magnar Sveen s-chop-suffix
magnars authored
151 ### s-chop-suffix `(suffix s)`
152
153 Remove `suffix` if it is at end of `s`.
154
155 ```cl
156 (s-chop-suffix "-test.js" "penguin-test.js") ;; => "penguin"
d110b9c Magnar Sveen Improved docs
magnars authored
157 (s-chop-suffix "\n" "no newlines\n") ;; => "no newlines"
158 (s-chop-suffix "\n" "some newlines\n\n") ;; => "some newlines\n"
1004cd5 Magnar Sveen s-chop-suffix
magnars authored
159 ```
160
be0c595 Magnar Sveen s-chomp
magnars authored
161 ### s-chomp `(s)`
162
163 Remove trailing newline from `s`.
164
165 ```cl
166 (s-chomp "no newlines\n") ;; => "no newlines"
167 (s-chomp "some newlines\n\n") ;; => "some newlines\n"
168 ```
169
2952738 Magnar Sveen s-ends-with-p
magnars authored
170 ### s-ends-with-p `(suffix s)`
171
172 Does `s` end in `suffix`?
173
174 ```cl
175 (s-ends-with-p ".md" "readme.md") ;; => t
176 (s-ends-with-p ".md" "readme.txt") ;; => nil
6610bb6 Magnar Sveen s-starts-with-p
magnars authored
177 (s-ends-with-p ".md" "md") ;; => nil
178 ```
179
eae9409 Magnar Sveen Start of string: prefix, not suffix
magnars authored
180 ### s-starts-with-p `(prefix s)`
6610bb6 Magnar Sveen s-starts-with-p
magnars authored
181
eae9409 Magnar Sveen Start of string: prefix, not suffix
magnars authored
182 Does `s` start with `prefix`?
6610bb6 Magnar Sveen s-starts-with-p
magnars authored
183
184 ```cl
185 (s-starts-with-p "lib/" "lib/file.js") ;; => t
186 (s-starts-with-p "test/" "lib/file.js") ;; => nil
187 (s-starts-with-p "lib/" "lib") ;; => nil
2952738 Magnar Sveen s-ends-with-p
magnars authored
188 ```
189
4ac3b06 Magnar Sveen s-contains-p
magnars authored
190 ### s-contains-p `(needle s)`
191
192 Does `s` contain `needle`?
193
194 ```cl
195 (s-contains-p "file" "lib/file.js") ;; => t
e7c658a Magnar Sveen Improve examples
magnars authored
196 (s-contains-p "nope" "lib/file.js") ;; => nil
ae0dd75 Magnar Sveen Text tweaks
magnars authored
197 (s-contains-p "^a" "it's not ^a regexp") ;; => t
4ac3b06 Magnar Sveen s-contains-p
magnars authored
198 ```
199
f01d014 Magnar Sveen s-replace
magnars authored
200 ### s-replace `(old new s)`
201
202 Replaces `old` with `new` in `s`.
203
204 ```cl
205 (s-replace "file" "nope" "lib/file.js") ;; => "lib/nope.js"
206 (s-replace "^a" "\\1" "it's not ^a regexp") ;; => "it's not \\1 regexp"
207 ```
208
02d5abb Magnar Sveen s-split-words
magnars authored
209 ### s-split-words `(s)`
210
ae0dd75 Magnar Sveen Text tweaks
magnars authored
211 Split `s` into list of words.
02d5abb Magnar Sveen s-split-words
magnars authored
212
213 ```cl
214 (s-split-words "under_score") ;; => '("under" "score")
215 (s-split-words "some-dashed-words") ;; => '("some" "dashed" "words")
216 (s-split-words "evenCamelCase") ;; => '("even" "Camel" "Case")
217 ```
218
4556706 Magnar Sveen s-lower-camel-case, s-upper-camel-case, s-snake-case, s-dashed-words and...
magnars authored
219 ### s-lower-camel-case `(s)`
220
221 Convert `s` to lowerCamelCase.
222
223 ```cl
224 (s-lower-camel-case "some words") ;; => "someWords"
225 (s-lower-camel-case "dashed-words") ;; => "dashedWords"
226 (s-lower-camel-case "under_scored_words") ;; => "underScoredWords"
227 ```
228
229 ### s-upper-camel-case `(s)`
230
231 Convert `s` to UpperCamelCase.
232
233 ```cl
234 (s-upper-camel-case "some words") ;; => "SomeWords"
235 (s-upper-camel-case "dashed-words") ;; => "DashedWords"
236 (s-upper-camel-case "under_scored_words") ;; => "UnderScoredWords"
237 ```
238
239 ### s-snake-case `(s)`
240
241 Convert `s` to snake_case.
242
243 ```cl
244 (s-snake-case "some words") ;; => "some_words"
245 (s-snake-case "dashed-words") ;; => "dashed_words"
246 (s-snake-case "camelCasedWords") ;; => "camel_cased_words"
247 ```
248
249 ### s-dashed-words `(s)`
250
251 Convert `s` to dashed-words.
252
253 ```cl
254 (s-dashed-words "some words") ;; => "some-words"
255 (s-dashed-words "under_scored_words") ;; => "under-scored-words"
256 (s-dashed-words "camelCasedWords") ;; => "camel-cased-words"
257 ```
258
259 ### s-capitalized-words `(s)`
260
261 Convert `s` to Capitalized Words.
262
263 ```cl
264 (s-capitalized-words "some words") ;; => "Some Words"
265 (s-capitalized-words "under_scored_words") ;; => "Under Scored Words"
266 (s-capitalized-words "camelCasedWords") ;; => "Camel Cased Words"
267 ```
268
2bf06bc Magnar Sveen Initial commit
magnars authored
269
5aeb516 Magnar Sveen Added notes about installation and contribution.
magnars authored
270 ## Contribute
271
272 Yes, please do. There's a suite of tests, so remember to add tests for your
273 specific feature, or I might break it later.
274
275 You'll find the repo at:
276
277 https://github.com/magnars/s.el
278
2bf06bc Magnar Sveen Initial commit
magnars authored
279 ## Development
280
281 Run the tests with
282
283 ./run-tests.sh
284
285 Create the docs with
286
287 ./create-docs.sh
288
289 I highly recommend that you install these as a pre-commit hook, so that
290 the tests are always running and the docs are always in sync:
291
292 cp pre-commit.sh .git/hooks/pre-commit
293
294 Oh, and don't edit `README.md` directly, it is auto-generated.
295 Change `readme-template.md` or `examples-to-docs.el` instead.
296
297 ## License
298
299 Copyright (C) 2012 Magnar Sveen
300
301 Authors: Magnar Sveen <magnars@gmail.com>
302 Keywords: strings
303
304 This program is free software; you can redistribute it and/or modify
305 it under the terms of the GNU General Public License as published by
306 the Free Software Foundation, either version 3 of the License, or
307 (at your option) any later version.
308
309 This program is distributed in the hope that it will be useful,
310 but WITHOUT ANY WARRANTY; without even the implied warranty of
311 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
312 GNU General Public License for more details.
313
314 You should have received a copy of the GNU General Public License
315 along with this program. If not, see <http://www.gnu.org/licenses/>.
Something went wrong with that request. Please try again.