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;var myString = SS;
For creating empty string just call function without parameters
var emptyString =
This return Object of type SmartString
For Getting value use value
method
var v =
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;var options = ;var x = // x is "My name is {{name}}, I love {{thing}}" myString.substitute;// now value method will return new string.x = // 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;var options = ;var x = // x is "My name is {{name}}, I love {{thing}}" var y = myString.returnSubstitute;// y is "My name is John, I love Node.JS"// but value of string is not changed.x = // 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;x.equalToString // return truex.equalToString // return falsex.equalToString // return truex.equalToString // return false
relevance
Takes argument of type SmartString and returns integer in range 0-1 and shows how relevant are strings.
var x = SS;var y = SS;x.relevance; // returns 1var z = SS;x.relevance; // returns 0.666 ...var k = SS;x.relevance; // 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;var y = SS;console.log;//returns 1 because x contains on yconsole.log;// returns 0.2727272727272727 because only part of y contains on x y = SS;console.log;//returns 1. For search "Smith" and "smith" are same y = SS;console.log;//returns 1. For search "John Smith" and "smith John" are same var k = SS;console.log;//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;console.log