{"version":3,"file":"naming-style.min.cjs","sources":["../src/impl/first-char-only-to-upper.js","../src/impl/find-first.js","../src/naming-style.js","../src/index.js"],"sourcesContent":["////////////////////////////////////////////////////////////////////////////////\n//\n//    Copyright (c) 2022 - 2023.\n//    Haixing Hu, Qubit Co. Ltd.\n//\n//    All rights reserved.\n//\n////////////////////////////////////////////////////////////////////////////////\n\n/**\n * Converts the first character of a word to upper case and converts the\n * remaining characters to lower case.\n *\n * @param {string} word\n *     the word to be converted.\n * @returns {string}\n *     the converted word.\n * @author Haixing Hu\n * @private\n */\nfunction firstCharOnlyToUpper(word) {\n  if (word.length === 0) {\n    return word;\n  } else {\n    return word[0].toUpperCase() + word.substring(1).toLowerCase();\n  }\n}\n\nexport default firstCharOnlyToUpper;\n","////////////////////////////////////////////////////////////////////////////////\n//\n//    Copyright (c) 2022 - 2023.\n//    Haixing Hu, Qubit Co. Ltd.\n//\n//    All rights reserved.\n//\n////////////////////////////////////////////////////////////////////////////////\n\n/**\n * Finds the first character in a string satisfying the specified filter.\n *\n * @param {String} str\n *     the string to be searched, which must be an ASCII string.\n * @param {Number} startIndex\n *     the index of the first character to be searched, which must be in the\n *     range `[0, str.length)`.\n * @param {Number} endIndex\n *     the index after the last character to be searched, which must be in the\n *     range `[0, str.length]`.\n * @param {Function} filter\n *     the filter function.\n * @return {Number}\n *     the index of the first character satisfying the specified filter; or\n *     -1 if no such character is found.\n * @author Haixing Hu\n * @private\n */\nfunction findFirst(str, startIndex, endIndex, filter) {\n  for (let i = startIndex; i < endIndex; ++i) {\n    if (filter(str.charAt(i))) {\n      return i;\n    }\n  }\n  return -1;\n}\n\nexport default findFirst;\n","////////////////////////////////////////////////////////////////////////////////\n//\n//    Copyright (c) 2022 - 2023.\n//    Haixing Hu, Qubit Co. Ltd.\n//\n//    All rights reserved.\n//\n////////////////////////////////////////////////////////////////////////////////\nimport firstCharOnlyToUpper from './impl/first-char-only-to-upper';\nimport findFirst from './impl/find-first';\n\n/**\n * A enumeration class that represents naming styles of identifiers.\n *\n * This class is used to convert a string from one naming style to another.\n * This class provides the following naming style constants:\n * - {@link NamingStyle#LOWER_HYPHEN}: XML hyphenated variable naming style,\n *   e.g., `\"lower-hyphen\"`.\n * - {@link NamingStyle#LOWER_UNDERSCORE}: C++/Python variable naming style,\n *   e.g., `\"lower_underscore\"`.\n * - {@link NamingStyle#LOWER_CAMEL}: Java variable naming style, e.g.,\n *   `\"lowerCamel\"`.\n * - {@link NamingStyle#UPPER_CAMEL}: Java and C++ class naming style, e.g.,\n *   `\"UpperCamel\"`.\n * - {@link NamingStyle#UPPER_UNDERSCORE}: Java and C++ constant naming style,\n *   e.g., `\"UPPER_UNDERSCORE\"`.\n *\n * Each constant is a {@link NamingStyle} instance. You can use the\n * {@link NamingStyle#to} method to convert a string from one naming style to\n * another.\n *\n * Example usage:\n * ```js\n * const str = 'hello-world';\n * const result = NamingStyle.LOWER_HYPHEN.to(NamingStyle.LOWER_CAMEL, str);\n * expect(result).toBe('helloWorld');\n * ```\n *\n * @author Haixing Hu\n */\nclass NamingStyle {\n  /**\n   * XML hyphenated variable naming style, e.g., `\"lower-hyphen\"`.\n   */\n  static LOWER_HYPHEN = Object.freeze(new NamingStyle(\n    'lower-hyphen',                 // name\n    (c) => c === '-',               // wordBoundaryFilter\n    '-',                            // wordSeparator\n    (w) => w.toLowerCase(),         // wordNormalizer\n    undefined,                      // firstWordNormalizer\n    (style, str) => {               // quickOptimizer\n      if (style === NamingStyle.LOWER_UNDERSCORE) {\n        return str.replace(/-/g, '_');\n      } else if (style === NamingStyle.UPPER_UNDERSCORE) {\n        return str.replace(/-/g, '_').toUpperCase();\n      }\n      return undefined;\n    },\n  ));\n\n  /**\n   * C++/Python variable naming style, e.g., `\"lower_underscore\"`.\n   */\n  static LOWER_UNDERSCORE = Object.freeze(new NamingStyle(\n    'lower-underscore',             // name\n    (c) => c === '_',               // wordBoundaryFilter\n    '_',                            // wordSeparator\n    (w) => w.toLowerCase(),         // wordNormalizer\n    undefined,                      // firstWordNormalizer\n    (style, str) => {               // quickOptimizer\n      if (style === NamingStyle.LOWER_HYPHEN) {\n        return str.replace(/_/g, '-');\n      } else if (style === NamingStyle.UPPER_UNDERSCORE) {\n        return str.toUpperCase();\n      }\n      return undefined;\n    },\n  ));\n\n  /**\n   * Java variable naming style, e.g., `\"lowerCamel\"`.\n   */\n  static LOWER_CAMEL = Object.freeze(new NamingStyle(\n    'lower-camel',                  // name\n    (c) => (c >= 'A' && c <= 'Z'),  // wordBoundaryFilter\n    '',                             // wordSeparator\n    (w) => firstCharOnlyToUpper(w), // wordNormalizer\n    (w) => w.toLowerCase(),         // firstWordNormalizer\n  ));\n\n  /**\n   * Java and C++ class naming style, e.g., `\"UpperCamel\"`.\n   */\n  static UPPER_CAMEL = Object.freeze(new NamingStyle(\n    'upper-camel',                  // name\n    (c) => (c >= 'A' && c <= 'Z'),  // wordBoundaryFilter\n    '',                             // wordSeparator\n    (w) => firstCharOnlyToUpper(w), // wordNormalizer\n  ));\n\n  /**\n   * Java and C++ constant naming style, e.g., `\"UPPER_UNDERSCORE\"`.\n   */\n  static UPPER_UNDERSCORE = Object.freeze(new NamingStyle(\n    'upper-underscore',             // name\n    (c) => c === '_',               // wordBoundaryFilter\n    '_',                            // wordSeparator\n    (w) => w.toUpperCase(),         // wordNormalizer\n    undefined,                      // firstWordNormalizer\n    (style, str) => {               // quickOptimizer\n      if (style === NamingStyle.LOWER_HYPHEN) {\n        return str.replace(/_/g, '-').toLowerCase();\n      } else if (style === NamingStyle.LOWER_UNDERSCORE) {\n        return str.toLowerCase();\n      }\n      return undefined;\n    },\n  ));\n\n  /**\n   * Returns all the naming style constants.\n   *\n   * @return {Array<NamingStyle>}\n   *     the array of all naming style constants.\n   */\n  static values() {\n    return [\n      NamingStyle.LOWER_HYPHEN,\n      NamingStyle.LOWER_UNDERSCORE,\n      NamingStyle.LOWER_CAMEL,\n      NamingStyle.UPPER_CAMEL,\n      NamingStyle.UPPER_UNDERSCORE,\n    ];\n  }\n\n  /**\n   * Returns the naming style constant of the specified name.\n   *\n   * @param {String|NamingStyle} name\n   *     the name of the naming style constant, or a `NamingStyle` instance. The\n   *     name is compared case-insensitively and the characters '-' and '_' are\n   *     treated as the same.\n   * @return {NamingStyle}\n   *     the naming style constant of the specified name, or the specified\n   *     `NamingStyle` instance if the argument `name` is a `NamingStyle` instance.\n   * @throws {TypeError}\n   *     if the argument `name` is not a string nor a `NamingStyle` instance.\n   * @throws {Error}\n   *     if there is no naming style constant of the specified name.\n   */\n  static of(name) {\n    if (name instanceof NamingStyle) {\n      return name;\n    }\n    if (typeof name !== 'string') {\n      throw new TypeError('The argument of `NamingStyle.of()` must be a string or a `NamingStyle` instance.');\n    }\n    const normalizedName = name.replace(/_/g, '-').toLowerCase();\n    const values = NamingStyle.values();\n    for (let i = 0; i < values.length; ++i) {\n      const value = values[i];\n      if (value.name.toLowerCase() === normalizedName) {\n        return value;\n      }\n    }\n    throw new Error(`Unknown naming style: '${name}'.`);\n  }\n\n  /**\n   * Constructor a {@link NamingStyle} instance.\n   *\n   * **NOTE:** this constructor is private, you should use the static constants\n   * instead.\n   *\n   * @param {String} name\n   *     The name of this instance.\n   * @param {Function} wordBoundaryFilter\n   *     A function that determines whether a character is a word boundary.\n   * @param {String} wordSeparator\n   *     The word separator string.\n   * @param {Function} wordNormalizer\n   *     The function that normalizes a word.\n   * @param {Function|undefined} firstWordNormalizer\n   *     The function that normalizes the first word. If this argument is\n   *     `undefined`, then the `wordNormalizer` will be used instead.\n   * @param {Function|undefined} quickOptimizer\n   *     The function that optimizes the conversion from this naming style to\n   *     some special naming style. If this argument is `undefined`, then it is\n   *     ignored.\n   */\n  constructor(name, wordBoundaryFilter, wordSeparator, wordNormalizer, firstWordNormalizer, quickOptimizer) {\n    this.name = name;\n    this.wordBoundaryFilter = wordBoundaryFilter;\n    this.wordSeparator = wordSeparator;\n    this.wordNormalizer = wordNormalizer;\n    this.firstWordNormalizer = firstWordNormalizer ?? wordNormalizer;\n    this.quickOptimizer = quickOptimizer;\n  }\n\n  /**\n   * Converts a specified string from this naming style to another naming style.\n   *\n   * @param {NamingStyle} style\n   *     The target naming style.\n   * @param {String} str\n   *     The string to be converted.\n   * @return {String}\n   *     The converted string.\n   */\n  to(style, str) {\n    if (str === null || str === undefined) {\n      return str;\n    }\n    if (style === this) {\n      return str;\n    }\n    if (this.quickOptimizer) {\n      const result = this.quickOptimizer(style, str);\n      if (result !== undefined) {\n        return result;\n      }\n    }\n    let result = '';\n    let i = 0;\n    let j = -1;\n    while ((j = findFirst(str, ++j, str.length, this.wordBoundaryFilter)) >= 0) {\n      if (i === j) continue;\n      const word = str.substring(i, j);\n      if (i === 0) {\n        const normalizedWord = style.firstWordNormalizer(word);\n        result += normalizedWord;\n      } else {\n        const normalizedWord = style.wordNormalizer(word);\n        result += normalizedWord;\n      }\n      result += style.wordSeparator;\n      i = j + this.wordSeparator.length;\n    }\n    if (i === 0) {\n      return style.firstWordNormalizer(str);\n    } else {\n      const word = str.substring(i);\n      const normalizedWord = style.wordNormalizer(word);\n      result += normalizedWord;\n      return result;\n    }\n  }\n}\n\n// freeze the constants and the class\n// Object.freeze(NamingStyle);\n\nexport default NamingStyle;\n","////////////////////////////////////////////////////////////////////////////////\n//\n//    Copyright (c) 2022 - 2023.\n//    Haixing Hu, Qubit Co. Ltd.\n//\n//    All rights reserved.\n//\n////////////////////////////////////////////////////////////////////////////////\nimport NamingStyle from './naming-style';\n\n/**\n * XML hyphenated variable naming convention, e.g., \"lower-hyphen\".\n *\n * This constant is a {@link NamingStyle} instance, it is a shorthand of\n * {@link NamingStyle#LOWER_HYPHEN}. You can use the {@link NamingStyle#to}\n * method to convert a string from one case format to another.\n *\n * @type {NamingStyle}\n */\nconst LOWER_HYPHEN = NamingStyle.LOWER_HYPHEN;\n\n/**\n * C++/Python variable naming convention, e.g., \"lower_underscore\".\n *\n * This constant is a {@link NamingStyle} instance, it is a shorthand of\n * {@link NamingStyle#LOWER_UNDERSCORE}. You can use the {@link NamingStyle#to}\n * method to convert a string from one case format to another.\n *\n * @type {NamingStyle}\n */\nconst LOWER_UNDERSCORE = NamingStyle.LOWER_UNDERSCORE;\n\n/**\n * Java variable naming convention, e.g., \"lowerCamel\".\n *\n * This constant is a {@link NamingStyle} instance, it is a shorthand of\n * {@link NamingStyle#LOWER_CAMEL}. You can use the {@link NamingStyle#to} method\n * to convert a string from one case format to another.\n *\n * @type {NamingStyle}\n */\nconst LOWER_CAMEL = NamingStyle.LOWER_CAMEL;\n\n/**\n * Java and C++ class naming convention, e.g., \"UpperCamel\".\n *\n * This constant is a {@link NamingStyle} instance, it is a short-hand of\n * {@link NamingStyle#UPPER_CAMEL}. You can use the {@link NamingStyle#to} method\n * to convert a string from one case format to another.\n *\n * @type {NamingStyle}\n */\nconst UPPER_CAMEL = NamingStyle.UPPER_CAMEL;\n\n/**\n * Java and C++ constant naming convention, e.g., \"UPPER_UNDERSCORE\".\n *\n * This constant is a {@link NamingStyle} instance, it is a shorthand of\n * {@link NamingStyle#UPPER_UNDERSCORE}. You can use the {@link NamingStyle#to}\n * method to convert a string from one case format to another.\n *\n * @type {NamingStyle}\n */\nconst UPPER_UNDERSCORE = NamingStyle.UPPER_UNDERSCORE;\n\nexport {\n  NamingStyle,\n  LOWER_HYPHEN,\n  LOWER_UNDERSCORE,\n  LOWER_CAMEL,\n  UPPER_CAMEL,\n  UPPER_UNDERSCORE,\n};\n\nexport default NamingStyle;\n"],"names":["Object","defineProperty","exports","value","_classCallCheck","require","_createClass","_defineProperty","firstCharOnlyToUpper","word","length","toUpperCase","substring","toLowerCase","findFirst","str","startIndex","endIndex","filter","i","charAt","NamingStyle","name","wordBoundaryFilter","wordSeparator","wordNormalizer","firstWordNormalizer","quickOptimizer","this","key","to","style","result","undefined","j","values","LOWER_HYPHEN","LOWER_UNDERSCORE","LOWER_CAMEL","UPPER_CAMEL","UPPER_UNDERSCORE","of","TypeError","normalizedName","replace","Error","concat","_NamingStyle","freeze","c","w"],"mappings":"AAAA,aAAAA,O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