Skip to content

Instantly share code, notes, and snippets.

@iamnoah
Created June 4, 2013 18:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamnoah/5708063 to your computer and use it in GitHub Desktop.
Save iamnoah/5708063 to your computer and use it in GitHub Desktop.
JSShaper script to alphabetize object literals.
/**
* Usage:
* 1. git clone git://github.com/olov/jsshaper.git
* 2. mv alpha.js plugins/
* 3. node run-shaper.js TARGET_FILE plugins/alpha.js --source > ALPHABATIZED_FILE
*/
if (typeof define !== 'function') { var define = require('amdefine')(module); }
define(['../shaper', '../fmt', '../ref', '../tkn'], function(Shaper, Fmt, Ref, tkn) {
"use strict"; "use restrict";
var IGNORE = /[_\.]/g;
var PRIORITY = {
init: 1,
destroy: 2
};
/*jshint newcap:false */
Shaper("alpha", function(root) {
function value(v) {
var priority = PRIORITY[v] || 9;
return priority + "_" + v.toLowerCase().replace(IGNORE, "");
}
return Shaper.traverse(root, {pre: function(node, ref) {
if(node.type === tkn.OBJECT_INIT) {
node.children.sort(function(a, b) {
a = value(a.children[0].value);
b = value(b.children[0].value);
return a < b ? -1 : a > b ? 1 : 0;
});
}
}});
});
return Shaper.get("alpha");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment