All files / src/utils joda-to-regexp.ts

90.91% Statements 10/11
50% Branches 1/2
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78                                      34x 34x 34x 34x                                                                                             34x 21x 105x 105x 105x   21x    
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
// Refer to https://www.joda.org/joda-time/key_format.html
const TEXT = '\\w+';
const NUMBER_2_DIGIT = '[0-9]{2}';
const NUMBER_4_DIGIT = '[0-9]{4}';
const JODA_FRAGMENT_TO_REG_EXP_STRING: Record<string, string> = {
  C: '[0-9]{1,2}',
  CC: NUMBER_2_DIGIT,
  YY: NUMBER_2_DIGIT,
  YYYY: NUMBER_4_DIGIT,
 
  xx: NUMBER_2_DIGIT,
  xxxx: NUMBER_4_DIGIT,
  w: '[0-9]{1,2}',
  ww: NUMBER_2_DIGIT,
  e: '[0-7]',
  E: TEXT,
  EEEE: TEXT,
 
  yy: NUMBER_2_DIGIT,
  yyyy: NUMBER_4_DIGIT,
  D: '[0-9]{1,3}',
  DD: '[0-9]{2,3}',
  DDD: '[0-9]{3}',
  M: '(?:1[0-2]|[1-9])',
  MM: '(?:1[0-2]|0[1-9])',
  MMM: TEXT,
  MMMM: TEXT,
  d: '(?:3[0-1]|[12][0-9]|[1-9])',
  dd: '(?:3[0-1]|[12][0-9]|0[1-9])',
 
  a: '[ap]m',
  K: '(?:1[01]|[0-9])',
  KK: '(?:1[01]|0[0-9])',
  h: '(?:1[0-2]|[1-9])',
  hh: '(?:1[0-2]|0[1-9])',
 
  H: '(?:2[0-3]|1[0-9]|[0-9])',
  HH: '(?:2[0-3]|1[0-9]|0[0-9])',
  k: '(?:2[0-4]|1[0-9]|[1-9])',
  kk: '(?:2[0-4]|1[0-9]|0[1-9])',
  m: '(?:[1-5][0-9]|[0-9])',
  mm: '[0-5][0-9]',
  s: '(?:[1-5][0-9]|[0-9])',
  ss: '[0-5][0-9]',
  S: '[0-9]{1,3}',
  SS: '[0-9]{2,3}',
  SSS: '[0-9]{3}',
  z: TEXT,
  Z: TEXT,
};
 
export function jodaFormatToRegExp(jodaFormat: string): RegExp {
  const regExpStr = jodaFormat.replace(/([a-zA-Z])\1{0,3}/g, jodaPart => {
    const re = JODA_FRAGMENT_TO_REG_EXP_STRING[jodaPart];
    Iif (!re) throw new Error(`could not convert ${jodaPart} to RegExp`);
    return re;
  });
  return new RegExp(`^${regExpStr}$`, 'i');
}