🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

string

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string - npm Package Compare versions

Comparing version
0.1.2
to
0.2.0
+22
test_browser/browser.test.html
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link rel="stylesheet" href="https://raw.github.com/visionmedia/mocha/master/mocha.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://raw.github.com/visionmedia/mocha/master/mocha.js"></script>
<script src="../lib/string.js"></script>
<script>mocha.setup('bdd')</script>
<script src="string.test.js"></script>
<script>
$(function () {
mocha
.run()
.globals(['S']) // acceptable globals
})
</script>
</head>
<body>
<div id="mocha"></div>
</body>
</html>
(function() {
var F, S, T;
T = function(v) {
if (!v) throw new Error('Should be true.');
};
F = function(v) {
if (v) throw new Error('Should be false.');
};
if (typeof window !== "undefined" && window !== null) {
S = window.S;
} else {
S = require('../lib/string');
}
describe('string.js', function() {
describe('- camelize()', function() {
return it('should remove any underscores or dashes and convert a string into camel casing', function() {
T(S('data_rate').camelize().s === 'dataRate');
T(S('background-color').camelize().s === 'backgroundColor');
T(S('-moz-something').camelize().s === 'MozSomething');
T(S('_car_speed_').camelize().s === 'CarSpeed');
return T(S('yes_we_can').camelize().s === 'yesWeCan');
});
});
describe('- capitalize()', function() {
return it('should capitalize the string', function() {
T(S('jon').capitalize().s === 'Jon');
return T(S('JP').capitalize().s === 'Jp');
});
});
describe('+ clobberPrototype()', function() {
return it('should extend the String prototype with the extra methods', function() {
S.clobberPrototype();
T(" hello!".endsWith('!'));
return S.restorePrototype();
});
});
describe('- collapseWhitespace()', function() {
return it('should convert all adjacent whitespace characters to a single space and trim the ends', function() {
return T(S(' String \t libraries are \n\n\t fun\n! ').collapseWhitespace().s === 'String libraries are fun !');
});
});
describe('- contains(substring)', function() {
return it('should return true if the string contains the specified input string', function() {
T(S('JavaScript is one of the best languages!').contains('one'));
return F(S('What do you think?').contains('YES!'));
});
});
describe('- dasherize()', function() {
return it('should convert a camel cased string into a string delimited by dashes', function() {
T(S('dataRate').dasherize().s === 'data-rate');
T(S('CarSpeed').dasherize().s === '-car-speed');
T(S('yesWeCan').dasherize().s === 'yes-we-can');
return T(S('backgroundColor').dasherize().s === 'background-color');
});
});
describe('- endsWith(suffix)', function() {
return it("should return true if the string ends with the input string", function() {
console.log('SE: ' + S);
console.log('T: ' + T);
T(S("hello jon").endsWith('jon'));
F(S('ffffaaa').endsWith('jon'));
T(S("").endsWith(''));
T(S("hi").endsWith(''));
return T(S("hi").endsWith('hi'));
});
});
describe('- include(substring)', function() {
return it('should return true if the string contains the specified input string', function() {
T(S('JavaScript is one of the best languages!').include('one'));
return F(S('What do you think?').include('YES!'));
});
});
describe('- isAlpha()', function() {
return it("should return true if the string contains only letters", function() {
T(S("afaf").isAlpha());
T(S("FJslfjkasfs").isAlpha());
F(S("adflj43faljsdf").isAlpha());
F(S("33").isAlpha());
return F(S("TT....TTTafafetstYY").isAlpha());
});
});
describe('- isAlphaNumeric()', function() {
return it("should return true if the string contains only letters and digits", function() {
T(S("afaf35353afaf").isAlphaNumeric());
T(S("FFFF99fff").isAlphaNumeric());
T(S("99").isAlphaNumeric());
T(S("afff").isAlphaNumeric());
T(S("Infinity").isAlphaNumeric());
F(S("-Infinity").isAlphaNumeric());
F(S("-33").isAlphaNumeric());
return F(S("aaff..").isAlphaNumeric());
});
});
describe('- isEmpty()', function() {
return it('should return true if the string is solely composed of whitespace', function() {
T(S(' ').isEmpty());
T(S('\t\t\t ').isEmpty());
return T(S('\n\n ').isEmpty());
});
});
describe('- isLower()', function() {
return it('should return true if the character or string is lowercase', function() {
T(S('a').isLower());
T(S('z').isLower());
F(S('B').isLower());
T(S('hijp').isLower());
F(S('hi jp').isLower());
return F(S('HelLO').isLower());
});
});
describe('- isNumeric()', function() {
return it("should return true if the string only contains digits, this would not include Infinity or -Infinity", function() {
T(S("3").isNumeric());
F(S("34.22").isNumeric());
F(S("-22.33").isNumeric());
F(S("NaN").isNumeric());
F(S("Infinity").isNumeric());
F(S("-Infinity").isNumeric());
F(S("JP").isNumeric());
F(S("-5").isNumeric());
return T(S("000992424242").isNumeric());
});
});
describe('- isUpper()', function() {
return it('should return true if the character or string is uppercase', function() {
F(S('a').isUpper());
F(S('z').isUpper());
T(S('B').isUpper());
T(S('HIJP').isUpper());
F(S('HI JP').isUpper());
return F(S('HelLO').isUpper());
});
});
describe('- ltrim()', function() {
return it('should return the string with leading whitespace removed', function() {
T(S(' How are you?').ltrim().s === 'How are you?');
return T(S(' JP ').ltrim().s === 'JP ');
});
});
describe('- left(N)', function() {
it('should return the substring denoted by N positive left-most characters', function() {
T(S('My name is JP').left(2).s === 'My');
return T(S('Hi').left(0).s === '');
});
return it('should return the substring denoted by N negative left-most characters, equivalent to calling right(-N)', function() {
return T(S('My name is JP').left(-2).s === 'JP');
});
});
describe('- repeat(n)', function() {
return it('should return a string with that is concatenated n times', function() {
T(S(' ').repeat(5).s === ' ');
return T(S('*').repeat(3).s === '***');
});
});
describe('- replaceAll(substring, replacement)', function() {
return it('should return the new string with all occurrences of substring replaced with the replacment string', function() {
T(S(' does IT work? ').replaceAll(' ', '_').s === '_does_IT_work?_');
return T(S('Yes it does!').replaceAll(' ', '').s === 'Yesitdoes!');
});
});
describe('+ restorePrototype()', function() {
return it('should restore the original String prototype', function() {
T(typeof ' hi'.endsWith === 'undefined');
S.clobberPrototype();
T(' hi'.endsWith('hi'));
S.restorePrototype();
return T(typeof ' hi'.endsWith === 'undefined');
});
});
describe('- right(N)', function() {
it('should return the substring denoted by N positive right-most characters', function() {
T(S('I AM CRAZY').right(2).s === 'ZY');
T(S('Does it work? ').right(4).s === 'k? ');
return T(S('Hi').right(0).s === '');
});
return it('should return the substring denoted by N negative right-most characters, equivalent to calling left(-N)', function() {
return T(S('My name is JP').right(-2).s === 'My');
});
});
describe('- rtrim()', function() {
return it('should return the string with trailing whitespace removed', function() {
T(S('How are you? ').rtrim().s === 'How are you?');
return T(S(' JP ').rtrim().s === ' JP');
});
});
describe('- s', function() {
return it('should return the native string', function() {
T(S('hi').s === 'hi');
return T(S('hi').toString() === S('hi').s);
});
});
describe('- startsWith(prefix)', function() {
return it("should return true if the string starts with the input string", function() {
T(S("JP is a software engineer").startsWith("JP"));
F(S('wants to change the world').startsWith("politicians"));
T(S("").startsWith(""));
T(S("Hi").startsWith(""));
return T(S("JP").startsWith("JP"));
});
});
describe('- times(n)', function() {
return it('should return a string with that is concatenated n times', function() {
T(S(' ').times(5).s === ' ');
return T(S('*').times(3).s === '***');
});
});
describe('- toString()', function() {
return it('should return the native string', function() {
T(S('hi').toString() === 'hi');
return T(S('hi').toString() === S('hi').s);
});
});
describe('- trim()', function() {
return it('should return the string with leading and trailing whitespace removed', function() {
T(S('hello ').trim().s === 'hello');
T(S(' hello ').trim().s === 'hello');
T(S('\nhello').trim().s === 'hello');
T(S('\nhello\r\n').trim().s === 'hello');
return T(S('\thello\t').trim().s === 'hello');
});
});
return describe('- underscore()', function() {
return it('should convert a camel cased string into a string separated by underscores', function() {
T(S('dataRate').underscore().s === 'data_rate');
T(S('CarSpeed').underscore().s === '_car_speed');
return T(S('yesWeCan').underscore().s === 'yes_we_can');
});
});
});
}).call(this);
+11
-0

