Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:collection.next_json [2016/11/02 16:23]
jlucas
programming:collection.next_json [2016/11/02 17:04] (current)
jlucas
Line 1175: Line 1175:
     </p>     </p>
          
-    <pre><div style="text-align:left;color:#000000; font-size:1em; font-family:monospace; "><em><span style="color:#236e25;">/** +    <pre> 
 +/** 
  * encodeURIComponent() function is not 100% compatible with  * encodeURIComponent() function is not 100% compatible with
  * RFC3986 http://www.faqs.org/rfcs/rfc3986.html  * RFC3986 http://www.faqs.org/rfcs/rfc3986.html
- */</span></em> + */ 
-<span style="color:#881350;">function</span> <span style="color:#003369;">encodeRFC3986</span>(value) { + function</span>  encodeRFC3986</span>(value) { 
-  <span style="color:#881350;">return</span> <span style="color:#003369;">encodeURIComponent</span>(value) +   return</span>  encodeURIComponent</span>(value) 
-    .<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/!/g</span>,  <span style="color:#760f15;">&quot;%21&quot;</span>+    . replace</span>( /!/g</span>,   &quot;%21&quot;</span>
-    .<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/</span><span style="color:#be73fd;">\*</span><span style="color:#661aa9;">/g</span>, <span style="color:#760f15;">&quot;%2A&quot;</span>+    . replace</span>( /</span> \*</span> /g</span>,  &quot;%2A&quot;</span>
-    .<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/</span><span style="color:#be73fd;">\(</span><span style="color:#661aa9;">/g</span>, <span style="color:#760f15;">&quot;%28&quot;</span>+    . replace</span>( /</span> \(</span> /g</span>,  &quot;%28&quot;</span>
-    .<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/</span><span style="color:#be73fd;">\)</span><span style="color:#661aa9;">/g</span>, <span style="color:#760f15;">&quot;%29&quot;</span>+    . replace</span>( /</span> \)</span> /g</span>,  &quot;%29&quot;</span>
-    .<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/'/g</span>,  <span style="color:#760f15;">&quot;%27&quot;</span>);+    . replace</span>( /'/g</span>,   &quot;%27&quot;</span>);
 } }
  
-<span style="color:#881350;">function</span> <span style="color:#003369;">translateToFormUrlEncoded</span>(data) { + function</span>  translateToFormUrlEncoded</span>(data) { 
-  <span style="color:#881350;">var</span> buffer = [];+   var</span> buffer = [];
  
-  <span style="color:#881350;">for</span><span style="color:#003369;"> </span>(<span style="color:#881350;">var</span> i = <span style="color:#0000ff;">0</span>; i &lt; data.length; i++) { +   for</span>  </span>( var</span> i =  0</span>; i &lt; data.length; i++) { 
-    <span style="color:#881350;">var</span> input = data[i]; +     var</span> input = data[i]; 
-    <span style="color:#881350;">var</span> name = input.name; +     var</span> name = input.name; 
-    <span style="color:#881350;">var</span> value = input.value;+     var</span> value = input.value;
          
-    <span style="color:#881350;">if</span><span style="color:#003369;"> </span>(value === null) { <em><span style="color:#236e25;">// convert null value to empty string +     if</span>  </span>(value === null) { <em> // convert null value to empty string 
-</span></em>      value = <span style="color:#760f15;">''</span>; +</span></em>      value =  ''</span>; 
-    } <span style="color:#881350;">else</span> <span style="color:#881350;">if</span><span style="color:#003369;"> </span>(<span style="color:#881350;">typeof</span> value === <span style="color:#760f15;">&quot;boolean&quot;</span>) { <em><span style="color:#236e25;">// convert boolean value to integer 0 or 1 +    }  else</span>  if</span>  </span>( typeof</span> value ===  &quot;boolean&quot;</span>) { <em> // convert boolean value to integer 0 or 1 
-</span></em>      value = <span style="color:#003369;">Number</span>(value);+</span></em>      value =  Number</span>(value);
     }     }
  
-    buffer.<span style="color:#003369;">push</span>(<span style="color:#003369;">encodeRFC3986</span>(name) + <span style="color:#760f15;">&quot;=&quot;</span><span style="color:#003369;">encodeRFC3986</span>(value));+    buffer. push</span>( encodeRFC3986</span>(name) +  &quot;=&quot;</span> encodeRFC3986</span>(value));
   }   }
  
-  <span style="color:#881350;">return</span> buffer.<span style="color:#003369;">join</span>(<span style="color:#760f15;">&quot;&amp;&quot;</span>);+   return</span> buffer. join</span>( &quot;&amp;&quot;</span>);
 } }
  
-<span style="color:#881350;">var</span> data = [ + var</span> data = [ 
-  {<span style="color:#760f15;">&quot;name&quot;</span>: <span style="color:#760f15;">&quot;first-name&quot;</span>, <span style="color:#760f15;">&quot;value&quot;</span>: <span style="color:#760f15;">&quot;John&quot;</span>}, +  { &quot;name&quot;</span>:  &quot;first-name&quot;</span>,  &quot;value&quot;</span>:  &quot;John&quot;</span>}, 
-  {<span style="color:#760f15;">&quot;name&quot;</span>: <span style="color:#760f15;">&quot;last-name&quot;</span>,  <span style="color:#760f15;">&quot;value&quot;</span>: <span style="color:#760f15;">&quot;Doe&quot;</span>}, +  { &quot;name&quot;</span>:  &quot;last-name&quot;</span>,   &quot;value&quot;</span>:  &quot;Doe&quot;</span>}, 
-  {<span style="color:#760f15;">&quot;name&quot;</span>: <span style="color:#760f15;">&quot;email&quot;</span>,      <span style="color:#760f15;">&quot;value&quot;</span>: <span style="color:#760f15;">&quot;john@doe.com&quot;</span>}, +  { &quot;name&quot;</span>:  &quot;email&quot;</span>,       &quot;value&quot;</span>:  &quot;john@doe.com&quot;</span>}, 
-  {<span style="color:#760f15;">&quot;name&quot;</span>: <span style="color:#760f15;">&quot;website&quot;</span>,    <span style="color:#760f15;">&quot;value&quot;</span>: <span style="color:#760f15;">&quot;http://john.doe.com&quot;</span>}, +  { &quot;name&quot;</span>:  &quot;website&quot;</span>,     &quot;value&quot;</span>:  &quot;http://john.doe.com&quot;</span>}, 
-  {<span style="color:#760f15;">&quot;name&quot;</span>: <span style="color:#760f15;">&quot;age&quot;</span>,        <span style="color:#760f15;">&quot;value&quot;</span>: <span style="color:#0000ff;">37</span>}, +  { &quot;name&quot;</span>:  &quot;age&quot;</span>,         &quot;value&quot;</span>:  37</span>}, 
-  {<span style="color:#760f15;">&quot;name&quot;</span>: <span style="color:#760f15;">&quot;interests&quot;</span>,  <span style="color:#760f15;">&quot;value&quot;</span>: <span style="color:#760f15;">&quot;music&quot;</span>}, +  { &quot;name&quot;</span>:  &quot;interests&quot;</span>,   &quot;value&quot;</span>:  &quot;music&quot;</span>}, 
-  {<span style="color:#760f15;">&quot;name&quot;</span>: <span style="color:#760f15;">&quot;interests&quot;</span>,  <span style="color:#760f15;">&quot;value&quot;</span>: <span style="color:#760f15;">&quot;sports&quot;</span>}, +  { &quot;name&quot;</span>:  &quot;interests&quot;</span>,   &quot;value&quot;</span>:  &quot;sports&quot;</span>}, 
-  {<span style="color:#760f15;">&quot;name&quot;</span>: <span style="color:#760f15;">&quot;interests&quot;</span>,  <span style="color:#760f15;">&quot;value&quot;</span>: <span style="color:#760f15;">&quot;cars&quot;</span>}, +  { &quot;name&quot;</span>:  &quot;interests&quot;</span>,   &quot;value&quot;</span>:  &quot;cars&quot;</span>}, 
-  {<span style="color:#760f15;">&quot;name&quot;</span>: <span style="color:#760f15;">&quot;subscribe&quot;</span>,  <span style="color:#760f15;">&quot;value&quot;</span>: <span style="color:#881350;">false</span>}+  { &quot;name&quot;</span>:  &quot;subscribe&quot;</span>,   &quot;value&quot;</span>:  false</span>}
 ]; ];
  
-<em><span style="color:#236e25;">// convert data array to application/x-www-form-urlencoded +<em> // convert data array to application/x-www-form-urlencoded 
-</span></em><span style="color:#881350;">var</span> postData = <span style="color:#003369;">translateToFormUrlEncoded</span>(data); +</span></em> var</span> postData =  translateToFormUrlEncoded</span>(data);
-</div></div>+
 </pre> </pre>
  
-      <pre><span class="comment">// encoded contents of resulting postData variable</span> +      <pre> 
-<code> +// encoded contents of resulting postData variable 
-first-name=John&last-name=Doe&email=john%40doe.com&website=http%3A%2F%2Fjohn.doe.com&age=37&interests=music&interests=sports&interests=cars&subscribe=0 +first-name=John&last-name=Doe&email=john%40doe.com&website=http%3A%2F%2Fjohn.doe.com&age=37&interests=music&interests=sports&interests=cars&subscribe=0</pre>
-</code></pre>+
  
   </section><!-- /translating to form urlencoded -->   </section><!-- /translating to form urlencoded -->