gulp-json-sort

1.0.0 • Public • Published

gulp-json-sort NPM version Build Status

Gulp plugin for deterministic sorting of JSON files. Supports deep structures, custom compare functions, replacers, and formatting options.

Thin wrapper over substack's json-stable-stringify, which does all the hard work.

Usage

This plugin is authored in ES6, so until Node picks up ES6 module support you'll need to pluck the default property from the require'd import.

var gulp = require('gulp');
var sortJSON = require('gulp-json-sort').default;
 
gulp.task('sort-json', function() {
    return gulp.src('./**/*.json')
        .pipe(sortJSON({ space: 2 }))
        .pipe(gulp.dest('./'));
});

By default (with no compare function provided), sorting happens alphabetically by key.

If a file contains invalid JSON, a parse error will be emitted on the stream.

Newlines in the output are always LF. Pipe the results through gulp-eol as needed.

API

sortJSON(options?: {
    cmp?: (left: { key: string, value: any }, right: { key: string, value: any }) => number,
    cycles?: boolean,
    replacer?: (key: string, value: any) => any,
    space?: number | string 
}); 

See json-stable-stringify for details and behavior for each option; the params are passed straight through.

License

MIT License (Expat)

/gulp-json-sort/

    Package Sidebar

    Install

    npm i gulp-json-sort

    Weekly Downloads

    1,295

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • jbay