经常会有字符串转换处理的场景,比如一个实体user,对应的Restful资源名是复数users,数据库表名也是users,模型类名可能是User等等,这就需要一个方便的npm包,名字有点奇怪,不过它就是一个单字母:i

npm: https://www.npmjs.com/package/i

安装

cnpm install i --save

使用

不一一列举了,看名字就知道了

var inflect = require('i')();

var inflect = require('i')(true);

//如果是传一个`true`的话就相当于这些方法都加在了String.prototype上,所有的字符串都可以直接用这些方法。

//'messages to store'.titleize // === 'Messages To Store'

Pluralize

inflect.pluralize('person'); // === 'people'

inflect.pluralize('octopus'); // === 'octopi'

inflect.pluralize('Hat'); // === 'Hats'

Singularize

inflect.singularize('people'); // === 'person'

inflect.singularize('octopi'); // === 'octopus'

inflect.singularize('Hats'); // === 'Hat'

Camelize

inflect.camelize('message_properties'); // === 'MessageProperties'

inflect.camelize('message_properties', false); // === 'messageProperties'

Underscore

inflect.underscore('MessageProperties'); // === 'message_properties'

inflect.underscore('messageProperties'); // === 'message_properties'

Humanize

inflect.humanize('message_id'); // === 'Message'

Dasherize

inflect.dasherize('message_properties'); // === 'message-properties'

inflect.dasherize('Message Properties'); // === 'Message Properties'

Titleize

inflect.titleize('message_properties'); // === 'Message Properties'

inflect.titleize('message properties to keep'); // === 'Message Properties to Keep'

Demodulize

inflect.demodulize('Message.Bus.Properties'); // === 'Properties'

Tableize

inflect.tableize('MessageBusProperty'); // === 'message_bus_properties'

Classify

inflect.classify('message_bus_properties'); // === 'MessageBusProperty'

Foreign key

inflect.foreign_key('MessageBusProperty'); // === 'message_bus_property_id'

inflect.foreign_key('MessageBusProperty', false); // === 'message_bus_propertyid'

Ordinalize

inflect.ordinalize( '1' ); // === '1st'

自定义规则

Custom plural

可以使用正则表达式来定义

inflect.inflections.plural('person', 'guys');

inflect.pluralize('person'); // === 'guys'

inflect.singularize('guys'); // === 'guy'

Custom singular

inflect.inflections.singular('guys', 'person')

inflect.singularize('guys'); // === 'person'

inflect.pluralize('person'); // === 'people'

Custom irregular

inflect.inflections.irregular('person', 'guys')

inflect.pluralize('person'); // === 'guys'

inflect.singularize('guys'); // === 'person'

Custom human

inflect.inflections.human(/^(.*)_cnt$/i, '$1_count');

inflect.inflections.humanize('jargon_cnt'); // === 'Jargon count'

Custom uncountable

inflect.inflections.uncountable('oil')

inflect.pluralize('oil'); // === 'oil'

inflect.singularize('oil'); // === 'oil'

前后端通用哦...就这样吧。

