diff --git a/lib/defaults.js b/lib/defaults.js index 05bdaf2..859d464 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -1,6 +1,5 @@ // Default inflections module.exports = function (inflect) { - inflect.plural(/$/, 's'); inflect.plural(/s$/i, 's'); inflect.plural(/(ax|test)is$/i, '$1es'); @@ -64,5 +63,16 @@ module.exports = function (inflect) { inflect.irregular('safe', 'safes'); inflect.irregular('fife', 'fifes'); - inflect.uncountable(['equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep', 'jeans', 'sushi']); -} + inflect.uncountable([ + 'equipment', + 'information', + 'rice', + 'money', + 'species', + 'series', + 'fish', + 'sheep', + 'jeans', + 'sushi', + ]); +}; diff --git a/lib/inflect.js b/lib/inflect.js index 5e0cc70..ea5c230 100644 --- a/lib/inflect.js +++ b/lib/inflect.js @@ -7,5 +7,5 @@ module.exports = function (attach) { require('./native')(methods); } - return methods + return methods; }; diff --git a/lib/inflections.js b/lib/inflections.js index 1972551..cde19c6 100644 --- a/lib/inflections.js +++ b/lib/inflections.js @@ -49,24 +49,42 @@ Inflections.prototype.singular = function (rule, replacement) { // // irregular 'octopus', 'octopi' // irregular 'person', 'people' -Inflections.prototype.irregular = function (singular, plural, fullMatchRequired) { +Inflections.prototype.irregular = function (singular, plural, fullMatchRequired) { this.uncountables = util.array.del(this.uncountables, singular); this.uncountables = util.array.del(this.uncountables, plural); - var prefix = ""; + var prefix = ''; if (fullMatchRequired) { - prefix = "^"; + prefix = '^'; } if (singular[0].toUpperCase() == plural[0].toUpperCase()) { - this.plural(new RegExp("(" + prefix + singular[0] + ")" + singular.slice(1) + "$", "i"), '$1' + plural.slice(1)); - this.plural(new RegExp("(" + prefix + plural[0] + ")" + plural.slice(1) + "$", "i"), '$1' + plural.slice(1)); - this.singular(new RegExp("(" + prefix + plural[0] + ")" + plural.slice(1) + "$", "i"), '$1' + singular.slice(1)); + this.plural(new RegExp('(' + prefix + singular[0] + ')' + singular.slice(1) + '$', 'i'), '$1' + plural.slice(1)); + this.plural(new RegExp('(' + prefix + plural[0] + ')' + plural.slice(1) + '$', 'i'), '$1' + plural.slice(1)); + this.singular(new RegExp('(' + prefix + plural[0] + ')' + plural.slice(1) + '$', 'i'), '$1' + singular.slice(1)); } else { - this.plural(new RegExp(prefix + (singular[0].toUpperCase()) + singular.slice(1) + "$"), plural[0].toUpperCase() + plural.slice(1)); - this.plural(new RegExp(prefix + (singular[0].toLowerCase()) + singular.slice(1) + "$"), plural[0].toLowerCase() + plural.slice(1)); - this.plural(new RegExp(prefix + (plural[0].toUpperCase()) + plural.slice(1) + "$"), plural[0].toUpperCase() + plural.slice(1)); - this.plural(new RegExp(prefix + (plural[0].toLowerCase()) + plural.slice(1) + "$"), plural[0].toLowerCase() + plural.slice(1)); - this.singular(new RegExp(prefix + (plural[0].toUpperCase()) + plural.slice(1) + "$"), singular[0].toUpperCase() + singular.slice(1)); - this.singular(new RegExp(prefix + (plural[0].toLowerCase()) + plural.slice(1) + "$"), singular[0].toLowerCase() + singular.slice(1)); + this.plural( + new RegExp(prefix + singular[0].toUpperCase() + singular.slice(1) + '$'), + plural[0].toUpperCase() + plural.slice(1) + ); + this.plural( + new RegExp(prefix + singular[0].toLowerCase() + singular.slice(1) + '$'), + plural[0].toLowerCase() + plural.slice(1) + ); + this.plural( + new RegExp(prefix + plural[0].toUpperCase() + plural.slice(1) + '$'), + plural[0].toUpperCase() + plural.slice(1) + ); + this.plural( + new RegExp(prefix + plural[0].toLowerCase() + plural.slice(1) + '$'), + plural[0].toLowerCase() + plural.slice(1) + ); + this.singular( + new RegExp(prefix + plural[0].toUpperCase() + plural.slice(1) + '$'), + singular[0].toUpperCase() + singular.slice(1) + ); + this.singular( + new RegExp(prefix + plural[0].toLowerCase() + plural.slice(1) + '$'), + singular[0].toLowerCase() + singular.slice(1) + ); } }; @@ -78,7 +96,7 @@ Inflections.prototype.irregular = function (singular, plural, fullMatchRequired // human "legacy_col_person_name", "Name" Inflections.prototype.human = function (rule, replacement) { this.humans.unshift([rule, replacement]); -} +}; // Add uncountable words that shouldn't be attempted inflected. // @@ -86,7 +104,7 @@ Inflections.prototype.human = function (rule, replacement) { // uncountable ["money", "information"] Inflections.prototype.uncountable = function (words) { this.uncountables = this.uncountables.concat(words); -} +}; // Clears the loaded inflections within a given scope (default is _'all'_). // Give the scope as a symbol of the inflection type, the options are: _'plurals'_, @@ -105,7 +123,7 @@ Inflections.prototype.clear = function (scope) { default: this[scope] = []; } -} +}; // Clears the loaded inflections and initializes them to [default](../inflections.html) Inflections.prototype.default = function () { diff --git a/lib/methods.js b/lib/methods.js index 8bbc9be..00f83ce 100644 --- a/lib/methods.js +++ b/lib/methods.js @@ -10,7 +10,7 @@ var util = require('./util'); var inflect = module.exports; // Import [inflections](inflections.html) instance -inflect.inflections = require('./inflections') +inflect.inflections = require('./inflections'); // Gives easy access to add inflections to this class inflect.inflect = function (fn) { @@ -31,13 +31,13 @@ inflect.inflect = function (fn) { // though there are cases where that does not hold: // // "SSLError".underscore.camelize // => "SslError" -inflect.camelize = function(lower_case_and_underscored_word, first_letter_in_uppercase) { +inflect.camelize = function (lower_case_and_underscored_word, first_letter_in_uppercase) { var result; if (first_letter_in_uppercase == null) first_letter_in_uppercase = true; - result = util.string.gsub(lower_case_and_underscored_word, /\/(.?)/, function($) { - return "." + (util.string.upcase($[1])); + result = util.string.gsub(lower_case_and_underscored_word, /\/(.?)/, function ($) { + return '.' + util.string.upcase($[1]); }); - result = util.string.gsub(result, /(?:_)(.)/, function($) { + result = util.string.gsub(result, /(?:_)(.)/, function ($) { return util.string.upcase($[1]); }); if (first_letter_in_uppercase) { @@ -61,8 +61,8 @@ inflect.camelize = function(lower_case_and_underscored_word, first_letter_in_upp inflect.underscore = function (camel_cased_word) { var self; self = util.string.gsub(camel_cased_word, /\./, '/'); - self = util.string.gsub(self, /([A-Z]+)([A-Z][a-z])/, "$1_$2"); - self = util.string.gsub(self, /([a-z\d])([A-Z])/, "$1_$2"); + self = util.string.gsub(self, /([A-Z]+)([A-Z][a-z])/, '$1_$2'); + self = util.string.gsub(self, /([a-z\d])([A-Z])/, '$1_$2'); self = util.string.gsub(self, /-/, '_'); return self.toLowerCase(); }; @@ -93,7 +93,9 @@ inflect.foreign_key = function (class_name, separate_class_name_and_id_with_unde if (separate_class_name_and_id_with_underscore == null) { separate_class_name_and_id_with_underscore = true; } - return inflect.underscore(inflect.demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id"); + return ( + inflect.underscore(inflect.demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? '_id' : 'id') + ); }; // Turns a number into an ordinal string used to denote the position in an @@ -109,17 +111,17 @@ inflect.ordinalize = function (number) { var _ref; number = parseInt(number); if ((_ref = Math.abs(number) % 100) === 11 || _ref === 12 || _ref === 13) { - return "" + number + "th"; + return '' + number + 'th'; } else { switch (Math.abs(number) % 10) { case 1: - return "" + number + "st"; + return '' + number + 'st'; case 2: - return "" + number + "nd"; + return '' + number + 'nd'; case 3: - return "" + number + "rd"; + return '' + number + 'rd'; default: - return "" + number + "th"; + return '' + number + 'th'; } } }; @@ -129,8 +131,8 @@ inflect.ordinalize = function (number) { // "money".uncountability() // => true // "my money".uncountability() // => true inflect.uncountability = function (word) { - return inflect.inflections.uncountables.some(function(ele, ind, arr) { - return word.match(new RegExp("(\\b|_)" + ele + "$", 'i')) != null; + return inflect.inflections.uncountables.some(function (ele, ind, arr) { + return word.match(new RegExp('(\\b|_)' + ele + '$', 'i')) != null; }); }; @@ -190,8 +192,8 @@ inflect.humanize = function (lower_case_and_underscored_word) { human = inflect.inflections.humans[i]; result = util.string.gsub(result, human[0], human[1]); } - result = util.string.gsub(result, /_id$/, ""); - result = util.string.gsub(result, /_/, " "); + result = util.string.gsub(result, /_id$/, ''); + result = util.string.gsub(result, /_/, ' '); return util.string.capitalize(result, true); }; @@ -229,4 +231,4 @@ inflect.tableize = function (class_name) { // "business".classify() // => "Busines" inflect.classify = function (table_name) { return inflect.camelize(inflect.singularize(util.string.gsub(table_name, /.*\./, ''))); -} +}; diff --git a/lib/native.js b/lib/native.js index d2c8de1..3c944c7 100644 --- a/lib/native.js +++ b/lib/native.js @@ -1,14 +1,40 @@ module.exports = function (obj) { - var addProperty = function (method, func) { String.prototype.__defineGetter__(method, func); - } + }; var stringPrototypeBlacklist = [ - '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor', - 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', 'charCodeAt', - 'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'replace', 'search', 'slice', 'split', 'substring', - 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight', 'gsub' + '__defineGetter__', + '__defineSetter__', + '__lookupGetter__', + '__lookupSetter__', + 'charAt', + 'constructor', + 'hasOwnProperty', + 'isPrototypeOf', + 'propertyIsEnumerable', + 'toLocaleString', + 'toString', + 'valueOf', + 'charCodeAt', + 'indexOf', + 'lastIndexof', + 'length', + 'localeCompare', + 'match', + 'replace', + 'search', + 'slice', + 'split', + 'substring', + 'toLocaleLowerCase', + 'toLocaleUpperCase', + 'toLowerCase', + 'toUpperCase', + 'trim', + 'trimLeft', + 'trimRight', + 'gsub', ]; Object.keys(obj).forEach(function (key) { @@ -22,5 +48,4 @@ module.exports = function (obj) { } } }); - -} +}; diff --git a/lib/util.js b/lib/util.js index 7af707d..183ae6e 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,6 @@ // Some utility functions in js -var u = module.exports = { +var u = (module.exports = { array: { // Returns a copy of the array with the value removed once // @@ -11,9 +11,9 @@ var u = module.exports = { if (index != -1) { if (index == 0) { - return arr.slice(1) + return arr.slice(1); } else { - return arr.slice(0, index).concat(arr.slice(index+1)); + return arr.slice(0, index).concat(arr.slice(index + 1)); } } else { return arr; @@ -23,16 +23,16 @@ var u = module.exports = { // Returns the first element of the array // // [1, 2, 3].first() #=> 1 - first: function(arr) { + first: function (arr) { return arr[0]; }, // Returns the last element of the array // // [1, 2, 3].last() #=> 3 - last: function(arr) { - return arr[arr.length-1]; - } + last: function (arr) { + return arr[arr.length - 1]; + }, }, string: { // Returns a copy of str with all occurrences of pattern replaced with either replacement or the return value of a function. @@ -52,7 +52,7 @@ var u = module.exports = { // gsub: function (str, pattern, replacement) { var i, match, matchCmpr, matchCmprPrev, replacementStr, result, self; - if (!((pattern != null) && (replacement != null))) return u.string.value(str); + if (!(pattern != null && replacement != null)) return u.string.value(str); result = ''; self = str; while (self.length > 0) { @@ -72,7 +72,7 @@ var u = module.exports = { replacementStr = replacement; for (i = 1; i <= 9; i++) { if (matchCmpr[i]) { - replacementStr = u.string.gsub(replacementStr, new RegExp("\\\$" + i), matchCmpr[i]); + replacementStr = u.string.gsub(replacementStr, new RegExp('\\$' + i), matchCmpr[i]); } } result += replacementStr; @@ -91,13 +91,13 @@ var u = module.exports = { // Returns a copy of the String with the first letter being upper case // // "hello".upcase #=> "Hello" - upcase: function(str) { + upcase: function (str) { var self = u.string.gsub(str, /_([a-z])/, function ($) { - return "_" + $[1].toUpperCase(); + return '_' + $[1].toUpperCase(); }); self = u.string.gsub(self, /\/([a-z])/, function ($) { - return "/" + $[1].toUpperCase(); + return '/' + $[1].toUpperCase(); }); return self[0].toUpperCase() + self.substr(1); @@ -115,7 +115,7 @@ var u = module.exports = { if (!spaces) { self = u.string.gsub(self, /\s([a-z])/, function ($) { - return " " + $[1].toUpperCase(); + return ' ' + $[1].toUpperCase(); }); } @@ -125,13 +125,13 @@ var u = module.exports = { // Returns a copy of the String with the first letter being lower case // // "HELLO".downcase #=> "hELLO" - downcase: function(str) { + downcase: function (str) { var self = u.string.gsub(str, /_([A-Z])/, function ($) { - return "_" + $[1].toLowerCase(); + return '_' + $[1].toLowerCase(); }); self = u.string.gsub(self, /\/([A-Z])/, function ($) { - return "/" + $[1].toLowerCase(); + return '/' + $[1].toLowerCase(); }); return self[0].toLowerCase() + self.substr(1); @@ -142,6 +142,6 @@ var u = module.exports = { // "hello".value() #=> "hello" value: function (str) { return str.substr(0); - } - } -} + }, + }, +});