Referência rápida

Guia Rápido de Underscore.string

Guia de referência para a biblioteca underscore.string no JavaScript: conversão de caixas, preenchimento, limpeza e formatação numérica.

· Publicado

O underscore.string fornece extensões úteis para manipulação de strings em JavaScript.

Conversão de maiúsculas/minúsculas e formatos

s.capitalize("foo bar")       //=> "Foo bar"
s.humanize("hey-there foo")   //=> "Hey there foo"
s.titleize('my name is hi')   //=> "My Name Is Hi"

s.dasherize('MozTransform')   //=> "-moz-transform"
s.underscored('MozTransform') //=> "moz_transform"
s.classify('-moz-transform')  //=> "MozTransform"
s.camelize('moz_transform')   //=> "mozTransform"

s.slugify("hey there")        //=> "hey-there"
s.swapCase("hELLO")           //=> "Hello"

Inspeções de strings

s.startsWith('image.gif', 'image') //=> true
s.endsWith('image.gif', '.gif')    //=> true
s.isBlank("   ")                   //=> true

Entidades HTML e remoção de tags

s.escapeHTML("<div>")       //=> "&lt;div&gt;"
s.unescapeHTML("&lt;div&gt;") //=> "<div>"
s.stripTags("<div>hi</div>")  //=> "hi"

Aspas e divisão de strings

s.quote("hi", '"')    //=> '"hi"'
s.unquote('"hi"')     //=> "hi"

s.lines("hi\nthere")   //=> ["hi", "there"]
s.words("hi  there")  //=> ["hi", "there"]

Preenchimento (Padding) e formatação

s.pad("1", 8)               //=> "       1"
s.pad("1", 8, "0")          //=> "00000001"
s.pad("1", 8, " ", "right") //=> "1       "
s.pad("1", 8, " ", "both")  //=> "    1   "

s.numberFormat(1000, 2)     //=> "1,000.00"
s.sprintf("%.1f", 1.17)     //=> "1.2"

Referências

Temas relacionados