IvanMathy / Boop

A scriptable scratchpad for developers. In slow yet steady progress.
https://boop.okat.best
MIT License
3.84k stars 356 forks source link

Feature Request: Turn lines into array #329

Open jschuur opened 2 years ago

jschuur commented 2 years ago

Turing a block of text into an array where each line is an element would be useful.

I currently use https://arraythis.com/ for that.

jschuur commented 2 years ago

I ended up throwing together a little custom script to do this:

function main(state) {
  const lines = state.text.split('\n');

  const output = lines
    .filter(line => line.trim() !== '')
    .map((line) => `  ${(Number.isNaN(Number.parseFloat(line)) ? `'${line}'` : Number.parseFloat(line))}`)
    .join(',\n');

  state.fullText = `[\n${output}\n]`;
  state.postInfo(`${lines.length} array items converted`);
}

This also checks if something is a string or a number and preserves the type too.