Skip to content

Instantly share code, notes, and snippets.

@symmetriq
Created November 13, 2017 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save symmetriq/6b52e484da7cc26375c249ed7463b4aa to your computer and use it in GitHub Desktop.
Save symmetriq/6b52e484da7cc26375c249ed7463b4aa to your computer and use it in GitHub Desktop.
BBEdit: JSON to CS
#!/usr/bin/env node
//-------------------------------------------------------------------------------
// JSON to CS
//-------------------------------------------------------------------------------
// Jason Sims <jason@symmetriq.com>
//-------------------------------------------------------------------------------
//
// Transforms a JSON object into CoffeeScript
//
// The CoffeeScript version omits all commas that occur at the end of a line
//
// Based on json-to-js:
// https://github.com/Dinoshauer/json-to-js
//
//-------------------------------------------------------------------------------
//
// Installation in BBEdit:
//
// 1. Place this file in your "Text Filters" directory:
// ~/Library/Application Support/BBEdit/Text Filters/
// or
// ~/Dropbox/Application Support/BBEdit/Text Filters/
//
// 2. Install javascript-stringify:
// npm i -g javascript-stringify
//
// 3. Assign a keyboard shortcut to "JSON to CS" in:
// BBEdit → Preferences → Menus & Shortcuts → Text → Apply Text Filter
//
//-------------------------------------------------------------------------------
// More scripts & snippets here:
// https://gist.github.com/symmetriq
//-------------------------------------------------------------------------------
var jsStringify = require('javascript-stringify')
, data = ''
, stdin = process.openStdin();
stdin.on('data', function (chunk) {
data += chunk;
});
stdin.on('end', function () {
var text = jsStringify(JSON.parse(data), null, 2);
text = text.replace(/,\n/g, '\n')
process.stdout.write(text);
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment