UNPKG

3.96 kBtext/coffeescriptView Raw
1'use strict'
2
3deepEqual = require 'deep-eql'
4clone = require 'clone'
5
6###*
7@method Util
8###
9class Util
10
11 ###*
12 get __proto__ of the given object
13
14 @method getProto
15 @static
16 @param {Object} obj
17 @return {Object} __proto__
18 ###
19 @getProto: (obj) ->
20 if Object.getPrototypeOf?
21 return Object.getPrototypeOf(obj)
22 else
23 obj.__proto__
24
25 ###*
26 converts hyphenation to camel case
27
28 'shinout-no-macbook-pro' => 'ShinoutNoMacbookPro'
29 'shinout-no-macbook-pro' => 'shinoutNoMacbookPro' # if lowerFirst = true
30
31 @method camelize
32 @static
33 @param {String} hyphened
34 @param {Boolean} [lowerFirst=false] make capital char lower
35 @return {String} cameled
36 ###
37 @camelize: (hyphened, lowerFirst = false) ->
38 (for substr, i in hyphened.split('-')
39 if i is 0 and lowerFirst
40 substr
41 else
42 substr.charAt(0).toUpperCase() + substr.slice(1)
43 ).join('')
44
45
46 ###*
47 converts hyphenation to camel case
48
49 'ShinoutNoMacbookPro' => 'shinout-no-macbook-pro'
50 'ABC' => 'a-b-c' # current implementation... FIXME ?
51
52 @method hyphenize
53 @static
54 @param {String} hyphened
55 @return {String} cameled
56 ###
57 @hyphenize: (cameled) ->
58
59 cameled = cameled.charAt(0).toUpperCase() + cameled.slice(1)
60 cameled.replace(/([A-Z])/g, (st)-> '-' + st.charAt(0).toLowerCase()).slice(1)
61
62
63 @serialize: (v) ->
64
65 JSON.stringify do attachClassName = (val = v, inModel = false) ->
66
67 return val if not val? or typeof val isnt 'object'
68
69 if Array.isArray val
70 return (attachClassName(item, inModel) for item in val)
71
72 ret = {}
73 isModel = val.constructor.className?
74 Object.keys(val).forEach (key) ->
75 ret[key] = attachClassName(val[key], isModel || inModel)
76
77 if val instanceof Error
78 ret.stack = val.stack
79 ret.__errorMessage__ = val.message
80
81 else if isModel and not inModel
82 ret.__className__ = val.constructor.className
83
84 return ret
85
86
87 @deserialize: (str, facade) ->
88
89 return str if not str?
90
91 do restore = (val = JSON.parse str) ->
92
93 return val if not val? or typeof val isnt 'object'
94
95 if Array.isArray val
96 return (restore(item) for item in val)
97
98 if val.__errorMessage__
99 ret = new Error(val.__errorMessage__)
100 ret[key] = value for key, value of val
101 delete ret.__errorMessage__
102 return ret
103
104 else if val.__className__
105 className = val.__className__
106 delete val.__className__
107 return facade.createModel(className, val)
108 else
109 ret = {}
110 ret[key] = restore(value) for key, value of val
111 return ret
112
113 ###*
114 in Titanium, "A instanceof B" sometimes fails.
115 this is the alternative.
116
117 @method isInstance
118 @static
119 @param {Object} instance
120 @param {Function} class
121 @return {Boolean} A is instance of B
122 ###
123 @isInstance: (instance, Class) ->
124
125 if not Ti?
126 return instance instanceof Class
127
128 return false if not instance?.constructor
129 return true if Class is Object
130
131 className = Class.name
132
133 until instance.constructor is Object
134
135 return true if instance.constructor.name is className
136
137 instance = Object.getPrototypeOf instance
138
139 return false
140
141 @deepEqual: (a, b) ->
142
143 deepEqual(a, b)
144
145
146 @clone: (v) ->
147
148 clone v
149
150
151 ###*
152 Check if the given value is instanceof Promise.
153
154 "val instanceof Promise" fails when native Promise and its polyfill are mixed
155 ###
156 @isPromise: (val) ->
157
158 typeof val?.then is 'function'
159
160
161module.exports = Util