UNPKG

5.2 kBJavaScriptView Raw
1'use strict';
2var Util, clone, deepEqual;
3
4deepEqual = require('deep-eql');
5
6clone = require('clone');
7
8
9/**
10@method Util
11 */
12
13Util = (function() {
14 function Util() {}
15
16
17 /**
18 get __proto__ of the given object
19
20 @method getProto
21 @static
22 @param {Object} obj
23 @return {Object} __proto__
24 */
25
26 Util.getProto = function(obj) {
27 if (Object.getPrototypeOf != null) {
28 return Object.getPrototypeOf(obj);
29 } else {
30 return obj.__proto__;
31 }
32 };
33
34
35 /**
36 converts hyphenation to camel case
37
38 'shinout-no-macbook-pro' => 'ShinoutNoMacbookPro'
39 'shinout-no-macbook-pro' => 'shinoutNoMacbookPro' # if lowerFirst = true
40
41 @method camelize
42 @static
43 @param {String} hyphened
44 @param {Boolean} [lowerFirst=false] make capital char lower
45 @return {String} cameled
46 */
47
48 Util.camelize = function(hyphened, lowerFirst) {
49 var i, substr;
50 if (lowerFirst == null) {
51 lowerFirst = false;
52 }
53 return ((function() {
54 var j, len, ref, results;
55 ref = hyphened.split('-');
56 results = [];
57 for (i = j = 0, len = ref.length; j < len; i = ++j) {
58 substr = ref[i];
59 if (i === 0 && lowerFirst) {
60 results.push(substr);
61 } else {
62 results.push(substr.charAt(0).toUpperCase() + substr.slice(1));
63 }
64 }
65 return results;
66 })()).join('');
67 };
68
69
70 /**
71 converts hyphenation to camel case
72
73 'ShinoutNoMacbookPro' => 'shinout-no-macbook-pro'
74 'ABC' => 'a-b-c' # current implementation... FIXME ?
75
76 @method hyphenize
77 @static
78 @param {String} hyphened
79 @return {String} cameled
80 */
81
82 Util.hyphenize = function(cameled) {
83 cameled = cameled.charAt(0).toUpperCase() + cameled.slice(1);
84 return cameled.replace(/([A-Z])/g, function(st) {
85 return '-' + st.charAt(0).toLowerCase();
86 }).slice(1);
87 };
88
89 Util.serialize = function(v) {
90 var attachClassName;
91 return JSON.stringify((attachClassName = function(val, inModel) {
92 var isModel, item, ret;
93 if ((val == null) || typeof val !== 'object') {
94 return val;
95 }
96 if (Array.isArray(val)) {
97 return (function() {
98 var j, len, results;
99 results = [];
100 for (j = 0, len = val.length; j < len; j++) {
101 item = val[j];
102 results.push(attachClassName(item, inModel));
103 }
104 return results;
105 })();
106 }
107 ret = {};
108 isModel = val.constructor.className != null;
109 Object.keys(val).forEach(function(key) {
110 return ret[key] = attachClassName(val[key], isModel || inModel);
111 });
112 if (val instanceof Error) {
113 ret.stack = val.stack;
114 ret.__errorMessage__ = val.message;
115 } else if (isModel && !inModel) {
116 ret.__className__ = val.constructor.className;
117 }
118 return ret;
119 })(v, false));
120 };
121
122 Util.deserialize = function(str, facade) {
123 var restore;
124 if (str == null) {
125 return str;
126 }
127 return (restore = function(val) {
128 var className, item, key, ret, value;
129 if ((val == null) || typeof val !== 'object') {
130 return val;
131 }
132 if (Array.isArray(val)) {
133 return (function() {
134 var j, len, results;
135 results = [];
136 for (j = 0, len = val.length; j < len; j++) {
137 item = val[j];
138 results.push(restore(item));
139 }
140 return results;
141 })();
142 }
143 if (val.__errorMessage__) {
144 ret = new Error(val.__errorMessage__);
145 for (key in val) {
146 value = val[key];
147 ret[key] = value;
148 }
149 delete ret.__errorMessage__;
150 return ret;
151 } else if (val.__className__) {
152 className = val.__className__;
153 delete val.__className__;
154 return facade.createModel(className, val);
155 } else {
156 ret = {};
157 for (key in val) {
158 value = val[key];
159 ret[key] = restore(value);
160 }
161 return ret;
162 }
163 })(JSON.parse(str));
164 };
165
166
167 /**
168 in Titanium, "A instanceof B" sometimes fails.
169 this is the alternative.
170
171 @method isInstance
172 @static
173 @param {Object} instance
174 @param {Function} class
175 @return {Boolean} A is instance of B
176 */
177
178 Util.isInstance = function(instance, Class) {
179 var className;
180 if (typeof Ti === "undefined" || Ti === null) {
181 return instance instanceof Class;
182 }
183 if (!(instance != null ? instance.constructor : void 0)) {
184 return false;
185 }
186 if (Class === Object) {
187 return true;
188 }
189 className = Class.name;
190 while (instance.constructor !== Object) {
191 if (instance.constructor.name === className) {
192 return true;
193 }
194 instance = Object.getPrototypeOf(instance);
195 }
196 return false;
197 };
198
199 Util.deepEqual = function(a, b) {
200 return deepEqual(a, b);
201 };
202
203 Util.clone = function(v) {
204 return clone(v);
205 };
206
207
208 /**
209 Check if the given value is instanceof Promise.
210
211 "val instanceof Promise" fails when native Promise and its polyfill are mixed
212 */
213
214 Util.isPromise = function(val) {
215 return typeof (val != null ? val.then : void 0) === 'function';
216 };
217
218 return Util;
219
220})();
221
222module.exports = Util;