• Stars
    star
    237
  • Rank 163,972 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ’ฏ Super fast unicode-aware string manipulation Javascript library

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.

More Repositories

1

github-contributions-chart

:octocat: Generate an image of all your Github contributions
JavaScript
5,096
star
2

mac-local-rtmp-server

Local RTMP Streaming Server
JavaScript
509
star
3

github-contributions-api

:octocat: Github contributions API (bootleg)
JavaScript
199
star
4

github-contributions-canvas

A tool for drawing a heat-map of Github contributions on HTML Canvas
TypeScript
176
star
5

jDateTime

[UNMAINTAINED] Jalali DateTime Class for PHP
PHP
176
star
6

led-matrix-simulator

๐Ÿ”ฎ A simple HTML5 LED Matrix Simulator for fun
JavaScript
117
star
7

vscode-duotone-dark

DuoTone Dark Sea Syntax theme for Visual Studio Code
JavaScript
84
star
8

SVGDraw

Simple SVG Drawing with Backbone, RequireJS & Snap.svg
JavaScript
56
star
9

serialgraphy

7-Days TV Shows airtime guide. Using CSS3 and AngularJS.
JavaScript
54
star
10

dropbox-fs

๐Ÿ“ฆ Node FS wrapper for Dropbox
JavaScript
36
star
11

persianParser

A javascript library to convert persian text for using on iOS/Android browsers.
JavaScript
36
star
12

rpi-matrix

A simple RaspberryPi clock and browser simulator
TypeScript
32
star
13

country-tools

๐Ÿ‡ช๐Ÿ‡บ A simple NodeJS helper for getting information about countries of the world.
TypeScript
31
star
14

lunchify-swift

๐Ÿด 10โ‚ฌ Lunch Buffet Guide in Finland
Swift
30
star
15

matrix-display-store

Tools for creating Led Matrix content
TypeScript
28
star
16

top-tweets

A simple Node.js app to retweet top tweets on Twitter
JavaScript
26
star
17

laravel-jdate

A Laravel Bundle to help working with Jalali (Shamsi) Dates
PHP
26
star
18

about

๐Ÿ‘จ About Me - A summary of who I am, what I like and what I work on.
21
star
19

led-matrix

An HTML5 Canvas LED Matrix Simulator
TypeScript
18
star
20

promise-arrays

A set of tools for working with arrays in an async manner
JavaScript
17
star
21

vscode-json-to-js-object

VSCode extension for converting JSON objects to JavaScript objects
TypeScript
16
star
22

booksy-app

TypeScript
14
star
23

node-mac-app-icon

Get App icons by pid on macOS in NodeJS
JavaScript
12
star
24

sqs-parallel

Simple NodeJS wrapper for AWS SQS with concurrency support
TypeScript
12
star
25

typescript-api-starter

A simple functional typescript API starter
TypeScript
10
star
26

rwd-conf-92

Website for 1st Responsive Design Conference in Iran.
CSS
10
star
27

GetAppIcon

Get App icons by pid on macOS
Swift
8
star
28

farsi-blog

JavaScript
8
star
29

styled-flexbox

JavaScript
7
star
30

gatsby-plugin-css-modules-typings

A Gatsby plugin that provides typings for css modules
JavaScript
7
star
31

booksy-api

Booksy API
TypeScript
7
star
32

lunchify-react-native

JavaScript
6
star
33

lunchify-scraper

JavaScript
6
star
34

unicode-astral-regex

A regex to match astral symbols that supports unicode strings.
JavaScript
5
star
35

lunchify-api

JavaScript
5
star
36

buttercup-desktop

JavaScript
5
star
37

ng-game-of-life

Game of Life written in Rust, UI in Angular
TypeScript
5
star
38

dotfiles

My ~/.dotfiles
Shell
3
star
39

lunchify-cordova

JavaScript
3
star
40

github-contribs-canvas-playground

Created with CodeSandbox
JavaScript
2
star
41

secure-dev-server

Development proxy server with SSL support
JavaScript
2
star
42

booksy-app-backup

TypeScript
2
star
43

rpi-matrix-homekit

HomeKit Integration for Rpi Matrix Project
JavaScript
2
star
44

homebridge-nibe

TypeScript
2
star
45

sms-collection-convert

JavaScript
2
star
46

keyboard-designer

JavaScript
2
star
47

game-of-life

Conway's Game of Life in Rust
JavaScript
1
star
48

lunchify

Venues for lunch around Keilaniemi
HTML
1
star
49

react-native-week-strip

TypeScript
1
star
50

wlf

CSS
1
star
51

firebase-explorer

TypeScript
1
star
52

sallar

1
star
53

kurdishtap

Swift
1
star
54

rn-stacks

TypeScript
1
star
55

check-os-tray-support

Check if the current OS supports showing Tray (useful for Electron apps)
JavaScript
1
star
56

rpi-matrix-pwa

JavaScript
1
star
57

js-pong-clock

JavaScript
1
star