Permalink
Browse files

Improved speed & jslint compatibility of serialize()

  • Loading branch information...
1 parent 1af86bf commit d98ce0a441e4d6f45a5fddb8cf066f8d1a569e65 @kvz committed Sep 8, 2010
Showing with 35 additions and 100 deletions.
  1. +33 −15 functions/var/serialize.js
  2. +2 −85 index.html
@@ -8,7 +8,9 @@ function serialize (mixed_value) {
// + bugfixed by: Russell Walker (http://www.nbill.co.uk/)
// + bugfixed by: Jamie Beck (http://www.terabit.ca/)
// + input by: Martin (http://www.erlenwiese.de/)
- // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
+ // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net/)
+ // + improved by: Le Torbi (http://www.letorbi.de/)
+ // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net/)
// - depends on: utf8_encode
// % note: We feel the main purpose of this function should be to ease the transport of data between php & js
// % note: Aiming for PHP-compatibility, we have to translate objects to arrays
@@ -17,13 +19,28 @@ function serialize (mixed_value) {
// * example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
// * returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'
+ var _utf8Size = function (str) {
+ var size = 0, i = 0, l = str.length, code = '';
+ for (i = 0; i < l; i++) {
+ code = str[i].charCodeAt(0);
+ if (code < 0x0080) {
+ size += 1;
+ } else if (code < 0x0800) {
+ size += 2;
+ } else {
+ size += 3;
+ }
+ }
+ return size;
+ };
var _getType = function (inp) {
var type = typeof inp, match;
var key;
- if (type == 'object' && !inp) {
+
+ if (type === 'object' && !inp) {
return 'null';
}
- if (type == "object") {
+ if (type === "object") {
if (!inp.constructor) {
return 'object';
}
@@ -56,8 +73,7 @@ function serialize (mixed_value) {
val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
break;
case "string":
- mixed_value = this.utf8_encode(mixed_value);
- val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\"";
+ val = "s:" + _utf8Size(mixed_value) + ":\"" + mixed_value + "\"";
break;
case "array":
case "object":
@@ -77,15 +93,17 @@ function serialize (mixed_value) {
var okey;
var key;
for (key in mixed_value) {
- ktype = _getType(mixed_value[key]);
- if (ktype == "function") {
- continue;
- }
-
- okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
- vals += this.serialize(okey) +
- this.serialize(mixed_value[key]);
- count++;
+ if (mixed_value.hasOwnProperty(key)) {
+ ktype = _getType(mixed_value[key]);
+ if (ktype === "function") {
+ continue;
+ }
+
+ okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
+ vals += this.serialize(okey) +
+ this.serialize(mixed_value[key]);
+ count++;
+ }
}
val += ":" + count + ":{" + vals + "}";
break;
@@ -94,7 +112,7 @@ function serialize (mixed_value) {
val = "N";
break;
}
- if (type != "object" && type != "array") {
+ if (type !== "object" && type !== "array") {
val += ";";
}
return val;
View
@@ -1,87 +1,4 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+objname [1] = this.serialize ( objname [1 ]);
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title>kvz/phpjs @ GitHub</title>
-
- <style type="text/css">
- body {
- margin-top: 1.0em;
- background-color: #3D3263;
- font-family: "Helvetica,Arial,FreeSans";
- color: #ffffff;
- }
- #container {
- margin: 0 auto;
- width: 700px;
- }
- h1 { font-size: 3.8em; color: #c2cd9c; margin-bottom: 3px; }
- h1 .small { font-size: 0.4em; }
- h1 a { text-decoration: none }
- h2 { font-size: 1.5em; color: #c2cd9c; }
- h3 { text-align: center; color: #c2cd9c; }
- a { color: #c2cd9c; }
- .description { font-size: 1.2em; margin-bottom: 30px; margin-top: 30px; font-style: italic;}
- .download { float: right; }
- pre { background: #000; color: #fff; padding: 15px;}
- hr { border: 0; width: 80%; border-bottom: 1px solid #aaa}
- .footer { text-align:center; padding-top:30px; font-style: italic; }
- </style>
-
-</head>
-
-<body>
- <a href="http://github.com/kvz/phpjs"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" /></a>
-
- <div id="container">
-
- <div class="download">
- <a href="http://github.com/kvz/phpjs/zipball/master">
- <img border="0" width="90" src="http://github.com/images/modules/download/zip.png"></a>
- <a href="http://github.com/kvz/phpjs/tarball/master">
- <img border="0" width="90" src="http://github.com/images/modules/download/tar.png"></a>
- </div>
-
- <h1><a href="http://github.com/kvz/phpjs">phpjs</a>
- <span class="small">by <a href="http://github.com/kvz">kvz</a></span></h1>
-
- <div class="description">
- Use PHP functions in JavaScript
- </div>
-
- <p>PHP.JS is an open source project in which we try to port PHP functions to JavaScript. By including the PHP.JS library in your own projects, you can use your favorite PHP functions client-side.</p><h2>Install</h2>
-<p>No server component required. To use PHP.JS you can either:
-
- * Download the default package
- * Customize & download a package (only select what you need)
- * Goto one of the Functions and just Copy-Paste it
-
-More info at http://phpjs.org</p>
-<h2>License</h2>
-<p>PHP.JS is dual licensed under the MIT and GPL (GPL-LICENSE.txt) licenses.
-The MIT license allows you to use the library as you see fit (even in commercial projects) as long as you redistribute original the license with it. You don't need to open up your modifications per se, but obviously that would be greatly appreciated. </p>
-
-
- <h2>Download</h2>
- <p>
- You can download this project in either
- <a href="http://github.com/kvz/phpjs/zipball/master">zip</a> or
- <a href="http://github.com/kvz/phpjs/tarball/master">tar</a> formats.
- </p>
- <p>You can also clone the project with <a href="http://git-scm.com">Git</a>
- by running:
- <pre>$ git clone git://github.com/kvz/phpjs</pre>
- </p>
-
- <div class="footer">
- get the source code on GitHub : <a href="http://github.com/kvz/phpjs">kvz/phpjs</a>
- </div>
-
- </div>
-
-
-</body>
-</html>
+objname [1] = this.serialize ( objname [1] == "Object [" ? "stdClass": objname [1 ]);

0 comments on commit d98ce0a

Please sign in to comment.