Ensuring

import { ensureLeft, ensureRight } from '@ppabari/strio/str';

Idempotent affix helpers โ€” guarantee a string starts or ends a certain way without duplicating an affix that is already present.

ensureLeft

ensureLeft(str: string, prefix: string): string
ensureLeft('example.com', 'https://');         // โ†’ 'https://example.com'
ensureLeft('https://example.com', 'https://'); // โ†’ 'https://example.com'

ensureRight

ensureRight(str: string, suffix: string): string
ensureRight('path', '/');  // โ†’ 'path/'
ensureRight('path/', '/'); // โ†’ 'path/'