9741c4c657847b55fba89c92d81e9e99124063c0
[platform/upstream/iotjs.git] / src / js / util.js
1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16
17 function isNull(arg) {
18   return arg === null;
19 }
20
21
22 function isUndefined(arg) {
23   return arg === undefined;
24 }
25
26
27 function isNullOrUndefined(arg) {
28   return isNull(arg) || isUndefined(arg);
29 }
30
31
32 function isNumber(arg) {
33   return typeof arg === 'number';
34 }
35
36 function isFinite(arg) {
37   return (arg == 0) || (arg != arg / 2);
38 }
39
40 function isBoolean(arg) {
41   return typeof arg === 'boolean';
42 }
43
44
45 function isString(arg) {
46   return typeof arg === 'string';
47 }
48
49
50 function isObject(arg) {
51   return typeof arg === 'object' && arg != null;
52 }
53
54
55 function isFunction(arg) {
56   return typeof arg === 'function';
57 }
58
59
60 function isBuffer(arg) {
61   return arg instanceof Buffer;
62 }
63
64
65 function inherits(ctor, superCtor) {
66   ctor.prototype = Object.create(superCtor.prototype, {
67     constructor: {
68       value: ctor,
69       enumerable: false,
70       writable: true,
71       configurable: true
72     }
73   });
74 };
75
76
77 function format(s) {
78   if (!isString(s)) {
79     var arrs = [];
80     for (var i = 0; i < arguments.length; ++i) {
81         arrs.push(formatValue(arguments[i]));
82     }
83     return arrs.join(' ');
84   }
85
86   var i = 1;
87   var args = arguments;
88   var str = String(s).replace(/%[sdj%]/g, function(m) {
89     if (m === '%%') {
90       return '%';
91     }
92     if (i >= args.length) {
93       return m;
94     }
95     switch (m) {
96       case '%s': return String(args[i++]);
97       case '%d': return Number(args[i++]);
98       case '%j': return '[JSON object]';
99       default: return m;
100     }
101   });
102
103   while (i < args.length) {
104       str += ' ' + args[i++].toString();
105   }
106
107   return str;
108 }
109
110 function formatValue(v) {
111   if (isUndefined(v)) {
112     return 'undefined';
113   } else if (isNull(v)) {
114     return 'null';
115   } else {
116     return v.toString();
117   }
118 }
119
120
121 function errnoException(err, syscall, original) {
122   var errname = "error"; // uv.errname(err);
123   var message = syscall + ' ' + errname;
124
125   if (original)
126     message += ' ' + original;
127
128   var e = new Error(message);
129   e.code = errname;
130   e.errno = errname;
131   e.syscall = syscall;
132
133   return e;
134 };
135
136
137 function exceptionWithHostPort(err, syscall, address, port, additional) {
138   var details;
139   if (port && port > 0) {
140     details = address + ':' + port;
141   } else {
142     details = address;
143   }
144
145   if (additional) {
146     details += ' - Local (' + additional + ')';
147   }
148
149   var ex = exports.errnoException(err, syscall, details);
150   ex.address = address;
151   if (port) {
152     ex.port = port;
153   }
154
155   return ex;
156 };
157
158
159 exports.isNull = isNull;
160 exports.isUndefined = isUndefined;
161 exports.isNullOrUndefined = isNullOrUndefined;
162 exports.isNumber = isNumber;
163 exports.isBoolean = isBoolean;
164 exports.isString = isString;
165 exports.isObject = isObject;
166 exports.isFinite = isFinite;
167 exports.isFunction = isFunction;
168 exports.isBuffer = isBuffer;
169 exports.isArray = Array.isArray;
170 exports.exceptionWithHostPort = exceptionWithHostPort;
171 exports.errnoException = errnoException;
172
173 exports.inherits = inherits;
174
175 exports.format = format;