Skip to content
forked from sallar/stringz

💯 Zero dependency unicode-aware string tools for NodeJS

License

Notifications You must be signed in to change notification settings

Canedo/stringz

Repository files navigation

Stringz Build Statuscodecov

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

JavaScript Style Guide

Features

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

Install

$ npm install stringz --save

And import it in your awesome node app:

// ES2015+import*asstringzfrom'stringz';// OR:import{limit,substring,length,substr}from'stringz';// CommonJSvarstringz=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 🍆"

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 🇮🇹"

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 (accruate) x 861,039 ops/sec ±1.57% (84 runs sampled) Lodash .toArray (accruate) 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

VersionDateNotes
0.2.22017-06-20Fix Typescript Definition Issue #14
0.2.12017-05-27Add Typescript Definitions
0.2.02017-04-30New substr function
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.

About

💯 Zero dependency unicode-aware string tools for NodeJS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript100.0%