@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
38 lines • 1.68 kB
JavaScript
import _pushInstanceProperty from "core-js-pure/stable/instance/push.js";
import { placeholderChar as defaultPlaceholderChar } from "./constants.js";
const emptyArray = [];
export function convertMaskToPlaceholder(mask = emptyArray, placeholderChar = defaultPlaceholderChar) {
if (!isArray(mask)) {
throw new Error('Text-mask:convertMaskToPlaceholder; The mask property must be an array.');
}
if (mask.indexOf(placeholderChar) !== -1) {
throw new Error('Placeholder character must not be used as part of the mask. Please specify a character ' + 'that is not present in your mask as your placeholder character.\n\n' + `The placeholder character that was received is: ${JSON.stringify(placeholderChar)}\n\n` + `The mask that was received is: ${JSON.stringify(mask)}`);
}
return mask.map(char => char instanceof RegExp ? placeholderChar : String(char)).join('');
}
export function isArray(value) {
return Array.isArray && Array.isArray(value) || value instanceof Array;
}
export function isString(value) {
return typeof value === 'string' || value instanceof String;
}
export function isNumber(value) {
return typeof value === 'number' && Number.isFinite(value);
}
export function isNil(value) {
return typeof value === 'undefined' || value === null;
}
const strCaretTrap = '[]';
export function processCaretTraps(mask) {
const indexes = [];
let indexOfCaretTrap;
while (indexOfCaretTrap = mask.indexOf(strCaretTrap), indexOfCaretTrap !== -1) {
_pushInstanceProperty(indexes).call(indexes, indexOfCaretTrap);
mask.splice(indexOfCaretTrap, 1);
}
return {
maskWithoutCaretTraps: mask,
indexes
};
}
//# sourceMappingURL=utilities.js.map