简体   繁体   English

如何在Lodash中使用特殊字符(如'&')格式化字符串?

[英]How to format strings with special characters like '&' in Lodash?

I'm using Lodash to format a string like DIGITAL & TECHNOLOGY into Digital & Technology . 我正在使用Lodash将DIGITAL & TECHNOLOGY类的字符串格式化为Digital & Technology I've tried with the following code: 我尝试使用以下代码:

_.startCase(_.lowerCase('DIGITAL & TECHNOLOGY'));

However, this returns me Digital Technology . 但是,这使我回到了Digital Technology

This didn't work either: 这也不起作用:

var words = _.words('DIGITAL & TECHNOLOGY', /[^ ]+/g);
var newWords = '';
_.forEach(words, function(w){
  newWords += _.capitalize(w);
})

This returns Digital & technology . 这将返回Digital & technology

What would be an appropriate way to properly format strings like these using Lodash only? 仅使用Lodash正确格式化此类字符串的合适方法是什么? Or must I use a mix of Lodash and Javascript? 还是我必须混合使用Lodash和Javascript?

Replace each sequence of word characters, with a capitalized version: 用大写字母替换每个单词字符序列:

 var str = 'DIGITAL & TECHNOLOGY'; var result = str.replace(/\\w+/g, _.capitalize); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> 

First to clarify, there's no such thing as lodash without javascript. 首先要弄清楚,没有javascript就没有lodash之类的东西。

Now that that's cleared up, try this on for size: 现在已经清除了,尝试以下大小:

_.map(_.split('DIGITAL & TECHNOLOGY', /\s+/), _.capitalize).join(' ')

This splits up the words between arbitrary amounts of space, maps each one to capitalize, and rejoins them into a string (you'll notice that calling capitalize on just the ampersand is safe). 这分裂了空间任意金额的话,将每个一个大写,并把它们重新加入到一个字符串(你会发现,呼吁capitalize只是符号是安全的)。


EDIT: I wanted to see if I could make this more reusable by turning the above statement into a function with just lodash. 编辑:我想看看是否可以通过将上面的语句转换为只包含lodash的函数来使其更可重用。 Here's what I came up with: 这是我想出的:

const split = _.partialRight(_.split, /\s+/)
    , map   = _.partialRight(_.map, _.capitalize)
    , join  = _.partialRight(_.join, ' ');
const mycapitalize = _.flow([split, map, join]);

mycapitalize('DIGITAL & TECHNOLOGY');  // => "Digital & Technology"

I like this approach because it semantically lays out the steps taken to process the string (ie first you split it, then you map it, then you join it). 我喜欢这种方法,因为它在语义上列出了处理字符串的步骤(即,首先将其拆分,然后将其映射,然后再将其加入)。 Here it is as "one-liner" if that's your thing: 如果这是您的事情,它就是“单线”:

const mycapitalize = _.flow([
    _.partialRight(_.split, /\s+/)
  , _.partialRight(_.map, _.capitalize)
  , _.partialRight(_.join, ' ')
]);

mycapitalize('DIGITAL & TECHNOLOGY');  // => "Digital & Technology"

jsfiddle demo jsfiddle演示

Just to elaborate on what I did here: partialRight is kind of like a special call to bind . 只是为了详细说明我在这里所做的事情: partialRight有点像对bind的特殊调用。 It partially applies the function (which is basically a way to say that it returns the function with one of the arguments hard coded). 它部分地应用了函数(这基本上是说它返回带有硬编码参数之一的函数的一种方式)。 Conventionally, partial methods apply arguments in order, so partialRight applies them in reverse order (ie if a function has two parameters, partial would "hard code" the first one while partialRight would "hard code" the second/ last one). 按照惯例, partial方法按顺序应用参数,所以partialRight按相反的顺序应用参数(即,如果一个函数有两个参数, partial将“硬编码”第一个参数,而partialRight将“硬编码”第二个/最后一个)。 For example, _.split takes two arguments, which are, in order, a string to split and a pattern to split it up by. 例如, _.split接受两个参数,依次是要拆分的字符串和用于拆分的模式。 _.partial(_.split, 'foo bar') gives a function that takes a pattern to split up "foo bar" on, while _.partialRight(_.split, /\\s+/) gives a function that takes a string to split on /\\s+/ . _.partial(_.split, 'foo bar')提供了一个函数,该函数采用一种模式来分割"foo bar" ,而_.partialRight(_.split, /\\s+/)提供了一个采用字符串的函数在/\\s+/上分割。

I apologize for any excess verbosity. 对于任何多余的详细信息,我深表歉意。

The other part of this solution is _.flow . 此解决方案的另一部分是_.flow Flow is basically lodash parlance for compose (though most implementations of compose tend to apply functions in the opposite order; from right to left). 基本上是撰写 lodash用语(虽然撰写的大多数实现倾向于以相反的顺序应用功能;从右到左)。 _.flow takes an array of functions and returns a single function that applies each function in that array in order. _.flow接受一个函数数组,并返回一个函数,该函数按顺序应用该数组中的每个函数。 For example, _.flow([add1, mult2]) would give you a function that adds one to a number, then multiplies it by two and returns the result. 例如, _.flow([add1, mult2])将为您提供一个函数,该函数将一个数字加一,然后将其乘以2并返回结果。

In this context, flow takes our custom operations that we created with partialRight and applies them in order to the argument it gets. 在这种情况下, flow接受了我们使用partialRight创建的自定义操作,并将其应用到它获得的参数上。 In this case, that means it gets a string, splits it on /\\s+/ , maps to _.capitalize , and joins on ' ' , in that order. 在这种情况下,这意味着它将获得一个字符串,在/\\s+/ _.capitalize其分割,映射到_.capitalize ,并以该顺序在' '上进行连接。

This might be over-engineered for your purposes but it was fun to put together nonetheless. 可能出于您的目的过度设计了这些,但是仍然很有趣。

You can use _.capitalize to capitalize the first character, and then use regular expression to replace all other first letters of words: 您可以使用_.capitalize大写第一个字符,然后使用正则表达式替换所有其他的第一个字母:

 var result = _.capitalize('DIGITAL & TECHNOLOGY').replace(/\\s(\\S)/g, function(v) { return v.toUpperCase(); }); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> 

Lodash has another method, toLower, that takes the string as a whole and not as space separated words like lowerCase. Lodash还有另一种方法toLower,该方法将字符串作为一个整体,而不是像lowerCase这样的空格分隔的单词。

https://lodash.com/docs/4.17.4#toLower https://lodash.com/docs/4.17.4#toLower

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM