JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 382487
  • Score
    100M100P100Q178035F
  • License MIT

Zero-dependency unicode-aware string tools

Package Exports

  • stringz

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (stringz) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.

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]]])
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("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)
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 ๐Ÿ†"

Test

$ npm test

Changelog

Version Date Notes
0.1.0 2016-07-29 Renamed to Stringz, more tools
0.0.10 2016-07-29 Fixed substring issue
0.0.9 2016-07-28 Fixed unicode string length issue
0.0.8 2016-07-26 First usable release

License

This software is released under the MIT License.

Copyright ยฉ 2016 Sallar Kaboli <sallar.kaboli@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the โ€œSoftwareโ€), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED โ€œAS ISโ€, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.