提交 0aa2f8f1 编写于 作者: Mosher's avatar Mosher

update:增加工具方法

上级 ed47084f
/**
* 复数变化规则
*
*
*/
export class PluralizeRule{
/**
* 不规则
*
* @protected
* @type {string[]}
* @memberof PluralizeRule
*/
protected irregular: Map<string, string> = new Map();
/**
* 不可数
*
* @protected
* @type {string[]}
* @memberof PluralizeRule
*/
protected uncountable: string[] = [
"equipment", "information", "rice", "money", "species", "series", "fish", "sheep", "people", "men",
"children", "sexes", "moves", "stadiums", "oxen", "octopi", "viri", "aliases", "quizzes",
];
/**
* 初始化pluralizeRule对象
*
* @param opts 额外参数
* @memberof PluralizeRule
*/
public constructor(opts: any = {}) {
this.initIrregular();
}
/**
* 初始化不规则变化
*
* @param opts 额外参数
* @memberof PluralizeRule
*/
protected initIrregular(){
this.irregular.set("person", "people");
this.irregular.set("man", "men");
this.irregular.set("child", "children");
this.irregular.set("sex", "sexes");
this.irregular.set("move", "moves");
this.irregular.set("stadium", "stadiums");
}
/**
* 是否为不可数
*
* @param word 单词
* @returns 返回判断
* @memberof PluralizeRule
*/
public isUncountable(word: string){
const index: number = this.uncountable.findIndex((wordStr: string) =>{
return Object.is(word, wordStr);
})
if (index == -1) {
return false;
} else {
return true;
}
}
/**
* 不规则变化
*
* @param word 单词
* @returns 返回变化值
* @memberof PluralizeRule
*/
public irregularChange(word: string){
for(let item of this.irregular.entries()){
if(word && word.endsWith(item[0])){
return word.replace(new RegExp(item[0],'g'),item[1]);
}
}
return this.irregular.get(word);
}
/**
* 规则变化
*
* @param word 单词
* @returns 返回变化值
* @memberof PluralizeRule
*/
public ruleChange(word: string){
if(/(ax|test)is$/.test(word)) return word.replace(/is$/,"es");
if(/(octop|vir)us$/.test(word)) return word.replace(/us$/,"i");
if(/(octop|vir)i$/.test(word)) return word;
if(/(alias|status)$/.test(word)) return word+"es";
if(/(bu)s$/.test(word)) return word.replace(/s$/,"ses");
if(/(buffal|tomat)o$/.test(word)) return word.replace(/o$/,"oes");
if(/([ti])um$/.test(word)) return word.replace(/um$/,"a");
if(/([ti])a$/.test(word)) return word;
if(/sis$/.test(word)) return word.replace(/sis$/,"ses");
if(/(?:([^f])fe|([lr])f)$/.test(word)) return word.substring(0, word.length - 1)+"ves";
if(/(hive)$/.test(word)) return word+"s";
if(/([^aeiouy]|qu)y$/.test(word)) return word.replace(/y$/,"ies");
if(/(x|ch|ss|sh)$/.test(word)) return word+"es";
if(/(matr|vert|ind)ix|ex$/.test(word)) return word.replace(/ix|ex$/,"ices");
if(/([m|l])ouse$/.test(word)) return word.replace(/ouse$/,"ice");
if(/([m|l])ice$/.test(word)) return word;
if(/^(ox)$$/.test(word)) return word+"en";
if(/(quiz)$/.test(word)) return word+"zes";
if(/s$/.test(word)) return word.replace(/s$/,"s");
return word+"s";
}
}
\ No newline at end of file
import qs from 'qs';
import { Route } from 'vue-router';
import Schema from "async-validator";
import { PanelContainerModel, PanelRawitemModel, PanelFieldModel, PanelControlModel, PanelButtonModel, PanelUserControlModel, PanelTabPanelModel, PanelTabPageModel, PanelCtrlPosModel } from '@/model/panel-detail';
import { PluralizeRule } from './pluralizeRule';
/**
* 平台工具类
*
......@@ -10,6 +9,14 @@ import { PanelContainerModel, PanelRawitemModel, PanelFieldModel, PanelControlMo
*/
export class Util {
/**
* @description PluralizeRule对象
* @private
* @type {PluralizeRule}
* @memberof Util
*/
public static pluralizeRule: PluralizeRule = new PluralizeRule();
/**
* 创建 UUID
*
......@@ -292,6 +299,29 @@ export class Util {
return _str;
}
/**
* 计算单词复数
*
* @static
* @returns {string}
* @memberof Util
*/
public static srfpluralize(word: string) {
let wordStr = word.trim().toLowerCase();
if (wordStr.length == 0) {
return wordStr;
}
if (this.pluralizeRule.isUncountable(wordStr)) {
return wordStr;
}
const newWordStr = this.pluralizeRule.irregularChange(wordStr);
if (newWordStr) {
return newWordStr;
} else {
return this.pluralizeRule.ruleChange(wordStr);
}
}
/**
* 附加参数格式化
*
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册