资讯专栏INFORMATION COLUMN

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

ybak / 554人阅读

经常会有字符串转换处理的场景,比如一个实体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"

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

原文: https://adonis-china.org/post...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/17592.html

相关文章

  • 【Python3学习】走进Python

    摘要:如为,小数点后两位是,存在四舍五入。在中,有一个小坑,就是并不是真正的四舍五入如图而可以这样的总结为在奇数的时候,是四舍五入,在偶数的时候是五舍六入函数执行后输出的提示返回的是数据类型是,所以有时需要进行类型转换,这样的函数。 Python语言的概述 Pyhthon 是 蟒蛇的意思。python语言的拥有者是 Python Software Foundation(PSF)RSF是一个非...

    fuchenxuan 评论0 收藏0
  • [前端开发]--分享个人习惯的命名方式

    摘要:最近在知乎上看到这个作为程序员,有没有让你感到既无语又崩溃的程序命名。今天,也分享下最近自己在使用的命名习惯,当然只是个人习惯。但是两个函数的命名,一个是,另一个是。关于的命名规范,应该很多人都是习惯用大驼峰命名。 把一件平凡的事情做好,很平凡。把一件平凡的事情坚持的做好,很不平凡。 1.前言 如果说计算机科学只存在两个难题:缓存失效和命名。那么我就觉得命名的难点只有两个:词汇量和坚持...

    Rocture 评论0 收藏0
  • [前端开发]--分享个人习惯的命名方式

    摘要:最近在知乎上看到这个作为程序员,有没有让你感到既无语又崩溃的程序命名。今天,也分享下最近自己在使用的命名习惯,当然只是个人习惯。但是两个函数的命名,一个是,另一个是。关于的命名规范,应该很多人都是习惯用大驼峰命名。 把一件平凡的事情做好,很平凡。把一件平凡的事情坚持的做好,很不平凡。 1.前言 如果说计算机科学只存在两个难题:缓存失效和命名。那么我就觉得命名的难点只有两个:词汇量和坚持...

    Half 评论0 收藏0
  • [前端开发]--分享个人习惯的命名方式

    摘要:最近在知乎上看到这个作为程序员,有没有让你感到既无语又崩溃的程序命名。今天,也分享下最近自己在使用的命名习惯,当然只是个人习惯。但是两个函数的命名,一个是,另一个是。关于的命名规范,应该很多人都是习惯用大驼峰命名。 把一件平凡的事情做好,很平凡。把一件平凡的事情坚持的做好,很不平凡。 1.前言 如果说计算机科学只存在两个难题:缓存失效和命名。那么我就觉得命名的难点只有两个:词汇量和坚持...

    EastWoodYang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<