https://github.com/dperrymorrow/lil-p
Javascript Prototype Extensions
https://github.com/dperrymorrow/lil-p
Last synced: about 1 year ago
JSON representation
Javascript Prototype Extensions
- Host: GitHub
- URL: https://github.com/dperrymorrow/lil-p
- Owner: dperrymorrow
- License: other
- Created: 2012-09-26T18:16:39.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2014-06-24T04:06:07.000Z (almost 12 years ago)
- Last Synced: 2025-01-22T01:42:49.294Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 235 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
#lil-p (Javascript Prototype Extensions)
Add on functions to native ojects in Javascript. See the tests directory for examples of use.
The fastest way to get started is to include the [minified version](https://raw.github.com/dperrymorrow/lil-p/master/lib/lil_p.min.js).
##String Object Extensions
###truncate(charLimit:Number, ext:String);
Truncates a string to the charLimit number, and then appends the ext if needed. Defaults to 10 and ... if params are ommited.
````javascript
'Hey there im a really long string'.truncate(5);
// returns => 'Hey t...'
'Hey there im a really long string'.truncate(5, '(shortened)');
// returns => 'Hey t(shortened)'
'Hey there im a really long string'.truncate();
// returns => 'Hey there...'
````
###contains(substring:String);
Searches the string for the substring, and returns true/false for if found.
````javascript
"foobar".contains('foo');
// returns => true
````
###safe();
Strips encodes HTMl characters in the string.
````javascript
"foobarbar".safe();
// returns => "foobar<a href='foo'>bar</a>"
````
###humanize();
Converts camelcase, underscored, or dashed string to human readable space separated.
````javascript
'monkeyPants'.humanize();
// returns => 'monkey pants'
'monkey_Pants'.humanize();
// returns => 'monkey pants'
'monkey-Pants'.humanize();
// returns => 'monkey pants'
````
###inject(obj:Object);
Inject an object into a string just like in Ruby or Coffeescript #{} syntax
````javascript
var obj = {victim: "cat"};
"dog bit #{victim}".inject(obj);
// returns => 'dogs bit cat'
````
###pluralize(amount:Number);
Converts a string to plural if the number passed is greater than 1. If nothing passed, converts to plural.
````javascript
'dog'.pluralize(3);
// returns => 'dogs'
'dog'.pluralize(1);
// returns => 'dog'
````
###singularize();
Converts a plural string back to singular. No params.
````javascript
'dogs'.singularize();
// returns => 'dog'
````
###wordWrap(chars:Number, breakChars:Array);
Takes a string and inserts wbr HTML elements. This allows the browser to wrap within a word.
Defaults to 25 and ["/", "-", " "]
```javascript
"hello there-somethingerather".wordWrap()
// returns => "hello there-somethingerather"
"123456789".wordWrap(5);
//returns => "123456789"
````
##Date Object Extensions
###setTimeAgo(distance:String);
Takes a date object and sets it back in time to human readable string such as "2 weeks", "3 minutes", "5 years", or "30 seconds"
````javascript
var stamp = new Date("March 11, 1985 09:25:00 GMT-0800 (PST)");
stamp.setTimeAgo('6 weeks');
// returns => 'Mon Jan 28 1985 09:25:00 GMT-0800 (PST)'
````
###distanceInWords(dateObject:Date, suffix:String);
Returns a string of the distance in words from one Date object to another. Suffix defaults to "ago".
````javascript
var start = new Date("March 11, 1985 09:25:00 GMT-0800 (PST)");
var stop = new Date("July 30, 1999 10:50:00 GMT-0800 (PST)");
stop.distanceInWords(start);
// returns => '14 years, 5 months ago';
````