@technobuddha/library
Version:
A large library of useful functions
22 lines (21 loc) • 806 B
JavaScript
import { space } from '../constants';
import splitChars from '../splitChars';
const tokenizer = /\\#|#|./gu;
/**
* Use a simple mask to display a string
*
* @remark The simple mask is a string where '#' characters are replaced by characters from the input string. Other characters in the mask
* are output as-is, to output a '#' use '\#'
*
* @param input The string
* @param mask The mask
* @param __namedParameters see {@link Options}
* @default missing space
* @returns The mask filled with characters from the string
*/
export function mask(input, maskStr, { missing = space } = {}) {
const chars = splitChars(input);
let index = 0;
return maskStr.replace(tokenizer, token => (token === '\\#' ? '#' : token === '#' ? (chars[index++] ?? missing) : token));
}
export default mask;