--- warning: This file is auto generated by `yarn web:inject`, do not edit by hand examples: - padright('42', 5) - padright('42', 5, '0') - padright('hello', 3) returns: - '''42 ''' - '''42000''' - '''hello''' dependencies: [] authors: original by: - Kevin van Zonneveld (https://kvz.io) notes: [] parityVerified: - PowerShell 7.4 isTypescript: true type: function layout: function title: PowerShell's string.padright in TypeScript description: >- Here's what our current TypeScript equivalent to PowerShell's string.padright looks like. function: padright category: string language: powershell permalink: powershell/string/padright/ alias: - /functions/powershell/padright/ - /functions/string/padright/ - /powershell/padright/ ---
{% codeblock lang:typescript %}export function padright(str: string, totalWidth: number, paddingChar?: string): string { // discuss at: https://locutus.io/powershell/padright/ // parity verified: PowerShell 7.4 // original by: Kevin van Zonneveld (https://kvz.io) // example 1: padright('42', 5) // returns 1: '42 ' // example 2: padright('42', 5, '0') // returns 2: '42000' // example 3: padright('hello', 3) // returns 3: 'hello' const source = String(str) const width = Math.trunc(Number(totalWidth)) if (!Number.isFinite(width) || width < 0) { throw new RangeError('padright(): totalWidth must be a non-negative integer') } const fill = paddingChar === undefined ? ' ' : String(paddingChar) if (fill === '') { return source } return source.padEnd(width, fill[0] ?? ' ') } {% endcodeblock %}