Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Javascript Examples

Jason Milkins edited this page May 24, 2018 · 26 revisions

Example ~/.cutbox.js

var join = i => i.join(i.length > 0 ? "\n" : "");

var squeeze = i => join( i.map( s => s.replace(/\s+/g, ' ')));

var operatorSpacing = i => join(i.map(string =>
                                 string
                                 .replace(/\s*([-=+/*%<>])\s*/g," $1 ")
                                 .replace(/([-+=/*%<>])\s*([-+=])/g, "$1$2")
                                ));

var markdownCodeIndent = str => str.replace(/^/mg, "    ");

var cutboxFunctions = [
  {
    // Acts like shell command: tr -s ' '
    name: "Squeeze",
    fn: squeeze,
  },
  {
    // Fix up operator spacing
    name: "Operator spacing",
    fn: operatorSpacing
  },
  {
    // Double quote each selected item
    name: "Double Quoted",
    fn: i => join(i.map(e => `"${e}"`)),
  },
  {
    // Single quote each selected item
    name: "Single Quoted",
    fn: i => join(i.map(e => `'${e}'`)),
  },
  {
    // Double quote each item and wrap and comma separate as a multi-language array
    name: "Array formatted (quoted)",
    fn: i => `[
${join(i.map(e => `"${e}",`))}
]`,
  },
  {
    // wrap and comma separate as a multi-language array
    name: "Array formatted (unquoted)",
    fn: i => `[
${join(i.map(e => `${e},`))}
]`,
  },
  {
    // Add a localized key and localized text as a Localizable.strings definition pair.
    name: "Localized Pair",
    fn: i => `"${i[0]}" = "${i[1]}";`
  },
  {
    // Wrap Markdown code fence around the selection
    name: "Markdown code fenced",
    fn: i => `\`\`\`
${join(i)}
\`\`\``
  },
  {
    // Markdown Code indent the selection 
    // (including at newlines in each item)
    name: "Markdown code indented",
    fn: i => join(
      i.map(e => markdownCodeIndent(e) )
    )
  },
  {
    // Upper case all text in selection
    name: "Upper cased",
    fn: i => join(i.map(e => e.toUpperCase())),
  },
  {
    // Lower case all text in selection
    name: "Lower cased",
    fn: i => join(i.map(e => e.toLowerCase())),
  },
  {

    // Evalate the selection in the CutBox 
    // Javascript environment. 
    // -------------------------------------
    // WARN/NOTE: JUST PREVIEWING SELECTED 
    // JS CODE THROUGH THIS WILL FUNC EXECUTE IT!

    name: "Eval",
    fn: i => eval(i.join(";"))
  }
];

Using Eval

Eval is an interesting way to use JS with CutBox. You can evaluate arithmetic:

1 + 2 + 3 -> Eval -> 6

Clone this wiki locally