jquery-serialize-json-sprockets 0.0.1 → 2.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f62f51cd83e40320487c0bc480ff3425207d4cf1
4
- data.tar.gz: b5372cbae34604c647deb52d4b012e46662efe27
3
+ metadata.gz: 5099ac6a276dc24189a126ff8ea43e1c82483ee4
4
+ data.tar.gz: 86ad7be49bc1d7f4427b7ee33fa6e65f53a8f728
5
5
  SHA512:
6
- metadata.gz: eb93131cac6d034bc09968af0961ba9df8e301170a4e15753641feb0bd43c3302d912eb97e8396d44da64e8a119248c62c9f4df00d451204aa04b4638d12d891
7
- data.tar.gz: c8503dd20dfdd87ef288941c5b7d2a00fa1784c28ad705a3f7671f925e83a6d7669b11f4688b1518b25cadfd1703d6979e9e817e5fc998be40f15fc7824e9908
6
+ metadata.gz: 891527223faa07f95e1d61cbd74cf0d058ece1187d4efbf53c99f7dcaf74bb0eb535099db37cdb62059fb206c6a3ebbbad236510671d306be6d7ab591401f744
7
+ data.tar.gz: 8b1875af5a3062cd08049ccb83974da93542b0e9f2e82db0d1a85d7bde9572a4a41ea99028481bb8175657da4f762481c10de637e9dc59c173a8dc1a2d70aa88
@@ -1,7 +1,7 @@
1
1
  /*!
2
2
  SerializeJSON jQuery plugin.
3
3
  https://github.com/marioizquierdo/jquery.serializeJSON
4
- version 2.1.0 (May, 2014)
4
+ version 2.4.1 (Oct, 2014)
5
5
 
6
6
  Copyright (c) 2014 Mario Izquierdo
7
7
  Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
@@ -12,17 +12,22 @@
12
12
 
13
13
  // jQuery('form').serializeJSON()
14
14
  $.fn.serializeJSON = function (options) {
15
- var serializedObject, formAsArray, keys, value, f, opts;
15
+ var serializedObject, formAsArray, keys, type, value, _ref, f, opts;
16
16
  f = $.serializeJSON;
17
- formAsArray = this.serializeArray(); // array of objects {name, value}
18
17
  opts = f.optsWithDefaults(options); // calculate values for options {parseNumbers, parseBoolens, parseNulls}
18
+ f.validateOptions(opts);
19
+ formAsArray = this.serializeArray(); // array of objects {name, value}
20
+ f.readCheckboxUncheckedValues(formAsArray, this, opts); // add {name, value} of unchecked checkboxes if needed
19
21
 
20
22
  serializedObject = {};
21
23
  $.each(formAsArray, function (i, input) {
22
- keys = f.splitInputNameIntoKeysArray(input.name); // "some[deep][key]" => ['some', 'deep', 'key']
23
- value = f.parseValue(input.value, opts); // string, number, boolean or null
24
- if (opts.parseWithFunction) value = opts.parseWithFunction(value); // allow for custom parsing
25
- f.deepSet(serializedObject, keys, value, opts);
24
+ keys = f.splitInputNameIntoKeysArray(input.name);
25
+ type = keys.pop(); // the last element is always the type ("string" by default)
26
+ if (type !== 'skip') { // easy way to skip a value
27
+ value = f.parseValue(input.value, type, opts); // string, number, boolean or null
28
+ if (opts.parseWithFunction && type === '_') value = opts.parseWithFunction(value, input.name); // allow for custom parsing
29
+ f.deepSet(serializedObject, keys, value, opts);
30
+ }
26
31
  });
27
32
  return serializedObject;
28
33
  };
@@ -36,7 +41,8 @@
36
41
  parseBooleans: false, // convert "true", "false" to true, false
37
42
  parseNulls: false, // convert "null" to null
38
43
  parseAll: false, // all of the above
39
- parseWithFunction: null, // to use custom parser, use a function like: function (val) => parsed_val
44
+ parseWithFunction: null, // to use custom parser, a function like: function(val){ return parsed_val; }
45
+ checkboxUncheckedValue: undefined, // to include that value for unchecked checkboxes (instead of ignoring them)
40
46
  useIntKeysAsArrayIndex: false // name="foo[2]" value="v" => {foo: [null, null, "v"]}, instead of {foo: ["2": "v"]}
41
47
  },
42
48
 
@@ -51,21 +57,35 @@
51
57
  parseBooleans: parseAll || f.optWithDefaults('parseBooleans', options),
52
58
  parseNulls: parseAll || f.optWithDefaults('parseNulls', options),
53
59
  parseWithFunction: f.optWithDefaults('parseWithFunction', options),
60
+ checkboxUncheckedValue: f.optWithDefaults('checkboxUncheckedValue', options),
54
61
  useIntKeysAsArrayIndex: f.optWithDefaults('useIntKeysAsArrayIndex', options)
55
62
  }
56
63
  },
57
64
 
58
65
  optWithDefaults: function(key, options) {
59
- return (options[key] !== false) && (options[key] || $.serializeJSON.defaultOptions[key]);
66
+ return (options[key] !== false) && (options[key] !== '') && (options[key] || $.serializeJSON.defaultOptions[key]);
67
+ },
68
+
69
+ validateOptions: function(opts) {
70
+ var opt, validOpts;
71
+ validOpts = ['parseNumbers', 'parseBooleans', 'parseNulls', 'parseAll', 'parseWithFunction', 'checkboxUncheckedValue', 'useIntKeysAsArrayIndex']
72
+ for (opt in opts) {
73
+ if (validOpts.indexOf(opt) === -1) {
74
+ throw new Error("serializeJSON ERROR: invalid option '" + opt + "'. Please use one of " + validOpts.join(','));
75
+ }
76
+ }
60
77
  },
61
78
 
62
79
  // Convert the string to a number, boolean or null, depending on the enable option and the string format.
63
- parseValue: function(str, opts) {
80
+ parseValue: function(str, type, opts) {
64
81
  var value, f;
65
82
  f = $.serializeJSON;
66
- if (opts.parseNumbers && f.isNumeric(str)) return Number(str); // number
67
- if (opts.parseBooleans && (str === "true" || str === "false")) return str === "true"; // boolean
68
- if (opts.parseNulls && str == "null") return null; // null
83
+ if (type == 'string') return str; // force string
84
+ if (type == 'number' || (opts.parseNumbers && f.isNumeric(str))) return Number(str); // number
85
+ if (type == 'boolean' || (opts.parseBooleans && (str === "true" || str === "false"))) return (["false", "null", "undefined", "", "0"].indexOf(str) === -1); // boolean
86
+ if (type == 'null' || (opts.parseNulls && str == "null")) return ["false", "null", "undefined", "", "0"].indexOf(str) !== -1 ? null : str; // null
87
+ if (type == 'array' || type == 'object') return JSON.parse(str); // array or objects require JSON
88
+ if (type == 'auto') return f.parseValue(str, null, {parseNumbers: true, parseBooleans: true, parseNulls: true}); // try again with something like "parseAll"
69
89
  return str; // otherwise, keep same string
70
90
  },
71
91
 
@@ -74,24 +94,48 @@
74
94
  isValidArrayIndex: function(val) { return /^[0-9]+$/.test(String(val)); }, // 1,2,3,4 ... are valid array indexes
75
95
  isNumeric: function(obj) { return obj - parseFloat(obj) >= 0; }, // taken from jQuery.isNumeric implementation. Not using jQuery.isNumeric to support old jQuery and Zepto versions
76
96
 
77
- // Split the input name in programatically readable keys
78
- // "foo" => ['foo']
79
- // "[foo]" => ['foo']
80
- // "foo[inn][bar]" => ['foo', 'inn', 'bar']
81
- // "foo[inn][arr][0]" => ['foo', 'inn', 'arr', '0']
82
- // "arr[][val]" => ['arr', '', 'val']
97
+ // Split the input name in programatically readable keys.
98
+ // The last element is always the type (default "_").
99
+ // Examples:
100
+ // "foo" => ['foo', '_']
101
+ // "foo:string" => ['foo', 'string']
102
+ // "foo:boolean" => ['foo', 'boolean']
103
+ // "[foo]" => ['foo', '_']
104
+ // "foo[inn][bar]" => ['foo', 'inn', 'bar', '_']
105
+ // "foo[inn[bar]]" => ['foo', 'inn', 'bar', '_']
106
+ // "foo[inn][arr][0]" => ['foo', 'inn', 'arr', '0', '_']
107
+ // "arr[][val]" => ['arr', '', 'val', '_']
108
+ // "arr[][val]:null" => ['arr', '', 'val', 'null']
83
109
  splitInputNameIntoKeysArray: function (name) {
84
- var keys, last, f;
110
+ var keys, nameWithoutType, type, _ref, f;
85
111
  f = $.serializeJSON;
86
- if (f.isUndefined(name)) { throw new Error("ArgumentError: param 'name' expected to be a string, found undefined"); }
87
- keys = $.map(name.split('['), function (key) {
88
- last = key[key.length - 1];
89
- return last === ']' ? key.substring(0, key.length - 1) : key;
90
- });
91
- if (keys[0] === '') { keys.shift(); } // "[foo][inn]" should be same as "foo[inn]"
112
+ _ref = f.extractTypeFromInputName(name), nameWithoutType = _ref[0], type = _ref[1];
113
+ keys = nameWithoutType.split('['); // split string into array
114
+ keys = $.map(keys, function (key) { return key.replace(/]/g, ''); }); // remove closing brackets
115
+ if (keys[0] === '') { keys.shift(); } // ensure no opening bracket ("[foo][inn]" should be same as "foo[inn]")
116
+ keys.push(type); // add type at the end
92
117
  return keys;
93
118
  },
94
119
 
120
+ // Returns [name-without-type, type] from name.
121
+ // "foo" => ["foo", "_"]
122
+ // "foo:boolean" => ["foo", "boolean"]
123
+ // "foo[bar]:null" => ["foo[bar]", "null"]
124
+ extractTypeFromInputName: function(name) {
125
+ var match, f;
126
+ f = $.serializeJSON;
127
+ if (match = name.match(/(.*):([^:]+)$/)){
128
+ var validTypes = ['string', 'number', 'boolean', 'null', 'array', 'object', 'skip', 'auto']; // validate type
129
+ if (validTypes.indexOf(match[2]) !== -1) {
130
+ return [match[1], match[2]];
131
+ } else {
132
+ throw new Error("serializeJSON ERROR: Invalid type " + match[2] + " found in input name '" + name + "', please use one of " + validTypes.join(', '))
133
+ }
134
+ } else {
135
+ return [name, '_']; // no defined type, then use parse options
136
+ }
137
+ },
138
+
95
139
  // Set a value in an object or array, using multiple keys to set in a nested object or array:
96
140
  //
97
141
  // deepSet(obj, ['foo'], v) // obj['foo'] = v
@@ -159,6 +203,30 @@
159
203
  tail = keys.slice(1);
160
204
  f.deepSet(o[key], tail, value, opts);
161
205
  }
206
+ },
207
+
208
+ // Fill the formAsArray object with values for the unchecked checkbox inputs,
209
+ // using the same format as the jquery.serializeArray function.
210
+ // The value of the unchecked values is determined from the opts.checkboxUncheckedValue
211
+ // and/or the data-unchecked-value attribute of the inputs.
212
+ readCheckboxUncheckedValues: function (formAsArray, $form, opts) {
213
+ var selector, $uncheckedCheckboxes, $el, dataUncheckedValue, f;
214
+ if (opts == null) opts = {};
215
+ f = $.serializeJSON;
216
+
217
+ selector = 'input[type=checkbox][name]:not(:checked)';
218
+ $uncheckedCheckboxes = $form.find(selector).add($form.filter(selector));
219
+ $uncheckedCheckboxes.each(function (i, el) {
220
+ $el = $(el);
221
+ dataUncheckedValue = $el.attr('data-unchecked-value');
222
+ if(dataUncheckedValue) { // data-unchecked-value has precedence over option opts.checkboxUncheckedValue
223
+ formAsArray.push({name: el.name, value: dataUncheckedValue});
224
+ } else {
225
+ if (!f.isUndefined(opts.checkboxUncheckedValue)) {
226
+ formAsArray.push({name: el.name, value: opts.checkboxUncheckedValue});
227
+ }
228
+ }
229
+ });
162
230
  }
163
231
 
164
232
  };
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jquery-serialize-json-sprockets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 2.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - shelling
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-10 00:00:00.000000000 Z
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sprockets
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.11.0
19
+ version: '2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.11.0
26
+ version: '2'
27
27
  description:
28
28
  email:
29
29
  - navyblueshellingford@gmail.com
@@ -44,12 +44,12 @@ require_paths:
44
44
  - lib
45
45
  required_ruby_version: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - '>='
47
+ - - ">="
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
50
  required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  requirements: []
@@ -57,5 +57,5 @@ rubyforge_project:
57
57
  rubygems_version: 2.2.2
58
58
  signing_key:
59
59
  specification_version: 4
60
- summary: the ladda button effect assets
60
+ summary: marioizquierdo/jquery.serializeJSON for assets pipeline
61
61
  test_files: []