Skip to content

Commit

Permalink
feat: enable comment support by default
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Sep 10, 2024
1 parent 034b4e5 commit b5f68d2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
11 changes: 5 additions & 6 deletions src/native/cli.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ import argparse;
# Minify the specified files
minijson ./dist/**/*.json ./build/a.json
minijson file1_with_comment.json file2_with_comment.json
# Minify the specified files (supports comments)
minijson --comment file1_with_comment.json file2_with_comment.json
# Minify the specified files and disable comment support for faster minification
minijson --comment=false file1_no_comment.json file2_no_comment.json
# Minify the specified json string
minijson --str '{"some_json": "string_here"}'
# Minify the specified json string (supports comments)
minijson --comment --str '{"some_json": "string_here"} //comment'
minijson --str '{"some_json": "string_here"} //comment'
More information at https://github.com/aminya/minijson
`))
struct Options
{
bool comment = false;
bool comment = true;
string[] str;
// (Deprecated) A list of files to minify (for backwards compatiblitity with getopt)
string[] file;
Expand Down
8 changes: 4 additions & 4 deletions src/native/lib.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const tokenizerNoComment = ctRegex!(`[\n\r"[]]`, "g");
Params:
jsonString = the json string you want to minify
hasComment = a boolean to support comments in json. Default: `false`.
hasComment = a boolean to support comments in json. Default: `true`.
Return:
the minified json string
*/
string minifyString(in string jsonString, in bool hasComment = false) @trusted
string minifyString(in string jsonString, in bool hasComment = true) @trusted
{
return hasComment ? minifyStringWithComments(jsonString) : minifyStringNoComments(jsonString);
}
Expand Down Expand Up @@ -227,7 +227,7 @@ private bool hasNoSpace(const ref string str) @trusted
Return:
the minified json strings
*/
string[] minifyStrings(in string[] jsonStrings, in bool hasComment = false) @trusted
string[] minifyStrings(in string[] jsonStrings, in bool hasComment = true) @trusted
{
import std.algorithm : map;
import std.array : array;
Expand All @@ -242,7 +242,7 @@ string[] minifyStrings(in string[] jsonStrings, in bool hasComment = false) @tru
paths = the paths to the files. It could be glob patterns.
hasComment = a boolean to support comments in json. Default: `false`.
*/
void minifyFiles(in string[] paths, in bool hasComment = false) @trusted
void minifyFiles(in string[] paths, in bool hasComment = true) @trusted
{
import std.parallelism : parallel;
import std.algorithm;
Expand Down
4 changes: 2 additions & 2 deletions src/native/libc.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import minijson.lib : minifyString;
Params:
jsonString = the json string you want to minify
hasComment = a boolean to support comments in json. Default: `false`.
hasComment = a boolean to support comments in json. Default: `true`.
Return:
the minified json string
*/
extern (C) auto c_minifyString(char* jsonCString, bool hasComment = false)
extern (C) auto c_minifyString(char* jsonCString, bool hasComment = true)
{
import std : fromStringz, toStringz;

Expand Down
14 changes: 6 additions & 8 deletions src/node/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ import { join } from "path"
* Minify all the given JSON files in place. It minifies the files in parallel.
*
* @param files An array of paths to the files
* @param hasComment A boolean to support comments in json. Default `false`.
* @param hasComment A boolean to support comments in json. Default `true`.
* @returns {Promise<void>} Returns a void promise that resolves when all the files are minified
* @throws {Promise<string | Error>} The promise is rejected with the reason for failure
*/
export async function minifyFiles(files: string[], hasComment = false): Promise<void> {
export async function minifyFiles(files: readonly string[], hasComment = true): Promise<void> {
try {
const filesNum = files.length
if (filesNum === 0) {
return Promise.resolve()
}

const minijsonArgs = hasComment ? ["--comment", ...files] : files

await spawnMinijson(minijsonArgs)
await spawnMinijson([...files, hasComment ? "--comment=true" : "--comment=false"])
} catch (e) {
console.error(e, "Falling back to jsonminify")
await minifyFilesFallback(files)
Expand Down Expand Up @@ -48,7 +46,7 @@ export function spawnMinijson(args: string[]): Promise<string> {
})
}

async function minifyFilesFallback(files: string[]) {
async function minifyFilesFallback(files: readonly string[]) {
const jsonminify = require("jsonminify")
await Promise.all(
files.map(async (file) => {
Expand All @@ -64,11 +62,11 @@ async function minifyFilesFallback(files: string[]) {
* Minify the given JSON string
*
* @param jsonString The json string you want to minify
* @param hasComment A boolean to support comments in json. Default `false`.
* @param hasComment A boolean to support comments in json. Default `true`.
* @returns {Promise<string>} The minified json string
* @throws {Promise<string | Error>} The promise is rejected with the reason for failure
*/
export async function minifyString(jsonString: string, hasComment = false): Promise<string> {
export async function minifyString(jsonString: string, hasComment = true): Promise<string> {
const args = ["--str", jsonString]
if (hasComment) {
args.push("--comment")
Expand Down

0 comments on commit b5f68d2

Please sign in to comment.