經(jīng)常會(huì)有字符串轉(zhuǎn)換處理的場(chǎng)景,比如一個(gè)實(shí)體user,對(duì)應(yīng)的Restful資源名是復(fù)數(shù)users,數(shù)據(jù)庫(kù)表名也是users,模型類名可能是User等等,這就需要一個(gè)方便的npm包,名字有點(diǎn)奇怪,不過(guò)它就是一個(gè)單字母:i
npm: https://www.npmjs.com/package/i
cnpm install i --save使用
不一一列舉了,看名字就知道了
var inflect = require("i")(); var inflect = require("i")(true); //如果是傳一個(gè)`true`的話就相當(dāng)于這些方法都加在了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
可以使用正則表達(dá)式來(lái)定義
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...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/17592.html
摘要:如為,小數(shù)點(diǎn)后兩位是,存在四舍五入。在中,有一個(gè)小坑,就是并不是真正的四舍五入如圖而可以這樣的總結(jié)為在奇數(shù)的時(shí)候,是四舍五入,在偶數(shù)的時(shí)候是五舍六入函數(shù)執(zhí)行后輸出的提示返回的是數(shù)據(jù)類型是,所以有時(shí)需要進(jìn)行類型轉(zhuǎn)換,這樣的函數(shù)。 Python語(yǔ)言的概述 Pyhthon 是 蟒蛇的意思。python語(yǔ)言的擁有者是 Python Software Foundation(PSF)RSF是一個(gè)非...
摘要:最近在知乎上看到這個(gè)作為程序員,有沒(méi)有讓你感到既無(wú)語(yǔ)又崩潰的程序命名。今天,也分享下最近自己在使用的命名習(xí)慣,當(dāng)然只是個(gè)人習(xí)慣。但是兩個(gè)函數(shù)的命名,一個(gè)是,另一個(gè)是。關(guān)于的命名規(guī)范,應(yīng)該很多人都是習(xí)慣用大駝峰命名。 把一件平凡的事情做好,很平凡。把一件平凡的事情堅(jiān)持的做好,很不平凡。 1.前言 如果說(shuō)計(jì)算機(jī)科學(xué)只存在兩個(gè)難題:緩存失效和命名。那么我就覺(jué)得命名的難點(diǎn)只有兩個(gè):詞匯量和堅(jiān)持...
摘要:最近在知乎上看到這個(gè)作為程序員,有沒(méi)有讓你感到既無(wú)語(yǔ)又崩潰的程序命名。今天,也分享下最近自己在使用的命名習(xí)慣,當(dāng)然只是個(gè)人習(xí)慣。但是兩個(gè)函數(shù)的命名,一個(gè)是,另一個(gè)是。關(guān)于的命名規(guī)范,應(yīng)該很多人都是習(xí)慣用大駝峰命名。 把一件平凡的事情做好,很平凡。把一件平凡的事情堅(jiān)持的做好,很不平凡。 1.前言 如果說(shuō)計(jì)算機(jī)科學(xué)只存在兩個(gè)難題:緩存失效和命名。那么我就覺(jué)得命名的難點(diǎn)只有兩個(gè):詞匯量和堅(jiān)持...
摘要:最近在知乎上看到這個(gè)作為程序員,有沒(méi)有讓你感到既無(wú)語(yǔ)又崩潰的程序命名。今天,也分享下最近自己在使用的命名習(xí)慣,當(dāng)然只是個(gè)人習(xí)慣。但是兩個(gè)函數(shù)的命名,一個(gè)是,另一個(gè)是。關(guān)于的命名規(guī)范,應(yīng)該很多人都是習(xí)慣用大駝峰命名。 把一件平凡的事情做好,很平凡。把一件平凡的事情堅(jiān)持的做好,很不平凡。 1.前言 如果說(shuō)計(jì)算機(jī)科學(xué)只存在兩個(gè)難題:緩存失效和命名。那么我就覺(jué)得命名的難點(diǎn)只有兩個(gè):詞匯量和堅(jiān)持...
閱讀 2016·2021-09-13 10:23
閱讀 2352·2021-09-02 09:47
閱讀 3808·2021-08-16 11:01
閱讀 1227·2021-07-25 21:37
閱讀 1610·2019-08-30 15:56
閱讀 544·2019-08-30 13:52
閱讀 3138·2019-08-26 10:17
閱讀 2456·2019-08-23 18:17