@@ -0,1 +1,12 @@

0.2.0 / 2012-03-02
==================
* Fixed method type `cloberPrototype()` to `clobberPrototype()`.
* Fixed Node.js testing bug that caused `T` and `F` to be undefined functions.
* Moved browser tests to its own directory.
* Updated README.
* Added `captialize()`.
* Added `repeat()`/`times()`.
* Added `isUpper()`/`isLower()`.
* Added `dasherize()`, `camelize()`, and `underscore()`.
### 0.1.2 / 2012-02-27

@@ -2,0 +13,0 @@ * Package.json updates.

+51
-8
(function() {
var S, cloberPrototype, methodsAdded, restorePrototype, wrap;
var S, clobberPrototype, methodsAdded, restorePrototype, wrap;

@@ -11,2 +11,14 @@ S = (function() {

S.prototype.camelize = function() {
var s;
s = this.trim().s.replace(/(\-|_|\s)+(.)?/g, function(match, sep, c) {
return (c ? c.toUpperCase() : '');
});
return new S(s);
};
S.prototype.capitalize = function() {
return new S(this.s.substr(0, 1).toUpperCase() + this.s.substring(1).toLowerCase());
};
S.prototype.collapseWhitespace = function() {

@@ -22,2 +34,8 @@ var s;

S.prototype.dasherize = function() {
var s;
s = this.trim().s.replace(/[_\s]+/g, '-').replace(/([A-Z])/g, '-$1').replace(/-+/g, '-').toLowerCase();
return new S(s);
};
S.prototype.endsWith = function(suffix) {

@@ -29,6 +47,2 @@ var l;

S.prototype.includes = S.prototype.contains;
S.prototype.include = S.prototype.contains;
S.prototype.isAlpha = function() {

@@ -46,2 +60,6 @@ return !/[^a-zA-Z]/.test(this.s);

S.prototype.isLower = function() {
return !/[^a-z]/.test(this.s);
};
S.prototype.isNumeric = function() {

@@ -51,2 +69,6 @@ return !/[^0-9]/.test(this.s);

S.prototype.isUpper = function() {
return !/[^A-Z]/.test(this.s);
};
S.prototype.left = function(N) {

@@ -84,2 +106,8 @@ var s;

S.prototype.rtrim = function() {
var s;
s = this.s.replace(/\s+$/, '');
return new S(s);
};
S.prototype.startsWith = function(prefix) {

@@ -89,2 +117,6 @@ return this.s.lastIndexOf(prefix, 0) === 0;

S.prototype.times = function(n) {
return new S(new Array(n + 1).join(this.s));
};
S.prototype.trim = function() {

@@ -104,2 +136,13 @@ var s;

S.prototype.underscore = function() {
var s;
s = this.trim().s.replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase();
if ((new S(this.s.charAt(0))).isUpper()) s = '_' + s;
return new S(s);
};
S.prototype.repeat = S.prototype.times;
S.prototype.include = S.prototype.contains;
return S;

@@ -115,3 +158,3 @@

cloberPrototype = function() {
clobberPrototype = function() {
var func, name, newS, _results;

@@ -146,7 +189,7 @@ newS = new S();

window.S = wrap;
window.S.cloberPrototype = cloberPrototype;
window.S.clobberPrototype = clobberPrototype;
window.S.restorePrototype = restorePrototype;
} else {
module.exports = wrap;
module.exports.cloberPrototype = cloberPrototype;
module.exports.clobberPrototype = clobberPrototype;
module.exports.restorePrototype = restorePrototype;

@@ -153,0 +196,0 @@ }

+1
-1

@@ -1,1 +0,1 @@

((function(){var a,b,c,d,e;a=function(){function a(a){this.s=a,this.s==null&&(this.s=this)}return a.prototype.collapseWhitespace=function(){var b;return b=this.s.replace(/[\s\xa0]+/g," ").replace(/^\s+|\s+$/g,""),new a(b)},a.prototype.contains=function(a){return this.s.indexOf(a)>=0},a.prototype.endsWith=function(a){var b;return b=this.s.length-a.length,b>=0&&this.s.indexOf(a,b)===b},a.prototype.includes=a.prototype.contains,a.prototype.include=a.prototype.contains,a.prototype.isAlpha=function(){return!/[^a-zA-Z]/.test(this.s)},a.prototype.isAlphaNumeric=function(){return!/[^a-zA-Z0-9]/.test(this.s)},a.prototype.isEmpty=function(){return/^[\s\xa0]*$/.test(this.s)},a.prototype.isNumeric=function(){return!/[^0-9]/.test(this.s)},a.prototype.left=function(b){var c;return b>=2?(c=this.s.substr(0,b),new a(c)):this.right(-b)},a.prototype.ltrim=function(){var b;return b=this.s.replace(/(^\s*)/g,""),new a(b)},a.prototype.replaceAll=function(b,c){var d;return d=this.s.replace(new RegExp(b,"g"),c),new a(d)},a.prototype.right=function(b){var c;return b>=0?(c=this.s.substr(this.s.length-b,b),new a(c)):this.left(-b)},a.prototype.startsWith=function(a){return this.s.lastIndexOf(a,0)===0},a.prototype.trim=function(){var b;return typeof String.prototype.trim=="undefined"?b=this.s.replace(/(^\s*|\s*$)/g,""):b=this.s.trim(),new a(b)},a.prototype.toString=function(){return this.s},a}(),e=function(b){return new a(b)},c=[],b=function(){var b,d,e,f;e=new a,f=[];for(d in e)b=e[d],String.prototype.hasOwnProperty(d)?f.push(void 0):(c.push(d),f.push(String.prototype[d]=function(){return String.prototype.s=this,b.apply(this,arguments)}));return f},d=function(){var a,b,d;for(b=0,d=c.length;b<d;b++)a=c[b],delete String.prototype[a];return c.length=0},typeof window!="undefined"&&window!==null?(window.S=e,window.S.cloberPrototype=b,window.S.restorePrototype=d):(module.exports=e,module.exports.cloberPrototype=b,module.exports.restorePrototype=d)})).call(this);
((function(){var a,b,c,d,e;a=function(){function a(a){this.s=a,this.s==null&&(this.s=this)}return a.prototype.camelize=function(){var b;return b=this.trim().s.replace(/(\-|_|\s)+(.)?/g,function(a,b,c){return c?c.toUpperCase():""}),new a(b)},a.prototype.capitalize=function(){return new a(this.s.substr(0,1).toUpperCase()+this.s.substring(1).toLowerCase())},a.prototype.collapseWhitespace=function(){var b;return b=this.s.replace(/[\s\xa0]+/g," ").replace(/^\s+|\s+$/g,""),new a(b)},a.prototype.contains=function(a){return this.s.indexOf(a)>=0},a.prototype.dasherize=function(){var b;return b=this.trim().s.replace(/[_\s]+/g,"-").replace(/([A-Z])/g,"-$1").replace(/-+/g,"-").toLowerCase(),new a(b)},a.prototype.endsWith=function(a){var b;return b=this.s.length-a.length,b>=0&&this.s.indexOf(a,b)===b},a.prototype.isAlpha=function(){return!/[^a-zA-Z]/.test(this.s)},a.prototype.isAlphaNumeric=function(){return!/[^a-zA-Z0-9]/.test(this.s)},a.prototype.isEmpty=function(){return/^[\s\xa0]*$/.test(this.s)},a.prototype.isLower=function(){return!/[^a-z]/.test(this.s)},a.prototype.isNumeric=function(){return!/[^0-9]/.test(this.s)},a.prototype.isUpper=function(){return!/[^A-Z]/.test(this.s)},a.prototype.left=function(b){var c;return b>=2?(c=this.s.substr(0,b),new a(c)):this.right(-b)},a.prototype.ltrim=function(){var b;return b=this.s.replace(/(^\s*)/g,""),new a(b)},a.prototype.replaceAll=function(b,c){var d;return d=this.s.replace(new RegExp(b,"g"),c),new a(d)},a.prototype.right=function(b){var c;return b>=0?(c=this.s.substr(this.s.length-b,b),new a(c)):this.left(-b)},a.prototype.rtrim=function(){var b;return b=this.s.replace(/\s+$/,""),new a(b)},a.prototype.startsWith=function(a){return this.s.lastIndexOf(a,0)===0},a.prototype.times=function(b){return new a((new Array(b+1)).join(this.s))},a.prototype.trim=function(){var b;return typeof String.prototype.trim=="undefined"?b=this.s.replace(/(^\s*|\s*$)/g,""):b=this.s.trim(),new a(b)},a.prototype.toString=function(){return this.s},a.prototype.underscore=function(){var b;return b=this.trim().s.replace(/([a-z\d])([A-Z]+)/g,"$1_$2").replace(/[-\s]+/g,"_").toLowerCase(),(new a(this.s.charAt(0))).isUpper()&&(b="_"+b),new a(b)},a.prototype.repeat=a.prototype.times,a.prototype.include=a.prototype.contains,a}(),e=function(b){return new a(b)},c=[],b=function(){var b,d,e,f;e=new a,f=[];for(d in e)b=e[d],String.prototype.hasOwnProperty(d)?f.push(void 0):(c.push(d),f.push(String.prototype[d]=function(){return String.prototype.s=this,b.apply(this,arguments)}));return f},d=function(){var a,b,d;for(b=0,d=c.length;b<d;b++)a=c[b],delete String.prototype[a];return c.length=0},typeof window!="undefined"&&window!==null?(window.S=e,window.S.clobberPrototype=b,window.S.restorePrototype=d):(module.exports=e,module.exports.clobberPrototype=b,module.exports.restorePrototype=d)})).call(this);
{
"name": "string",
"version": "0.1.2",
"version": "0.2.0",
"description": "string contains methods that aren't included in the vanilla JavaScript string.",

@@ -22,6 +22,5 @@ "homepage": [

"uglify-js": "1.2.x",
"growl": "1.5.x",
"testutil": "0.1.0"
"growl": "1.5.x"
},
"main": "lib/string.js"
}
+236
-33
[string.js](http://stringjs.com)
=========
`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.
`string.js`, or simply `S` is a lightweight (< 2k Gzipped) JavaScript library for the browser or for Node.js that provides extra String methods. Originally, it modified the String prototype. But I quickly learned that in JavaScript, this is considered poor practice.

@@ -11,3 +11,3 @@

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.
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 `string.js`, then the app's string prototype in every module would be modified as well. This could be troublesome. So I settled on creating a wrapper a la jQuery style. For those of you prototype hatin' fools, such as myself, there is the method `clobberPrototype()`.

@@ -20,4 +20,5 @@ Here's a list of alternative frameworks:

* [Underscore.string](http://epeli.github.com/underscore.string/)
* [Sugar.js](http://sugarjs.com)
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.
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.

@@ -29,3 +30,3 @@

npm install --production string
npm install --production string

@@ -46,3 +47,3 @@

### Browsers (IE/Chrome/Safari/Firefox)
### Browsers

@@ -82,6 +83,6 @@ ```html

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.
Still like the clean look of calling these methods directly on native Strings? No problem. Call `clobberPrototype()`. 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();
S.clobberPrototype();
var name = 'Your name is JP'.right(2); //'JP'

@@ -100,3 +101,31 @@ S.restorePrototype(); //be a good citizen and clean up

### + cloberPrototype()
### - camelize()
Remove any underscores or dashes and convert a string into camel casing.
Example:
```javascript
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:
```javascript
S('jon').capitalize().s; //'Jon'
S('JP').capitalize().s; //'Jp'
```
### + clobberPrototype() ###
Modifies `String.prototype` to have all of the methods found in string.js.

@@ -107,7 +136,7 @@

```javascript
S.cloberPrototype();
S.clobberPrototype();
```
### - collapseWhitespace()
### - collapseWhitespace() ###

@@ -123,6 +152,8 @@ Converts all adjacent whitespace characters to a single space.

### - contains(substring) Aliases: include/includes
### - contains(ss) ###
Returns true if the string contains the substring.
Returns true if the string contains `ss`.
Alias: `include()`
Example:

@@ -135,6 +166,20 @@

### - endsWith(substring)
### - dasherize() ###
Returns true if the string ends with the substring.
Returns a converted camel cased string into a string delimited by dashes.
Examples:
```javascript
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'
```
### - endsWith(ss) ###
Returns true if the string ends with `ss`.
Example:

@@ -147,4 +192,17 @@

### - isAlpha()
### - include(ss) ###
Returns true if the string contains the `ss`.
Alias: `contains()`
Example:
```javascript
S('JavaScript is one of the best languages!').include('one'); //true
```
### - isAlpha() ###
Return true if the string contains only letters.

@@ -161,3 +219,3 @@

### - isAlphaNumeric()
### - isAlphaNumeric() ###

@@ -180,3 +238,3 @@ Return true if the string contains only letters and numbers

### - isEmpty()
### - isEmpty() ###

@@ -194,4 +252,20 @@ Return true if the string is solely composed of whitespace

### - isNumeric()
### - isLower() ###
Return true if the character or string is lowercase
Example:
```javascript
S('a').isLower(); //true
S('z').isLower(); //true
S('B').isLower(); //false
S('hijp').isLower(); //true
S('hi jp').isLower(); //false
S('HelLO').isLower(); //false
```
### - isNumeric() ###
Return true if the string only contains digits

@@ -210,9 +284,9 @@

S("-5").isNumeric(); //false
S("000992424242").isNumeric(); //false
S("000992424242").isNumeric(); //true
```
### - ltrim()
### - isUpper() ###
Return the string with leading and trailing whitespace removed
Returns true if the character or string is uppercase

@@ -222,2 +296,18 @@ Example:

```javascript
S('a').isUpper() //false
S('z').isUpper() //false
S('B').isUpper() //true
S('HIJP').isUpper() //true
S('HI JP').isUpper() //false
S('HelLO').isUpper() //true
```
### - ltrim() ###
Return the string with leading and whitespace removed
Example:
```javascript
S(' How are you?').ltrim().s; //'How are you?';

@@ -227,5 +317,5 @@ ```

### - left(N)
### - left(n) ###
Return the substring denoted by N positive left-most characters.
Return the substring denoted by `n` positive left-most characters.

@@ -241,9 +331,23 @@ Example:

### - replaceAll(substring, replacement)
### - repeat(n) ###
Return the new string with all occurrences of substring replaced with the replacement string
Returns a string repeated `n` times.
Alias: `times()`
Example:
```javascript
S(' ').repeat(5).s; //' '
S('*').repeat(3).s; //'***'
```
### - replaceAll(ss, newstr) ###
Return the new string with all occurrences of `ss` replaced with `newstr`.
Example:
```javascript
S(' does IT work? ').replaceAll(' ', '_').s; //'_does_IT_work?_'

@@ -254,5 +358,5 @@ S('Yes it does!').replaceAll(' ', '').s; //'Yesitdoes!'

### + restorePrototype()
### + restorePrototype() ###
Restore the original String prototype.
Restore the original String prototype. Typically used in conjunction with `clobberPrototype()`.

@@ -266,5 +370,5 @@ Example:

### - right(N)
### - right(n) ###
Return the substring denoted by N positive right-most characters
Return the substring denoted by `n` positive right-most characters.

@@ -281,5 +385,5 @@ Example:

### - startsWith(prefix)
### - rtrim() ###
Return true if the string starts with the input string
Return the string with trailing whitespace removed.

@@ -289,2 +393,28 @@ Example:

```javascript
S('How are you? ').rtrim().s; //'How are you?';
```
### - s ###
Alias: `toString()`
The encapsulated native string representation of an `S` object.
Example:
```javascript
S('my name is JP.').capitalize().s; //My name is JP.
var a = "Hello " + S('joe!'); //a = "Hello joe!"
S("Hello").toString() === S("Hello").s; //true
```
### - startsWith(prefix) ###
Return true if the string starts with `prefix`.
Example:
```javascript
S("JP is a software engineer").startsWith("JP"); //true

@@ -294,4 +424,19 @@ S('wants to change the world').startsWith("politicians"); //false

### - trim()
### - times(n) ###
Returns a string repeated `n` times.
Alias: `repeat()`
Example:
```javascript
S(' ').times(5).s //' '
S('*').times(3).s //'***'
```
### - trim() ###
Return the string with leading and trailing whitespace removed. Reverts to native `trim()` if it exists.

@@ -309,7 +454,65 @@

### - toString() ###
Alias: `s`
Return the string representation of an `S` object. Not really necessary to use. However, JS engines will look at an object and display its `toString()` result.
Example:
```javascript
S('my name is JP.').capitalize().toString(); //My name is JP.
var a = "Hello " + S('joe!'); //a = "Hello joe!"
S("Hello").toString() === S("Hello").s; //true
```
### - underscore()
Returns converted camel cased string into a string delimited by underscores.
Example:
```javascript
S('dataRate').underscore().s; //'data_rate'
S('CarSpeed').underscore().s; //'_car_speed'
S('yesWeCan').underscore().s; //'yes_we_can'
```
I will definitely add more methods, I'll be adding them on as-needed basis.
## License
Testing
-------
### Node.js
Install the dev dependencies:
$ npm install string
Then navigate to the installed directory:
$ cd node_modules/string/
Run test package:
$ cake test
### Browser ###
[Click Here](http://stringjs.com/browser.test.html)
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.

@@ -316,0 +519,0 @@

@@ -6,2 +6,12 @@

# modified slightly from https://github.com/epeli/underscore.string
camelize: ->
s = @.trim().s.replace /(\-|_|\s)+(.)?/g, (match, sep, c) ->
return (if c then c.toUpperCase() else '')
#s = s.charAt(0).toLowerCase() + s.substring(1)
new S(s)
capitalize: ->
new S(@s.substr(0,1).toUpperCase() + @s.substring(1).toLowerCase())
collapseWhitespace: ->

@@ -14,2 +24,7 @@ s = @s.replace(/[\s\xa0]+/g, ' ').replace(/^\s+|\s+$/g, ''); #thanks Google

dasherize: -> #modified from https://github.com/epeli/underscore.string
s = @.trim().s.replace(/[_\s]+/g, '-').replace(/([A-Z])/g, '-$1').replace(/-+/g, '-').toLowerCase()
#if (new S(@s.charAt(0))).isUpper() then s = '-' + s
new S(s)
endsWith: (suffix) ->

@@ -19,6 +34,2 @@ l = @s.length - suffix.length;

includes: S::.contains
include: S::.contains
isAlpha: ->

@@ -33,5 +44,11 @@ !/[^a-zA-Z]/.test(@s)

isLower: ->
!/[^a-z]/.test(@s)
isNumeric: ->
!/[^0-9]/.test(@s)
isUpper: ->
!/[^A-Z]/.test(@s)
left: (N) ->

@@ -59,5 +76,12 @@ if N >= 2

rtrim: ->
s = @s.replace(/\s+$/, '')
new S(s)
startsWith: (prefix) ->
@s.lastIndexOf(prefix, 0) is 0 #Google says this is the fastest
times: (n) ->
new S(new Array(n+1).join(@s))
trim: ->

@@ -73,3 +97,14 @@ if typeof String::trim is 'undefined'

underscore: -> #modified from https://github.com/epeli/underscore.string
s = @.trim().s.replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase()
if (new S(@s.charAt(0))).isUpper() then s = '_' + s
new S(s)
#aliases
repeat: S::.times
include: S::.contains
wrap = (str) ->

@@ -79,3 +114,3 @@ new S(str)

methodsAdded = []
cloberPrototype = ->
clobberPrototype = ->
newS = new S()

@@ -96,8 +131,8 @@ for name,func of newS

window.S = wrap
window.S.cloberPrototype = cloberPrototype
window.S.clobberPrototype = clobberPrototype
window.S.restorePrototype = restorePrototype
else
module.exports = wrap
module.exports.cloberPrototype = cloberPrototype
module.exports.clobberPrototype = clobberPrototype
module.exports.restorePrototype = restorePrototype

@@ -0,8 +1,7 @@

T = (v) -> if !v then throw new Error('Should be true.')
F = (v) -> if v then throw new Error('Should be false.')
if window?
T = (v) -> if !v then throw new Exception('False, should be true.')
F = (v) -> if v then throw new Exception('True, should be false.')
S = window.S
else
testutil = require('testutil')
S = require('../lib/string')

@@ -12,5 +11,18 @@

describe 'string.js', ->
describe '+ cloberPrototype()', ->
describe '- camelize()', ->
it 'should remove any underscores or dashes and convert a string into camel casing', ->
T S('data_rate').camelize().s is 'dataRate'
T S('background-color').camelize().s is 'backgroundColor'
T S('-moz-something').camelize().s is 'MozSomething'
T S('_car_speed_').camelize().s is 'CarSpeed'
T S('yes_we_can').camelize().s is 'yesWeCan'
describe '- capitalize()', ->
it 'should capitalize the string', ->
T S('jon').capitalize().s is 'Jon'
T S('JP').capitalize().s is 'Jp'
describe '+ clobberPrototype()', ->
it 'should extend the String prototype with the extra methods', ->
S.cloberPrototype()
S.clobberPrototype()
T " hello!".endsWith('!')

@@ -21,5 +33,5 @@ S.restorePrototype()

it 'should convert all adjacent whitespace characters to a single space and trim the ends', ->
console.log S(' String \t libraries are \n\n\t fun\n! ').collapseWhitespace().s is 'String libraries are fun !'
T S(' String \t libraries are \n\n\t fun\n! ').collapseWhitespace().s is 'String libraries are fun !'
describe '- contains(substring) ALIAS: include/includes', ->
describe '- contains(substring)', ->
it 'should return true if the string contains the specified input string', ->

@@ -29,4 +41,13 @@ T S('JavaScript is one of the best languages!').contains('one')

describe '- dasherize()', ->
it 'should convert a camel cased string into a string delimited by dashes', ->
T S('dataRate').dasherize().s is 'data-rate'
T S('CarSpeed').dasherize().s is '-car-speed'
T S('yesWeCan').dasherize().s is 'yes-we-can'
T S('backgroundColor').dasherize().s is 'background-color'
describe '- endsWith(suffix)', ->
it "should return true if the string ends with the input string", ->
console.log 'SE: ' + S
console.log 'T: ' + T
T S("hello jon").endsWith('jon')

@@ -38,2 +59,7 @@ F S('ffffaaa').endsWith('jon')

describe '- include(substring)', ->
it 'should return true if the string contains the specified input string', ->
T S('JavaScript is one of the best languages!').include('one')
F S('What do you think?').include('YES!')
describe '- isAlpha()', ->

@@ -64,2 +90,11 @@ it "should return true if the string contains only letters", ->

describe '- isLower()', ->
it 'should return true if the character or string is lowercase', ->
T S('a').isLower()
T S('z').isLower()
F S('B').isLower()
T S('hijp').isLower()
F S('hi jp').isLower()
F S('HelLO').isLower()
describe '- isNumeric()', ->

@@ -77,4 +112,13 @@ it "should return true if the string only contains digits, this would not include Infinity or -Infinity", ->

describe '- isUpper()', ->
it 'should return true if the character or string is uppercase', ->
F S('a').isUpper()
F S('z').isUpper()
T S('B').isUpper()
T S('HIJP').isUpper()
F S('HI JP').isUpper()
F S('HelLO').isUpper()
describe '- ltrim()', ->
it 'should return the string with leading and trailing whitespace removed', ->
it 'should return the string with leading whitespace removed', ->
T S(' How are you?').ltrim().s is 'How are you?'

@@ -91,2 +135,7 @@ T S(' JP ').ltrim().s is 'JP '

describe '- repeat(n)', ->
it 'should return a string with that is concatenated n times', ->
T S(' ').repeat(5).s is ' '
T S('*').repeat(3).s is '***'
describe '- replaceAll(substring, replacement)', ->

@@ -100,3 +149,3 @@ it 'should return the new string with all occurrences of substring replaced with the replacment string', ->

T typeof ' hi'.endsWith is 'undefined'
S.cloberPrototype()
S.clobberPrototype()
T ' hi'.endsWith('hi')

@@ -115,2 +164,12 @@ S.restorePrototype()

describe '- rtrim()', ->
it 'should return the string with trailing whitespace removed', ->
T S('How are you? ').rtrim().s is 'How are you?'
T S(' JP ').rtrim().s is ' JP'
describe '- s', ->
it 'should return the native string', ->
T S('hi').s is 'hi'
T S('hi').toString() is S('hi').s
describe '- startsWith(prefix)', ->

@@ -124,2 +183,12 @@ it "should return true if the string starts with the input string", ->

describe '- times(n)', ->
it 'should return a string with that is concatenated n times', ->
T S(' ').times(5).s is ' '
T S('*').times(3).s is '***'
describe '- toString()', ->
it 'should return the native string', ->
T S('hi').toString() is 'hi'
T S('hi').toString() is S('hi').s
describe '- trim()', ->

@@ -133,2 +202,8 @@ it 'should return the string with leading and trailing whitespace removed', ->

describe '- underscore()', ->
it 'should convert a camel cased string into a string separated by underscores', ->
T S('dataRate').underscore().s is 'data_rate'
T S('CarSpeed').underscore().s is '_car_speed'
T S('yesWeCan').underscore().s is 'yes_we_can'
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link rel="stylesheet" href="https://raw.github.com/visionmedia/mocha/master/mocha.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://raw.github.com/visionmedia/mocha/master/mocha.js"></script>
<script src="../lib/string.js"></script>
<script>mocha.setup('bdd')</script>
<script src="string.test.js"></script>
<script>
$(function () {
mocha
.run()
.globals(['S']) // acceptable globals
})
</script>
</head>
<body>
<div id="mocha"></div>
</body>
</html>
(function() {
var F, S, T, testutil;
if (typeof window !== "undefined" && window !== null) {
T = function(v) {
if (!v) throw new Exception('False, should be true.');
};
F = function(v) {
if (v) throw new Exception('True, should be false.');
};
S = window.S;
} else {
testutil = require('testutil');
S = require('../lib/string');
}
describe('string.js', function() {
describe('+ cloberPrototype()', function() {
return it('should extend the String prototype with the extra methods', function() {
S.cloberPrototype();
T(" hello!".endsWith('!'));
return S.restorePrototype();
});
});
describe('- collapseWhitespace()', function() {
return it('should convert all adjacent whitespace characters to a single space and trim the ends', function() {
return console.log(S(' String \t libraries are \n\n\t fun\n! ').collapseWhitespace().s === 'String libraries are fun !');
});
});
describe('- contains(substring) ALIAS: include/includes', function() {
return it('should return true if the string contains the specified input string', function() {
T(S('JavaScript is one of the best languages!').contains('one'));
return F(S('What do you think?').contains('YES!'));
});
});
describe('- endsWith(suffix)', function() {
return it("should return true if the string ends with the input string", function() {
T(S("hello jon").endsWith('jon'));
F(S('ffffaaa').endsWith('jon'));
T(S("").endsWith(''));
T(S("hi").endsWith(''));
return T(S("hi").endsWith('hi'));
});
});
describe('- isAlpha()', function() {
return it("should return true if the string contains only letters", function() {
T(S("afaf").isAlpha());
T(S("FJslfjkasfs").isAlpha());
F(S("adflj43faljsdf").isAlpha());
F(S("33").isAlpha());
return F(S("TT....TTTafafetstYY").isAlpha());
});
});
describe('- isAlphaNumeric()', function() {
return it("should return true if the string contains only letters and digits", function() {
T(S("afaf35353afaf").isAlphaNumeric());
T(S("FFFF99fff").isAlphaNumeric());
T(S("99").isAlphaNumeric());
T(S("afff").isAlphaNumeric());
T(S("Infinity").isAlphaNumeric());
F(S("-Infinity").isAlphaNumeric());
F(S("-33").isAlphaNumeric());
return F(S("aaff..").isAlphaNumeric());
});
});
describe('- isEmpty()', function() {
return it('should return true if the string is solely composed of whitespace', function() {
T(S(' ').isEmpty());
T(S('\t\t\t ').isEmpty());
return T(S('\n\n ').isEmpty());
});
});
describe('- isNumeric()', function() {
return it("should return true if the string only contains digits, this would not include Infinity or -Infinity", function() {
T(S("3").isNumeric());
F(S("34.22").isNumeric());
F(S("-22.33").isNumeric());
F(S("NaN").isNumeric());
F(S("Infinity").isNumeric());
F(S("-Infinity").isNumeric());
F(S("JP").isNumeric());
F(S("-5").isNumeric());
return T(S("000992424242").isNumeric());
});
});
describe('- ltrim()', function() {
return it('should return the string with leading and trailing whitespace removed', function() {
T(S(' How are you?').ltrim().s === 'How are you?');
return T(S(' JP ').ltrim().s === 'JP ');
});
});
describe('- left(N)', function() {
it('should return the substring denoted by N positive left-most characters', function() {
T(S('My name is JP').left(2).s === 'My');
return T(S('Hi').left(0).s === '');
});
return it('should return the substring denoted by N negative left-most characters, equivalent to calling right(-N)', function() {
return T(S('My name is JP').left(-2).s === 'JP');
});
});
describe('- replaceAll(substring, replacement)', function() {
return it('should return the new string with all occurrences of substring replaced with the replacment string', function() {
T(S(' does IT work? ').replaceAll(' ', '_').s === '_does_IT_work?_');
return T(S('Yes it does!').replaceAll(' ', '').s === 'Yesitdoes!');
});
});
describe('+ restorePrototype()', function() {
return it('should restore the original String prototype', function() {
T(typeof ' hi'.endsWith === 'undefined');
S.cloberPrototype();
T(' hi'.endsWith('hi'));
S.restorePrototype();
return T(typeof ' hi'.endsWith === 'undefined');
});
});
describe('- right(N)', function() {
it('should return the substring denoted by N positive right-most characters', function() {
T(S('I AM CRAZY').right(2).s === 'ZY');
T(S('Does it work? ').right(4).s === 'k? ');
return T(S('Hi').right(0).s === '');
});
return it('should return the substring denoted by N negative right-most characters, equivalent to calling left(-N)', function() {
return T(S('My name is JP').right(-2).s === 'My');
});
});
describe('- startsWith(prefix)', function() {
return it("should return true if the string starts with the input string", function() {
T(S("JP is a software engineer").startsWith("JP"));
F(S('wants to change the world').startsWith("politicians"));
T(S("").startsWith(""));
T(S("Hi").startsWith(""));
return T(S("JP").startsWith("JP"));
});
});
return describe('- trim()', function() {
return it('should return the string with leading and trailing whitespace removed', function() {
T(S('hello ').trim().s === 'hello');
T(S(' hello ').trim().s === 'hello');
T(S('\nhello').trim().s === 'hello');
T(S('\nhello\r\n').trim().s === 'hello');
return T(S('\thello\t').trim().s === 'hello');
});
});
});
}).call(this);

Sorry, the diff of this file is not supported yet