Skip to content

Albert-IV/extensions.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

extensions.js

The aim of this plugin is to add methods JavaScript natives to include missing and helpful functionality.

DATES

Date.prototype.isDate()

var date = new Date()
  , badDate = new Date('This is not a date')

date.isDate();    // returns true
badDate.isDate(); // returns false

{}.isDate();      // returns false
"March".isDate()  // returns false
(55).isDate()     // returns false

This method returns true or false depending on if the Date object is a valid date. No more !isNaN(dateObj.getTime())

Date.prototype.getLabel()

var date = new Date();
date.getLabel();    //  Returns in this format: "MTH DD YYYY"

This returns the date as a formatted date string.

Date.prototype.getDelta()

var date1 = new Date( 2000, 0, 1), date2 = new Date( 2000, 0, 4);
date1.getDelta( date2 );  // Returns object with delta

//  ============ OR ==============

var date1 = new Date( 2000, 0, 1), date2 = "January 4th, 2000";
date1.getDelta( date2 );  // Also accepts date strings

Returns an object with the caluclated delta difference. Currently only up to day (ie no months are ever returned).

ARRAYS

###Array.prototype.merge()

var arr = [1, 2, 3];
arr.merge([4, 5, 6];  // Returns [1, 2, 3, 4, 5, 6]

This combines the two arrays together. This does alter the original array, as well as returns the new value of the array.

OBJECTS

###Object.prototype.peel()

var obj = { name : { first : "Al", last : "Bundy" } };
obj.peel('name.last');  // returns "Bundy"
obj.peel('path.not.in.object'); // Returns 0

This allows you to reach into objects and get their values, if it exists. If it doesn't, will return 0. The advantage of this is you avoid a dreaded obj.name && obj.name.first situation, and not have to worry about a malformed object blowing up your application.

STRINGS

###String.prototype.formatPhone()

var badlyFormattedPhone = "737-4844832 x58";
badlyFormattedPhone.formatPhone()
// returns "(737) 484-4832 x58"

This strips every non-number in the string, then formats occordingly. If there aren't enough numbers, will fill the missing numbers with an underscore "_". If the number is longer than a US phone number, it will consider the extra numbers an extension.

###String.prototype.trim()

var str = "   No more spaces!          ";
str.trim();  // Returns "No more spaces!"

This method returns the string without leading or ending spaces.

####A Small Note

According to some, you shouldn't be adding prototypes to native JavaScript. Here's an argument for that.

About

Useful Extensions for JavaScript Natives

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published