Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/commands/__tests__/case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ beforeEach(() => {
vi.restoreAllMocks()
})

const ESC = String.fromCharCode(27)
function stripAnsi(text: string): string {
return text.replace(/\u001b\[[0-9;]*m/g, '')
return text.replace(new RegExp(ESC + '\\[[0-9;]*m', 'g'), '')
}

describe('case', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function highlightJson(value: unknown, indent = 2, depth = 0): string {
}

export function json(args: string[]) {
const { flags, rest } = parseCommonFlags(args)
const { rest } = parseCommonFlags(args)
const minify = rest.includes('--minify') || rest.includes('-m')
const validateOnly = rest.includes('--validate')
const filteredArgs = rest.filter((a) => a !== '--minify' && a !== '-m' && a !== '--validate')
Expand Down
2 changes: 1 addition & 1 deletion src/commands/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function math(args: string[]) {
const precisionIdx = rest.indexOf('--precision')
let precision = 10
if (precisionIdx >= 0) {
precision = Number(rest[precisionIdx + 1]) ?? 10
precision = Number(rest[precisionIdx + 1]) || 10
rest.splice(precisionIdx, 2)
}

Expand Down
22 changes: 2 additions & 20 deletions src/commands/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,6 @@ const PASS_UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
const PASS_DIGITS = '0123456789'
const PASS_SYMBOLS = '!@#$%^&*()_+-=[]{}|;:,.<>?'

function cryptoRandomInt(max: number): number {
const bits = Math.ceil(Math.log2(max))
const bytes = Math.ceil(bits / 8)
const mask = (1 << bits) - 1
const buf = new Uint8Array(1)

while (true) {
const arr = new Uint8Array(bytes)
for (let i = 0; i < bytes; i++) {
crypto.getRandomValues(arr)
}
const val = Number(BigInt.asUintN(bits, BigInt('0x' + Buffer.from(arr).toString('hex'))))
if (val < mask - (mask % max)) {
return val % max
}
}
}

function generatePassword(length: number, useSymbols: boolean): string {
const chars = PASS_LOWERCASE + PASS_UPPERCASE + PASS_DIGITS + (useSymbols ? PASS_SYMBOLS : '')
const buf = new Uint8Array(length)
Expand Down Expand Up @@ -74,10 +56,10 @@ export function random(args: string[]) {
count = parsed
i++
} else if (a === '--min') {
min = Number(rest[i + 1]) ?? 0
min = Number(rest[i + 1]) || 0
i++
} else if (a === '--max') {
max = Number(rest[i + 1]) ?? 100
max = Number(rest[i + 1]) || 100
i++
}
}
Expand Down
Loading