inflect java_驼峰、下划线、单复数、表名-类名转换...通通交给字符串处理库:i...相关推荐

  1. mysql 表名带下划线_在MySQL中可以使用下划线作为表名吗?

    是的,我们可以在表名周围使用反引号将下划线添加为表名.以下是语法-INSERT INTO `yourTableName` values(yourValue1,.......N); 让我们首先创建一个表 ...

  2. linux 下 设置 MySQL8 表名大小写不敏感方法,解决设置后无法启动 MySQL 服务的问题

    linux 下 设置 MySQL8 表名大小写不敏感方法,解决设置后无法启动 MySQL 服务的问题 参考文章: (1)linux 下 设置 MySQL8 表名大小写不敏感方法,解决设置后无法启动 M ...

  3. Spark Structured : HIve jdbc方式访问待下划线的表,找不到表的错误

    1.背景 Spark Structured : HIve jdbc方式访问待下划线的表,找不到表的错误 > select * from default._xd_after limit 1; &g ...

  4. mysql 获取一个库下的所有表名

    http://dev.mysql.com/doc/refman/5.1/zh/information-schema.html 官网上的第一个例子: mysql> SELECT table_nam ...

  5. mysql 参数名 下划线,MySQL表名称中的下划线会引起问题吗?

    不,MySQL表名中的下划线不会有任何问题.您将在MySQL表名称中出现破折号. 这是演示,使用表名的下划线没有任何问题-_StudentTrackerDemo 让我们在创建表时看到相同的内容.创建表 ...

  6. Spring Boot——MyBatis配置带下划线命名的字段自动转换驼峰命名解决方案

    问题描述 MyBatis无法查询出属性名和数据库字段名不完全相同的数据. 即:属性名和数据库字段名分别为驼峰命名和下划线命名时查出的数据为NULL. 问题分析 MyBatis默认是属性名和数据库字段名 ...

  7. php json 驼峰 下划线,Go语言json编码驼峰转下划线、下划线转驼峰

    一.需求 golang默认的结构体json转码出来,都是根据字段名生成的大写驼峰格式,但是一般我们最常用的json格式是小写驼峰或者小写下划线,因此,我非常需要一个统一的方法去转换,而不想挨个写jso ...

  8. Java下变量大小写驼峰、大小写下划线、大小写连线转换

    写在前面 想很简单,做很难,坚持更难,克服惰性.每天学一点,不会的就少一点. 养成习惯很重要,先从点赞开始吧!关注[程序员之道],前行道路不再迷茫! 有时候需要处理对象属性的getter.setter ...

  9. 使用FastJson进行驼峰下划线相互转换写法及误区

    PropertyNamingStrategy 有四种序列化方式. CamelCase策略,Java对象属性:personId,序列化后属性:persionId – 实际只改了首字母 大写变小写 Pas ...

最新文章

  1. 第一章:1.1.2 信号分类
  2. urlencode python_python爬虫之url中的中文问题
  3. mysql的SQL_NO_CACHE(在查询时不使用缓存)和sql_cache用法
  4. 基于单样本单统计推断-假设检验
  5. php实现姓名按首字母排序的类与方法
  6. SSM-水果商城-项目展示-学习-1
  7. Leetcode:Longest Substring Without Repeating Characters分析和实现
  8. PSB格式转换太麻烦?Aspose.PSD教你用代码将大型PSB文件转换为PDF/JPEG格式
  9. 计算机三级网络架构图,三级网络技术局域网基础:网络拓扑结构
  10. 不同数据类型混合运算及类型转换
  11. 2008~2009流行歌曲
  12. c语言项目实战 —— 图书管理系统
  13. mysql三表关联查询
  14. 惠普暗影精灵4P拆机换风扇
  15. html输入框只能扫描枪,输入框禁止手动输入,只允许扫描枪
  16. Windows安装乌班图(Ubuntu)教程及错误解决办法
  17. 烂泥:mysql5.5主从同步复制配置
  18. 普通大学,多年功能测试,3年进入小米,软件测试如何有效的学习?
  19. 蓝牙技术|蓝牙Mesh在照明网络上的应用
  20. html如何判断某个控件是否focus

热门文章

  1. Unity3D手游项目的总结和思考(6) - Xlua的使用心得
  2. 小程序“扫码购”的自助收银模式可以为商家带来什么?
  3. JavaWeb之02_tomcat
  4. unity3D -- 压缩图片
  5. Hadoop数据迁移工具DistCp
  6. js jquery 3D模型展示
  7. element 刷新
  8. FPGA中CDC问题
  9. 将CAD图纸转换为黑白的PDF文件要怎么操作
  10. 程序员被PUA的一天有多可怕......35 岁,真的是职场荣枯线吗?