2.0.5 • Published 2 months ago
@zerodep/string-padleft v2.0.5
@zerodep/string-padleft
A utility to prefix a value with a specified character to create a string of a specific length. Non-string values will cause a ZeroDepError
to be thrown.
Full documentation is available at the zerodep.app page.
Examples
All @zerodep packages support both ESM and CJS.
import { stringPadLeft } from '@zerodep/string-padleft';
// or
const { stringPadLeft } = require('@zerodep/string-padleft');
Using Default Space Separator
stringPadLeft('abc', 10); // " abc"
stringPadLeft(123, 10); // " 123"
stringPadLeft(456n, 10); // " 456"
Using Custom Separator
stringPadLeft('bc', 5, 'a'); // "aaabc"
stringPadLeft(123, 6, '0'); // "000123"
stringPadLeft(456n, 7, '_'); // "____456"
Edge Cases
// when the value exceeds the requested size the full value is returned
stringPadLeft('abcdefghij', 5, 'x'); // "abcdefghij"
// non-string values
stringPadLeft({ not: 'a string' }, 2); // throws ZeroDepError: Value is not a string