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

smart-string

v0.0.1

Published

String directory

Downloads

535

Readme

Smart - String

Install with npm

$ npm install smart-string

Or download from github

Usage

Create Instance

smart-string is using Factory design pattern, so you cant use new method. For creating new instance use SS( str ) function.

var SS = require('smart-string');
var myString = SS( "This is my first string" );

For creating empty string just call function without parameters

var emptyString = SS();

This return Object of type SmartString

For Getting value use value method

var v = myString.value();

Substitite

SS can substitute string. substitute method takes options and replaces all substitutable parameters in string. For making substitutable word use {{ and }} symbols.

For example

var myString = SS( "My name is {{name}}, I love {{thing}}" );
var options = {
    name: "John",
    thing: "Node.JS"
};
var x = mystring.value();
// x is "My name is {{name}}, I love {{thing}}"

myString.substitute( options );
// now value method will return new string.
x = mystring.value();
// x now is "My name is John, I love Node.JS"

NOTE: if some data cant be found in options object, method will just erase scopes.

There is also a method that substitute string, but does not change value, just return new string

var myString = SS( "My name is {{name}}, I love {{thing}}" );
var options = {
    name: "John",
    thing: "Node.JS"
};
var x = mystring.value();
// x is "My name is {{name}}, I love {{thing}}"

var y = myString.returnSubstitute( options );
// y is "My name is John, I love Node.JS"
// but value of string is not changed.
x = mystring.value();
// x is still "My name is {{name}}, I love {{thing}}"

Compare

Three are few method for comparing.

equalToString

This method takes argument string or SmartString and return true only if strings are exactly the same.

var x = SS("First String");
x.equalToString("First String") // return true
x.equalToString(" First String") // return false
x.equalToString( SS( "First String") ) // return true
x.equalToString( SS(" First String" )) // return false

relevance

Takes argument of type SmartString and returns integer in range 0-1 and shows how relevant are strings.

var x = SS("Hi John Smith");
var y = SS("Hi Smith john");
x.relevance( y ); // returns 1
var z = SS("Hello John Smith");
x.relevance( y ); // returns 0.666 ...
var k = SS("Hello Jack Smith");
x.relevance( y ); // returns 0.333 ...

search

this method is full text search. takes argument of type SmartString and returns integer in range 0-1. And shows how is string relevant to our parameter.

var x = SS("Hi John Smith. My Name is Gor. Nice to meet you");
var y = SS("Hi John Smith");
console.log( x.search( y ));
//returns 1 because x contains on y
console.log( y.search( x ));
// returns 0.2727272727272727 because only part of y contains on x

y = SS("Hi John smith");
console.log( y.search( x ));
//returns 1. For search "Smith" and "smith" are same

y = SS("Hi smith John");
console.log( y.search( x ));
//returns 1. For search "John Smith" and "smith John" are same

var k = SS("Hi Jack Smith");
console.log( x.search( k ));
//returns 0.6666666666666666. Only two word from k contains on y

If you want to get all terms on string, just use terms method. It will return all terms in lowercase.

var x = SS("Hi John Smith. My Name is Gor. Nice to meet you");
console.log( x.terms() );
// returns [ 'hi', 'john', 'smith', 'my', 'name', 'is', 'gor', 'nice', 'to', 'meet', 'you' ]