-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmos.js
162 lines (135 loc) · 4.34 KB
/
mos.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
*
* Helpers and tools to ease your JavaScript day.
*
* @author Mikael Roos (me@mikaelroos.se)
*/
window.Mos = (function(window, document, undefined ) {
var Mos = {};
/**
* Display the type of the object
* @param the object to check
*/
Mos.reflection = function (obj) {
// if(typeof obj === 'undefined') console.log("is type: Undefined");
// if(typeof obj === 'boolean') console.log("is type: Boolean");
// if(typeof obj === 'number') console.log("is type: Number");
// if(typeof obj === 'string') console.log("is type: String");
// if(typeof obj === 'function') console.log("is type: Function");
// if(typeof obj === 'object') console.log("is type: Object");
console.log("is type: " + typeof obj);
if(isNaN(obj)) {
console.log("is NaN, Not a Number");
}
if(isFinite(obj)) {
console.log("is a real number, not Infinite nor NaN");
}
if(obj instanceof Array) console.log("is instance of: Array");
if(obj instanceof Boolean) console.log("is instance of: Boolean");
if(obj instanceof Date) console.log("is instance of: Date");
if(obj instanceof Function) console.log("is instance of: Function");
if(obj instanceof Number) console.log("is instance of: Number");
if(obj instanceof Object) console.log("is instance of: Object");
if(obj instanceof RegExp) console.log("is instance of: RegExp");
if(obj instanceof String) console.log("is instance of: String");
if(obj instanceof Error) console.log("is instance of: Error");
if(obj !== null && typeof obj !== 'undefined') {
console.log("toString(): " + obj.toString());
console.log("valueOf(): " + obj.valueOf());
}
};
/*
function getAllPropertyNames( obj ) {
var props = [];
do {
Object.getOwnPropertyNames( obj ).forEach(function ( prop ) {
if ( props.indexOf( prop ) === -1 ) {
props.push( prop );
}
});
} while ( obj = Object.getPrototypeOf( obj ) );
return props;
}*/
/**
* Display properties of an object
* @param the object to show
*/
Mos.properties = function (obj) {
if(typeof obj !== 'object') {
console.log('Not an object.');
} else {
console.log('Object has the following properties:')
for (var prop in obj) {
//if (obj.hasOwnProperty(prop)) {
console.log(prop + ' : ' + obj[prop]);
//}
}
}
};
/**
* Dump own properties of an object
* @param the object to show
*/
Mos.dump = function (obj) {
if(typeof obj !== 'object') {
console.log('Not an object.');
} else {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
console.log(prop);
}
}
}
};
/**
* Dump properties and values of an object
* @param the object to show
* @returns string
*/
Mos.dumpAsString = function (obj) {
var s = '\n';
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
s += prop + ' : ' + obj[prop] + '\n';
}
}
return s;
};
/**
* Delete cached LESS files from local storage.
* @param regular expressen to search for, for example /style.less/
*/
Mos.deleteLESSFromLocalStorage = function (which) {
for (var item in window.localStorage) {
if (item.match(which)) {
console.log('Deleted ' + item + ':' + (delete window.localStorage[item]));
}
}
};
/**
* Generate a random number.
* @param min the smallest possible number
* @param max the largest possible number
* @returns a random number where min >= number <= max
*/
Mos.random = function (min, max) {
return Math.floor(Math.random()*(max+1-min)+min);
};
/**
* Get the position of an element.
* http://stackoverflow.com/questions/442404/dynamically-retrieve-html-element-x-y-position-with-javascript
* @param el the element.
*/
Mos.getOffset = function ( el ) {
var _x = 0;
var _y = 0;
while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
_x += el.offsetLeft - el.scrollLeft;
_y += el.offsetTop - el.scrollTop;
el = el.offsetParent;
}
return { top: _y, left: _x };
}
//var x = getOffset( document.getElementById('yourElId') ).left;
// Expose public methods
return Mos;
})(window, window.document);