在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:jprichardson/string.js开源软件地址:https://github.com/jprichardson/string.js开源编程语言:JavaScript 100.0%开源软件介绍:string.js
Why?Personally, I prefer the cleanliness of the way code looks when it appears to be native methods. i.e. when you modify native JavaScript prototypes. However, if any app dependency required Here's a list of alternative frameworks:
Why wasn't I happy with any of them? They are all static methods that don't seem to support chaining in a clean way 'OR' they have an odd dependency. Sugar is the notable exception. Installation
Experiment with String.js NowAssuming you're on http://stringjs.com, just simply open up the Webkit inspector in either Chrome or Safari, or the web console in Firefox and you'll notice that UsageNode.jsvar S = require('string'); Originally, I was using RailsCheckout this gem to easily use string.js on the asset pipeline: https://github.com/jesjos/stringjs-rails Browsers<!-- HTML5 -->
<script src="https://cdn.rawgit.com/jprichardson/string.js/master/dist/string.min.js"></script>
<!-- Note that in the mime type for Javascript is now officially 'application/javascript'. If you
set the type to application/javascript in IE browsers, your Javacript will fail. Just don't set a
type via the script tag and set the mime type from your server. Most browsers look at the server mime
type anyway -->
<!-- For HTML4/IE -->
<script type="text/javascript" src="https://cdn.rawgit.com/jprichardson/string.js/master/dist/string.min.js"></script> A global variable AMD SupportIt now has AMD support. See require.js on how to use with AMD modules. Bothvar doesIt = S('my cool string').left(2).endsWith('y'); //true Access the wrapped string using var name = S('Your name is JP').right(2).s; //'JP' is the same as… var name = S('Your name is JP').right(2).toString(); //'JP' Still like the clean look of calling these methods directly on native Strings? No problem. Call S.extendPrototype();
var name = 'Your name is JP'.right(2); //'JP'
S.restorePrototype(); //be a good citizen and clean up Browser Compatibility
Extending string.jsSee: #57 Native JavaScript Methods
All of the native methods support chaining with the Example: var S = require('string');
var phrase = S('JavaScript is the best scripting language ever!');
var sub = 'best scripting';
var pos = phrase.indexOf(sub);
console.log(phrase.substr(pos, sub.length).truncate(8)); //best... MethodsSee test file for more details. I use the same nomenclature as Objective-C regarding methods. + means - constructor(nativeJsString)This creates a new Example: S('hello').s //"hello"
S(['a,b']).s //"a,b"
S({hi: 'jp'}).s //"[object Object]"" - between(left, right)Extracts a string between Example: S('<a>foo</a>').between('<a>', '</a>').s // => 'foo'
S('<a>foo</a></a>').between('<a>', '</a>').s // => 'foo'
S('<a><a>foo</a></a>').between('<a>', '</a>').s // => '<a>foo'
S('<a>foo').between('<a>', '</a>').s // => ''
S('Some strings } are very {weird}, dont you think?').between('{', '}').s // => 'weird'
S('This is a test string').between('test').s // => ' string'
S('This is a test string').between('', 'test').s // => 'This is a ' - camelize()Remove any underscores or dashes and convert a string into camel casing. Example: S('data_rate').camelize().s; //'dataRate'
S('background-color').camelize().s; //'backgroundColor'
S('-moz-something').camelize().s; //'MozSomething'
S('_car_speed_').camelize().s; //'CarSpeed'
S('yes_we_can').camelize().s; //'yesWeCan' - capitalize()Capitalizes the first character of a string. Example: S('jon').capitalize().s; //'Jon'
S('JP').capitalize().s; //'Jp' - chompLeft(prefix)Removes Example: S('foobar').chompLeft('foo').s; //'bar'
S('foobar').chompLeft('bar').s; //'foobar' - chompRight(suffix)Removes Example: S('foobar').chompRight('bar').s; //'foo'
S('foobar').chompRight('foo').s; //'foobar' - collapseWhitespace()Converts all adjacent whitespace characters to a single space. Example: var str = S(' String \t libraries are \n\n\t fun\n! ').collapseWhitespace().s; //'String libraries are fun !' - contains(ss)Returns true if the string contains Alias: Example: S('JavaScript is one of the best languages!').contains('one'); //true - count(substring)Returns the count of the number of occurrences of the substring. Example: S('JP likes to program. JP does not play in the NBA.').count("JP")// 2
S('Does not exist.').count("Flying Spaghetti Monster") //0
S('Does not exist.').count("Bigfoot") //0
S('JavaScript is fun, therefore Node.js is fun').count("fun") //2
S('funfunfun').count("fun") //3 - dasherize()Returns a converted camel cased string into a string delimited by dashes. Examples: S('dataRate').dasherize().s; //'data-rate'
S('CarSpeed').dasherize().s; //'-car-speed'
S('yesWeCan').dasherize().s; //'yes-we-can'
S('backgroundColor').dasherize().s; //'background-color' - decodeHTMLEntities()Decodes HTML entities into their string representation. S('Ken Thompson & Dennis Ritchie').decodeHTMLEntities().s; //'Ken Thompson & Dennis Ritchie'
S('3 < 4').decodeHTMLEntities().s; //'3 < 4' - endsWith(ss)Returns true if the string ends with Example: S("hello jon").endsWith('jon'); //true - escapeHTML()Escapes the html. Example: S('<div>hi</div>').escapeHTML().s; //<div>hi</div> + extendPrototype()Modifies Example: S.extendPrototype(); - ensureLeft(prefix)Ensures string starts with Example: S('subdir').ensureLeft('/').s; //'/subdir'
S('/subdir').ensureLeft('/').s; //'/subdir' - ensureRight(suffix)Ensures string ends with Example: S('dir').ensureRight('/').s; //'dir/'
S('dir/').ensureRight('/').s; //'dir/' - humanize()Transforms the input into a human friendly form. Example: S('the_humanize_string_method').humanize().s //'The humanize string method'
S('ThehumanizeStringMethod').humanize().s //'Thehumanize string method'
S('the humanize string method').humanize().s //'The humanize string method'
S('the humanize_id string method_id').humanize().s //'The humanize id string method'
S('the humanize string method ').humanize().s //'The humanize string method'
S(' capitalize dash-CamelCase_underscore trim ').humanize().s //'Capitalize dash camel case underscore trim' - include(ss)Returns true if the string contains the Alias: Example: S('JavaScript is one of the best languages!').include('one'); //true - isAlpha()Return true if the string contains only letters. Example: S("afaf").isAlpha(); //true
S('fdafaf3').isAlpha(); //false
S('dfdf--dfd').isAlpha(); //false - isAlphaNumeric()Return true if the string contains only letters and numbers Example: S("afaf35353afaf").isAlphaNumeric(); //true
S("FFFF99fff").isAlphaNumeric(); //true
S(
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论