Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Sep 21, 2021
1 parent dace42b commit c025e15
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 64 deletions.
16 changes: 13 additions & 3 deletions lib/defaults.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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',
]);
};
2 changes: 1 addition & 1 deletion lib/inflect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ module.exports = function (attach) {
require('./native')(methods);
}

return methods
return methods;
};
48 changes: 33 additions & 15 deletions lib/inflections.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
};

Expand All @@ -78,15 +96,15 @@ 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.
//
// uncountable "money"
// 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'_,
Expand All @@ -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 () {
Expand Down
38 changes: 20 additions & 18 deletions lib/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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();
};
Expand Down Expand Up @@ -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
Expand All @@ -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';
}
}
};
Expand All @@ -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;
});
};

Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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, /.*\./, '')));
}
};
41 changes: 33 additions & 8 deletions lib/native.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -22,5 +48,4 @@ module.exports = function (obj) {
}
}
});

}
};
38 changes: 19 additions & 19 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -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
//
Expand All @@ -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;
Expand All @@ -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.
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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();
});
}

Expand All @@ -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);
Expand All @@ -142,6 +142,6 @@ var u = module.exports = {
// "hello".value() #=> "hello"
value: function (str) {
return str.substr(0);
}
}
}
},
},
});

0 comments on commit c025e15

Please sign in to comment.