Skip to content

Instantly share code, notes, and snippets.

@symmetriq
Created November 13, 2017 01:45
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/70aacd65a054e69fa176c5b4b108f62f to your computer and use it in GitHub Desktop.
Save symmetriq/70aacd65a054e69fa176c5b4b108f62f to your computer and use it in GitHub Desktop.
BBEdit: JSON to JS
#!/usr/bin/env node
//-------------------------------------------------------------------------------
// JSON to JS
//-------------------------------------------------------------------------------
// Jason Sims <jason@symmetriq.com>
//-------------------------------------------------------------------------------
//
// Transforms a JSON object into JavaScript
//
// 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 JS" 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);
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