Skip to content

Instantly share code, notes, and snippets.

@oyvindkinsey
Created November 20, 2012 22:27
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oyvindkinsey/4121687 to your computer and use it in GitHub Desktop.
Save oyvindkinsey/4121687 to your computer and use it in GitHub Desktop.
Core implementation needed for ES5 to ES3 transforms using 'jspatch'

Core implementation needed for ES5 to ES3 transforms using 'jspatch'

  1. Install jsgrep
  2. Convert your source code using node spatch-cli.js es5-to-es3.spatch source.js // yields the converted source
  3. Prepare the implementation of ES5 by implemeting the missing polyfills in es5.js
  4. Concat es5.js and the converted code
  5. Wrap the result of the previous steps in a closure and export your object
    var myLib = (function() {
      /* include code needed to support the ES5 function */
      var ES5 = (function() {
        ...
      })();

      /* include the converted source */
      var myLib = {
        bindSomething: function(foo, bar) {
          return ES5(foo, 'bind', true, bar);
        },
        ...
      };

      /* export your object */
      return myLib;
    })();
-JSON.stringify
+ES5
(
+'JSON', 'stringify', false,
...)
---
-JSON.parse
+ES5
(
+'JSON', 'parse', false,
...)
---
-Date.now
+ES5
(
+'Date', 'now', false
)
---
-Object.create
+ES5
(
+'Object', 'create', false,
...)
---
-Object.keys
+ES5
(
+'Object', 'keys', false,
...)
---
-Array.isArray
+ES5
(
+'Array', 'isArray', false,
...)
---
-A.bind
+ES5
(
+A, 'bind', true,
...)
---
-A.map
+ES5
(
+A, 'map', true,
...)
---
-A.forEach
+ES5
(
+A, 'forEach', true,
...)
---
-A.indexOf
+ES5
(
+A, 'indexOf', true,
...)
---
-A.filter
+ES5
(
+A, 'filter', true,
...)
---
-A.every
+ES5
(
+A, 'every', true,
...)
---
-A.some
+ES5
(
+A, 'some', true,
...)
---
-A.trim
+ES5
(
+A,'trim', true
)
/**
* This is the basis of the ES5 function needed to support the transform done
* by the es5-to-es3.spatch file.
*
* As you can see, the implementation is incomplete as the individual polyfills
* are not present.
*
* Using implementations from eg https://github.com/kriskowal/es5-shim/ and
* https://github.com/bestiejs/json3, this is how you would include them:
*
* var ES5ArrayPrototype = {
* forEach: function(fn, scope) {
* for(var i = 0, len = this.length; i < len; ++i) {
* fn.call(scope, this[i], i, this);
* }
* },
* map: function (fn, scope) {
* if (typeof fn != 'function') {
* throw new TypeError();
* }
*
* var i, len = this.length, r = new Array(len);
* for (i = 0; i < len; ++i) {
* if (i in this) {
* r[i] = fn.call(scope, this[i], i, this);
* }
* }
*
* return r;
* }
* };
*/
var ES5 = (function() {
var toString = Object.prototype.toString;
/* Add, or pull in your polyfills here */
var methodCache = {
// Always use the polyfill for JSON to work around Prototype 1.6.x issues.
// JSON3 will use the native versions if possible.
'JSON.stringify': JSON3.stringify,
'JSON.parse': JSON3.parse
};
var polyfills = {
'array' : ES5ArrayPrototype,
'function': ES5FunctionPrototype,
'string' : ES5StringPrototype,
'Object' : ES5Object,
'Array' : ES5Array,
'Date' : ES5Date
};
// Iterate over the polyfills, and add either a valid native implementation or
// a polyfill to the methodCache
for (var pName in polyfills) {
if (!polyfills.hasOwnProperty(pName)) { continue; }
var polyfillObject = polyfills[pName];
// Resolve which native object holds the function we are looking for
var nativeObject = pName === pName.toLowerCase()
? window[pName.replace(/^\w/, function(m) { return m.toUpperCase(); })]
.prototype
: window[pName];
// Iterate over the shimmed methods, testing the native implementation
for (var fName in polyfillObject) {
if (!polyfillObject.hasOwnProperty(fName)) { continue; }
var nativeFunction = nativeObject[fName];
// If the native function exist, and tests as a native function, then
// we save it for later
methodCache[pName + '.' + fName] =
nativeFunction && /\{\s+\[native code\]\s\}/.test(nativeFunction)
? nativeFunction
: polyfillObject[fName];
}
}
return function(lhs, rhs, proto/*, args*/) {
// Normalize the type information
var type = proto
? toString.call(lhs).slice(8, -1).toLowerCase()
: lhs;
// Locate the method to use
var method = methodCache[type + '.' + rhs] || lhs[rhs];
return method.apply(lhs, slice.call(arguments, 3));
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment