Permalink
Browse files

First Release

  • Loading branch information...
1 parent 512309b commit ca622112bbf2eca57a6ef0e8ee675615863fb0e1 @Qard committed Jul 26, 2011
Showing with 122 additions and 0 deletions.
  1. BIN .DS_Store
  2. +13 −0 README
  3. +101 −0 index.js
  4. +8 −0 package.json
View
Binary file not shown.
View
@@ -0,0 +1,13 @@
+# InflectJS
+InflectJS Provides basic inflection utilities and formatters for camelCase and separations.
+
+## API
+plural(str [, keep_case]) -- Convert a singular word to plural. Lowercases by default.
+singular(str [, keep_case]) -- Convert a plural word to singular. Lowercases by default.
+camel(str) -- Merge separated words, spaces or otherwise, to a camelcased word.
+separate(str, splitter, keep_case) -- Separate words using define separator. Also converts camelcased words. Lowercases by default.
+underscores(str, keep_case) -- Alias for separating with _
+dashes(str, keep_case) -- Alias for separating with -
+pluses(str, keep_case) -- Alias for separating with +
+human(str, caps_all) -- Convert separated or camelcased words to a human readable format.
+trim(str) -- Trim whitespace from a string.
View
@@ -0,0 +1,101 @@
+/*!
+ * Inflect
+ * Copyright(c) 2011 Stephen Belanger <admin@stephenbelanger.com>
+ * MIT Licensed
+ */
+(function(exports){
+ // Remove whitespace.
+ exports.trim = function(str) {
+ return str.replace(/^\s+|\s+$/, '');
+ }
+
+ // Make singular words plural.
+ exports.plural = function(str, keep_case) {
+ var s = this.trim(str);
+ if ( ! keep_case) { s = s.toLowerCase(); }
+ switch (s.substr(-1)) {
+ case 'y':
+ s = s.substr(-2, 1) in ['a', 'e', 'i', 'o', 'u']
+ ? s + 's'
+ : s.substr(0, s.length-1) + 'ies';
+ break;
+
+ case 'h':
+ s += s.substr(-2) == 'ch' || s.substr(-2) == 'sh' ? 'es' : 's';
+ break;
+
+ case 's':
+ s += 'es';
+ break;
+
+ default:
+ s += 's';
+ }
+ return s;
+ }
+
+ // Make plural words singular.
+ exports.singular = function(str, keep_case) {
+ var s = this.trim(str);
+ if ( ! keep_case) { s = s.toLowerCase(); }
+ switch (s.substr(-3)) {
+ case 'ies':
+ s = s.substr(0, s.length-3) + 'y';
+ break;
+
+ case 'ses':
+ s = s.substr(0, s.length-2);
+ break;
+
+ default:
+ if (s.substr(-1) == 's') {
+ s = s.substr(0, s.length-1);
+ }
+ }
+ return s;
+ }
+
+ // Remove word separators and uppercase first char of each word.
+ exports.camel = function(str) {
+ var s = this.human(str).toLowerCase();
+ s = s.replace(/[\s_]/g, ' ');
+ s = s.replace(/^(.)|\s(.)/g, function($1) {
+ return $1.toUpperCase();
+ });
+ return s.replace(/ /g, '');
+ }
+
+ // Replace spaces with a different word separator.
+ exports.separate = function(str, splitter, keep_case) {
+ var s = this.human(str);
+ if ( ! keep_case) { s = s.toLowerCase(); }
+ return s.replace(/[\s]+/g, splitter);
+ }
+
+ // Separate aliases.
+ exports.underscores = function(str, keep_case) {
+ return this.separate(str, '_', keep_case);
+ }
+ exports.dashes = function(str, keep_case) {
+ return this.separate(str, '-', keep_case);
+ }
+ exports.pluses = function(str, keep_case) {
+ return this.separate(str, '+', keep_case);
+ }
+
+ // Make camelCasedStrings or separated_strings human readable.
+ exports.human = function(str, caps_all) {
+ var s = this.trim(str);
+ s = str.replace(/[A-Z]/g, '_$&');
+ if (s.charAt(0) === '_') { s = s.substr(1); }
+ s = s.toLowerCase().replace(/[^A-Za-z0-9]+/g, ' ');
+ if (caps_all) {
+ s = s.replace(/^(.)|\s(.)/g, function($1) {
+ return $1.toUpperCase();
+ });
+ } else {
+ s = s.charAt(0).toUpperCase()+s.substr(1);
+ }
+ return s;
+ }
+})(typeof module !== 'undefined' ? module.exports : (window.Inflect || (window.Inflect = {})));
View
@@ -0,0 +1,8 @@
+{
+ "name": "inflectjs",
+ "version":"0.0.1",
+ "engines": {
+ "node": ">=v0.4.0"
+ },
+ "dependencies": {}
+}

0 comments on commit ca62211

Please sign in to comment.