From 7083e87871973c3582e04758495fac24f43a1839 Mon Sep 17 00:00:00 2001 From: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> Date: Mon, 22 Apr 2024 17:51:13 +0200 Subject: [PATCH] update Drafts to format number to text (#22) * build: add to-words dependency Signed-off-by: ahmad-kashkoush --- package-lock.json | 10 ++++++++ package.json | 1 + src/drafting/Double/format.ts | 45 ++++++++++++++++++++-------------- src/drafting/Integer/format.ts | 35 ++++++++++++++++---------- test/DraftFormat.test.ts | 14 +++++++++++ 5 files changed, 73 insertions(+), 32 deletions(-) create mode 100644 test/DraftFormat.test.ts diff --git a/package-lock.json b/package-lock.json index f775b55..2f564e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -42,6 +42,7 @@ "rollup-plugin-dts": "^5.3.0", "rollup-plugin-esbuild": "^5.0.0", "terser": "^5.16.6", + "to-words": "^4.0.1", "ts-jest": "^28.0.4" }, "engines": { @@ -8049,6 +8050,15 @@ "node": ">=8.0" } }, + "node_modules/to-words": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/to-words/-/to-words-4.0.1.tgz", + "integrity": "sha512-HzVs4DbkPk1nJsBnllotj44tEXc5SIZIKpSwRanNe1fy1hFbBgipZSOmNYVvmZQMkQfBQf9mA6F6XbNU2QuA0A==", + "dev": true, + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/tough-cookie": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.1.3.tgz", diff --git a/package.json b/package.json index 3acf3e7..93fa2b8 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,7 @@ "rollup-plugin-dts": "^5.3.0", "rollup-plugin-esbuild": "^5.0.0", "terser": "^5.16.6", + "to-words": "^4.0.1", "ts-jest": "^28.0.4" }, "license-check-and-add-config": { diff --git a/src/drafting/Double/format.ts b/src/drafting/Double/format.ts index 6b5e847..52d0f34 100644 --- a/src/drafting/Double/format.ts +++ b/src/drafting/Double/format.ts @@ -11,7 +11,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +import {ToWords} from 'to-words'; +import { DraftFormat } from '../Integer/format'; /** * Creates a drafter for Double * @param {number} value - the double @@ -28,23 +29,29 @@ export function draftDoubleIEEE(value:number) : string { /** * Creates a drafter for a formatted Double * @param {number} value - the Double - * @param {string} format - the format - * @returns {object} the parser + * @param {string|DraftFormat} format - the format + * @returns {string} formatted double value as string */ -export function draftDoubleFormat(value:number,format:string) : string { - return format.replace(/0(.)0((.)(0+))?/gi, function(_a,sep1,_b,sep2,digits){ - const len = digits ? digits.length : 0; - const vs = value.toFixed(len); - let res = ''; - if (sep2) { - const d = vs.substring(vs.length - len); - res += sep2 + d; - } - let i = vs.substring(0,vs.length - (len === 0 ? 0 : len+1)); - while (i.length > 3) { - res = sep1 + i.substring(i.length - 3) + res; - i = i.substring(0, i.length - 3); - } - return i + res; - }); +export function draftDoubleFormat(value:number,format:string|DraftFormat=DraftFormat.NUMBER) : string { + if(format===DraftFormat.TEXT) { + const converter:ToWords = new ToWords(); + const res:string=converter.convert(value); + return res; + } else { + return format.replace(/0(.)0((.)(0+))?/gi, function(_a,sep1,_b,sep2,digits){ + const len = digits ? digits.length : 0; + const vs = value.toFixed(len); + let res = ''; + if (sep2) { + const d = vs.substring(vs.length - len); + res += sep2 + d; + } + let i = vs.substring(0,vs.length - (len === 0 ? 0 : len+1)); + while (i.length > 3) { + res = sep1 + i.substring(i.length - 3) + res; + i = i.substring(0, i.length - 3); + } + return i + res; + }); + } } \ No newline at end of file diff --git a/src/drafting/Integer/format.ts b/src/drafting/Integer/format.ts index 10dea08..90f0cd2 100644 --- a/src/drafting/Integer/format.ts +++ b/src/drafting/Integer/format.ts @@ -12,6 +12,9 @@ * limitations under the License. */ +import {ToWords} from 'to-words'; +export enum DraftFormat{NUMBER='', TEXT='word'} + /** * Creates a drafter for Integer * @param {number} value - the integer @@ -24,18 +27,24 @@ export function draftInteger(value:number) : string { /** * Creates a drafter for a formatted Integer * @param {number} value - the Integer - * @param {string} format - the format - * @returns {object} the parser + * @param {string|DraftFormat} format - the format + * @returns {string} formatted integer value as string */ -export function draftIntegerFormat(value:number,format:string) : string { - return format.replace(/0(.)0/gi, function(_a,sep1){ - const vs = value.toFixed(0); - let res = ''; - let i = vs.substring(0,vs.length); - while (i.length > 3) { - res = sep1 + i.substring(i.length - 3) + res; - i = i.substring(0, i.length - 3); - } - return i + res; - }); +export function draftIntegerFormat(value:number,format:string|DraftFormat=DraftFormat.NUMBER) : string { + if (format === DraftFormat.TEXT) { + const converter:ToWords = new ToWords(); + const res:string=converter.convert(value); + return res; + } else { + return format.replace(/0(.)0/gi, function(_a,sep1){ + const vs = value.toFixed(0); + let res = ''; + let i = vs.substring(0,vs.length); + while (i.length > 3) { + res = sep1 + i.substring(i.length - 3) + res; + i = i.substring(0, i.length - 3); + } + return i + res; + }); + } } \ No newline at end of file diff --git a/test/DraftFormat.test.ts b/test/DraftFormat.test.ts new file mode 100644 index 0000000..60ba7ad --- /dev/null +++ b/test/DraftFormat.test.ts @@ -0,0 +1,14 @@ +import { getDrafter } from '../src/drafting'; + +describe('number to words format', ()=>{ + test('should format integer to words', ()=>{ + const drafter:any=getDrafter('Integer'); + expect(drafter(123,'word' )).toBe('One Hundred Twenty Three'); + expect(drafter(123456,'word' )).toBe('One Lakh Twenty Three Thousand Four Hundred Fifty Six'); + }); + test('should format double to words', ()=>{ + const drafter:any=getDrafter('Double'); + expect(drafter(123.045,'word' )).toBe('One Hundred Twenty Three Point Zero Four Five'); + expect(drafter(1234.045,'word' )).toBe('One Thousand Two Hundred Thirty Four Point Zero Four Five'); + }); +}); \ No newline at end of file