![](/img/trans.png)
[英]Do you have to check for null & length or is there a shorter way to verify a non-empty string?
[英]Is there a much more cleaner and shorter way to check if string is empty?
我正在我的快遞服務器上工作,我正在嘗試驗證一些查詢參數。 例如,我可以有查詢參數date
,它可能是 null 或空字符串。 現在當前的驗證是這樣的
if(req.query.date && req.query.date !== '')
有沒有像 function 或像_lodash
這樣我可以使用的庫來保持更整潔?
您的解決方案已經很簡潔了,您可以將其簡化為
if(req.query.date)
盡量不要過度使用lodash
或其他包,你可以自己編寫這些東西,但是所有這些多余的包只會淹沒你的包,讓它變得更重。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.