diff --git a/dist/classified.js b/dist/classified.js index 26267fc..c110e19 100644 --- a/dist/classified.js +++ b/dist/classified.js @@ -786,6 +786,16 @@ classify(String, function() { return this.replace(/[-_]+/g, ' ').capitalize(); }); + // Returns the plural form of the word in the string. + def('pluralize', function() { + return Inflector.pluralize(this); + }); + + // The reverse of `pluralize`, returns the singular form of a word in a string. + def('singularize', function() { + return Inflector.singularize(this); + }); + // Capitalizes all the words and replaces some characters in the string to create a nicer looking title. def('titleize', function() { return this.underscore().humanize().replace(/\b('?[a-z])/g, function(match, word) { diff --git a/dist/classified.min.js b/dist/classified.min.js index 18b149c..00af05c 100644 --- a/dist/classified.min.js +++ b/dist/classified.min.js @@ -9,4 +9,4 @@ // - classify 0.10.7 http://github.com/petebrowne/classify // //-------------------------------------------------------------------------- -(function(){var h=this,d=h.def,j=h.classify,l=h.module,m=h.include,n=h.extend,g=h.alias,i=h.$break={};n(Object,function(){var f=Object.prototype.toString;d('isUndefined',function(a){return typeof a==='undefined'});d('isDefined',function(a){return!Object.isUndefined(a)});d('isObject',function(a){if(a==null)return false;return f.call(a)==='[object Object]'});d('isFunction',function(a){return f.call(a)==='[object Function]'});d('isString',function(a){return f.call(a)==='[object String]'});d('isNumber',function(a){return f.call(a)==='[object Number]'});d('isDate',function(a){return f.call(a)==='[object Date]'});d('isRegExp',function(a){return f.call(a)==='[object RegExp]'});if(Object.isFunction(Array.isArray)){d('isArray',Array.isArray)}else{d('isArray',function(a){return f.call(a)==='[object Array]'})}d('extend',function(a,b){for(var c in b){a[c]=b[c]}return a});g('merge','extend');d('clone',function(a){return this.extend({},a)});d('each',function(a,b,c){for(var e in a){if(a.hasOwnProperty(e)){b.call(c,e,a[e])}}});if(!Object.isFunction(Object.keys)){d('keys',function(c){if(!Object.isObject(c))throw new TypeError();var e=[];Object.each(c,function(a,b){e.push(a)});return e})}d('values',function(c){var e=[];Object.each(c,function(a,b){e.push(b)});return e})});l('Enumerable',function(){var k=function(a){return a};d('each',function(b,c){var e=0;try{this.__each__(function(a){b.call(c,a,e++)})}catch(error){if(error!=i){throw error;}}return this});d('all',function(c,e){c=c||k;var f=true;this.each(function(a,b){f=f&&!!c.call(e,a,b);if(!f){throw i;}});return f});g('every','all');d('any',function(c,e){c=c||k;var f=false;this.each(function(a,b){if(f=!!c.call(e,a,b)){throw i;}});return f});g('some','any');d('collect',function(e,f){e=e||k;return this.inject([],function(a,b,c){a.push(e.call(f,b,c));return a})});g('map','collect');d('detect',function(c,e){var f=null;this.each(function(a,b){if(c.call(e,a,b)){f=a;throw i;}});return f});g('find','detect');d('include',function(b){if(Object.isFunction(this.indexOf)){return this.indexOf(b)!=-1}var c=false;this.each(function(a){if(Object.isDefined(a.value)){a=a.value}if(a==b){c=true;throw i;}});return c});g('contains','include');d('inject',function(c,e,f){this.each(function(a,b){c=e.call(f,c,a,b)});return c});g('reduce','inject');d('invoke',function(b){var c=Array.slice(arguments,1);return this.map(function(a){return a[b].apply(a,c)})});d('pluck',function(b){return this.map(function(a){return a[b]})});d('reject',function(e,f){return this.inject([],function(a,b,c){if(!e.call(f,b,c)){a.push(b)}return a})});g('not','reject');d('select',function(e,f){return this.inject([],function(a,b,c){if(e.call(f,b,c)){a.push(b)}return a})});g('findAll','select');g('filter','select')});l('Events',function(){d('bind',function(a,b){var c=this.__events__||(this.__events__={}),e=c[a]||(c[a]=[]);e.push(b);return this});d('unbind',function(a,b){var c,e;if(!a){delete this.__events__}else if(c=this.__events__){if(!b){delete c[a]}else if(e=c[a]){for(var f=0,k=e.length;f0&&a=0&&this.indexOf(a,b)===b});d('include',function(a){return this.indexOf(a)>-1});g('contains','include');d('isBlank',function(){return/^\s*$/.test(this)});d('isEmpty',function(){return this==''});d('startsWith',function(a){return this.lastIndexOf(a,0)===0});d('strip',function(){return this.replace(/^\s+/,'').replace(/\s+$/,'')});d('camelize',function(){return this.replace(/[-_]+(.)?/g,function(a,b){return b?b.toUpperCase():''})});d('capitalize',function(){return this.charAt(0).toUpperCase()+this.substring(1)});d('constantize',function(){var b=this.split('.'),c=h;b.each(function(a){c=c[a];if(Object.isUndefined(c))throw i;});return c});d('dasherize',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1-$2').replace(/([a-z\d])([A-Z])/g,'$1-$2').replace(/_/g,'-').toLowerCase()});d('humanize',function(){return this.replace(/[-_]+/g,' ').capitalize()});d('titleize',function(){return this.underscore().humanize().replace(/\b('?[a-z])/g,function(a,b){return b.capitalize()})});g('toTitleCase','titleize');d('underscore',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1_$2').replace(/([a-z\d])([A-Z])/g,'$1_$2').replace(/-/g,'_').toLowerCase()})});(function(a,b,c){a(/$/,'s');a(/s$/i,'s');a(/(ax|test)is$/i,'$1es');a(/(octop|vir)us$/i,'$1i');a(/(alias|status)$/i,'$1es');a(/(bu)s$/i,'$1ses');a(/(buffal|tomat)o$/i,'$1oes');a(/([ti])um$/i,'$1a');a(/sis$/i,'ses');a(/(?:([^f])fe|([lr])f)$/i,'$1$2ves');a(/(hive)$/i,'$1s');a(/([^aeiouy]|qu)y$/i,'$1ies');a(/(x|ch|ss|sh)$/i,'$1es');a(/(matr|vert|ind)(?:ix|ex)$/i,'$1ices');a(/([m|l])ouse$/i,'$1ice');a(/^(ox)$/i,'$1en');a(/(quiz)$/i,'$1zes');b(/s$/i,'');b(/(n)ews$/i,'$1ews');b(/([ti])a$/i,'$1um');b(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i,'$1$2sis');b(/(^analy)ses$/i,'$1sis');b(/([^f])ves$/i,'$1fe');b(/(hive)s$/i,'$1');b(/(tive)s$/i,'$1');b(/([lr])ves$/i,'$1f');b(/([^aeiouy]|qu)ies$/i,'$1y');b(/(s)eries$/i,'$1eries');b(/(m)ovies$/i,'$1ovie');b(/(x|ch|ss|sh)es$/i,'$1');b(/([m|l])ice$/i,'$1ouse');b(/(bus)es$/i,'$1');b(/(o)es$/i,'$1');b(/(shoe)s$/i,'$1');b(/(cris|ax|test)es$/i,'$1is');b(/(octop|vir)i$/i,'$1us');b(/(alias|status)es$/i,'$1');b(/^(ox)en/i,'$1');b(/(vert|ind)ices$/i,'$1ex');b(/(matr)ices$/i,'$1ix');b(/(quiz)zes$/i,'$1');b(/(database)s$/i,'$1');c('person','people');c('man','men');c('child','children');c('sex','sexes');c('move','moves');c('cow','kine');Inflector.uncountable('equipment','information','rice','money','species','series','fish','sheep')})(Inflector.plural,Inflector.singular,Inflector.irregular)})(); \ No newline at end of file +(function(){var h=this,d=h.def,j=h.classify,l=h.module,m=h.include,n=h.extend,g=h.alias,i=h.$break={};n(Object,function(){var f=Object.prototype.toString;d('isUndefined',function(a){return typeof a==='undefined'});d('isDefined',function(a){return!Object.isUndefined(a)});d('isObject',function(a){if(a==null)return false;return f.call(a)==='[object Object]'});d('isFunction',function(a){return f.call(a)==='[object Function]'});d('isString',function(a){return f.call(a)==='[object String]'});d('isNumber',function(a){return f.call(a)==='[object Number]'});d('isDate',function(a){return f.call(a)==='[object Date]'});d('isRegExp',function(a){return f.call(a)==='[object RegExp]'});if(Object.isFunction(Array.isArray)){d('isArray',Array.isArray)}else{d('isArray',function(a){return f.call(a)==='[object Array]'})}d('extend',function(a,b){for(var c in b){a[c]=b[c]}return a});g('merge','extend');d('clone',function(a){return this.extend({},a)});d('each',function(a,b,c){for(var e in a){if(a.hasOwnProperty(e)){b.call(c,e,a[e])}}});if(!Object.isFunction(Object.keys)){d('keys',function(c){if(!Object.isObject(c))throw new TypeError();var e=[];Object.each(c,function(a,b){e.push(a)});return e})}d('values',function(c){var e=[];Object.each(c,function(a,b){e.push(b)});return e})});l('Enumerable',function(){var k=function(a){return a};d('each',function(b,c){var e=0;try{this.__each__(function(a){b.call(c,a,e++)})}catch(error){if(error!=i){throw error;}}return this});d('all',function(c,e){c=c||k;var f=true;this.each(function(a,b){f=f&&!!c.call(e,a,b);if(!f){throw i;}});return f});g('every','all');d('any',function(c,e){c=c||k;var f=false;this.each(function(a,b){if(f=!!c.call(e,a,b)){throw i;}});return f});g('some','any');d('collect',function(e,f){e=e||k;return this.inject([],function(a,b,c){a.push(e.call(f,b,c));return a})});g('map','collect');d('detect',function(c,e){var f=null;this.each(function(a,b){if(c.call(e,a,b)){f=a;throw i;}});return f});g('find','detect');d('include',function(b){if(Object.isFunction(this.indexOf)){return this.indexOf(b)!=-1}var c=false;this.each(function(a){if(Object.isDefined(a.value)){a=a.value}if(a==b){c=true;throw i;}});return c});g('contains','include');d('inject',function(c,e,f){this.each(function(a,b){c=e.call(f,c,a,b)});return c});g('reduce','inject');d('invoke',function(b){var c=Array.slice(arguments,1);return this.map(function(a){return a[b].apply(a,c)})});d('pluck',function(b){return this.map(function(a){return a[b]})});d('reject',function(e,f){return this.inject([],function(a,b,c){if(!e.call(f,b,c)){a.push(b)}return a})});g('not','reject');d('select',function(e,f){return this.inject([],function(a,b,c){if(e.call(f,b,c)){a.push(b)}return a})});g('findAll','select');g('filter','select')});l('Events',function(){d('bind',function(a,b){var c=this.__events__||(this.__events__={}),e=c[a]||(c[a]=[]);e.push(b);return this});d('unbind',function(a,b){var c,e;if(!a){delete this.__events__}else if(c=this.__events__){if(!b){delete c[a]}else if(e=c[a]){for(var f=0,k=e.length;f0&&a=0&&this.indexOf(a,b)===b});d('include',function(a){return this.indexOf(a)>-1});g('contains','include');d('isBlank',function(){return/^\s*$/.test(this)});d('isEmpty',function(){return this==''});d('startsWith',function(a){return this.lastIndexOf(a,0)===0});d('strip',function(){return this.replace(/^\s+/,'').replace(/\s+$/,'')});d('camelize',function(){return this.replace(/[-_]+(.)?/g,function(a,b){return b?b.toUpperCase():''})});d('capitalize',function(){return this.charAt(0).toUpperCase()+this.substring(1)});d('constantize',function(){var b=this.split('.'),c=h;b.each(function(a){c=c[a];if(Object.isUndefined(c))throw i;});return c});d('dasherize',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1-$2').replace(/([a-z\d])([A-Z])/g,'$1-$2').replace(/_/g,'-').toLowerCase()});d('humanize',function(){return this.replace(/[-_]+/g,' ').capitalize()});d('pluralize',function(){return Inflector.pluralize(this)});d('singularize',function(){return Inflector.singularize(this)});d('titleize',function(){return this.underscore().humanize().replace(/\b('?[a-z])/g,function(a,b){return b.capitalize()})});g('toTitleCase','titleize');d('underscore',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1_$2').replace(/([a-z\d])([A-Z])/g,'$1_$2').replace(/-/g,'_').toLowerCase()})});(function(a,b,c){a(/$/,'s');a(/s$/i,'s');a(/(ax|test)is$/i,'$1es');a(/(octop|vir)us$/i,'$1i');a(/(alias|status)$/i,'$1es');a(/(bu)s$/i,'$1ses');a(/(buffal|tomat)o$/i,'$1oes');a(/([ti])um$/i,'$1a');a(/sis$/i,'ses');a(/(?:([^f])fe|([lr])f)$/i,'$1$2ves');a(/(hive)$/i,'$1s');a(/([^aeiouy]|qu)y$/i,'$1ies');a(/(x|ch|ss|sh)$/i,'$1es');a(/(matr|vert|ind)(?:ix|ex)$/i,'$1ices');a(/([m|l])ouse$/i,'$1ice');a(/^(ox)$/i,'$1en');a(/(quiz)$/i,'$1zes');b(/s$/i,'');b(/(n)ews$/i,'$1ews');b(/([ti])a$/i,'$1um');b(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i,'$1$2sis');b(/(^analy)ses$/i,'$1sis');b(/([^f])ves$/i,'$1fe');b(/(hive)s$/i,'$1');b(/(tive)s$/i,'$1');b(/([lr])ves$/i,'$1f');b(/([^aeiouy]|qu)ies$/i,'$1y');b(/(s)eries$/i,'$1eries');b(/(m)ovies$/i,'$1ovie');b(/(x|ch|ss|sh)es$/i,'$1');b(/([m|l])ice$/i,'$1ouse');b(/(bus)es$/i,'$1');b(/(o)es$/i,'$1');b(/(shoe)s$/i,'$1');b(/(cris|ax|test)es$/i,'$1is');b(/(octop|vir)i$/i,'$1us');b(/(alias|status)es$/i,'$1');b(/^(ox)en/i,'$1');b(/(vert|ind)ices$/i,'$1ex');b(/(matr)ices$/i,'$1ix');b(/(quiz)zes$/i,'$1');b(/(database)s$/i,'$1');c('person','people');c('man','men');c('child','children');c('sex','sexes');c('move','moves');c('cow','kine');Inflector.uncountable('equipment','information','rice','money','species','series','fish','sheep')})(Inflector.plural,Inflector.singular,Inflector.irregular)})(); \ No newline at end of file diff --git a/spec/classified/stringSpec.js b/spec/classified/stringSpec.js index 3b98b56..a359bc0 100755 --- a/spec/classified/stringSpec.js +++ b/spec/classified/stringSpec.js @@ -160,6 +160,18 @@ describe('String', function() { }); }); + describe('#pluralize', function() { + it('pluralizes words', function() { + expect('category'.pluralize()).toEqual('categories'); + }); + }); + + describe('#singularize', function() { + it('singularizes words', function() { + expect('people'.singularize()).toEqual('person'); + }); + }); + describe('#titleize', function() { it('capitalizes each word', function() { expect('man from the boondocks'.titleize()).toEqual('Man From The Boondocks'); diff --git a/src/extensions/string.js b/src/extensions/string.js index 5036023..1fc7c65 100755 --- a/src/extensions/string.js +++ b/src/extensions/string.js @@ -79,6 +79,16 @@ classify(String, function() { return this.replace(/[-_]+/g, ' ').capitalize(); }); + // Returns the plural form of the word in the string. + def('pluralize', function() { + return Inflector.pluralize(this); + }); + + // The reverse of `pluralize`, returns the singular form of a word in a string. + def('singularize', function() { + return Inflector.singularize(this); + }); + // Capitalizes all the words and replaces some characters in the string to create a nicer looking title. def('titleize', function() { return this.underscore().humanize().replace(/\b('?[a-z])/g, function(match, word) {