UNPKG

@onesy/date

Version:

Time and date utils library

58 lines (49 loc) 2.38 kB
import getStringVariables from '@onesy/utils/getStringVariables'; import setStringVariables from '@onesy/utils/setStringVariables'; import OnesyDate from './OnesyDate'; import formats from './formats'; // ISO as a default format value string export default function format() { let onesyDate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : OnesyDate.onesyDate; let value_ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "YYYY-MM-DDTHH:mm:ss"; let options = arguments.length > 2 ? arguments[2] : undefined; const l = (options === null || options === void 0 ? void 0 : options.l) || (value => value); if (onesyDate && onesyDate.valid) { let value = value_; const formatValues = formats(onesyDate); const abrs = formatValues.map(item => item.abr); // Extract and save all words quoted with: '', "", ``, {} or [], // in a string, so adding abr values doesn't override 'em const words = getStringVariables(value, { variablesRegExp: /('.*?'|".*?"|`.*?`|\{.*?\}|\[.*?\])/g }); // Add placeholders before adding all abr values value = words.valueWithPlaceholders; // Replace all abr values // tslint:disable-next-line const reAbr = abrs.reduce((result, current, index) => result += "".concat(current).concat(index !== abrs.length - 1 ? '|' : ''), ''); const abrVariables = getStringVariables(value, { variablesRegExp: new RegExp(reAbr, 'g'), cleanVariables: false, placeholderPrefix: '__' }); // Add placeholders for matches prior to adding all abr values value = abrVariables.valueWithPlaceholders; const abrVariablesToValue = []; abrVariables.variables.forEach(variable => { const format_ = formatValues.find(item => item.abr === variable); abrVariablesToValue.push({ key: variable, value: l(format_.value) }); }); // Replace abr variables with appropriate values value = setStringVariables(value, abrVariablesToValue, { getVariables: false, placeholderPrefix: '__' }); // Revert back all the saved words const wordsVariablesToValue = []; words.variables.forEach(variable => wordsVariablesToValue.push({ key: variable, value: variable })); value = setStringVariables(value, wordsVariablesToValue, { getVariables: false }); return value; } }