inflect java_驼峰、下划线、单复数、表名-类名转换...通通交给字符串处理库:i...
經(jīng)常會有字符串轉(zhuǎn)換處理的場景,比如一個實體user,對應的Restful資源名是復數(shù)users,數(shù)據(jù)庫表名也是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'
自定義規(guī)則
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'
前后端通用哦...就這樣吧。
總結(jié)
以上是生活随笔為你收集整理的inflect java_驼峰、下划线、单复数、表名-类名转换...通通交给字符串处理库:i...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab 子函数怎么写,matlab
- 下一篇: 【办公基本软件】万彩办公大师教程丨批量文