This repository has been archived by the owner on Dec 7, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobjectify.js
280 lines (237 loc) · 8.92 KB
/
objectify.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*!
* Copyright (c) 2015 Lyconic, LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/***
* @namespace Convert form fields into nested hashes quickly and painlessly.
***/
var Objectify = (function ($, undefined) {
var self;
/***
* Because javascript type-checking is something special.
***/
function primitive ( obj ) {
return Object.prototype.toString.call(obj).match(/\[object (\w+)\]/)[1];
}
/***
* Because `hasOwnProperty` can be overwritten.
***/
function hasOwnProperty ( obj, key ) {
return Object.prototype.hasOwnProperty.call(obj, key);
}
/***
* The secret sauce, as it were. This is what inserts a value into the correct nesting.
***/
function normalize ( key, value ) {
var isArrayValue = key.match( /\[\]$/ ),
namespace = self.unpack( key ),
p = this, k;
// TODO: profile this against a recursive func
while ( true ) {
k = namespace.shift();
if ( namespace.length === 0 ) break;
p[ k ] || ( p[ k ] = {} );
p = p[ k ];
}
if ( isArrayValue ) {
if ( primitive(p[k]) !== 'Array' ) p[ k ] = [];
p[ k ].push( value );
} else {
p[ k ] = value;
}
return this;
}
function denormalize ( obj, prefix, result ) {
for ( var i = 0, keys = Object.keys(obj); i < keys.length; i++ ) {
var key = keys[i], value = obj[key],
baseName = prefix ? self.pack(prefix, key) : key;
if ( primitive(value) === 'Object' ) denormalize( value, baseName, result );
else if ( primitive(value) === 'Array' ) result[ baseName + '[]' ] = value;
else result[ baseName ] = value;
}
return result;
}
function flatten ( array ) {
var flat = [];
for (var i = 0, l = array.length; i < l; i++){
flat = flat.concat(/^(array|collection|arguments|object)$/i.test(primitive(array[i])) ? flatten(array[i]) : array[i]);
}
return flat;
}
return self = {
/***
* This is the method that does the heavy lifting.
*
* @method convert(<selection>)
* @param <selection> can be a jQuery selector string, a jQuery instance, or a plain object (deprecated).
* @returns {Object}
* @example
*
* Objectify.convert('form') -> { ... }
* Objectify.convert( $('form') ) -> { ... }
* Objectify.convert({ ... }) -> { ... }
***/
'convert': function ( selection ) {
var fields = [], obj = {};
if ( (primitive(selection) === 'String') || (selection instanceof jQuery) ) {
if ( $(selection).is('form') ) fields = $(selection).serializeArray();
else fields = $(selection).find('input, select, textarea').serializeArray();
$(selection).find(':file').each(function (i, file) {
fields.push({ name: file.name, value: file.value.replace(/C:\\fakepath\\/i, '') });
});
} else if ( primitive(selection) == 'Object' ) {
if ( console && console.warn ) {
console.warn("Objectify.convert: passing an object is deprecated. Please use Objectify.normalize instead.");
}
Object.keys(selection, function (key) { fields.push({ name: key, value: selection[key] }) });
}
$.each( fields, function ( i, field ) {
normalize.call( obj, field.name, field.value );
});
return obj;
},
/***
* Converts a flat key-value object to a nested object.
***/
'normalize': function ( obj ) {
var newObj = {};
Object.keys(obj, function (key) { normalize.call(newObj, key, obj[key]) });
return newObj;
},
/**
* Takes a normalized object such that `convert` would give you, and turns it into a flat object.
*
* @method denormalize(<obj>)
* @returns Object
* @extra This would be especially useful for Ajax data, or data-binding.
* @example
*
* Objectify.denormalize({ foo: { bar: 'baz' } }) -> { 'foo[bar]': 'baz' }
**/
'denormalize': function ( obj ) {
return denormalize( obj, null, {} );
},
/***
* Takes a packed string and returns an `Array` of parsed tokens.
*
* @method unpack(<string>)
* @returns {Array} tokens
* @example
*
* Objectify.unpack('user[address][street_address]') -> ['user', 'address', 'street_address']
***/
'unpack': function ( string ) {
var pattern = /^[\[\]]*([^\[\]]+)\]*/,
namespace = [], s;
while ( s = string.match(pattern) ) {
namespace.push( s[1] );
string = string.replace( pattern, '' );
}
return namespace;
},
/***
* Takes one or more arguments and turns them into a single packed string. Pass in an empty `[]` for an array value.
*
* @method pack(<arg1>, [arg2-n...])
* @returns {String}
* @example
*
* Objectify.pack('user', 'address', 'street_address') -> 'user[address][street_address]'
* Objectify.pack('user', 'phone_numbers', []) -> 'user[phone_numbers][]'
***/
'pack': function () {
if ( arguments.length === 0 ) return;
if ( arguments.length === 1 )
if ( $.isArray(arguments[0]) ) return self.pack.apply(null, arguments[0]);
var args = Array.prototype.slice.call( arguments, 1 ),
fieldName = arguments[ 0 ];
$.each( args, function ( i, name ) {
if ( primitive(name) === 'Array' ) fieldName += '[]';
else fieldName += '[' + name + ']';
});
return fieldName;
},
/***
* Walk into a nested object, to arbitrary depth, and pull out the value
*
* @method walk(<obj>, [namespace...])
* @param {Object} <obj> The nested object
* @param <namespace> The chain of keys to walk down. Can be arguments or a single array.
* @extra If `walk` excounters an `undefined` value it will immediately break and return `undefined`.
* @example
*
* var obj = { user: { address: { street_address: '123 Nowhere st.' } } };
* Objectify.walk(obj, 'user', 'address', 'street_address') -> '123 Nowhere st.'
* Objectify.walk(obj, 'user', 'address', 'foo', 'bar') -> undefined
*
* // also supports passing the namespace as an array
* Objectify.walk(obj, ['user', 'address', 'street_address']) -> '123 Nowhere st.'
***/
'walk': function ( obj ) {
var namespace = Array.prototype.slice.call(arguments, 1);
if ( primitive(namespace[0]) === 'Array' ) {
namespace = flatten(namespace);
}
while ( namespace.length > 0 ) {
if ( obj === undefined ) break;
obj = obj[ namespace.shift() ];
}
return obj;
}
};
})(jQuery);
(function ($) {
/***
* jQuery plugin to convert the selected nodes. Parameters are treated like `walk` params.
*
* @example
*
* $('form').objectify() -> { ... }
* $('form').objectify('user', 'address', 'street_address') -> '123 Nowhere st.'
***/
$.fn.objectify = function () {
return Objectify.walk( Objectify.convert(this), Array.prototype.slice.call(arguments) );
}
})(jQuery);
/***
* Polyfill for older browsers that don't support JS 1.8.5
*
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys
***/
if ( !Object.keys ) {
Object.keys = (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = 'toString toLocaleString valueOf hasOwnProperty isPrototypeOf propertyIsEnumerable constructor'.split(/\s+/);
return function ( obj ) {
if ( typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
var result = [];
for ( var prop in obj ) {
if ( hasOwnProperty.call(obj, prop) ) result.push( prop );
}
if ( hasDontEnumBug ) {
for ( var i = 0; i < dontEnums.length; i++ ) {
if ( hasOwnProperty.call(obj, dontEnums[i]) ) result.push( dontEnums[i] );
}
}
return result;
}
})()
}