-
Notifications
You must be signed in to change notification settings - Fork 1
/
laravel_helpers.js
181 lines (171 loc) · 3.27 KB
/
laravel_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* replicates array_get feature from laravel
*/
function array_get(i,k,d)
{
if (typeof d === 'undefined') { d = null; }
if (!k) return i;
var s = k.split('.');
var o = i;
for(var x=0;x < s.length; x++)
{
if (null !== o && o.hasOwnProperty(s[x]))
{
o = o[s[x]];
}
else
{
return d;
}
}
return o;
}
/**
* replicates array_set feature from laravel
*/
function array_set(i,k,v)
{
if (!k) return;
var s = k.split('.');
h = i;
for(var x=0;x < s.length-1; x++)
{
if (h.hasOwnProperty(s[x]))
{
h = h[s[x]];
}
else
{
for(var y = s.length-1;x <= y; y--)
{
w = v;
v = {};
v[s[y]] = w;
}
h[s[x]] = v[s[x]];
return;
}
}
h[s[x]] = v;
}
/**
* replicates array_dot feature from laravel
*/
function array_dot(i, p){
var o = {};p = p || '';
if (p) { p = p+'.'; }
if (typeof i == 'object')
{
for (var k in i)
{
if (i.hasOwnProperty(k))
{
if (typeof i[k] == 'object')
{
x = array_dot(i[k],k);
for(var l in x)
{
o[p+l] = x[l];
}
}
else
{
o[p+k] = i[k];
}
}
}
}
return o;
}
/**
* replicates array_dot feature from laravel
*/
function array_flatten(i)
{
i = Object(i);
var o = [];
for(var k in i)
{
if (i.hasOwnProperty(k))
{
if (typeof i[k]=='object')
{
o = o.concat(array_flatten(i[k]));
}
else
{
o.push(i[k]);
}
}
}
return o;
}
/**
* replicates array_first feature from laravel
*/
function array_first(a,f,d)
{
if (!f && a.length)
{
return a[0];
}
for(var x = 0; x < a.length; x++)
{
if (f(a[x])) return a[x];
}
return d;
}
/**
* replicates array_except feature from laravel
*/
function array_except(a,ks){
/**
* does not account for array_dot sub-keys
*/
var b = jQuery.extend(true, {}, a);
ks.forEach(function(k){
delete b[k];
});
return b;
}
/**
* replicates array_only feature from laravel
*/
function array_only(a,ks){
/**
* does not account for array_dot sub-keys
*/
var b = jQuery.extend(true, {}, a);
for(var k in a)
{
if (ks.indexOf(k) === -1) delete b[k];
}
return b;
}
/**
* replicates array_fetch feature from laravel
*/
function array_fetch (arr,key)
{
var r = [];
for (var k in arr)
{
if (arr.hasOwnProperty(k))
{
if (array_has(arr[k],key))
{
r.push(
array_get(arr[k],key)
);
}
}
}
return r;
}
/**
* This checks if a deeply nested variable is set, similar to Laravel's Input::has('key')
*/
function array_has (arr,key)
{
return !(array_get(arr,key,false)===false && array_get(arr,key,true)===true);
}