我正在寻找一种将snake_case
转换为PascalCase
同时保持斜线不变的JS方法。
// examples:
post -> Post
admin_post -> AdminPost
admin_post/new -> AdminPost/New
admin_post/delete_post -> AdminPost/DeletePost
等
我有一些内容可以将snake_case
转换为camelCase
并保留斜线,但我无法将其转换为PascalCase
这是我到目前为止所得到的:
_snakeToPascal(string){
return string.replace(/(_\w)/g, (m) => {
return m[1].toUpperCase();
});
}
感谢任何建议!
这是我最终使用的内容。如果你使用它,请注意我正在使用this._upperFirst
,因为我在课堂上使用它。它有点油腻,但它有效。
_snakeToPascal(string){
return string.split('_').map((str) => {
return this._upperFirst(
str.split('/')
.map(this._upperFirst)
.join('/'));
}).join('');
}
_upperFirst(string) {
return string.slice(0, 1).toUpperCase() + string.slice(1, string.length);
}
答案 0 :(得分:1)
这应该可以解决问题。
function _snake2Pascal( str ){
str +='';
str = str.split('_');
for(var i=0;i<str.length;i++){
str[i] = str[i].slice(0,1).toUpperCase() + str[i].slice(1,str[i].length);
}
return str.join('');
}
编辑:
传递OP中显示的所有测试用例的版本:
function snake2Pascal( str ){
str +='';
str = str.split('_');
function upper( str ){
return str.slice(0,1).toUpperCase() + str.slice(1,str.length);
}
for(var i=0;i<str.length;i++){
var str2 = str[i].split('/');
for(var j=0;j<str2.length;j++){
str2[j] = upper(str2[j]);
}
str[i] = str2.join('');
}
return str.join('');
}
答案 1 :(得分:1)
这是一个保留斜线并将snake_case转换为PascalCase的解决方案。
setwd("D:/Santander")
## import train dataset
train<-read.csv("train.csv",header=T)
dim(train)
summary(train)
str(train)
prop.table(table(train2$TARGET))
stats<-function(x){
length<-length(x)
nmiss<-sum(is.na(x))
y<-x[!is.na(x)]
freq<-as.data.frame(table(y))
max_freq<-max(freq[,2])/length
min<-min(y)
median<-median(y)
max<-max(y)
mean<-mean(y)
freq<-length(unique(y))
return(c(nmiss=nmiss,min=min,median=median,mean=mean,max=max,freq=freq,max_freq=max_freq))
}
var_stats<-sapply(train,stats)
var_stats_1<-t(var_stats)
###将最大频数类别比例超过0.9999,其它类别小于1/10000的变量全删除
exclude_var<-rownames(var_stats_1)[var_stats_1[,7]>0.9999]
train2<-train[,! colnames(train) %in% c(exclude_var,"ID")]
rm(list=setdiff(ls(),"train2"))
train2<-train2[1:10000,]
write.csv(train2,"example data.csv",row.names = F)
##随机将数据分为训练集与测试集
set.seed(1)
ind<-sample(c(1,2),size=nrow(train2),replace=T,prob=c(0.8,0.2))
train2$TARGET<-factor(train2$TARGET)
train_set<-train2[ind==1,]
test_set<-train2[ind==2,]
rm(train2)
##1\用R randomForest构建预测模型 100棵树
library(randomForest)
memory.limit(4000)
random<-randomForest(TARGET~.,data=train_set,ntree=50)
print(random)
random.importance<-importance(random)
p_train<-predict(random,train_set,type="prob")
pred.auc<-prediction(p_train[,2],train_set$TARGET)
performance(pred.auc,"auc")
##train_set auc=0.8177
## predict test_set
p_test<-predict(random,newdata = test_set,type="prob")
pred.auc<-prediction(p_test[,2],test_set$TARGET)
performance(pred.auc,"auc")
##test_set auc=0.60
#________________________________________________#
##_________h2o.randomForest_______________
library(h2o)
h2o.init()
train.h2o<-as.h2o(train_set)
test.h2o<-as.h2o(test_set)
random.h2o<-h2o.randomForest(,"TARGET",training_frame = train.h2o,ntrees=50)
importance.h2o<-h2o.varimp(random.h2o)
p_train.h2o<-as.data.frame(h2o.predict(random.h2o,train.h2o))
pred.auc<-prediction(p_train.h2o$p1,train_set$TARGET)
performance(pred.auc,"auc")
##auc=0.9388, bigger than previous one
###test_set prediction
p_test.h2o<-as.data.frame(h2o.predict(random.h2o,test.h2o))
pred.auc<-prediction(p_test.h2o$p1,test_set$TARGET)
performance(pred.auc,"auc")
###auc=0.775
它首先将输入拆分为const snakeToPascal = (string) => {
return string.split("/")
.map(snake => snake.split("_")
.map(substr => substr.charAt(0)
.toUpperCase() +
substr.slice(1))
.join(""))
.join("/");
};
个字符,以生成需要转换的snake_case字符串数组。然后它将这些字符串拆分为'/'
个字符以生成一个子字符串数组。然后将此数组中的每个子字符串大写,然后重新连接到单个PascalCase字符串中。然后,PascalCase字符串将由分隔它们的'_'
字符重新加入。
答案 2 :(得分:0)
或类似的东西:
function snake2CamelCase(string) {
return string
.replace(
/_(\w)/g,
($, $1) => $1.toUpperCase()
)
;
}
function snake2PascalCase(string) {
let s = snake2CamelCase(string);
return `${s.charAt(0).toUpperCase()}${s.substr(1)}`;
}
[
'something_went_wrong',
'thisIs_my_snakecase'
]
.map(s => ({[s]: snake2PascalCase(s)}))
.forEach((s, i) => console.log(i, s))
;
&#13;
答案 3 :(得分:0)
const snakeToCamel = str => str.replace( /([-_]\w)/g, g => g[ 1 ].toUpperCase() );
const snakeToPascal = str => {
let camelCase = snakeToCamel( str );
let pascalCase = camelCase[ 0 ].toUpperCase() + camelCase.substr( 1 );
return pascalCase;
}
console.log( snakeToPascal( "i_call_shop_session" ) );
输入:i_call_shop_session
输出:ICallShopSession
答案 4 :(得分:0)
const toString = (snake_case_str) => {
const newStr = snake_case_str.replace(/([-_][a-z])/gi, ($1) => {
return $1.toUpperCase().replace('-', ' ').replace('_', ' ');
});
let changedStr =
newStr.slice(0, 1).toUpperCase() + newStr.slice(1, newStr.length);
return changedStr;
};
let str = 'first_name';
console.log(toString(str));