?
This document uses PHP Chinese website manual Release
轉(zhuǎn)換string
。
3.0.0
[string='']
(字符串):要轉(zhuǎn)換的字符串。
(字符串):返回駝峰字符串。
_.camelCase('Foo Bar');// => 'fooBar' _.camelCase('--foo-bar--');// => 'fooBar' _.camelCase('__FOO_BAR__');// => 'fooBar'
將第一個(gè)字符轉(zhuǎn)換string
為大寫(xiě),其余轉(zhuǎn)換為小寫(xiě)。
3.0.0
[string='']
(字符串):要大寫(xiě)的字符串。
(字符串):返回大寫(xiě)的字符串。
_.capitalize('FRED');// => 'Fred'
string
通過(guò)將拉丁文補(bǔ)充-1(https://en.wikipedia.org/wiki/Latin-1 補(bǔ)編(Unicode_block%29#Character_table)和拉丁字母擴(kuò)充-A字母基本拉丁字母和除去組合變音符號(hào)。
3.0.0
[string='']
(字符串):去毛刺的字符串。
(字符串):返回去毛刺字符串。
_.deburr('déjà vu');// => 'deja vu'
檢查是否string
以給定的目標(biāo)字符串結(jié)束。
3.0.0
[string='']
(字符串):要檢查的字符串。
[target]
(字符串):要搜索的字符串。
[position=string.length]
(數(shù)字):最多搜索的位置。
(boolean):返回true
如果string
以target
else 結(jié)束false
。
_.endsWith('abc', 'c');// => true _.endsWith('abc', 'b');// => false _.endsWith('abc', 'b', 2);// => true
將字符“&”,“<”,“>”,“”和“'” string
轉(zhuǎn)換為其對(duì)應(yīng)的HTML實(shí)體。
注意:沒(méi)有其他字符被轉(zhuǎn)義。為了逃避額外的角色,請(qǐng)使用像他這樣的第三方庫(kù)。
雖然“>”字符為了對(duì)稱而被轉(zhuǎn)義,但像“>”和“/”這樣的字符不需要在HTML中轉(zhuǎn)義,除非它們是標(biāo)記或未加引號(hào)屬性值的一部分,否則沒(méi)有特殊含義。有關(guān)更多詳細(xì)信息,請(qǐng)參閱Mathias Bynens的文章 (在“半相關(guān)趣味事實(shí)”下)。
使用HTML時(shí),應(yīng)始終引用屬性值以減少XSS向量。
0.1.0
[string='']
(字符串):要轉(zhuǎn)義的字符串。
(字符串):返回轉(zhuǎn)義字符串。
_.escape('fred, barney, & pebbles');// => 'fred, barney, & pebbles'
轉(zhuǎn)義RegExp
特殊字符“^”,“$”,“”,“?!保?”,“+”,“?”,“(”,“)”,“”,“”,“{”,“ }“和”|“ 中string
。
3.0.0
[string='']
(字符串):要轉(zhuǎn)義的字符串。
(字符串):返回轉(zhuǎn)義字符串。
_.escapeRegExp('[lodash](https://lodash.com/)');// => '\[lodash\]\(https://lodash\.com/\)'
轉(zhuǎn)換string
為Kebab的情況。
3.0.0
[string='']
(字符串):要轉(zhuǎn)換的字符串。
(字符串):返回烤肉串字符串。
_.kebabCase('Foo Bar');// => 'foo-bar' _.kebabCase('fooBar');// => 'foo-bar' _.kebabCase('__FOO_BAR__');// => 'foo-bar'
string
以空格分隔的字詞轉(zhuǎn)換為小寫(xiě)字母。
4.0.0
[string='']
(字符串):要轉(zhuǎn)換的字符串。
(字符串):返回下方的字符串。
_.lowerCase('--Foo-Bar--');// => 'foo bar' _.lowerCase('fooBar');// => 'foo bar' _.lowerCase('__FOO_BAR__');// => 'foo bar'
將第一個(gè)字符轉(zhuǎn)換string
為小寫(xiě)。
4.0.0
[string='']
(字符串):要轉(zhuǎn)換的字符串。
(字符串):返回轉(zhuǎn)換后的字符串。
_.lowerFirst('Fred');// => 'fred' _.lowerFirst('FRED');// => 'fRED'
string
如果它短于左側(cè)和右側(cè)墊length
。如果填充字符不能被平均分割,則會(huì)被截?cái)?code>length。
3.0.0
[string='']
(字符串):要填充的字符串。
[length=0]
(數(shù)字):填充長(zhǎng)度。
[chars=' ']
(字符串):用作填充的字符串。
(字符串):返回填充的字符串。
_.pad('abc', 8);// => ' abc ' _.pad('abc', 8, '_-');// => '_-abc_-_' _.pad('abc', 3);// => 'abc'
墊string
右側(cè),如果它比短length
。填充字符如果超過(guò),則會(huì)被截?cái)?code>length。
4.0.0
[string='']
(字符串):要填充的字符串。
[length=0]
(數(shù)字):填充長(zhǎng)度。
[chars=' ']
(字符串):用作填充的字符串。
(string): Returns the padded string.
_.padEnd('abc', 6);// => 'abc ' _.padEnd('abc', 6, '_-');// => 'abc_-_' _.padEnd('abc', 3);// => 'abc'
墊string
左側(cè),如果是比較短的length
。填充字符如果超過(guò),則會(huì)被截?cái)?code>length。
4.0.0
[string='']
(字符串):要填充的字符串。
[length=0]
(數(shù)字):填充長(zhǎng)度。
[chars=' ']
(字符串):用作填充的字符串。
(字符串):返回填充的字符串。
_.padStart('abc', 6);// => ' abc' _.padStart('abc', 6, '_-');// => '_-_abc' _.padStart('abc', 3);// => 'abc'
轉(zhuǎn)換string
為指定基數(shù)的整數(shù)。如果radix
是undefined
或 0
,一個(gè)radix
的10
使用,除非value
是一個(gè)十六進(jìn)制,在這種情況下radix
的16
使用。
注意:此方法與ES5的實(shí)現(xiàn)一致parseInt
。
1.1.0
string
(字符串):要轉(zhuǎn)換的字符串。
[radix=10]
(數(shù)字):解釋的基數(shù)value
。
(數(shù)字):返回轉(zhuǎn)換后的整數(shù)。
_.parseInt('08');// => 8 _.map(['6', '08', '10'], _.parseInt);// => [6, 8, 10]
重復(fù)給定的字符串n
時(shí)間。
3.0.0
[string='']
(字符串):要重復(fù)的字符串。
[n=1]
(數(shù)字):重復(fù)字符串的次數(shù)。
(字符串):返回重復(fù)的字符串。
_.repeat('*', 3);// => '***' _.repeat('abc', 2);// => 'abcabc' _.repeat('abc', 0);// => ''
替換比賽為pattern
在string
與 replacement
。
注意:此方法基于String#replace
。
4.0.0
[string='']
(字符串):要修改的字符串。
pattern
(RegExp | string):要替換的模式。
replacement
(功能|字符串):匹配替換。
(字符串):返回修改后的字符串。
_.replace('Hi Fred', 'Fred', 'Barney');// => 'Hi Barney'
轉(zhuǎn)換string
為蛇的情況。
3.0.0
[string='']
(字符串):要轉(zhuǎn)換的字符串。
(字符串):返回蛇字符串。
_.snakeCase('Foo Bar');// => 'foo_bar' _.snakeCase('fooBar');// => 'foo_bar' _.snakeCase('--FOO-BAR--');// => 'foo_bar'
拆分string
的separator
。
注意:此方法基于String#split
。
4.0.0
[string='']
(string): The string to split.
separator
(RegExp|string): The separator pattern to split by.
[limit]
(number): The length to truncate results to.
(數(shù)組):返回字符串段。
_.split('a-b-c', '-', 2);// => ['a', 'b']
轉(zhuǎn)換string
為啟動(dòng)大小寫(xiě)。
3.1.0
[string='']
(字符串):要轉(zhuǎn)換的字符串。
(字符串):返回開(kāi)始的套用字符串。
_.startCase('--foo-bar--');// => 'Foo Bar' _.startCase('fooBar');// => 'Foo Bar' _.startCase('__FOO_BAR__');// => 'FOO BAR'
檢查是否string
從給定的目標(biāo)字符串開(kāi)始。
3.0.0
[string='']
(字符串):要檢查的字符串。
[target]
(字符串):要搜索的字符串。
[position=0]
(數(shù)字):從中搜索的位置。
(boolean):返回true
如果string
以target
else 開(kāi)始false
。
_.startsWith('abc', 'a');// => true _.startsWith('abc', 'b');// => false _.startsWith('abc', 'b', 1);// => true
創(chuàng)建一個(gè)編譯的模板函數(shù),可以在“插入”分隔符中插入數(shù)據(jù)屬性,在“轉(zhuǎn)義”分隔符中插入HTML轉(zhuǎn)義插值數(shù)據(jù)屬性,并在“評(píng)估”分隔符中執(zhí)行JavaScript。數(shù)據(jù)屬性可以作為模板中的自由變量來(lái)訪問(wèn)。如果給定設(shè)置對(duì)象,則優(yōu)先于 _.templateSettings
值。
注意:在開(kāi)發(fā)版本中,_.template
利用sourceURL來(lái)更容易地進(jìn)行調(diào)試。
有關(guān)預(yù)編譯模板的更多信息,請(qǐng)參閱lodash的自定義構(gòu)建文檔。
有關(guān)Chrome擴(kuò)展程序沙箱的更多信息,請(qǐng)參閱Chrome的擴(kuò)展程序文檔。
0.1.0
[string='']
(string): The template string.
[options={}]
(Object): The options object.
[options.escape=_.templateSettings.escape]
(RegExp): The HTML "escape" delimiter.
[options.evaluate=_.templateSettings.evaluate]
(RegExp): The "evaluate" delimiter.
[options.imports=_.templateSettings.imports]
(Object): An object to import into the template as free variables.
[options.interpolate=_.templateSettings.interpolate]
(RegExp): The "interpolate" delimiter.
[options.sourceURL='lodash.templateSources[n]']
(string): The sourceURL of the compiled template.
[options.variable='obj']
(string): The data object variable name.
(功能):返回已編譯的模板功能。
// Use the "interpolate" delimiter to create a compiled template.var compiled = _.template('hello <%= user %>!');compiled({ 'user': 'fred' });// => 'hello fred!' // Use the HTML "escape" delimiter to escape data property values.var compiled = _.template('<b><%- value %></b>');compiled({ 'value': '<script>' });// => '<b>&lt;script&gt;</b>' // Use the "evaluate" delimiter to execute JavaScript and generate HTML.var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');compiled({ 'users': ['fred', 'barney'] });// => '<li>fred</li><li>barney</li>' // Use the internal `print` function in "evaluate" delimiters.var compiled = _.template('<% print("hello " + user); %>!');compiled({ 'user': 'barney' });// => 'hello barney!' // Use the ES template literal delimiter as an "interpolate" delimiter.// Disable support by replacing the "interpolate" delimiter.var compiled = _.template('hello ${ user }!');compiled({ 'user': 'pebbles' });// => 'hello pebbles!' // Use backslashes to treat delimiters as plain text.var compiled = _.template('<%= "\\<%- value %\\>" %>');compiled({ 'value': 'ignored' });// => '<%- value %>' // Use the `imports` option to import `jQuery` as `jq`.var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';var compiled = _.template(text, { 'imports': { 'jq': jQuery } });compiled({ 'users': ['fred', 'barney'] });// => '<li>fred</li><li>barney</li>' // Use the `sourceURL` option to specify a custom sourceURL for the template.var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });compiled(data);// => Find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector. // Use the `variable` option to ensure a with-statement isn't used in the compiled template.var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });compiled.source;// => function(data) {// var __t, __p = '';// __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';// return __p;// } // Use custom template delimiters._.templateSettings.interpolate = /{{([\s\S]+?)}}/g;var compiled = _.template('hello {{ user }}!');compiled({ 'user': 'mustache' });// => 'hello mustache!' // Use the `source` property to inline compiled templates for meaningful// line numbers in error messages and stack traces.fs.writeFileSync(path.join(process.cwd(), 'jst.js'), '\ var JST = {\ "main": ' + _.template(mainText).source + '\ };\ ');
string
整體而言,轉(zhuǎn)換為小寫(xiě)字母,就像String#toLowerCase一樣。
4.0.0
[string='']
(字符串):要轉(zhuǎn)換的字符串。
(字符串):返回下方的字符串。
_.toLower('--Foo-Bar--');// => '--foo-bar--' _.toLower('fooBar');// => 'foobar' _.toLower('__FOO_BAR__');// => '__foo_bar__'
string
整體轉(zhuǎn)換為大寫(xiě)字母,就像String#toUpperCase一樣。
4.0.0
[string='']
(字符串):要轉(zhuǎn)換的字符串。
(字符串):返回上方的字符串。
_.toUpper('--foo-bar--');// => '--FOO-BAR--' _.toUpper('fooBar');// => 'FOOBAR' _.toUpper('__foo_bar__');// => '__FOO_BAR__'
從中刪除前導(dǎo)和尾部的空格或指定的字符string
。
3.0.0
[string='']
(字符串):要修剪的字符串。
[chars=whitespace]
(字符串):要修剪的字符。
(字符串):返回修剪后的字符串。
_.trim(' abc ');// => 'abc' _.trim('-_-abc-_-', '_-');// => 'abc' _.map([' foo ', ' bar '], _.trim);// => ['foo', 'bar']
從中刪除尾部空白或指定的字符string
。
4.0.0
[string='']
(字符串):要修剪的字符串。
[chars=whitespace]
(字符串):要修剪的字符。
(字符串):返回修剪后的字符串。
_.trimEnd(' abc ');// => ' abc' _.trimEnd('-_-abc-_-', '_-');// => '-_-abc'
從中刪除前導(dǎo)空格或指定的字符string
。
4.0.0
[string='']
(字符串):要修剪的字符串。
[chars=whitespace]
(字符串):要修剪的字符。
(字符串):返回修剪后的字符串。
_.trimStart(' abc ');// => 'abc ' _.trimStart('-_-abc-_-', '_-');// => 'abc-_-'
string
如果它長(zhǎng)于給定的最大字符串長(zhǎng)度,則截?cái)?。截?cái)嘧址淖詈笠粋€(gè)字符被替換為缺省為“...”的省略字符串。
4.0.0
[string='']
(字符串):要截?cái)嗟淖址?/p>
[options={}]
(對(duì)象):選項(xiàng)對(duì)象。
[options.length=30]
(數(shù)字):最大字符串長(zhǎng)度。
[options.omission='...']
(字符串):省略表示文本的字符串。
[options.separator]
(RegExp | string):要截?cái)嗟姆指舴J健?/p>
(字符串):返回截?cái)嗟淖址?/p>
_.truncate('hi-diddly-ho there, neighborino');// => 'hi-diddly-ho there, neighbo...' _.truncate('hi-diddly-ho there, neighborino', { 'length': 24, 'separator': ' '});// => 'hi-diddly-ho there,...' _.truncate('hi-diddly-ho there, neighborino', { 'length': 24, 'separator': /,? +/});// => 'hi-diddly-ho there...' _.truncate('hi-diddly-ho there, neighborino', { 'omission': ' [...]'});// => 'hi-diddly-ho there, neig [...]'
相反的_.escape
; 這種方法的HTML實(shí)體轉(zhuǎn)換 &amp;
,&lt;
,&gt;
,&quot;
,和 &#39;
在string
其對(duì)應(yīng)的字符。
注意:沒(méi)有其他HTML實(shí)體未轉(zhuǎn)義。為了避免額外的HTML實(shí)體使用像他這樣的第三方庫(kù)。
0.6.0
[string='']
(字符串):unescape的字符串。
(字符串):返回未轉(zhuǎn)義的字符串。
_.unescape('fred, barney, & pebbles');// => 'fred, barney, & pebbles'
string
以空格分隔的字詞轉(zhuǎn)換為大寫(xiě)字母。
4.0.0
[string='']
(字符串):要轉(zhuǎn)換的字符串。
(字符串):返回上方的字符串。
_.upperCase('--foo-bar');// => 'FOO BAR' _.upperCase('fooBar');// => 'FOO BAR' _.upperCase('__foo_bar__');// => 'FOO BAR'
將第一個(gè)字符轉(zhuǎn)換string
為大寫(xiě)。
4.0.0
[string='']
(字符串):要轉(zhuǎn)換的字符串。
(字符串):返回轉(zhuǎn)換后的字符串。
_.upperFirst('fred');// => 'Fred' _.upperFirst('FRED');// => 'FRED'
拆分string
成它的單詞的數(shù)組。
3.0.0
[string='']
(字符串):要檢查的字符串。
[pattern]
(RegExp | string):匹配單詞的模式。
(數(shù)組):返回單詞string
。
_.words('fred, barney, & pebbles');// => ['fred', 'barney', 'pebbles'] _.words('fred, barney, & pebbles', /[^, ]+/g);// => ['fred', 'barney', '&', 'pebbles']