You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

stringz

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stringz

Zero-dependency unicode-aware string tools

2.1.0
latest
Source
npmnpm
Version published
Weekly downloads
338K
-16.59%
Maintainers
1
Weekly downloads
 
Created
Source

Stringz Build Status codecov npm

A really small, performant, unicode-aware library for working with Strings in Node.js.

Javascript has a serious problem with unicode. Even ES6 can’t solve the problem entirely since some characters like the new colored emojis are three bytes instead of two bytes. Sometimes even more! "👍🏽".length returns 4 which is totally wrong (hint: it should be 1!). ES6's Array.from tried to solve this, but that even fails: Array.from("👍🏽") returns ["👍", "🏽"] which is incorrect. This library tries to tackle all these problems with a mega RegExp. Read More Here.

Features

  • Unicode-aware string manipulation tools
  • High performance

Install

$ npm install stringz --save

And import it in your awesome node app:

// ES2015+
import * as stringz from 'stringz'; // OR:
import { limit, substring, length, substr } from 'stringz';
// CommonJS
const stringz = require('stringz'); // OR:
const { limit, substr } = require('stringz');

Usage

Limit String to Width

function limit(str[, limit[, padStr[, padPosition]]])
ParamTypeDefaultDescription
strStringnoneThe string to be limited
limitNumber16Desired string length
padStrString"#"Character to pad the output with
padPositionString"right"Pad position: "right" or "left"

Examples

// Truncate:
limit('Life’s like a box of chocolates.', 20); // "Life's like a box of"

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

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

String Length

function length(str)
ParamTypeDefaultDescription
strStringnoneString to return the length for

Examples

length('Iñtërnâtiônàlizætiøn☃💩'); // 22

Substring

function substring(str, start[, end])
ParamTypeDefaultDescription
strStringnoneString to be devided
startNumbernoneStart position
endNumberEnd of stringEnd position

Examples

substring('Emojis 👍🏽 are 🍆 poison. 🌮s are bad.', 7, 14); // "👍🏽 are 🍆"

Substr

function substr(str[, start[, length]])
ParamTypeDefaultDescription
strStringnoneString to be devided
startNumberStart of stringStart position
lengthNumberString length minus start parameterLength of result

Examples

substr('A.C. Milan 🇮🇹⚽️', 5, 7); // "Milan 🇮🇹"

IndexOf

function indexOf(str[, searchStr[, position]])
ParamTypeDefaultDescription
strStringnoneString to get index
searchStrStringnoneString to be searched
positionNumber0Start of searching

Examples

indexOf('Emojis 👍🏽 are 🍆 poison. 🌮s are bad.', 'are'); // 9
indexOf('Emojis 👍🏽 are 🍆 poison. 🌮s are bad.', 'are', 10); // 26

ToArray

function toArray(str)
ParamTypeDefaultDescription
strStringnoneString to convert to array

Examples

toArray('👍🏽🍆🌮'); // ['👍🏽', '🍆', '🌮']

Test

$ npm test

Benchmark

This library scores high in a length benchmark (it's intended usage) and should be fast for most use case.

Stringz .length (accurate) x 861,039 ops/sec ±1.57% (84 runs sampled)
Lodash .toArray (accurate) x 795,108 ops/sec ±2.13% (82 runs sampled)
Emoji Aware .split (inaccurate) x 2,269 ops/sec ±1.38% (85 runs sampled)
Spliddit .length (inaccurate) x 487,718 ops/sec ±2.21% (83 runs sampled)
UTF8 Length (inaccurate) x 232,918 ops/sec ±1.02% (87 runs sampled)
Fastest is Stringz .length

To run benchmarks yourself:

$ cd ./benchmark
$ npm install
$ node run.js

Changelog

Moved to CHANGELOG.md

License

This software is released under the MIT License.

Keywords

string

FAQs

Package last updated on 21 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts