Skip to content

cutbox.js specification

Jason Milkins edited this page Sep 5, 2023 · 3 revisions

This page outlines the specification of $HOME/.cutbox.js (i.e. ~/.cutbox.js)

NOTE: using javascript + pseudo type hints, function interface syntax inspired by Swift

Abstract

A JavaScript file which contains an array (cutboxFunctions) of objects containing a function fn and descriptive metadata name.

Note: With subsequent versions of CutBox function description metadata may extend to allow for new features.

1.6.0

In a nutshell

// Note: use of top level 'this' (aka 'globalThis') is 
// required to persist state across evaluations.
this.cutboxFunctions = [
    { fn: CutBoxFunction, name: String }
    ...
];

CutBoxFunction

A JavaScript Function adhering to the interface:

[String] -> String

All functions must accept a single argument, an array of strings, and return a single string.

e.g. strings => strings.join(" ")

Example

Any valid JS function, or function reference can be used, providing it adheres to the interface spec above:

let joinWithDashes = items => items.join(" - ")

this.cutboxFunctions = [
    { 
        name: "Join with spaces",
        fn: (i) => i.join(" ")
    },
    { 
        name: "Join with dashes",
        fn: joinWithDashes
    },
    {
        name: "Uppercase",
        fn: i => i.map( s => s.toUpperCase() ).join("\n")
    },
    {
        name: "Lowercase",
        fn: i => i.map( (s) => {
           return s.toLowerCase() ).join("\n")
        }
    },
    {
        name: "Join with commas",
        fn: function(items){ return items.join(",") }
    }
]

For further examples, see: JavaScript Examples