xref: /dokuwiki/lib/scripts/helpers.js (revision ba0b2baf89f088dfb447115f8fbc5d0c8c041395)
1/**
2 *  $Id: helpers.js 156 2006-12-23 08:48:25Z wingedfox $
3 *  $HeadURL: https://svn.debugger.ru/repos/jslibs/BrowserExtensions/trunk/helpers.js $
4 *
5 *  File contains differrent helper functions
6 *
7 * @author Ilya Lebedev <ilya@lebedev.net>
8 * @license LGPL
9 * @version $Rev: 156 $
10 */
11//-----------------------------------------------------------------------------
12//  Variable/property checks
13//-----------------------------------------------------------------------------
14/**
15 *  Checks if property is undefined
16 *
17 *  @param {Object} prop value to check
18 *  @return {Boolean} true if matched
19 *  @scope public
20 */
21function isUndefined (prop /* :Object */) /* :Boolean */ {
22  return (typeof prop == 'undefined');
23}
24/**
25 *  Checks if property is function
26 *
27 *  @param {Object} prop value to check
28 *  @return {Boolean} true if matched
29 *  @scope public
30 */
31function isFunction (prop /* :Object */) /* :Boolean */ {
32  return (typeof prop == 'function');
33}
34/**
35 *  Checks if property is string
36 *
37 *  @param {Object} prop value to check
38 *  @return {Boolean} true if matched
39 *  @scope public
40 */
41function isString (prop /* :Object */) /* :Boolean */ {
42  return (typeof prop == 'string');
43}
44/**
45 *  Checks if property is number
46 *
47 *  @param {Object} prop value to check
48 *  @return {Boolean} true if matched
49 *  @scope public
50 */
51function isNumber (prop /* :Object */) /* :Boolean */ {
52  return (typeof prop == 'number');
53}
54/**
55 *  Checks if property is the calculable number
56 *
57 *  @param {Object} prop value to check
58 *  @return {Boolean} true if matched
59 *  @scope public
60 */
61function isNumeric (prop /* :Object */) /* :Boolean */ {
62  return isNumber(prop)&&!isNaN(prop)&&isFinite(prop);
63}
64/**
65 *  Checks if property is array
66 *
67 *  @param {Object} prop value to check
68 *  @return {Boolean} true if matched
69 *  @scope public
70 */
71function isArray (prop /* :Object */) /* :Boolean */ {
72  return (prop instanceof Array);
73}
74/**
75 *  Checks if property is regexp
76 *
77 *  @param {Object} prop value to check
78 *  @return {Boolean} true if matched
79 *  @scope public
80 */
81function isRegExp (prop /* :Object */) /* :Boolean */ {
82  return (prop instanceof RegExp);
83}
84/**
85 *  Checks if property is a boolean value
86 *
87 *  @param {Object} prop value to check
88 *  @return {Boolean} true if matched
89 *  @scope public
90 */
91function isBoolean (prop /* :Object */) /* :Boolean */ {
92  return ('boolean' == typeof prop);
93}
94/**
95 *  Checks if property is a scalar value (value that could be used as the hash key)
96 *
97 *  @param {Object} prop value to check
98 *  @return {Boolean} true if matched
99 *  @scope public
100 */
101function isScalar (prop /* :Object */) /* :Boolean */ {
102  return isNumeric(prop)||isString(prop);
103}
104/**
105 *  Checks if property is empty
106 *
107 *  @param {Object} prop value to check
108 *  @return {Boolean} true if matched
109 *  @scope public
110 */
111function isEmpty (prop /* :Object */) /* :Boolean */ {
112  if (isBoolean(prop)) return false;
113  if (isRegExp(prop) && new RegExp("").toString() == prop.toString()) return true;
114  if (isString(prop) || isNumber(prop)) return !prop;
115  if (Boolean(prop)&&false != prop) {
116    for (var i in prop) if(prop.hasOwnProperty(i)) return false
117  }
118  return true;
119}
120
121/*
122*  Checks if property is derived from prototype, applies method if it is not exists
123*
124*  @param string property name
125*  @return bool true if prototyped
126*  @access public
127*/
128if ('undefined' == typeof Object.hasOwnProperty) {
129  Object.prototype.hasOwnProperty = function (prop) {
130    return !('undefined' == typeof this[prop] || this.constructor && this.constructor.prototype[prop] && this[prop] === this.constructor.prototype[prop]);
131  }
132}
133