在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:esamattis/underscore.string开源软件地址:https://github.com/esamattis/underscore.string开源编程语言:JavaScript 100.0%开源软件介绍:The stable release documentation can be found here https://epeli.github.io/underscore.string/ Underscore.stringJavascript lacks complete string manipulation operations. This is an attempt to fill that gap. List of build-in methods can be found for example from Dive Into JavaScript. Originally started as an Underscore.js extension but is a full standalone library nowadays. Upgrading from 2.x to 3.x? Please read the changelog. UsageFor Node.js, Browserify and WebpackInstall from npm
Require individual functions var slugify = require("underscore.string/slugify");
slugify("Hello world!");
// => hello-world or load the full library to enable chaining var s = require("underscore.string");
s(" epeli ").trim().capitalize().value();
// => "Epeli" but especially when using with Browserify the individual function approach is recommended because using it you only add those functions to your bundle you use. In MeteorFrom your Meteor project folder meteor add underscorestring:underscore.string and you'll be able to access the library with the s global from both the server and the client. s.slugify("Hello world!");
// => hello-world
s(" epeli ").trim().capitalize().value();
// => "Epeli" OthersThe Underscore.js/Lo-Dash integrationIt is still possible use as Underscore.js/Lo-Dash extension _.mixin(s.exports()); But it's not recommended since Lo-Dash-FP/Ramda integrationIf you want to use underscore.string with ramdajs or Lo-Dash-FP you can use underscore.string.fp.
var S = require('underscore.string.fp');
var filter = require('lodash-fp').filter;
var filter = require('ramda').filter;
filter(S.startsWith('.'), [
'.vimrc',
'foo.md',
'.zshrc'
]);
// => ['.vimrc', '.zshrc'] Download
APIIndividual functionsnumberFormat(number, [ decimals=0, decimalSeparator='.', orderSeparator=',']) => stringFormats the numbers. numberFormat(1000, 2);
// => "1,000.00"
numberFormat(123456789.123, 5, ".", ",");
// => "123,456,789.12300" levenshtein(string1, string2) => numberCalculates [Levenshtein distance][ld] between two strings. [ld]: http://en.wikipedia.org/wiki/Levenshtein_distance levenshtein("kitten", "kittah");
// => 2 capitalize(string, [lowercaseRest=false]) => stringConverts first letter of the string to uppercase. If capitalize("foo Bar");
// => "Foo Bar"
capitalize("FOO Bar", true);
// => "Foo bar" decapitalize(string) => stringConverts first letter of the string to lowercase. decapitalize("Foo Bar");
// => "foo Bar" chop(string, step) => arraychop("whitespace", 3);
// => ["whi", "tes", "pac", "e"] clean(string) => stringTrim and replace multiple spaces with a single space. clean(" foo bar ");
// => "foo bar" cleanDiacritics(string) => stringReplace diacritic characters with closest ASCII equivalents. Check the source for supported characters. Pull requests welcome for missing characters! cleanDiacritics("ääkkönen");
// => "aakkonen" chars(string) => arraychars("Hello");
// => ["H", "e", "l", "l", "o"] swapCase(string) => stringReturns a copy of the string in which all the case-based characters have had their case swapped. swapCase("hELLO");
// => "Hello" include(string, substring) => booleanTests if string contains a substring. include("foobar", "ob");
// => true count(string, substring) => numberReturns number of occurrences of substring in string. count("Hello world", "l");
// => 3 escapeHTML(string) => stringConverts HTML special characters to their entity equivalents. This function supports cent, yen, euro, pound, lt, gt, copy, reg, quote, amp, apos. escapeHTML("<div>Blah blah blah</div>");
// => "<div>Blah blah blah</div>" unescapeHTML(string) => stringConverts entity characters to HTML equivalents. This function supports cent, yen, euro, pound, lt, gt, copy, reg, quote, amp, apos, nbsp. unescapeHTML("<div>Blah blah blah</div>");
// => "<div>Blah blah blah</div>" insert(string, index, substring) => stringinsert("Hellworld", 4, "o ");
// => "Hello world" replaceAll(string, find, replace, [ignorecase=false]) => stringreplaceAll("foo", "o", "a");
// => "faa" isBlank(string) => booleanisBlank(""); // => true
isBlank("\n"); // => true
isBlank(" "); // => true
isBlank("a"); // => false join(separator, ...strings) => stringJoins strings together with given separator join(" ", "foo", "bar");
// => "foo bar" lines(str) => arraySplit lines to an array lines("Hello\nWorld");
// => ["Hello", "World"] wrap(str, options) => stringSplits a line wrap("Hello World", { width:5 })
// => "Hello\nWorld"
wrap("Hello World", { width:6, seperator:'.', trailingSpaces: true })
// => "Hello .World "
wrap("Hello World", { width:5, seperator:'.', cut:true, trailingSpaces: true })
// => "Hello. Worl.d "
wrap("Hello World", { width:5, seperator:'.', preserveSpaces: true })
// => "Hello .World" dedent(str, [pattern]) => stringDedent unnecessary indentation or dedent by a pattern. Credits go to @sindresorhus. This implementation is similar to https://github.com/sindresorhus/strip-indent dedent(" Hello\n World");
// => "Hello\n World"
dedent("\t\tHello\n\t\t\t\tWorld");
// => "Hello\n\t\tWorld"
dedent(" Hello\n World", " "); // Dedent by 2 spaces
// => " Hello\n World" reverse(string) => stringReturn reversed string: reverse("foobar");
// => "raboof" splice(string, index, howmany, substring) => stringLike an array splice. splice("https://[email protected]/edtsech/underscore.strings", 30, 7, "epeli");
// => "https://[email protected]/epeli/underscore.strings" startsWith(string, starts, [position]) => booleanThis method checks whether the string begins with startsWith("image.gif", "image");
// => true
startsWith(".vimrc", "vim", 1);
// => true endsWith(string, ends, [position]) => booleanThis method checks whether the string ends with endsWith("image.gif", "gif");
// => true
endsWith("image.old.gif", "old", 9);
// => true pred(string) => stringReturns the predecessor to str. pred("b");
// => "a"
pred("B");
// => "A" succ(string) => stringReturns the successor to str. succ("a");
// => "b"
succ("A");
// => "B" titleize(string) => stringtitleize("my name is epeli");
// => "My Name Is Epeli" camelize(string, [decapitalize=false]) => stringConverts underscored or dasherized string to a camelized one. Begins with a lower case letter unless it starts with an underscore, dash or an upper case letter. camelize("moz-transform");
// => "mozTransform"
camelize("-moz-transform");
// => "MozTransform"
camelize("_moz_transform");
// => "MozTransform"
camelize("Moz-transform");
// => "MozTransform"
camelize("-moz-transform", true);
// => "mozTransform" classify(string) => stringConverts string to camelized class name. First letter is always upper case classify("some_class_name");
// => "SomeClassName" underscored(string) => stringConverts a camelized or dasherized string into an underscored one underscored("MozTransform");
// => "moz_transform" dasherize(string) => stringConverts a underscored or camelized string into an dasherized one dasherize("MozTransform");
// => "-moz-transform" humanize(string) => stringConverts an underscored, camelized, or dasherized string into a humanized one. Also removes beginning and ending whitespace, and removes the postfix '_id'. humanize(" capitalize dash-CamelCase_underscore trim ");
// => "Capitalize dash camel case underscore trim" trim(string, [characters]) => stringTrims defined characters from begining and ending of the string. Defaults to whitespace characters. trim(" foobar ");
// => "foobar"
trim("_-foobar-_", "_-");
// => "foobar" |