-
Notifications
You must be signed in to change notification settings - Fork 1
/
spacegazebo_helpers.js
137 lines (126 loc) · 2.97 KB
/
spacegazebo_helpers.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
/**
* converts array_dot keys into form keys
*
* this is specifically made for multidimensional arrays that dynamically become HTML forms.
*
* e.g.
*
* var arr = array_dot(original_data);
* for (var key in arr)
* s = '<input type="text" name="'+ dot_key_to_form_key(key) +'" value="" />'
*/
function dot_key_to_form_key(i)
{
i = i.split('.');
for(var x = 0;x<i.length;x++)
{
if (x) i[x] = '['+i[x]+']';
}
return i.join('');
}
/**
* reports any root-level keys that have difference values
*/
function assoc_diff (a,b)
{
console.log([a,b]);
var ks = Object.keys(a).concat(Object.keys(b)).unique();
var Δ = [];
for(var x=0; x<ks.length; x++)
{
if (typeof a[ks[x]] == 'object' && typeof b[ks[x]] == 'object')
{
if (a[ks[x]]===null || b[ks[x]]===null)
{
if (!(a[ks[x]]===null && b[ks[x]]===null))
{
Δ.push(ks[x]);
}
}
else if (assoc_diff(a[ks[x]],b[ks[x]]).length)
{
Δ.push(ks[x]);
}
}
else if (a[ks[x]]!=b[ks[x]])
{
Δ.push(ks[x]);
}
}
return Δ;
}
/**
* returns inputed money value as string representing a valid decimal number
* intended for sending serverside or to be passed to other calls.
*
* OR returns NaN
*
* does not validate arrangements of $,space,comma,period, only strips them
*/
function money2DecimalString(m){
if (typeof m === 'undefined') return NaN;
var v;
var p = m;
if (typeof p == 'string')
{
p = money2DecimalArr(m);
}
if (typeof p == 'number' && isNaN(p))
{
return p;
}
if (p.invalid)
{
return NaN;
}
p.left = parseInt('0'+p.left,10);
if (p.left && p.right)
{
v = p.left+'.'+p.right;
}
else if (p.right)
{
v = '0.'+p.right;
}
else if (p.left)
{
v = p.left;
}
else
{
v = '0';
}
if (p.negative && v!=='0')
{
v = '-'+v;
}
return v;
}
/**
* Helper function for money2DecimalArr
*
* A money format validator may prefer to call this function instead of the above
* if you wanted to check the $,space,comma,period count/order.
*
* returns array describing number
*/
function money2DecimalArr(str){
if (typeof str == 'number' && isNaN(str)){ return str; }
//var decimal_pos = Math.max(str.lastIndexOf('.'),str.lastIndexOf(','));
var decimal_pos = str.lastIndexOf('.');
if (decimal_pos==-1) decimal_pos = str.length;
var num = {
left:str.substr(0,decimal_pos).replace(/[^0-9]+/g,""),
right:str.substr(decimal_pos+1,str.length).replace(/[^0-9]+/g,""),
source:str
};
if (str.trim().indexOf('-')==0)
{
num.negative = true;
}
if (num.left.length + num.right.length === 0)
{
num.invalid = true;
}
return num;
}