New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
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

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
317K
decreased by-1.25%
Maintainers
1
Weekly downloads
 
Created
Source

Stringz Build Status codecov

A really small, zero-dependency, 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.

🎈 Based on a RegExp copied from the Lodash library.

Features

  • Limit string to width (truncate/pad)
  • Unicode-aware string length
  • Unicode-aware substring

🔥 Please note that this library is built for accuracy, not performance. It uses complex regular expressions to calculate the string length and perform other operations which are not particularly super-jawdropping-fast like the native String.prototype.length.

Install

$ npm install stringz --save

And import it in your awesome node app:

// ES2015+
import * as stringz from 'stringz'; // OR:
import { limit, substring, length } from 'stringz';

// CommonJS
var stringz = require('stringz');
// use like: stringz.limit ...

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("Make emojis great again", 26, "💩"); // "Make emojis great again💩💩💩"
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 🍆"

Test

$ npm test

Changelog

VersionDateNotes
0.1.22017-04-25Fix null length issue #8
0.1.12016-07-31More strict type checking, more tests
0.1.02016-07-29Renamed to Stringz, more tools
0.0.102016-07-29Fixed substring issue
0.0.92016-07-28Fixed unicode string length issue
0.0.82016-07-26First usable release

License

This software is released under the MIT License.

Uses a RegExp from the Lodash which is released under the MIT License.

Keywords

FAQs

Package last updated on 25 Apr 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc