diff --git a/dev/index.ts b/dev/index.ts index 2389089..5e28c45 100644 --- a/dev/index.ts +++ b/dev/index.ts @@ -2,15 +2,8 @@ import Currency from "../src"; console.log( - Currency.stringify('3', { - isCurrency: false, - lang: Currency.Language.zh_sc - }) - + - '点' - + - Currency.stringify('45', { - isCurrency: false, + Currency.stringify('0.5', { + isCurrency: true, lang: Currency.Language.zh_sc }) // 8 0000 0001 diff --git a/dist/nomoney.min.js b/dist/nomoney.min.js index 0cd3aa4..46fe3ff 100644 --- a/dist/nomoney.min.js +++ b/dist/nomoney.min.js @@ -1 +1 @@ -!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.nomoney=n():e.nomoney=n()}(self,(function(){return(()=>{"use strict";var e,n={d:(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},o:(e,n)=>Object.prototype.hasOwnProperty.call(e,n)},t={};n.d(t,{default:()=>d}),function(e){e[e.zh_sc=0]="zh_sc",e[e.zh_cn=1]="zh_cn",e[e.tw_cn=2]="tw_cn",e[e.hk_cn=3]="hk_cn"}(e||(e={}));var r=["","萬","億","兆","京","垓","杼","穰","溝","澗","正","載","極","恒河沙","阿僧祇","那由他","不可思議","無量","大數"],u={num:{one:"壹",two:"贰",three:"叁",four:"肆",five:"伍",six:"陆",seven:"柒",eight:"捌",nigh:"玖",zero:"零",full:"整",and:"零",yuan:"元",jiao:"角",fen:"分",point:"点"},group:{unit:["","万","亿","兆","京","垓","杼","穰","沟","涧","正","载","极","恒河沙","阿僧祇","那由他","不可思议","无量","大数"],base:["拾","佰","仟"]}},o={num:{one:"壹",two:"贰",three:"叁",four:"肆",five:"伍",six:"陆",seven:"柒",eight:"捌",nigh:"玖",zero:"零",full:"整",and:"零",yuan:"元",jiao:"角",fen:"分",point:"点"},group:{unit:["","万","亿","兆","京","垓","杼","穰","沟","涧","正","载","极","恒河沙","阿僧祇","那由他","不可思议","无量","大数"],base:["拾","佰","仟"]}};o.num.six="陸",o.num.full="正",o.num.point="點",o.group.unit=r;var i={num:{one:"壹",two:"贰",three:"叁",four:"肆",five:"伍",six:"陆",seven:"柒",eight:"捌",nigh:"玖",zero:"零",full:"整",and:"零",yuan:"元",jiao:"角",fen:"分",point:"点"},group:{unit:["","万","亿","兆","京","垓","杼","穰","沟","涧","正","载","极","恒河沙","阿僧祇","那由他","不可思议","无量","大数"],base:["拾","佰","仟"]}};i.num.six="陸",o.num.point="點",i.group.unit=r;var a={num:{one:"壹",two:"贰",three:"叁",four:"肆",five:"伍",six:"陆",seven:"柒",eight:"捌",nigh:"玖",zero:"零",full:"整",and:"零",yuan:"元",jiao:"角",fen:"分",point:"点"},group:{unit:["","万","亿","兆","京","垓","杼","穰","沟","涧","正","载","极","恒河沙","阿僧祇","那由他","不可思议","无量","大数"],base:["拾","佰","仟"]}};function s(n){switch(n){case e.zh_cn:return u;case e.hk_cn:return o;case e.tw_cn:return i;case e.zh_sc:return a;default:return u}}function f(e,n){for(var t=0;t0&&r.push(u.substring(0,i));for(var a=0;a0&&void 0!==arguments[0]?arguments[0]:{};return Object.assign({lang:e.zh_cn,isCurrency:!0},n)}))}]),n}(),v=function(){function n(){p(this,n)}return y(n,null,[{key:"Language",get:function(){return e}},{key:"stringify",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!e&&"number"!=typeof e)return console.warn("please pass in the correct parameters, support numbers or strings"),"";var r=b.currencyStringifyConfig(t),u=new h(s(r.lang));if(e.toString().length>u.getMaxLength())return console.warn("maximum length is"+u.getMaxLength().toString()),"";if(!n.isValid(e))return console.warn("number or string that does not meet the rules"),"";if(c.getIsZeroString(e.toString()))return r.isCurrency?u.num.zero+u.num.yuan+u.num.full:u.num.zero;var o=c.splitMoney(e),i=c.formatMoon(o.intArea,u);if(r.isCurrency){var a=u.num.yuan,f=u.num.full;if(o.doubleArea){var l=c.formatSun(o.doubleArea,i,u);return l?(i?i+a:"")+l:(i?i+a:"")+f}return i?i+a+f:""}var g=u.num.point;if(o.doubleArea){var p=u.getBigNumberFormString(o.doubleArea);return p?r.isCurrency||i?(i?i+g:"")+p:u.num.zero+g+p:i}return i}},{key:"isValid",value:function(e){return c.isNumber(e)}},{key:"capital",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!e)return console.warn("please pass in the correct parameters, support numbers or strings"),"";var r=b.currencyStringifyConfig(t),u=new h(s(r.lang));return n.isValid(e)?u.getBigNumberFormString(e.toString()):(console.warn("number or string that does not meet the rules"),"")}}]),n}();const d=v;return t.default})()})); \ No newline at end of file +!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.nomoney=n():e.nomoney=n()}(self,(function(){return(()=>{"use strict";var e,n={d:(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},o:(e,n)=>Object.prototype.hasOwnProperty.call(e,n)},t={};n.d(t,{default:()=>d}),function(e){e[e.zh_sc=0]="zh_sc",e[e.zh_cn=1]="zh_cn",e[e.tw_cn=2]="tw_cn",e[e.hk_cn=3]="hk_cn"}(e||(e={}));var r=["","萬","億","兆","京","垓","杼","穰","溝","澗","正","載","極","恒河沙","阿僧祇","那由他","不可思議","無量","大數"],u={num:{one:"壹",two:"贰",three:"叁",four:"肆",five:"伍",six:"陆",seven:"柒",eight:"捌",nigh:"玖",zero:"零",full:"整",and:"零",yuan:"元",jiao:"角",fen:"分",point:"点"},group:{unit:["","万","亿","兆","京","垓","杼","穰","沟","涧","正","载","极","恒河沙","阿僧祇","那由他","不可思议","无量","大数"],base:["拾","佰","仟"]}},o={num:{one:"壹",two:"贰",three:"叁",four:"肆",five:"伍",six:"陆",seven:"柒",eight:"捌",nigh:"玖",zero:"零",full:"整",and:"零",yuan:"元",jiao:"角",fen:"分",point:"点"},group:{unit:["","万","亿","兆","京","垓","杼","穰","沟","涧","正","载","极","恒河沙","阿僧祇","那由他","不可思议","无量","大数"],base:["拾","佰","仟"]}};o.num.six="陸",o.num.full="正",o.num.point="點",o.group.unit=r;var i={num:{one:"壹",two:"贰",three:"叁",four:"肆",five:"伍",six:"陆",seven:"柒",eight:"捌",nigh:"玖",zero:"零",full:"整",and:"零",yuan:"元",jiao:"角",fen:"分",point:"点"},group:{unit:["","万","亿","兆","京","垓","杼","穰","沟","涧","正","载","极","恒河沙","阿僧祇","那由他","不可思议","无量","大数"],base:["拾","佰","仟"]}};i.num.six="陸",o.num.point="點",i.group.unit=r;var a={num:{one:"壹",two:"贰",three:"叁",four:"肆",five:"伍",six:"陆",seven:"柒",eight:"捌",nigh:"玖",zero:"零",full:"整",and:"零",yuan:"元",jiao:"角",fen:"分",point:"点"},group:{unit:["","万","亿","兆","京","垓","杼","穰","沟","涧","正","载","极","恒河沙","阿僧祇","那由他","不可思议","无量","大数"],base:["拾","佰","仟"]}};function s(n){switch(n){case e.zh_cn:return u;case e.hk_cn:return o;case e.tw_cn:return i;case e.zh_sc:return a;default:return u}}function f(e,n){for(var t=0;t0&&r.push(u.substring(0,i));for(var a=0;a0&&void 0!==arguments[0]?arguments[0]:{};return Object.assign({lang:e.zh_cn,isCurrency:!0},n)}))}]),n}(),v=function(){function n(){p(this,n)}return y(n,null,[{key:"Language",get:function(){return e}},{key:"stringify",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=b.currencyStringifyConfig(t),u=new h(s(r.lang)),o=u.num.yuan,i=u.num.full;if(!e&&"number"!=typeof e)return r.isCurrency?u.num.zero+o+i:u.num.zero;if(e.toString().length>u.getMaxLength())return console.warn("maximum length is"+u.getMaxLength().toString()),"";if(!n.isValid(e))return console.warn("number or string that does not meet the rules"),"";if(c.getIsZeroString(e.toString()))return r.isCurrency?u.num.zero+u.num.yuan+u.num.full:u.num.zero;var a=c.splitMoney(e),f=c.formatMoon(a.intArea,u);if(r.isCurrency){if(a.doubleArea){var l=c.formatSun(a.doubleArea,f,u);return l?f?(f?f+o:"")+l:(f?f+o:"")+l+i:f?(f?f+o:"")+i:u.num.zero+o+i}return f?f+o+i:""}var g=u.num.point;if(a.doubleArea){var p=u.getBigNumberFormString(a.doubleArea);return p?r.isCurrency||f?(f?f+g:"")+p:u.num.zero+g+p:f}return f}},{key:"isValid",value:function(e){return c.isNumber(e)}},{key:"capital",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!e)return console.warn("please pass in the correct parameters, support numbers or strings"),"";var r=b.currencyStringifyConfig(t),u=new h(s(r.lang));return n.isValid(e)?u.getBigNumberFormString(e.toString()):(console.warn("number or string that does not meet the rules"),"")}}]),n}();const d=v;return t.default})()})); \ No newline at end of file diff --git a/lib/core/currency.js b/lib/core/currency.js index 60514c9..d87c539 100644 --- a/lib/core/currency.js +++ b/lib/core/currency.js @@ -29,12 +29,16 @@ var Currency = /** @class */ (function () { // 转换数字货币或者字符串货币 为大写 Currency.stringify = function (rmb, config) { if (config === void 0) { config = {}; } - if (!rmb && typeof rmb !== "number") { - console.warn("please pass in the correct parameters, support numbers or strings"); - return ""; - } var inputConfig = DefaultOptions.currencyStringifyConfig(config); var cnCurrency = new currencyBuild_1.default((0, cn_1.default)(inputConfig.lang)); + var yuan = cnCurrency.num.yuan; + var full = cnCurrency.num.full; + if (!rmb && typeof rmb !== "number") { + if (inputConfig.isCurrency) { + return cnCurrency.num.zero + yuan + full; + } + return cnCurrency.num.zero; + } if (rmb.toString().length > cnCurrency.getMaxLength()) { console.warn("maximum length is" + cnCurrency.getMaxLength().toString()); return ""; @@ -52,15 +56,21 @@ var Currency = /** @class */ (function () { var parseLine = string_help_1.default.splitMoney(rmb); var betterMoon = string_help_1.default.formatMoon(parseLine.intArea, cnCurrency); if (inputConfig.isCurrency) { - var yuan = cnCurrency.num.yuan; - var full = cnCurrency.num.full; if (parseLine.doubleArea) { var doubleBack = string_help_1.default.formatSun(parseLine.doubleArea, betterMoon, cnCurrency); + // 如果有小数 if (doubleBack) { - return (betterMoon ? betterMoon + yuan : "") + doubleBack; + if (betterMoon) { + return (betterMoon ? betterMoon + yuan : "") + doubleBack; + } + return (betterMoon ? betterMoon + yuan : "") + doubleBack + full; } else { - return (betterMoon ? betterMoon + yuan : "") + full; + // 没有小数 + if (betterMoon) { + return (betterMoon ? betterMoon + yuan : "") + full; + } + return cnCurrency.num.zero + yuan + full; } } else { diff --git a/lib/core/currency.js.map b/lib/core/currency.js.map index 5eec967..6359112 100644 --- a/lib/core/currency.js.map +++ b/lib/core/currency.js.map @@ -1 +1 @@ -{"version":3,"file":"currency.js","sourceRoot":"","sources":["../../src/core/currency.ts"],"names":[],"mappings":";;AAAA,gCAA2B;AAC3B,iDAA2C;AAC3C,yDAA2E;AAC3E,uDAAkD;AAElD;IAAA;IAYA,CAAC;IAXQ,sCAAuB,GAA9B,UACE,MAAoC;QAApC,uBAAA,EAAA,WAAoC;QAEpC,OAAO,MAAM,CAAC,MAAM,CAClB;YACE,IAAI,EAAE,yBAAQ,CAAC,KAAK;YACpB,UAAU,EAAE,IAAI;SACjB,EACD,MAAM,CACP,CAAC;IACJ,CAAC;IACH,qBAAC;AAAD,CAAC,AAZD,IAYC;AAED;IAAA;IA0GA,CAAC;IAzGC,sBAAW,oBAAQ;aAAnB;YACE,OAAO,yBAAQ,CAAC;QAClB,CAAC;;;OAAA;IAED,oBAAoB;IACb,kBAAS,GAAhB,UACE,GAAoB,EACpB,MAAoC;QAApC,uBAAA,EAAA,WAAoC;QAEpC,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YACnC,OAAO,CAAC,IAAI,CACV,mEAAmE,CACpE,CAAC;YACF,OAAO,EAAE,CAAC;SACX;QAED,IAAM,WAAW,GAAG,cAAc,CAAC,uBAAuB,CAAC,MAAM,CAAC,CAAC;QACnE,IAAM,UAAU,GAAG,IAAI,uBAAa,CAAC,IAAA,YAAE,EAAC,WAAW,CAAC,IAAI,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F;IACH,CAAC;IAED,aAAa;IACN,gBAAO,GAAd,UAAe,GAAoB;QACjC,OAAO,qBAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IAClC,CAAC;IAED,uBAAuB;IAChB,gBAAO,GAAd,UACE,GAAoB,EACpB,MAAoC;QAApC,uBAAA,EAAA,WAAoC;QAEpC,IAAI,CAAC,GAAG,EAAE;YACR,OAAO,CAAC,IAAI,CACV,mEAAmE,CACpE,CAAC;YACF,OAAO,EAAE,CAAC;SACX;QAED,IAAM,WAAW,GAAG,cAAc,CAAC,uBAAuB,CAAC,MAAM,CAAC,CAAC;QACnE,IAAM,UAAU,GAAG,IAAI,uBAAa,CAAC,IAAA,YAAE,EAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;QAE3D,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YAC1B,OAAO,CAAC,IAAI,CAAC,+CAA+C,CAAC,CAAC;YAC9D,OAAO,EAAE,CAAC;SACX;QAED,OAAO,UAAU,CAAC,sBAAsB,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC3D,CAAC;IACH,eAAC;AAAD,CAAC,AApHD,IAoHC;AAED,kBAAe,QAAQ,CAAC"} \ No newline at end of file diff --git a/package.json b/package.json index 6285d93..6edfb6e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nomoney", - "version": "1.0.3", + "version": "1.0.4", "description": "人民币大写格式化,数字大写格式化。", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/core/currency.ts b/src/core/currency.ts index 546e9b4..28a855c 100644 --- a/src/core/currency.ts +++ b/src/core/currency.ts @@ -27,16 +27,20 @@ class Currency { rmb: string | number, config: currencyStringifyConfig = {} ): string { - if (!rmb && typeof rmb !== "number") { - console.warn( - "please pass in the correct parameters, support numbers or strings" - ); - return ""; - } const inputConfig = DefaultOptions.currencyStringifyConfig(config); const cnCurrency = new currencyBuild(cn(inputConfig.lang)); + const yuan = cnCurrency.num.yuan; + const full = cnCurrency.num.full; + + if (!rmb && typeof rmb !== "number") { + if (inputConfig.isCurrency) { + return cnCurrency.num.zero + yuan + full; + } + return cnCurrency.num.zero; + } + if (rmb.toString().length > cnCurrency.getMaxLength()) { console.warn("maximum length is" + cnCurrency.getMaxLength().toString()); return ""; @@ -58,8 +62,6 @@ class Currency { const betterMoon = StringHelp.formatMoon(parseLine.intArea, cnCurrency); if (inputConfig.isCurrency) { - const yuan = cnCurrency.num.yuan; - const full = cnCurrency.num.full; if (parseLine.doubleArea) { const doubleBack = StringHelp.formatSun( parseLine.doubleArea, @@ -67,10 +69,18 @@ class Currency { cnCurrency ); + // 如果有小数 if (doubleBack) { - return (betterMoon ? betterMoon + yuan : "") + doubleBack; + if (betterMoon) { + return (betterMoon ? betterMoon + yuan : "") + doubleBack; + } + return (betterMoon ? betterMoon + yuan : "") + doubleBack + full; } else { - return (betterMoon ? betterMoon + yuan : "") + full; + // 没有小数 + if (betterMoon) { + return (betterMoon ? betterMoon + yuan : "") + full; + } + return cnCurrency.num.zero + yuan + full; } } else { return betterMoon ? betterMoon + yuan + full : ""; diff --git a/test/index.ts b/test/index.ts index 837fa9b..775aeaa 100644 --- a/test/index.ts +++ b/test/index.ts @@ -5,6 +5,9 @@ describe("nomoney test", function() { // 基础测试 it("基础测试", function() { expect(Currency.stringify(0)).toBe('零元整') + expect(Currency.stringify('0.00001')).toBe('零元整') + expect(Currency.stringify('0.5')).toBe('伍角整') + expect(Currency.stringify('0.05')).toBe('伍分整') expect(Currency.stringify('0')).toBe('零元整') expect(Currency.stringify('1')).toBe('壹元整') expect(Currency.stringify('2')).toBe('贰元整') @@ -36,10 +39,10 @@ describe("nomoney test", function() { it("非常规测试", function() { - expect(Currency.stringify('')).toBe('') + expect(Currency.stringify('')).toBe('零元整') expect(Currency.stringify('000000000000000000')).toBe('零元整') expect(Currency.stringify('0000000000800000111')).toBe('捌亿零壹佰壹拾壹元整') - expect(Currency.stringify('.01')).toBe('壹分') + expect(Currency.stringify('.01')).toBe('壹分整') }) it("isCurrency = false", function() {