Comparing version 0.0.4 to 0.1.1
@@ -0,1 +1,7 @@ | ||
### 0.1.0 / 2012-02-27 | ||
* Added a few more methods. | ||
* Removed default behavior of modifying `String.prototype` | ||
* Updated README to be a bit more detailed. | ||
* Ditched Makefiles for Cakefiles. | ||
### 0.0.4 / 2012-01-27 | ||
@@ -2,0 +8,0 @@ * Added trim() method for IE browsers |
{ | ||
"name": "string", | ||
"version": "0.0.4", | ||
"description": "string contains methods that aren't included in the vanilla JavaScript string. It modifies your String prototype.", | ||
"version": "0.1.1", | ||
"description": "string contains methods that aren't included in the vanilla JavaScript string.", | ||
"homepage": [ | ||
@@ -12,14 +12,16 @@ "https://github.com/jprichardson/string.js" | ||
}, | ||
"keywords": ["string","strings"], | ||
"keywords": ["string","strings", "string.js", "S", "s"], | ||
"author": "JP Richardson <jprichardson@gmail.com>", | ||
"licenses":[{ | ||
"type" : "MIT,Apache", | ||
"url" : "http://github.com/jprichardson/string.js/raw/master/LICENSE" | ||
"type" : "MIT,Apache v2,LGPL" | ||
}], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"coffee-script" : ">=1.0.1", | ||
"mocha": ">=0.10" | ||
"coffee-script" : "1.2.x", | ||
"mocha": "0.12.x", | ||
"uglify-js": "1.2.x", | ||
"growl": "1.5.x", | ||
"testutil": "0.1.0" | ||
}, | ||
"main": "string.js" | ||
} |
283
README.md
@@ -1,24 +0,43 @@ | ||
# string.js | ||
[string.js](http://stringjs.com) | ||
========= | ||
This module modifies the String prototype. Yes, it modifies the prototype, get over it. | ||
`string.js`, simply `S` is a lightweight (< 2k Gzipped) JavaScript library for the browser or for Node.js that provides extra String methods. Originally, it would modify your String prototype. But I quickly learned that in JavaScript this is considered bad practice. | ||
## Installation | ||
npm install string | ||
Motivation | ||
---------- | ||
Personally, I prefer the cleanliness of the way code looks when you modify native Javascript prototypes. However, if any app dependency required `string.js` then the app's string prototype would modified as well. This could be troublesome. | ||
Make sure that you run the test script to verify that it works on your system. | ||
Here's a list of alternative frameworks: | ||
make test | ||
* [Prototype Framework's String library](http://prototypejs.org/api/string) | ||
* [Uize.String](http://www.uize.com/reference/Uize.String.html) | ||
* [Google Closure's String](http://closure-library.googlecode.com/svn/docs/namespace_goog_string.html) | ||
* [Underscore.string](http://epeli.github.com/underscore.string/) | ||
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. | ||
## Usage | ||
Installation | ||
------------ | ||
npm install --production string | ||
Usage | ||
----- | ||
### Node.js | ||
```coffeescript | ||
require('string') | ||
```javascript | ||
var S = require('string'); | ||
``` | ||
Originally, I was using `$s` but glancing over the code, it was easy to confuse `$s` for string.js with `$` for jQuery. Feel free to use the most convenient variable for you. | ||
### Browsers (IE/Chrome/Safari/Firefox) | ||
@@ -28,3 +47,3 @@ | ||
<!-- HTML5 --> | ||
<script src="https://raw.github.com/jprichardson/string.js/master/string.min.js"/></script> | ||
<script src="https://raw.github.com/jprichardson/string.js/master/lib/string.min.js"></script> | ||
@@ -37,29 +56,243 @@ <!-- Note that in the mime type for Javascript is now officially 'application/javascript'. If you | ||
<!-- For HTML4/IE --> | ||
<script type="text/javascript" src="https://raw.github.com/jprichardson/string.js/master/string.min.js"/></script> | ||
<script type="text/javascript" src="https://raw.github.com/jprichardson/lib/string.js/master/string.min.js"></script> | ||
``` | ||
### Methods | ||
A global variable `window.S` or simply `S` is created. | ||
See [test file][1] for more details. | ||
### Both | ||
```coffeescript | ||
includes(needle) or contains(needle) #true if string contains needle | ||
endsWith(suffix) #true if string ends with suffix | ||
startsWith(prefix) #true if string starts with prefix | ||
```javascript | ||
var doesIt = S('my cool string').left(2).endsWith('y'); //true | ||
``` | ||
isAlpha() #true if string is only letters | ||
isDigit() #true if the string only contains digits | ||
isNumber() #true if the string can be converted to a valid Number object | ||
isAlphaDigit() #true if the string only contains letters or numbers | ||
Access the wrapped string using `s` variable or `toString()` | ||
```javascript | ||
var name = S('Your name is JP').right(2).s; //'JP' | ||
``` | ||
is the same as… | ||
```javascript | ||
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 `cloberPrototype()`. Make sure to not call this at the module level, at it'll effect the entire application lifecycle. You should really only use this at the method level. The one exception being if your application will not be a dependency of another application. | ||
```javascript | ||
S.cloberPrototype(); | ||
var name = 'Your name is JP'.right(2); //'JP' | ||
S.restorePrototype(); //be a good citizen and clean up | ||
``` | ||
Methods | ||
------- | ||
See [test file][1] for more details. | ||
I use the same nomenclature as Objective-C regarding methods. **+** means `static` or `class` method. **-** means `non-static` or `instance` method. | ||
### + cloberPrototype() | ||
Modifies `String.prototype` to have all of the methods found in string.js. | ||
Example: | ||
```javascript | ||
S.cloberPrototype(); | ||
``` | ||
### - collapseWhitespace() | ||
Converts all adjacent whitespace characters to a single space. | ||
Example: | ||
```javascript | ||
var str = S(' String \t libraries are \n\n\t fun\n! ').collapseWhitespace().s; //'String libraries are fun !' | ||
``` | ||
### - contains(substring) Aliases: include/includes | ||
Returns true if the string contains the substring. | ||
Example: | ||
```javascript | ||
S('JavaScript is one of the best languages!').contains('one'); //true | ||
``` | ||
### - endsWith(substring) | ||
Returns true if the string ends with the substring. | ||
Example: | ||
```javascript | ||
S("hello jon").endsWith('jon'); //true | ||
``` | ||
### - isAlpha() | ||
Return true if the string contains only letters. | ||
Example: | ||
```javascript | ||
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: | ||
```javascript | ||
S("afaf35353afaf").isAlphaNumeric(); //true | ||
S("FFFF99fff").isAlphaNumeric(); //true | ||
S("99").isAlphaNumeric(); //true | ||
S("afff").isAlphaNumeric(); //true | ||
S("Infinity").isAlphaNumeric(); //true | ||
S("-Infinity").isAlphaNumeric(); //false | ||
S("-33").isAlphaNumeric(); //false | ||
S("aaff..").isAlphaNumeric(); //false | ||
``` | ||
### - isEmpty() | ||
Return true if the string is solely composed of whitespace | ||
Example: | ||
```javascript | ||
S(' ').isEmpty(); //true | ||
S('\t\t\t ').isEmpty(); //true | ||
S('\n\n ').isEmpty(); //true | ||
``` | ||
### - isNumeric() | ||
Return true if the string only contains digits | ||
Example: | ||
```javascript | ||
S("3").isNumeric(); //true | ||
S("34.22").isNumeric(); //false | ||
S("-22.33").isNumeric(); //false | ||
S("NaN").isNumeric(); //false | ||
S("Infinity").isNumeric(); //false | ||
S("-Infinity").isNumeric(); //false | ||
S("JP").isNumeric(); //false | ||
S("-5").isNumeric(); //false | ||
S("000992424242").isNumeric(); //false | ||
``` | ||
### - ltrim() | ||
Return the string with leading and trailing whitespace removed | ||
Example: | ||
```javascript | ||
S(' How are you?').ltrim().s; //'How are you?'; | ||
``` | ||
### - left(N) | ||
Return the substring denoted by N positive left-most characters. | ||
Example: | ||
```javascript | ||
S('My name is JP').left(2).s; //'My' | ||
S('Hi').left(0).s; //'' | ||
S('My name is JP').left(-2).s; //'JP', same as right(2) | ||
``` | ||
### - replaceAll(substring, replacement) | ||
Return the new string with all occurrences of substring replaced with the replacement string | ||
Example: | ||
```javascript | ||
S(' does IT work? ').replaceAll(' ', '_').s; //'_does_IT_work?_' | ||
S('Yes it does!').replaceAll(' ', '').s; //'Yesitdoes!' | ||
``` | ||
### + restorePrototype() | ||
Restore the original String prototype. | ||
Example: | ||
```javascript | ||
S.restorePrototype(); | ||
``` | ||
### - right(N) | ||
Return the substring denoted by N positive right-most characters | ||
Example: | ||
```javascript | ||
S('I AM CRAZY').right(2).s; //'ZY' | ||
S('Does it work? ').right(4).s; //'k? ' | ||
S('Hi').right(0).s; //'' | ||
S('My name is JP').right(-2).s; //'My', same as left(2) | ||
``` | ||
### - startsWith(prefix) | ||
Return true if the string starts with the input string | ||
Example: | ||
```javascript | ||
S("JP is a software engineer").startsWith("JP"); //true | ||
S('wants to change the world').startsWith("politicians"); //false | ||
``` | ||
### - trim() | ||
Return the string with leading and trailing whitespace removed. Reverts to native `trim()` if it exists. | ||
Example: | ||
```javascript | ||
S('hello ').trim().s; //'hello' | ||
S(' hello ').trim().s; //'hello' | ||
S('\nhello').trim().s; //'hello' | ||
S('\nhello\r\n').trim().s; //'hello' | ||
S('\thello\t').trim().s; //'hello' | ||
``` | ||
I will definitely add more methods, I'll be adding them on as-needed basis. | ||
## License | ||
(The Apache License) | ||
Triple licensed under MIT/X11, Apache v2, and LGPL. If you use this, pick which one works for you and your software. Attribution is always nice. | ||
Some methods as noted, are from Google. | ||
As noted, some of these methods were plucked from Google. | ||
(The MIT License) | ||
Copyright (c) 2012 JP Richardson | ||
@@ -66,0 +299,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28599
13
262
301
5
1
1