grunt-sort-json
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "grunt-sort-json", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Alphabetizing JSON files. Sort JSON files.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,3 +6,3 @@ 'use strict'; | ||
/** | ||
* recursive sorting of object | ||
* recursively sort a given object | ||
* | ||
@@ -17,9 +17,12 @@ * @method recursiveSort | ||
var i; | ||
var attr; | ||
for (i = 0; i < orderedAttributes.length; i++) { | ||
newObject[orderedAttributes[i]] = obj[orderedAttributes[i]]; | ||
attr = orderedAttributes[i]; | ||
newObject[attr] = obj[attr]; | ||
// if it's an object, sort it! | ||
if (typeof obj[orderedAttributes[i]] === 'object') { | ||
newObject[orderedAttributes[i]] = recursiveSort(newObject[orderedAttributes[i]]); | ||
if (typeof obj[attr] === 'object') { | ||
newObject[attr] = recursiveSort(newObject[attr]); | ||
} | ||
@@ -38,6 +41,7 @@ } | ||
function sortFile (file) { | ||
var fileContents = grunt.file.readJSON(file); | ||
var sortedObject = recursiveSort(fileContents); | ||
var object = grunt.file.readJSON(file); | ||
var sortedObject = recursiveSort(object); | ||
grunt.file.write(file, JSON.stringify(sortedObject, null, 4)); | ||
grunt.log.writeln('File sorted: ' + file.cyan); | ||
} | ||
@@ -48,3 +52,3 @@ | ||
// | ||
grunt.registerMultiTask('sortJSON', 'A grunt task for alphabetizing json files', function () { | ||
grunt.registerMultiTask('sortJSON', 'Alphabetizing JSON files. Sort JSON files.', function () { | ||
this.filesSrc.forEach(function (file) { | ||
@@ -51,0 +55,0 @@ sortFile(file); |
7411
108