Comparing version 1.5.0 to 1.6.0
@@ -297,2 +297,3 @@ declare type Partial$1<T> = { | ||
prefix: string; | ||
prefixWidth: number; | ||
maxWidth: number; | ||
@@ -303,6 +304,7 @@ chalk: any; | ||
showPercent: boolean; | ||
countWidth: number; | ||
progChar: string; | ||
emptyChar: string; | ||
prefixChar: string; | ||
suffixChar: string; | ||
startChar: string; | ||
endChar: string; | ||
} | ||
@@ -662,2 +664,16 @@ declare type ProgressBarOptions = Partial<ProgressBarOptionsFull>; | ||
declare const reverse: <T>(arr: T[]) => T[]; | ||
/** | ||
* Returns array of 'tuples' of index/value pairs | ||
* | ||
* ```typescript | ||
* const arr = ['a', 'b', 'c']; | ||
* entries(arr); // [ [0, 'a'], [1, 'b'], [2, 'c'] ] | ||
* | ||
* for (let [index, value] of entries(arr)) { | ||
* console.log(index); // 0, 1, 2 | ||
* console.log(value); // 'a', 'b', 'c' | ||
* } | ||
* ``` | ||
*/ | ||
declare const entries: <T>(arr: T[]) => [number, T][]; | ||
@@ -669,2 +685,3 @@ declare const ArrayUtils_range: typeof range; | ||
declare const ArrayUtils_reverse: typeof reverse; | ||
declare const ArrayUtils_entries: typeof entries; | ||
declare namespace ArrayUtils { | ||
@@ -677,5 +694,6 @@ export { | ||
ArrayUtils_reverse as reverse, | ||
ArrayUtils_entries as entries, | ||
}; | ||
} | ||
export { ArrayUtils, CENTURY, CustomEntryDict, DAY, DECADE, DeferredPromise, HOUR, KeysOnly, MILLENNIUM, MILLISECOND, MINUTE, MONTH, Numbered, Partial$1 as Partial, ProgressBarOptions, PromiseUtils, SECOND, WEEK, YEAR, all, allLimit, allLimitObj, allObj, centuries, century, day, days, decade, decades, each, eachLimit, getDeferred, getProgressBar, getTimer, hour, hours, interval, map, mapLimit, millennium, millenniums, milliseconds, minute, minutes, month, months, ms, printLn, progressBar, randomise, range, reverse, second, seconds, sortByMapped, stopInterval, timer, times, wait, waitEvery, waitFor, waitUntil, waiters, week, weeks, year, years, zip }; | ||
export { ArrayUtils, CENTURY, CustomEntryDict, DAY, DECADE, DeferredPromise, HOUR, KeysOnly, MILLENNIUM, MILLISECOND, MINUTE, MONTH, Numbered, Partial$1 as Partial, ProgressBarOptions, PromiseUtils, SECOND, WEEK, YEAR, all, allLimit, allLimitObj, allObj, centuries, century, day, days, decade, decades, each, eachLimit, entries, getDeferred, getProgressBar, getTimer, hour, hours, interval, map, mapLimit, millennium, millenniums, milliseconds, minute, minutes, month, months, ms, printLn, progressBar, randomise, range, reverse, second, seconds, sortByMapped, stopInterval, timer, times, wait, waitEvery, waitFor, waitUntil, waiters, week, weeks, year, years, zip }; |
@@ -44,2 +44,3 @@ var __defProp = Object.defineProperty; | ||
eachLimit: () => eachLimit, | ||
entries: () => entries, | ||
getDeferred: () => getDeferred, | ||
@@ -303,7 +304,7 @@ getProgressBar: () => getProgressBar, | ||
var getBarString = (current, max, width, opts) => { | ||
const { progChar, emptyChar, prefixChar, suffixChar, chalk } = opts; | ||
const { progChar, emptyChar, startChar, endChar, chalk } = opts; | ||
const numProgChars = Math.round(width * (Math.max(0, Math.min(current / max, 1)) / 1)); | ||
const numEmptyChars = width - numProgChars; | ||
const body = `${progChar.repeat(numProgChars)}${emptyChar.repeat(numEmptyChars)}`; | ||
return `${chalk.dim(prefixChar)}${chalk.bold(body)}${chalk.dim(suffixChar)}`; | ||
return `${chalk.dim(startChar)}${chalk.bold(body)}${chalk.dim(endChar)}`; | ||
}; | ||
@@ -313,3 +314,4 @@ var getSuffix = (current, max, opts) => { | ||
if (opts.showCount) { | ||
items.push(`[${current.toString().padStart(max.toString().length, " ")} / ${max}]`); | ||
const pad = Math.max(max.toString().length, opts.countWidth); | ||
items.push(`[${current.toString().padStart(pad, " ")} / ${max.toString().padStart(pad, " ")}]`); | ||
} | ||
@@ -324,2 +326,4 @@ if (opts.showPercent) { | ||
var getFullOptions = (opts = {}) => ({ | ||
prefix: "", | ||
prefixWidth: 1, | ||
maxWidth: (process == null ? void 0 : process.stdout) ? process.stdout.columns : 100, | ||
@@ -330,12 +334,12 @@ chalk: noChalk, | ||
showPercent: false, | ||
countWidth: 0, | ||
progChar: "\u2588", | ||
emptyChar: " ", | ||
prefixChar: "\u2595", | ||
suffixChar: "\u258F", | ||
...opts, | ||
prefix: (opts.prefix || "").length ? opts.prefix + " " : "" | ||
startChar: "\u2595", | ||
endChar: "\u258F", | ||
...opts | ||
}); | ||
var getProgressBar = (max, options = {}) => { | ||
const opts = getFullOptions(options); | ||
const { prefix, maxWidth, wrapperFn, prefixChar, suffixChar } = opts; | ||
const { prefix, prefixWidth, maxWidth, wrapperFn, startChar, endChar } = opts; | ||
let current = 0; | ||
@@ -345,6 +349,7 @@ let finished = false; | ||
const suffix = getSuffix(current, max, opts); | ||
const output = `${prefix}${getBarString( | ||
const fullPrefix = prefix.padEnd(prefixWidth); | ||
const output = `${fullPrefix}${getBarString( | ||
current, | ||
max, | ||
Math.max(0, maxWidth - [prefix, suffix, prefixChar, suffixChar].join("").length), | ||
Math.max(0, maxWidth - [fullPrefix, suffix, startChar, endChar].join("").length), | ||
opts | ||
@@ -497,2 +502,3 @@ )}${suffix}`; | ||
__export(ArrayUtils_exports, { | ||
entries: () => entries, | ||
randomise: () => randomise, | ||
@@ -512,2 +518,3 @@ range: () => range, | ||
var reverse = (arr) => [...arr].reverse(); | ||
var entries = (arr) => zip(range(arr.length), arr); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -537,2 +544,3 @@ 0 && (module.exports = { | ||
eachLimit, | ||
entries, | ||
getDeferred, | ||
@@ -539,0 +547,0 @@ getProgressBar, |
{ | ||
"name": "swiss-ak", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"author": "Jack Cannon <jackc@annon.co.uk> (http://c.annon.co.uk/)", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -220,2 +220,16 @@ # swiss-ak (Swiss Army Knife) | ||
### entries | ||
Returns array of 'tuples' of index/value pairs | ||
```typescript | ||
const arr = ['a', 'b', 'c']; | ||
entries(arr); // [ [0, 'a'], [1, 'b'], [2, 'c'] ] | ||
for (let [index, value] of entries(arr)) { | ||
console.log(index); // 0, 1, 2 | ||
console.log(value); // 'a', 'b', 'c' | ||
} | ||
``` | ||
## PromiseUtils | ||
@@ -434,14 +448,16 @@ | ||
| Property | Default | Description | | ||
| ----------- | --------------------------------- | ----------------------------------------------------- | | ||
| prefix | `''` | String to show to left of progress bar | | ||
| maxWidth | `process.stdout.columns` or `100` | The maximum width the entire string may extend | | ||
| chalk | nothing | the `chalk` module, if available | | ||
| wrapperFn | nothing | function to wrap the printed string (eg `chalk.cyan)` | | ||
| showCount | `true` | Show numerical values of the count - `[11 / 15]` | | ||
| showPercent | `false` | Show percentage completed - `( 69%)` | | ||
| progChar | `'█'` | Character to use for progress section of bar | | ||
| emptyChar | `' '` | Character to use for empty (rail) section of bar | | ||
| prefixChar | `'▕'` | Character to start the progress bar with | | ||
| suffixChar | `'▏'` | Character to end the progress bar with | | ||
| Property | Default | Description | | ||
| ----------- | --------------------------------- | ------------------------------------------------------ | | ||
| prefix | `''` | String to show to left of progress bar | | ||
| prefixWidth | `1` | Min width of prefix - `10` => `Example˽˽˽` | | ||
| maxWidth | `process.stdout.columns` or `100` | The maximum width the entire string may extend | | ||
| chalk | nothing | the `chalk` module, if available | | ||
| wrapperFn | nothing | function to wrap the printed string (eg `chalk.cyan)` | | ||
| showPercent | `false` | Show percentage completed - `( 69%)` | | ||
| showCount | `true` | Show numerical values of the count - `[11 / 15]` | | ||
| countWidth | `0` | Min width of nums for showCount - `3` => `[˽˽1 / ˽15]` | | ||
| progChar | `'█'` | Character to use for progress section of bar | | ||
| emptyChar | `' '` | Character to use for empty (rail) section of bar | | ||
| startChar | `'▕'` | Character to start the progress bar with | | ||
| endChar | `'▏'` | Character to end the progress bar with | | ||
@@ -448,0 +464,0 @@ ### Usage |
@@ -83,1 +83,16 @@ /** | ||
export const reverse = <T>(arr: T[]): T[] => [...arr].reverse(); | ||
/** | ||
* Returns array of 'tuples' of index/value pairs | ||
* | ||
* ```typescript | ||
* const arr = ['a', 'b', 'c']; | ||
* entries(arr); // [ [0, 'a'], [1, 'b'], [2, 'c'] ] | ||
* | ||
* for (let [index, value] of entries(arr)) { | ||
* console.log(index); // 0, 1, 2 | ||
* console.log(value); // 'a', 'b', 'c' | ||
* } | ||
* ``` | ||
*/ | ||
export const entries = <T>(arr: T[]): [number, T][] => zip(range(arr.length), arr) as any; |
@@ -49,3 +49,3 @@ import { noChalk, noWrap } from './fakeChalk'; | ||
const getBarString = (current: number, max: number, width: number, opts: ProgressBarOptionsFull) => { | ||
const { progChar, emptyChar, prefixChar, suffixChar, chalk } = opts; | ||
const { progChar, emptyChar, startChar, endChar, chalk } = opts; | ||
const numProgChars = Math.round(width * (Math.max(0, Math.min(current / max, 1)) / 1)); | ||
@@ -55,3 +55,3 @@ const numEmptyChars = width - numProgChars; | ||
return `${chalk.dim(prefixChar)}${chalk.bold(body)}${chalk.dim(suffixChar)}`; | ||
return `${chalk.dim(startChar)}${chalk.bold(body)}${chalk.dim(endChar)}`; | ||
}; | ||
@@ -62,3 +62,4 @@ | ||
if (opts.showCount) { | ||
items.push(`[${current.toString().padStart(max.toString().length, ' ')} / ${max}]`); | ||
const pad = Math.max(max.toString().length, opts.countWidth); | ||
items.push(`[${current.toString().padStart(pad, ' ')} / ${max.toString().padStart(pad, ' ')}]`); | ||
} | ||
@@ -75,2 +76,3 @@ if (opts.showPercent) { | ||
prefix: string; | ||
prefixWidth: number; | ||
maxWidth: number; | ||
@@ -81,9 +83,12 @@ chalk: any; | ||
showPercent: boolean; | ||
countWidth: number; | ||
progChar: string; | ||
emptyChar: string; | ||
prefixChar: string; | ||
suffixChar: string; | ||
startChar: string; | ||
endChar: string; | ||
} | ||
export type ProgressBarOptions = Partial<ProgressBarOptionsFull>; | ||
const getFullOptions = (opts: ProgressBarOptions = {}): ProgressBarOptionsFull => ({ | ||
prefix: '', | ||
prefixWidth: 1, | ||
maxWidth: process?.stdout ? process.stdout.columns : 100, | ||
@@ -94,8 +99,8 @@ chalk: noChalk, | ||
showPercent: false, | ||
countWidth: 0, | ||
progChar: '█', | ||
emptyChar: ' ', | ||
prefixChar: '▕', | ||
suffixChar: '▏', | ||
...opts, | ||
prefix: (opts.prefix || '').length ? opts.prefix + ' ' : '' | ||
startChar: '▕', | ||
endChar: '▏', | ||
...opts | ||
}); | ||
@@ -136,3 +141,3 @@ | ||
const opts = getFullOptions(options); | ||
const { prefix, maxWidth, wrapperFn, prefixChar, suffixChar } = opts; | ||
const { prefix, prefixWidth, maxWidth, wrapperFn, startChar, endChar } = opts; | ||
let current = 0; | ||
@@ -143,6 +148,7 @@ let finished = false; | ||
const suffix = getSuffix(current, max, opts); | ||
const output = `${prefix}${getBarString( | ||
const fullPrefix = prefix.padEnd(prefixWidth); | ||
const output = `${fullPrefix}${getBarString( | ||
current, | ||
max, | ||
Math.max(0, maxWidth - [prefix, suffix, prefixChar, suffixChar].join('').length), | ||
Math.max(0, maxWidth - [fullPrefix, suffix, startChar, endChar].join('').length), | ||
opts | ||
@@ -149,0 +155,0 @@ )}${suffix}`; |
Sorry, the diff of this file is not supported yet
97271
2881
524