Skip to content

Commit

Permalink
Add possibility to chose buffersize
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed Nov 27, 2021
1 parent ed11a6a commit 94b3062
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Global install to have the CLI accessible from everywhere on your operating syst
npm install -g turbo-json.js # install globaly
```

No installation needed when using `npx`.
No installation needed when using [npx](https://www.npmjs.com/package/npx).

```bash
npx turbo-json.js [options]
```
Expand Down
1 change: 1 addition & 0 deletions misc/empty_obj_and_array/empty_array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
1 change: 1 addition & 0 deletions misc/empty_obj_and_array/empty_obj.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions misc/special_char_jsons/normal_array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"hello"
]
3 changes: 3 additions & 0 deletions misc/special_char_jsons/russian_array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"Лорем ипсум долор сит амет"
]
7 changes: 6 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ program
'Check if output JSON is a valid JSON',
false
)
.option('-q, --quiet', 'Quiet mode, no logs are outputed', true)
.option('-q, --quiet', 'Quiet mode, no logs are outputed', false)
.option(
'-b, --buffer-size <number>',
'Size of the buffer containing the maximum data kept in memory at any time',
1000
)
.action(async (directory, options) => {
await combineJson(directory, options)
})
Expand Down
14 changes: 8 additions & 6 deletions src/combine-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ const {
logFailedOutputValidityCheck,
} = require('./log')

const BUFFER_SIZE = 1000

async function combine({
inputFiles,
inputDirPath,
outputFilePath,
validateInput,
validateOutput,
quiet,
bufferSize,
}) {
createOutputArrayFile(outputFilePath)
const numberOfFiles = inputFiles.length
Expand All @@ -41,19 +40,20 @@ async function combine({

const { isArray, startPosition, empty } = jsonRootType({
fd: inputFileFd,
bufferSize: BUFFER_SIZE,
bufferSize: bufferSize,
})

let stopPosition = undefined

if (isArray) {
stopPosition = closingArrayIndex({
fd: inputFileFd,

position:
fileSize(inputFile) - BUFFER_SIZE > 0
? fileSize(inputFile) - BUFFER_SIZE
fileSize(inputFile) - bufferSize > 0
? fileSize(inputFile) - bufferSize
: 0,
bufferSize: BUFFER_SIZE,
bufferSize: bufferSize,
})
}

Expand Down Expand Up @@ -132,6 +132,7 @@ async function combineJson(
validateInput = false,
validateOutput = false,
quiet = false,
bufferSize = 1000,
}
) {
const { inputDirPath, filesName } = inputFilesAndDir({ inputDir })
Expand All @@ -144,6 +145,7 @@ async function combineJson(
validateInput,
validateOutput,
quiet,
bufferSize,
})
}

Expand Down
1 change: 0 additions & 1 deletion src/json-root-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ function findClosingArrayIndex({ fd, buffer, position }) {

function closingArrayIndex({ fd, position, bufferSize = 1000 }) {
let buffer = new Int8Array(bufferSize)

return findClosingArrayIndex({ fd, buffer, position })
}

Expand Down
2 changes: 1 addition & 1 deletion src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const logSuccessfullWrite = (fileName, index, numberOfFiles) => {
chalk.green(
'file: ' +
chalk.blue.underline.bold(fileName) +
` has been added! index: ${index}, number of files: ${numberOfFiles}`
` has been added! index: ${index}/${numberOfFiles}`
)
)
}
Expand Down

0 comments on commit 94b3062

Please sign in to comment.