From 783e2d8765bc6d2860ec02adaecd67efa4a6ac4e Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Sat, 7 Jul 2018 00:58:15 +0200 Subject: [PATCH] Added byte truncate script to repo. Fixed yargs not enforcing choices. --- src/cli.js | 4 ++-- src/utils/truncate-bytes.js | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/utils/truncate-bytes.js diff --git a/src/cli.js b/src/cli.js index fcf66e1..3acbd05 100644 --- a/src/cli.js +++ b/src/cli.js @@ -59,11 +59,11 @@ function getArgs() { }) .option('after-indexed', { describe: 'Only include posts after the oldest or the latest entry in the index', - options: ['oldest', 'latest'], + choices: ['oldest', 'latest'], }) .option('before-indexed', { describe: 'Only include posts before the oldest or the latest entry in the index', - options: ['oldest', 'latest'], + choices: ['oldest', 'latest'], }) .option('redownload', { describe: 'Ignore index file and force a redownload of everything in the selection. Does not affect [before|after]-indexed', diff --git a/src/utils/truncate-bytes.js b/src/utils/truncate-bytes.js new file mode 100644 index 0000000..d7c9927 --- /dev/null +++ b/src/utils/truncate-bytes.js @@ -0,0 +1,23 @@ +'use strict'; + +function truncateBytes(str, limit) { + if (Buffer.from(str).length <= limit) { + return str; + } + + let acc = ''; + + for (let i = 0; i < str.length; i++) { + const newString = acc + str.charAt(i); + + if (Buffer.from(newString).length > limit) { + break; + } + + acc = newString; + } + + return acc; +} + +module.exports = truncateBytes;