Skip to content

Commit

Permalink
update Drafts to format number to text (#22)
Browse files Browse the repository at this point in the history
* build: add to-words dependency

Signed-off-by: ahmad-kashkoush <[email protected]>
  • Loading branch information
ahmad-kashkoush authored Apr 22, 2024
1 parent 6f7d0fb commit 7083e87
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 32 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
45 changes: 26 additions & 19 deletions src/drafting/Double/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
});
}
}
35 changes: 22 additions & 13 deletions src/drafting/Integer/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
});
}
}
14 changes: 14 additions & 0 deletions test/DraftFormat.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit 7083e87

Please sign in to comment.