Comparing version 0.1.2 to 0.2.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. |
(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 +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" | ||
} |
269
README.md
[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 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
42130
4
384
504
1