npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

stringz

v2.1.0

Published

Zero-dependency unicode-aware string tools

Downloads

1,331,189

Readme

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]]])

| Param | Type | Default | Description | | ----------- | ------------------- | -------------------- | --------------------------------------------------------- | | str | String | none | The string to be limited | | limit | Number | 16 | Desired string length | | padStr | String | "#" | Character to pad the output with | | padPosition | String | "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)

| Param | Type | Default | Description | | ----- | ------------------- | ------- | ------------------------------- | | str | String | none | String to return the length for |

Examples

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

Substring

function substring(str, start[, end])

| Param | Type | Default | Description | | ----- | ------------------- | ------------- | -------------------- | | str | String | none | String to be devided | | start | Number | none | Start position | | end | Number | End of string | End position |

Examples

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

Substr

function substr(str[, start[, length]])

| Param | Type | Default | Description | | ------ | ------------------- | ------------------------------------- | -------------------- | | str | String | none | String to be devided | | start | Number | Start of string | Start position | | length | Number | String length minus start parameter | Length of result |

Examples

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

IndexOf

function indexOf(str[, searchStr[, position]])

| Param | Type | Default | Description | | --------- | ------------------- | ------- | --------------------- | | str | String | none | String to get index | | searchStr | String | none | String to be searched | | position | Number | 0 | Start 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)

| Param | Type | Default | Description | | ----- | ------------------- | ------- | -------------------------- | | str | String | none | String 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.