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