-
Notifications
You must be signed in to change notification settings - Fork 2
/
formalize-data.js
153 lines (125 loc) · 3.79 KB
/
formalize-data.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
/*
Created by Ryan Quinn
Source at www.github.com/mazondo
*/
(function($) {
/*
formalizeFormats is where we keep all of the registered formats
Only number is included for now.
*/
var formalizeFormats = {
number : function(value) {
if (isNaN(value)) {
throw "Expected a number!";
} else {
value = parseFloat(value);
}
return value;
}
};
/*
This is where we'll let you define your own formats
Register them with:
$.formalizeFormat("custom", function(value) {return value;} );
Then you can:
<input name="whatever" data-format="custom"></input>
And we'll apply your transformation to the input
*/
$.formalizeFormat = function(name, operation) {
formalizeFormats[name] = operation;
}
$.fn.formalizeData = function(options) {
var data = {},
defaults = {attribute: "name"},
options = $.extend({}, defaults, options);
$(this).find(":input[" + options.attribute + "]").each(function(index, value) {
/*
Check for a radio button, if it is, make sure it is checked or move on
*/
if ($(this).attr("type") == "radio" && !$(this)[0].checked) {
return;
}
/*
Check for a checkbox, if it is, make sure it is checked or move on
*/
if ($(this).attr("type") == "checkbox" && !$(this)[0].checked) {
return;
}
var name = $(this).attr(options.attribute);
var elements = name.split(/[\.\[\]]/g),
result = $.extend({}, data),
dataBuilder,
elementValue,
format;
/*
Check for empty element values, this is mostly an issue with bracket syntax, but also
Catches if someone accidently ends a name with a period
*/
elements = $.map(elements, function (name) {
return name !== "" ? name : null;
});
/*
Setup the value by checking if the user specified a data type
Currently only supports numbers
*/
elementValue = $(this).val();
format = $(this).data("formalize");
if (format && formalizeFormats[format] && typeof(formalizeFormats[format]) == "function") {
elementValue = formalizeFormats[format](elementValue);
}
/*
Traverse through the elements in the name trying to get to the final result
save the final result for processing later
*/
$.each(elements, function(index, val) {
result = result[val] || false;
if (!result) {
return true;
}
});
/*
now that we have the result, we need to tell it what to be
we'll check to see if it's defined, and if not just set it to the value
If it is, we will decide if it's an object or an array and handle it accordingly
*/
if (!result) {
/*
When nothing exists in the end location, we just save the value
*/
result = elementValue;
} else {
/*
When something does exist at the end location, we turn it into an array and add this value to it
*/
if ($.isArray(result)) {
result.push(elementValue);
} else {
result = [result]
result.push(elementValue);
}
}
/*
Now we have a result saved, and we need to add it where it belongs in the data object
We'll need to build out the data structure as we go if any parent elements are not yet defined
dataBuidlder is used to store a reference to the current location in the data object
*/
dataBuilder = data;
$.each(elements, function(index, val) {
if ( (index + 1) == elements.length) {
//we're at the end location, so let's save the result here
//it's safe to overwrite a previous value because we've accounted for it above
dataBuilder[val] = result;
} else {
//not at the end, so let's make sure the item exists
if (!dataBuilder[val]) {
//doesn't exist, let's create it
dataBuilder[val] = {};
}
//now progress the dataBuilder up a level
dataBuilder = dataBuilder[val];
}
});
});
return data;
};
})(jQuery);