UNPKG

mm_os

Version:

MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。

206 lines (184 loc) 5.52 kB
/** * 单词工具类 * @class Word */ class Word { /** * 构造函数 */ constructor() { } } /** * 分割单词 * @param {string} name 名称 * @returns {Array} 单词数组 */ Word.prototype.splitWords = function (name) { if (!name || name.length === 0) { return []; } var words = []; var current_word = ''; var last_char_type = null; for (var i = 0; i < name.length; i++) { var char = name[i]; var char_info = this._getCharInfo(char); // 处理下划线和数字 if (char_info.is_under || char_info.is_digit) { last_char_type = this._handleSepa(words, current_word, char, char_info); current_word = ''; continue; } // 处理第一个字符 if (current_word.length === 0) { current_word = char; last_char_type = char_info.type; continue; } // 处理字符转换 var result = this._handleCharTran(words, current_word, char, last_char_type, char_info.type); words = result.words; current_word = result.current_word; last_char_type = char_info.type; } // 添加最后一个单词 if (current_word.length > 0) { words.push(current_word); } return words; }; /** * 获取字符信息 * @param {string} char 字符 * @returns {object} 字符信息 * @private */ Word.prototype._getCharInfo = function (char) { var is_upper = char >= 'A' && char <= 'Z'; var is_lower = char >= 'a' && char <= 'z'; var is_digit = char >= '0' && char <= '9'; var type = is_upper ? 'upper' : (is_lower ? 'lower' : (is_digit ? 'digit' : 'other')); return { is_upper, is_lower, is_digit, is_under: char === '_', type }; }; /** * 处理分隔符 * @param {Array} words 单词数组 * @param {string} current_word 当前单词 * @param {string} char 字符 * @param {object} char_info 字符信息 * @returns {string} 新的字符类型 * @private */ Word.prototype._handleSepa = function (words, current_word, char, char_info) { if (current_word.length > 0) { words.push(current_word); } if (char_info.is_digit) { words.push(char); return 'digit'; } return 'underscore'; }; /** * 处理字符转换 * @param {Array} words 单词数组 * @param {string} current_word 当前单词 * @param {string} char 字符 * @param {string} last_type 上一个字符类型 * @param {string} current_type 当前字符类型 * @returns {object} 处理结果 * @private */ Word.prototype._handleCharTran = function ( words, current_word, char, last_type, current_type ) { var is_last_letter = last_type === 'upper' || last_type === 'lower'; var is_current_letter = current_type === 'upper' || current_type === 'lower'; if (!is_last_letter || !is_current_letter) { return { words, current_word: current_word + char }; } if (last_type === 'upper' && current_type === 'lower') { // 大写转小写 if (current_word.length > 1) { var last_char = current_word[current_word.length - 1]; words.push(current_word.substring(0, current_word.length - 1)); return { words, current_word: last_char + char }; } return { words, current_word: current_word + char }; } if (last_type === 'lower' && current_type === 'upper') { // 小写转大写 words.push(current_word); return { words, current_word: char }; } // 相同类型 return { words, current_word: current_word + char }; }; /** * 将名称改为帕斯卡命名法 * @param {string} name 名称 * @returns {string} 帕斯卡命名法的名称 */ Word.prototype.toPascalCase = function (name) { var words = this.splitWords(name); for (let i = 0; i < words.length; i++) { words[i] = words[i].toLowerCase(); words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1); } return words.join(''); }; /** * 将名称改为驼峰命名法 * @param {string} name 名称 * @returns {string} 驼峰命名法的名称 */ Word.prototype.toCamelCase = function (name) { var words = this.splitWords(name); for (let i = 0; i < words.length; i++) { words[i] = words[i].toLowerCase(); if (i > 0) { words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1); } } return words.join(''); }; /** * 将名称改为小写蛇形(下划线)命名法 * @param {string} name 名称 * @returns {string} 小写蛇形(下划线)命名法的名称 */ Word.prototype.toSnakeCase = function (name) { var words = this.splitWords(name); for (let i = 0; i < words.length; i++) { words[i] = words[i].toLowerCase(); } return words.join('_'); }; /** * 将名称改为大写蛇形(下划线)命名法 * @param {string} name 名称 * @returns {string} 大写蛇形(下划线)命名法的名称 */ Word.prototype.toUpperSnakeCase = function (name) { var words = this.splitWords(name); for (let i = 0; i < words.length; i++) { words[i] = words[i].toUpperCase(); } return words.join('_'); }; /** * 将名称改为小写短横线(短横线)命名法 * @param {string} name 名称 * @returns {string} 小写短横线(短横线)命名法的名称 */ Word.prototype.toKebabCase = function (name) { var words = this.splitWords(name); for (let i = 0; i < words.length; i++) { words[i] = words[i].toLowerCase(); } return words.join('-'); }; module.exports = { Word };