OSDN Git Service

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