1024programmer Java 5 JavaScript String Processing Libraries You Don’t Know (Summary)

5 JavaScript String Processing Libraries You Don’t Know (Summary)

Handle case conversion, letter symbol removal, Unicode processing, URL processing, etc.

Working with strings can be a tedious task as there are many different use cases. For example, a simple task like converting a string to camelCase may require several lines of code to achieve the final goal.

 function camelize(str) {
  return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
   if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces
   return index === 0 ? match.toLowerCase() : match.toUpperCase();
  });
 }
 

The above code snippet is the most voted answer on StackOverflow. But this also does not solve the case where the string is —-Foo—-bAr—–.

This is where string processing libraries come to the rescue, they make it easy to implement complex string operations and consider all possible use cases for a given problem. This helps you because you only need to call one method to get a working solution.

Let’s look at some Javascript string processing libraries.

1. String.js

string.js, or S for short, is a lightweight Javascript library (less than 5kb minified) for use in the browser or Node.js that provides additional String methods.

Installation

 npm i string

Notable methods

between(left, right) – Extract the string between the left and right strings. You can use this method when trying to get an element between two tags in HTML.

 var S = require('string');
 S('This is a link').between('', '').s
 // 'This is a link'
 

camelize() – Removes any underscores or dashes and converts the string to camelCase. This feature can be used to solve the problem mentioned at the beginning of this article.

 var S = require('string');
 S('---Foo---bAr---').camelize().s;
 //'fooBar'
 

humanize() – Convert input to humanized form. Implementing this feature from scratch would definitely require quite a bit of code.

 var S = require('string');
 S(' capitalize dash-CamelCase_underscore trim ').humanize().s
 //'Capitalize dash camel case underscore trim'
 

stripPunctuation() – Removes all punctuation from the given string. If you implement this functionality from scratch, there’s a good chance you’ll miss punctuation.

 var S = require('string');
 S('My, st[ring] *full* of %punct)').stripPunctuation().s;
 //My string full of punct
 

You can see more methods here.

2. Voca

Voca is a Javascript library for handling strings. The Voca library provides useful functions to make string manipulation a breeze: change case, decorate, pad, flex, latinize, sprintfy, truncate, escape, and more. Modular design allows loading of entire libraries or individual functions to minimize application building. The library is fully tested, well documented, and has long-term support.

Installation

 npm i voca
 

Notable methods

Camel Case(String data) – Convert data to camel case format.

 var v = require('voca');
 v.camelCase('foo Bar');
 // => 'fooBar'

 v.camelCase('FooBar');
 // => 'fooBar'

 v.camelCase('---Foo---bAr---');
 // => 'fooBar'

 

Latinise(String data) – Latinizes the data by removing diacritics.

 var v = require('voca');
 v.latinise('cafe\u0301'); // or 'café'
 // => 'cafe'

 v.latinise('août décembre');
 // => 'aout decembre'

 v.latinise('как прекрасен этот мир');
 // => 'kak prekrasen etot mir'

 

isAlphaDigit(String data) – Checks whether the data contains only alphanumeric characters. (alphanumeric)

 var v = require('voca');
 v.isAlphaDigit('year2020');
 // => true

 v.isAlphaDigit('1448');
 // => true

 v.isAlphaDigit('40-20');
 // => false

 

CountWords(String data) – Count the number of words in the data.

 var v = require('voca');
 v.countWords('gravity can cross dimensions');
 // => 4

 v.countWords('GravityCanCrossDimensions');
 // => 4

 v.countWords('Gravity - can cross dimensions!');
 // => 4

 

EscapeRegExp(String data) – Escape regular expression special characters in data -[]/{}()* +? . \^$|.

 var v = require('voca');
 v.escapeRegExp('(hours)[minutes]{seconds}');
 // => '\(hours\)\[minutes\]\{seconds\}'
 

You can find more information here.

3. Anchorme.js

This is a tiny fast Javascript library that helps detect links/URLs/emails in text and convert them into clickable HTML anchor links.

  • It has high sensitivity and the lowest false alarm rate.
  • It validates URLs and emails against the full IANA list.
  • Verify the port number (if available).
  • Verify the IP octet number if present.
  • Applies to non-Latin URLs.

Installation

 npm i anchorme

Use

 import anchorme from "anchorme";
 // or
 // var anchorme = require("anchorme").default;
 const input = "some text with a link.com";
 const resultA = anchorme(input);
 //some text with a link.com
 

You can pass in additional extensions to further customize functionality.

You can find more information here.

4. Underscore.string

Underscore.string is a string manipulation extension for Javascript that can be used with or without Underscore.js. Underscore.string is a Javascript library for handling strings comfortably, it is an extension of Underscore.js inspired by Prototype.js, Right.js and Underscore.

Underscore.string provides you with several useful functions: capitalize, clear, include, count, escapeHTML, unescapeHTML, insert, splice, startsWith, endsWith, captioning, trim, truncate, etc.

Installation

 npm install underscore.string
 

Notable methods

numberFormat(number) – Format numbers. Format a number into a decimal- and sequence-separated string.

 var _ = require("underscore.string");
 _.numberFormat(1000, 3)
 => "1,000.000"
 _.numberFormat(123456789.123, 5, '.', ',');
 => "123,456,789.12300"
 

chop(string, step) – Cut the given string into small pieces.

 var _ = require("underscore.string");
 _.chop('whitespace', 3);
 => ['whi','tes','pac','e']
 

You can find more information here.

5. Stringz

The main highlight of this library is that it supports unicode. If you run the following code, the output is 2.

 "🤔".length
 // -> 2
 

This is because String.length() returns the number of code units in the string, not the number of characters.

You can read more about Javascript unicode issues here.

Installation

 npm install stringz

Notable methods

limit(string, limit, padString, padPosition) – Limit the string to the given width.

 const stringz = require('stringz');
 // Truncate:
 stringz.limit('Life's like a box of chocolates.', 20);
 // "Life's like a box of"

 // Pad:
 stringz.limit('Everybody loves emojis!', 26, '💩');
 // "Everybody loves emojis!💩💩💩"
 stringz.limit('What are you looking at?', 30, '+', 'left');
 // "++++++What are you looking at?"

 // Unicode Aware:
 stringz.limit('🤔🤔🤔', 2);
 // "🤔🤔"
 stringz.limit('👍🏽👍🏽', 4, '👍🏽');
 // "👍🏽👍🏽👍🏽👍🏽"

toArray(string) – Converts a string to an array.

 const stringz = require('stringz');
 stringz.toArray('abc');
 // ['a','b','c']
 //Unicode aware
 stringz.toArray('👍🏽🍆🌮');
 // ['👍🏽', '🍆', '🌮']
 

To learn more about Stringz, visit its Github here.

Source: blog.bitsrc.io, author: Mahdhi Rezvi, translation: front-end foreign language selection

That’s it for this article about 5 Javascript string processing libraries you don’t know (summary). For more related Javascript string processing libraries, please search previous articles or continue browsing the related ones below. The article hopes that everyone will support it in the future!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/697319

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索