OSDN Git Service

Regular updates
[twpd/master.git] / underscore-string.md
1 ---
2 title: Underscore-string
3 category: JavaScript libraries
4 layout: 2017/sheet
5 ---
6
7 ### Usage
8
9     // Use it like so:
10     _.str.trim("hey");
11     _s.trim("hey");
12
13     // Unless you do:
14     _.mixin(_.string.exports());
15
16     // So you can:
17     _.trim("hey");
18     _("hey").trim();
19
20 ### Trimming
21
22     _.truncate("Hello world", 4) // => "Hell..."
23     _.prune("Hello world", 5)    // => "Hello..."
24
25     _.trim(" foo ")              // => "foo"
26     _.trim("-foo-", '-')         // => "foo"
27     _.ltrim
28     _.rtrim
29
30 ### Numbers
31
32     _.numberFormat(1000, 2)  // => "1,000.00"
33
34 ### Caps
35
36     _.capitalize("foo bar")       // => "Foo Bar"
37     _.humanize("hey-there foo")   // => "Hey there foo"
38     _.titleize('My name is hi')   // => "My Name Is Hi"
39
40     _.dasherize('MozTransform')   // => "-moz-transform"
41     _.underscored('MozTransform') // => "moz_transform"
42     _.classify('-moz-transform')  // => "MozTransform"
43     _.camelize('moz_transform')   // => "MozTransform"
44
45     _.slugify("hey there")        // => "hey-there"
46
47     _.swapCase("hELLO")           // => "Hello"
48
49 ### Checks
50
51     _.startsWith('image.gif', 'image') // => true
52     _.endsWith('image.gif', '.gif')    // => true
53     _.isBlank(" ")                     // => true (also for "\n", "")
54
55 ### HTML
56
57     _.escapeHTML("<div>")
58     _.unescapeHTML("&lt;div&gt;")
59     _.stripTags("<div>hi</div>")
60
61 ### Quote
62
63     _.quote("hi", '"') // => '"hi"'
64     _.unquote('"hi"')  // => "hi"
65
66 ### Splits
67
68     _.lines("hi\nthere")     // => ["hi","there"]
69     _.words("hi  there you") // => ["hi","there","you"]
70
71 ### Sprintf
72
73     _.sprintf("%.1f", 1.17)
74
75 ### Pad
76
77     _.pad("1", 8)               // => "       1"
78     _.pad("1", 8, "0")          // => "00000001"
79     _.pad("1", 8, " ", "right") // => "1       "
80     _.pad("1", 8, " ", "both")  // => "    1   "
81
82     _.lpad(..)  // same as _.pad(.., 'left')
83     _.rpad(..)  // same as _.pad(.., 'right')
84     _.lrpad(..) // same as _.pad(.., 'both')
85
86 ### References
87
88 * https://github.com/epeli/underscore.string