-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdavid.utilities.js
More file actions
142 lines (125 loc) · 5.15 KB
/
david.utilities.js
File metadata and controls
142 lines (125 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/************************
* david.utilities
*
* The utilities classes are a library of helper functions to standardise functionality
* or make tasks easier.
*/
define(["jquery"],
function($jQ){
/*********************
* UPDATE THE STRING PROTOTYPE
*********************/
/**
* Pads the string on the left using the character provided, ensures the string is
* no longer than tnFinal length after padding.
*/
String.prototype.padLeft = function(tcPadPattern, tnFinalLength)
{
var loRE = new RegExp(".{" + tnFinalLength + "}$");
var lcPadding = "";
do
{
lcPadding += tcPadPattern;
} while(lcPadding.length < tnFinalLength);
return loRE.exec(lcPadding + this);
}
/**
* Pads the string on the right using the character provided, ensures the string is
* no longer than tnFinal length after padding.
*/
String.prototype.padRight = function(tcPadPattern, tnFinalLength)
{
var loRE = new RegExp("^.{" + tnFinalLength + "}");
var lcPadding = "";
do
{
lcPadding += tcPadPattern;
} while(lcPadding.length < tnFinalLength);
return loRE.exec(lcPadding + this);
}
/**
* Trims all white space from the front of the string
*/
String.prototype.lTrim = function()
{
return this.replace(/^\s+/, '');
}
/**
* Trims all white space from the back of the string
*/
String.prototype.rTrim = function()
{
return this.replace(/\s+$/, '');
}
/**
* Trims all white space from both sides of the string
*/
String.prototype.allTrim = function()
{
return this.replace(/^\s+|\s+$/g, '');
}
/***********************
* End of string prototype manipulation
***********************/
david.utilities = {
/**
* Checks if the object passed is the type specified
*/
isType : function(toObject, tcType)
{
return Object.prototype.toString.call(toObject) === ("[object " + tcType + "]");
},
/**
* Takes a URL and "Cleans" it by adding to the url, the default is to add the version from cachebuster
* This can be overridden in other modules
*/
cleanURL : function(tcURL)
{
return tcURL + (tcURL.indexOf("?") < 0 ? "?" : "&") + "version=" + require.config.cachebuster;
},
/**
* Initialises toModule by calling toInitFunction or "init" passing the
* element and index as a parmeter. toModule should already be loaded
* for this call. toInitConfig can be configuration options, or a function
* that returns configuration options, if it exists, the options will be passed to the
* init function as well
*/
initialiseModule : function(toModule, toElement, tnIndex, toInitFunction, toInitConfig)
{
toInitFunction = toInitFunction || "init";
toInitConfig = this.isType(toInitConfig, "Function") ? toInitConfig.call(null, toElement) : toInitConfig || {};
if (toModule[toInitFunction])
{
toModule[toInitFunction].apply(toModule, [toElement, toInitConfig, tnIndex]);
};
},
/** Creates an element using the namespace if possible **/
createFunction : function(tcName)
{
this.createFunction =
document.createElementNS ?
function(tcName){return document.createElementNS( 'http://www.w3.org/1999/xhtml', tcName);} :
function(tcName){return document.createElement(tcName);};
return this.createFunction(tcName);
},
/*
* Creates an element using the tag name
* Cloning of an element is faster than creating, so we keep a copy
* of every element that we have created in order to clone them if
* multiples are needed
*/
createElement : function(tcTagName)
{
tcTagName = tcTagName.toLowerCase();
return (this.createElement[tcTagName] || (this.createElement[tcTagName] = this.createFunction(tcTagName))).cloneNode(false);
},
/**
* Creates a callback function which will call toMethod on toTarget and return the result.
*/
createCallback : function(toMethod, toTarget){return function(){toMethod.apply(toTarget, arguments);}}
};
/*************
* The david.utilities object
*************/
return david.utilities;
});