--- warning: This file is auto generated by `yarn web:inject`, do not edit by hand examples: - padding('abc', 8, 'xy', 0) - padding('abcdef', 4, 'xy', 0) - padding('go', 7, '123', 1) returns: - '''abcxyxyx''' - '''abcd''' - '''go23123''' dependencies: [] authors: original by: - Kevin van Zonneveld (https://kvz.io) notes: - >- Mirrors Swift's padding(toLength:withPad:startingAt:) behavior for truncation and pad-fill. parityVerified: null isTypescript: true type: function layout: function title: Swift's String.padding in TypeScript description: >- Here's what our current TypeScript equivalent to Swift's String.padding looks like. function: padding category: String language: swift permalink: swift/String/padding/ alias: - /functions/swift/padding/ - /functions/String/padding/ - /swift/padding/ ---
{% codeblock lang:typescript %}export function padding(str: string, toLength: number, withPad: string = ' ', startingAt: number = 0): string { // discuss at: https://locutus.io/swift/String/padding/ // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Mirrors Swift's padding(toLength:withPad:startingAt:) behavior for truncation and pad-fill. // example 1: padding('abc', 8, 'xy', 0) // returns 1: 'abcxyxyx' // example 2: padding('abcdef', 4, 'xy', 0) // returns 2: 'abcd' // example 3: padding('go', 7, '123', 1) // returns 3: 'go23123' const source = String(str) const targetLength = Math.trunc(Number(toLength)) const pad = String(withPad) if (!Number.isFinite(targetLength) || targetLength <= 0) { return '' } if (source.length >= targetLength) { return source.slice(0, targetLength) } if (pad.length === 0) { return source } const start = Math.max(0, Math.trunc(Number(startingAt))) % pad.length const needed = targetLength - source.length let repeated = '' while (repeated.length < needed + start) { repeated += pad } return source + repeated.slice(start, start + needed) } {% endcodeblock %}