-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring.ls
83 lines (65 loc) · 2.17 KB
/
string.ls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict'
curry = require './curry'
array = require './array'
TRIM_LEFT = new RegExp '^[\s\uFEFF\xA0]+'
TRIM_RIGHT = new RegExp '[\s\uFEFF\xA0]+$'
TRIM_BOTH = new RegExp '^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$', 'g'
# empty :: string -> boolean
exports.empty = (str) ->
str.length is 0
# contains :: string -> boolean
exports.includes = (part, str) ->
(str.indexOf part) isnt -1
exports.contains = exports.includes
exports.startsWith = curry 1 (search, str) ->
search is (str.slice 0, search.length)
exports.endsWith = curry 1 (search, str) ->
search is (str.slice str.length - search.length)
# trim :: string -> string
exports.trim = (str) ->
str.replace TRIM_BOTH, ''
# trimLeft :: string -> string
exports.trimLeft = (str) ->
str.replace TRIM_LEFT, ''
# trimRight :: string -> string
exports.trimRight = (str) ->
str.replace TRIM_RIGHT, ''
# repeat :: string -> string
exports.repeat = curry (n, str) ->
[str for til n].join ''
# reverse :: string -> string
exports.reverse = (str) ->
i = 0
len = str.length
result = new Array len
until len is 0
result[--len] = str.char-at i++
result.join ''
# capitalize :: string -> string
exports.capitalize = (str) ->
(str.char-at 0).to-upper-case! + str.slice 1
# capitalizeSentence :: string -> string
exports.capitalizeSentence = (str) ->
str.replace /(^.|\s.)/g, (, c) -> (c ? '').to-upper-case!
# decapitalize :: string -> string
exports.decapitalize = (str) ->
(str.char-at 0).to-lower-case! + str.slice 1
# decapitalize :: string -> string
exports.decapitalizeSentence = (str) ->
str.replace /(^.|\s.)/g, (, c) -> (c ? '').to-lower-case!
# camelize :: string -> string
exports.camelize = (str) ->
str.replace /[-_]+(.)?/g, (, c) -> (c ? '').to-upper-case!
# dasherize :: string -> string
exports.dasherize = (str) ->
str
.replace /([^-A-Z])([A-Z]+)/g, (, lower, upper) ->
upper =
if upper.length > 1
then upper
else upper.to-lower-case!
lower + '-' + upper
.replace /^([A-Z]+)/, (, upper) ->
if upper.length > 1
then upper + '-'
else upper.to-lower-case!