Skip to content

Commit

Permalink
Added the properties directly to string
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Feb 14, 2012
1 parent b715760 commit 32b25fe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -16,6 +16,13 @@ Require the module before using
var inflect = require('node-inflect');
```

All the below api functions can be called directly on a string

```js
inflect.titleize('messages to store') // === 'Messages To Store'
'messages to store'.titleize // === 'Messages To Store'
```

### Pluralize

```js
Expand Down Expand Up @@ -97,6 +104,8 @@ inflect.foreign_key('MessageBusProperty', false); // === 'message_bus_propertyid
inflect.ordinalize( '1' ); // === '1st'
```

## Custom rules for inflection

### Custom plural

We can use regexp in any of these custom rules
Expand Down
2 changes: 2 additions & 0 deletions lib/inflect.js
Expand Up @@ -3,3 +3,5 @@ require('./utils/array');
require('./utils/string');

module.exports = require('./methods');

require('./utils/inflect')(module.exports);
26 changes: 26 additions & 0 deletions lib/utils/inflect.js
@@ -0,0 +1,26 @@
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'
];

Object.keys(obj).forEach(function (key) {
if (key != 'inflect' && key != 'inflections') {
if (stringPrototypeBlacklist.indexOf(key) !== -1) {
console.log('warn: You should not override String.prototype.' + key);
} else {
addProperty(key, function () {
return obj[key](this);
});
}
}
});

}

0 comments on commit 32b25fe

Please sign in to comment.