Created
June 4, 2013 18:01
-
-
Save iamnoah/5708063 to your computer and use it in GitHub Desktop.
JSShaper script to alphabetize object literals.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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