UNPKG

1.49 kBtext/coffeescriptView Raw
1{curry, compose} = require "fairmont-core"
2
3toString = (x) -> x.toString()
4
5toUpper = (s) -> s.toUpperCase()
6
7toLower = (s) -> s.toLowerCase()
8
9plainText = (string) ->
10 string
11 .replace( /^[A-Z]/g, (c) -> c.toLowerCase() )
12 .replace( /[A-Z]/g, (c) -> " #{c.toLowerCase()}" )
13 .replace( /\W+/g, " " )
14
15capitalize = (string) ->
16 string[0].toUpperCase() + string[1..]
17
18titleCase = (string) ->
19 string
20 .toLowerCase()
21 .replace(/^(\w)|\W(\w)/g, (char) -> char.toUpperCase())
22
23camelCase = (string) ->
24 string.toLowerCase().replace(/(\W+\w)/g, (string) ->
25 string.trim().toUpperCase())
26
27underscored = (string) -> plainText(string).replace(/\W+/g, "_")
28
29dashed = (string) -> plainText(string).replace(/\W+/g, "-")
30
31htmlEscape = do ->
32
33 map =
34 "&": "&"
35 "<": "&lt;"
36 ">": "&gt;"
37 '"': '&quot;'
38 "'": '&#39;'
39 "/": '&#x2F;'
40
41 entities = Object.keys( map )
42 re = new RegExp( "#{entities.join('|')}", "g" )
43 (string) -> string.replace( re, (s) -> map[s] )
44
45trim = (s) -> s.trim()
46
47split = curry (re, s) -> s.split re
48
49w = compose (split /\s+/), trim
50
51blank = (s) -> s.length == 0
52
53match = curry (pattern, string) -> string.match pattern
54
55isMatch = curry (pattern, string) -> pattern.test string
56
57replace = curry (pattern, replacement, string) ->
58 string.replace pattern, replacement
59
60module.exports = {toString, toUpper, toLower, capitalize,
61 titleCase, camelCase, underscored, dashed, plainText,
62 htmlEscape, split, w, blank, match, isMatch, replace}