Skip to content

Commit

Permalink
feat: Format JSON command (#12)
Browse files Browse the repository at this point in the history
* feat: add JSON format command

* chore: tab indentation
  • Loading branch information
colomolo authored Apr 19, 2024
1 parent 849eca9 commit 7b08756
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Binary file added Format JSON.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ Replace all *substring* occurences with *replacement* string. If no *replacement

`I'm Will, Will's son /R 'Will' 'Bill'``I'm Bill, Bill's son`

### Format JSON `/J '<indent>'`
Prettify your JSON and format it with custom indentation. *indent* argument takes either number of spaces or 't' for tab indentation. If no argument is provided, 2 spaces used as a default value.

`{"name":"George", "occupation":"dentist"} /J '4'`
```
{
"name": "George",
"occupation": "dentist"
}
```

![](./string-multitool-example.gif)

<a href='https://ko-fi.com/I2I0W98PT' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi3.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
23 changes: 21 additions & 2 deletions transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ function run(argv) {
return string.replaceAll(substring, replacement);
};

const toJSON = (string = '', indent = 2) => {
return JSON.stringify(JSON.parse(string), null, indent === 't' ? '\t' : parseInt(indent));
};

const toEncodedURI = (string = '') => {
return encodeURI(string);
};
Expand Down Expand Up @@ -212,6 +216,13 @@ function run(argv) {
args: 2,
pattern: `R${REQUIRED_ARGUMENT}${OPTIONAL_ARGUMENT}`,
},
J: {
name: 'Format JSON',
hint: `Takes one argument: ${COMMAND_SEPARATOR}J '<indent>'. Integer of indentation spaces or 't' for tab char`,
transform: toJSON,
args: 1,
pattern: `J${OPTIONAL_ARGUMENT}`,
},
};

const allCommands = {
Expand Down Expand Up @@ -294,7 +305,7 @@ function run(argv) {
items = [
{
uid: 'error',
title: 'Error',
title: 'Could not process. Invalid value or param',
subtitle,
arg: string,
icon,
Expand All @@ -316,7 +327,15 @@ function run(argv) {
},
};
} catch {
return {};
return {
uid: command.name.toLowerCase(),
title: 'Could not process. Invalid value or param',
subtitle: command.hint ? `${command.name}. ${command.hint}` : command.name,
arg: string,
icon: {
path: `./${command.name}.png`,
},
};
}
});
}
Expand Down

0 comments on commit 7b08756

Please sign in to comment.