1/5/1404
This commit is contained in:
+8
@@ -0,0 +1,8 @@
|
||||
export function buildFormatLongFn(args) {
|
||||
return (options = {}) => {
|
||||
// TODO: Remove String()
|
||||
const width = options.width ? String(options.width) : args.defaultWidth;
|
||||
const format = args.formats[width] || args.formats[args.defaultWidth];
|
||||
return format;
|
||||
};
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* The localize function argument callback which allows to convert raw value to
|
||||
* the actual type.
|
||||
*
|
||||
* @param value - The value to convert
|
||||
*
|
||||
* @returns The converted value
|
||||
*/
|
||||
|
||||
/**
|
||||
* The map of localized values for each width.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The index type of the locale unit value. It types conversion of units of
|
||||
* values that don't start at 0 (i.e. quarters).
|
||||
*/
|
||||
|
||||
/**
|
||||
* Converts the unit value to the tuple of values.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The tuple of localized era values. The first element represents BC,
|
||||
* the second element represents AD.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The tuple of localized quarter values. The first element represents Q1.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The tuple of localized day values. The first element represents Sunday.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The tuple of localized month values. The first element represents January.
|
||||
*/
|
||||
|
||||
export function buildLocalizeFn(args) {
|
||||
return (value, options) => {
|
||||
const context = options?.context ? String(options.context) : "standalone";
|
||||
|
||||
let valuesArray;
|
||||
if (context === "formatting" && args.formattingValues) {
|
||||
const defaultWidth = args.defaultFormattingWidth || args.defaultWidth;
|
||||
const width = options?.width ? String(options.width) : defaultWidth;
|
||||
|
||||
valuesArray =
|
||||
args.formattingValues[width] || args.formattingValues[defaultWidth];
|
||||
} else {
|
||||
const defaultWidth = args.defaultWidth;
|
||||
const width = options?.width ? String(options.width) : args.defaultWidth;
|
||||
|
||||
valuesArray = args.values[width] || args.values[defaultWidth];
|
||||
}
|
||||
const index = args.argumentCallback ? args.argumentCallback(value) : value;
|
||||
|
||||
// @ts-expect-error - For some reason TypeScript just don't want to match it, no matter how hard we try. I challenge you to try to remove it!
|
||||
return valuesArray[index];
|
||||
};
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
export function buildMatchFn(args) {
|
||||
return (string, options = {}) => {
|
||||
const width = options.width;
|
||||
|
||||
const matchPattern =
|
||||
(width && args.matchPatterns[width]) ||
|
||||
args.matchPatterns[args.defaultMatchWidth];
|
||||
const matchResult = string.match(matchPattern);
|
||||
|
||||
if (!matchResult) {
|
||||
return null;
|
||||
}
|
||||
const matchedString = matchResult[0];
|
||||
|
||||
const parsePatterns =
|
||||
(width && args.parsePatterns[width]) ||
|
||||
args.parsePatterns[args.defaultParseWidth];
|
||||
|
||||
const key = Array.isArray(parsePatterns)
|
||||
? findIndex(parsePatterns, (pattern) => pattern.test(matchedString))
|
||||
: // [TODO] -- I challenge you to fix the type
|
||||
findKey(parsePatterns, (pattern) => pattern.test(matchedString));
|
||||
|
||||
let value;
|
||||
|
||||
value = args.valueCallback ? args.valueCallback(key) : key;
|
||||
value = options.valueCallback
|
||||
? // [TODO] -- I challenge you to fix the type
|
||||
options.valueCallback(value)
|
||||
: value;
|
||||
|
||||
const rest = string.slice(matchedString.length);
|
||||
|
||||
return { value, rest };
|
||||
};
|
||||
}
|
||||
|
||||
function findKey(object, predicate) {
|
||||
for (const key in object) {
|
||||
if (
|
||||
Object.prototype.hasOwnProperty.call(object, key) &&
|
||||
predicate(object[key])
|
||||
) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function findIndex(array, predicate) {
|
||||
for (let key = 0; key < array.length; key++) {
|
||||
if (predicate(array[key])) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
export function buildMatchPatternFn(args) {
|
||||
return (string, options = {}) => {
|
||||
const matchResult = string.match(args.matchPattern);
|
||||
if (!matchResult) return null;
|
||||
const matchedString = matchResult[0];
|
||||
|
||||
const parseResult = string.match(args.parsePattern);
|
||||
if (!parseResult) return null;
|
||||
let value = args.valueCallback
|
||||
? args.valueCallback(parseResult[0])
|
||||
: parseResult[0];
|
||||
|
||||
// [TODO] I challenge you to fix the type
|
||||
value = options.valueCallback ? options.valueCallback(value) : value;
|
||||
|
||||
const rest = string.slice(matchedString.length);
|
||||
|
||||
return { value, rest };
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user