|
| 1 | +import parse, { AST, Element, SubMessages } from 'format-message-parse'; |
| 2 | + |
| 3 | +interface Param { |
| 4 | + name: string; |
| 5 | +} |
| 6 | + |
| 7 | +type Format = |
| 8 | + | 'plural' |
| 9 | + | 'selectordinal' |
| 10 | + | 'date' |
| 11 | + | 'time' |
| 12 | + | 'select' |
| 13 | + | 'number'; |
| 14 | + |
| 15 | +const isPlural = (format: Format): boolean => { |
| 16 | + return format === 'plural' || format === 'selectordinal'; |
| 17 | +}; |
| 18 | + |
| 19 | +const isSelect = (format: Format): boolean => { |
| 20 | + return format === 'select'; |
| 21 | +}; |
| 22 | + |
| 23 | +const hasHashtagOnly = (element: Element | undefined): boolean => { |
| 24 | + return element!.length === 1 && element![0] === '#'; |
| 25 | +}; |
| 26 | + |
| 27 | +const isStringOnly = (element: Format | undefined): boolean => { |
| 28 | + return typeof element === 'string'; |
| 29 | +}; |
| 30 | + |
| 31 | +const isValidSubMessages = (subMessages: {} | undefined): boolean => { |
| 32 | + return typeof subMessages === 'object'; |
| 33 | +}; |
| 34 | + |
| 35 | +const getSubMessages = ( |
| 36 | + element: Element | undefined, |
| 37 | + format: Format |
| 38 | +): SubMessages | undefined => { |
| 39 | + if (element) { |
| 40 | + let subMessages: SubMessages | undefined; |
| 41 | + if (isPlural(format)) { |
| 42 | + subMessages = element[3] as SubMessages; |
| 43 | + } else if (isSelect(format)) { |
| 44 | + subMessages = element[2] as SubMessages; |
| 45 | + } |
| 46 | + if (isValidSubMessages(subMessages)) { |
| 47 | + return subMessages as SubMessages; |
| 48 | + } |
| 49 | + } |
| 50 | + return undefined; |
| 51 | +}; |
| 52 | + |
| 53 | +const stackWithSubMessages = ( |
| 54 | + stack: Element[], |
| 55 | + subMessages: SubMessages |
| 56 | +): Element[] => { |
| 57 | + // eslint-disable-next-line prefer-spread |
| 58 | + return stack.concat.apply(stack, Object.values(subMessages)); |
| 59 | +}; |
| 60 | + |
| 61 | +const getParamsFromPatternAst = (parsedArray: AST): Param[] => { |
| 62 | + if (!parsedArray || !parsedArray.slice) return []; |
| 63 | + let stack = parsedArray.slice(); |
| 64 | + const params: Param[] = []; |
| 65 | + const used = new Set(); |
| 66 | + while (stack.length) { |
| 67 | + const element = stack.pop(); |
| 68 | + if (isStringOnly(element as Format)) continue; |
| 69 | + if (hasHashtagOnly(element)) continue; |
| 70 | + |
| 71 | + const [name, format] = element!; |
| 72 | + |
| 73 | + if (!used.has(name)) { |
| 74 | + params.push({ name: name as string }); |
| 75 | + used.add(name); |
| 76 | + } |
| 77 | + |
| 78 | + const subMessages = getSubMessages(element, format as Format); |
| 79 | + if (subMessages) { |
| 80 | + stack = stackWithSubMessages(stack, subMessages); |
| 81 | + } |
| 82 | + } |
| 83 | + return params.reverse(); |
| 84 | +}; |
| 85 | + |
| 86 | +export const getTypedParams = (text: string): Param[] => { |
| 87 | + try { |
| 88 | + return getParamsFromPatternAst(parse(text)); |
| 89 | + } catch (e) { |
| 90 | + return []; |
| 91 | + } |
| 92 | +}; |
0 commit comments