Comparing version 0.1.2 to 0.1.3
{ | ||
"name": "slang", | ||
"description": "A collection of utility functions for strings", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Devon Govett", |
@@ -97,6 +97,12 @@ Slang | ||
### slang.trimRight | ||
Remove the trailing whitespace from `input` | ||
Removes the trailing whitespace from `input` | ||
slang.trimRight(' hello '); // " hello" | ||
### slang.truncate | ||
Truncates `input` to `args.limit` or 10 and adds `args.omission` or "..." | ||
slang.truncate('Lorem ipsum dolor sit amet.'); // 'Lorem ipsu...' | ||
slang.truncate('Lorem ipsum dolor sit amet.', { limit: 5, omission: '...(read more)' }); // 'Lorem...(read more)' | ||
### slang.join | ||
@@ -107,2 +113,8 @@ Joins an array into a humanized list. The last element is joined by "and" by default, but you can change it. | ||
slang.join(['red', 'blue', 'green'], 'or'); // "red, blue or green" | ||
### slang.humanize | ||
Returns a humanized number with the correct suffix such as 1st, 2nd, 3rd or 4th. | ||
slang.humanize(2); // "2nd" | ||
slang.humanize(103); // "103rd" | ||
@@ -152,3 +164,3 @@ ### slang.contains | ||
### slang.addToPrototype | ||
Adds the methods from the slang object to String.prototype. Does not add slang.guid, slang.isString, slang.version, or itself. | ||
Adds the methods from the slang object to String.prototype. Does not add slang.guid, slang.humanize, slang.isString, slang.version, or itself. | ||
@@ -155,0 +167,0 @@ slang.addToPrototype(); |
40
slang.js
@@ -112,10 +112,18 @@ (function() { | ||
slang.trimLeft = function trimLeft(input) { | ||
return input.trimLeft ? input.trimLeft() : input.replace(/^s+/, ''); | ||
return input.trimLeft ? input.trimLeft() : input.replace(/^\s+/, ''); | ||
} | ||
// Remove the trailing whitespace from `input` | ||
// Removes the trailing whitespace from `input` | ||
slang.trimRight = function trimRight(input) { | ||
return input.trimRight ? input.trimRight() : input.replace(/s+$/, ''); | ||
return input.trimRight ? input.trimRight() : input.replace(/\s+$/, ''); | ||
} | ||
// Truncates `input` to `args.limit` or 10 and adds `args.omission` or "..." | ||
slang.truncate = function truncate(input, args) { | ||
var limit = args && args.limit || 10, | ||
omission = args && args.omission || '...'; | ||
return input.length <= limit ? input : input.slice(0, limit) + omission; | ||
} | ||
// Joins an array into a humanized list. The last element is joined | ||
@@ -130,2 +138,17 @@ // by "and" by default, but you can change it. | ||
// Returns a humanized number with the correct suffix | ||
// such as 1st, 2nd, 3rd or 4th | ||
slang.humanize = function humanize(number) { | ||
if(number % 100 >= 11 && number % 100 <= 13) | ||
return number + "th"; | ||
switch(number % 10) { | ||
case 1: return number + "st"; | ||
case 2: return number + "nd"; | ||
case 3: return number + "rd"; | ||
} | ||
return number + "th"; | ||
} | ||
// Returns whether `input` contains `string` | ||
@@ -236,4 +259,9 @@ slang.contains = function contains(input, string) { | ||
for (key in slang) { | ||
if (key === 'guid' || key === 'isString' || key === 'version' || key === 'addToPrototype') | ||
continue; | ||
if (key === 'guid' || | ||
key === 'humanize' || | ||
key === 'isString' || | ||
key === 'version' || | ||
key === 'addToPrototype') { | ||
continue; | ||
} | ||
@@ -249,2 +277,2 @@ (function(key) { | ||
})(); | ||
})(); |
@@ -1,1 +0,1 @@ | ||
(function(){var a={};typeof module!="undefined"&&module.exports?module.exports=a:this.slang=a,a.version="0.1.1",a.isString=function(a){return Object.prototype.toString.call(a)==="[object String]"},a.capitalize=function(a){return a.charAt(0).toUpperCase()+a.slice(1)},a.uncapitalize=function(a){return a.charAt(0).toLowerCase()+a.slice(1)},a.capitalizeWords=function(b){return b.replace(/\w+/g,function(b){return a.capitalize(b)})},a.uncapitalizeWords=function(b){return b.replace(/\w+/g,function(b){return a.uncapitalize(b)})},a.isUpperCaseAt=function(a,b){return a.charAt(b).toUpperCase()===a.charAt(b)},a.isLowerCaseAt=function(a,b){return a.charAt(b).toLowerCase()===a.charAt(b)},a.swapcase=function(a){return a.replace(/([a-z]+)|([A-Z]+)/g,function(a,b,c){return b?a.toUpperCase():a.toLowerCase()})},a.camelize=function(a){return a.replace(/\W+(.)/g,function(a,b){return b.toUpperCase()})},a.dasherize=function(a){return a.replace(/\W+/g,"-").replace(/([a-z\d])([A-Z])/g,"$1-$2").toLowerCase()},a.repeat=function(a,b){return b<1?"":Array(b+1).join(a)},a.insert=function(a,b,c){return a.slice(0,c)+b+a.slice(c)},a.remove=function(a,b,c){return a.slice(0,b)+a.slice(c)},a.chop=function(a){return a.slice(0,-1)},a.trim=function(a){return a.trim?a.trim():a.replace(/^\s+/,"").replace(/\s+$/,"")},a.trimLeft=function(a){return a.trimLeft?a.trimLeft():a.replace(/^s+/,"")},a.trimRight=function(a){return a.trimRight?a.trimRight():a.replace(/s+$/,"")},a.join=function(a,b){var c=a.pop(),b=b||"and";return a.join(", ")+" "+b+" "+c},a.contains=function(a,b){return a.indexOf(b)>-1},a.startsWith=function(a,b){return a.indexOf(b)===0},a.endsWith=function(a,b){return a.indexOf(b)===a.length-b.length},a.isBlank=function(a){return/^\s*$/.test(a)},a.successor=function(a){var b="abcdefghijklmnopqrstuvwxyz",c=b.length,d=a,e=a.length;while(e>=0){var f=a.charAt(--e),g="",h=!1;if(isNaN(f)){index=b.indexOf(f.toLowerCase());if(index===-1)g=f,h=!0;else{var i=f===f.toUpperCase();g=b.charAt((index+1)%c),i&&(g=g.toUpperCase()),h=index+1>=c;if(h&&e===0){var j=i?"A":"a";d=j+g+d.slice(1);break}}}else{g=+f+1,g>9&&(g=0,h=!0);if(h&&e===0){d="1"+g+d.slice(1);break}}d=d.slice(0,e)+g+d.slice(e+1);if(!h)break}return d},a.guid=function(a){var b=[],c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",d=c.length,a=a||32;for(var e=0;e<a;e++)b[e]=c.charAt(Math.floor(Math.random()*d));return b.join("")},a.addToPrototype=function(){for(key in a){if(key==="guid"||key==="isString"||key==="version"||key==="addToPrototype")continue;(function(b){String.prototype[b]=function(){var c=Array.prototype.slice.call(arguments);return a[b].apply(a,[this].concat(c))}})(key)}}})() | ||
(function(){var a={};typeof module!="undefined"&&module.exports?module.exports=a:this.slang=a,a.version="0.1.2",a.isString=function(a){return Object.prototype.toString.call(a)==="[object String]"},a.capitalize=function(a){return a.charAt(0).toUpperCase()+a.slice(1)},a.uncapitalize=function(a){return a.charAt(0).toLowerCase()+a.slice(1)},a.capitalizeWords=function(b){return b.replace(/\w+/g,function(b){return a.capitalize(b)})},a.uncapitalizeWords=function(b){return b.replace(/\w+/g,function(b){return a.uncapitalize(b)})},a.isUpperCaseAt=function(a,b){return a.charAt(b).toUpperCase()===a.charAt(b)},a.isLowerCaseAt=function(a,b){return a.charAt(b).toLowerCase()===a.charAt(b)},a.swapcase=function(a){return a.replace(/([a-z]+)|([A-Z]+)/g,function(a,b,c){return b?a.toUpperCase():a.toLowerCase()})},a.camelize=function(a){return a.replace(/\W+(.)/g,function(a,b){return b.toUpperCase()})},a.dasherize=function(a){return a.replace(/\W+/g,"-").replace(/([a-z\d])([A-Z])/g,"$1-$2").toLowerCase()},a.repeat=function(a,b){return b<1?"":(new Array(b+1)).join(a)},a.insert=function(a,b,c){return a.slice(0,c)+b+a.slice(c)},a.remove=function(a,b,c){return a.slice(0,b)+a.slice(c)},a.chop=function(a){return a.slice(0,-1)},a.trim=function(a){return a.trim?a.trim():a.replace(/^\s+/,"").replace(/\s+$/,"")},a.trimLeft=function(a){return a.trimLeft?a.trimLeft():a.replace(/^\s+/,"")},a.trimRight=function(a){return a.trimRight?a.trimRight():a.replace(/\s+$/,"")},a.truncate=function(a,b){var c=b&&b.limit||10,d=b&&b.omission||"...";return a.length<=c?a:a.slice(0,c)+d},a.join=function(a,b){var c=a.pop(),b=b||"and";return a.join(", ")+" "+b+" "+c},a.humanize=function(a){if(a%100>=11&&a%100<=13)return a+"th";switch(a%10){case 1:return a+"st";case 2:return a+"nd";case 3:return a+"rd"}return a+"th"},a.contains=function(a,b){return a.indexOf(b)>-1},a.startsWith=function(a,b){return a.indexOf(b)===0},a.endsWith=function(a,b){var c=a.length-b.length;return c>=0&&a.indexOf(b,c)>-1},a.isBlank=function(a){return/^\s*$/.test(a)},a.successor=function(a){var b="abcdefghijklmnopqrstuvwxyz",c=b.length,d=a,e=a.length;while(e>=0){var f=a.charAt(--e),g="",h=!1;if(isNaN(f)){index=b.indexOf(f.toLowerCase());if(index===-1)g=f,h=!0;else{var i=f===f.toUpperCase();g=b.charAt((index+1)%c),i&&(g=g.toUpperCase()),h=index+1>=c;if(h&&e===0){var j=i?"A":"a";d=j+g+d.slice(1);break}}}else{g=+f+1,g>9&&(g=0,h=!0);if(h&&e===0){d="1"+g+d.slice(1);break}}d=d.slice(0,e)+g+d.slice(e+1);if(!h)break}return d},a.guid=function(a){var b=[],c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",d=c.length,a=a||32;for(var e=0;e<a;e++)b[e]=c.charAt(Math.floor(Math.random()*d));return b.join("")},a.addToPrototype=function(){for(key in a){if(key==="guid"||key==="humanize"||key==="isString"||key==="version"||key==="addToPrototype")continue;(function(b){String.prototype[b]=function(){var c=Array.prototype.slice.call(arguments);return a[b].apply(a,[this].concat(c))}})(key)}}})() |
15
test.js
@@ -60,2 +60,8 @@ var slang = require('./slang'), | ||
// Test **slang.truncate** | ||
assert.equal('Lorem ipsu...', slang.truncate('Lorem ipsum dolor sit amet.')); | ||
assert.equal('Lorem ipsum...', slang.truncate('Lorem ipsum dolor sit amet.', { limit: 11 })); | ||
assert.equal('Lorem...(read more)', slang.truncate('Lorem ipsum dolor sit amet.', { limit: 5, omission: '...(read more)' })); | ||
assert.equal('Lorem ipsu...(read more)', slang.truncate('Lorem ipsum dolor sit amet.', { omission: '...(read more)' })); | ||
// Test **slang.join** | ||
@@ -65,2 +71,11 @@ assert.equal('red, blue and green', slang.join(['red', 'blue', 'green'])); | ||
// Test **slang.humanize** | ||
assert.equal('1st', slang.humanize(1)); | ||
assert.equal('2nd', slang.humanize(2)); | ||
assert.equal('3rd', slang.humanize(3)); | ||
assert.equal('4th', slang.humanize(4)); | ||
assert.equal('11th', slang.humanize(11)); | ||
assert.equal('23rd', slang.humanize(23)); | ||
assert.equal('104th', slang.humanize(104)); | ||
// Test **slang.contains** | ||
@@ -67,0 +82,0 @@ assert.ok(slang.contains('hello world', 'world'), 'slang.contains failed'); |
Sorry, the diff of this file is not supported yet
81687
490
169