Skip to content

Commit

Permalink
Use lastIndexOf() instead of split()
Browse files Browse the repository at this point in the history
  • Loading branch information
narze committed Oct 12, 2021
1 parent a9cc3ca commit 86c7882
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"devDependencies": {
"@size-limit/preset-small-lib": "^6.0.0",
"@to-da-moon/thai-baht-lib": "^0.0.10",
"baht": "^0.3.1",
"baht": "0.4.1",
"bahttext": "^2.0.2",
"husky": "^7.0.2",
"size-limit": "^6.0.0",
Expand Down
30 changes: 21 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,33 @@ export function convert(input: number | string): string | boolean {
satang = Number.isInteger(input) ? 0 : Math.floor((input * 100) % 100);
bahtStr = baht.toString();
} else if (typeof input === 'string') {
const inputNum = Number(input);
let inputNum = Number(input);

if (isNaN(inputNum)) {
return false;
}

[bahtStr, satangStr] = input.toString().split('.');

baht = Math.floor(Number(bahtStr));
satang = satangStr ? Math.floor(Number('0.' + satangStr) * 100) : 0;

if (baht < 0) {
if (inputNum < 0) {
isNegative = true;
baht = -baht;
bahtStr = baht.toString();
inputNum = -inputNum;
input = input.slice(1);
}

const inputStr = input;

let periodIdx;
if (
inputStr.includes('.') &&
(periodIdx = inputStr.lastIndexOf('.')) !== -1
) {
bahtStr = inputStr.slice(0, periodIdx);
baht = +bahtStr;
satangStr = inputStr.slice(periodIdx + 1);
satang = satangStr ? Math.floor(Number('0.' + satangStr) * 100) : 0;
} else {
baht = inputNum;
bahtStr = inputStr;
satang = 0;
}
} else {
return false;
Expand Down
19 changes: 13 additions & 6 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { convert } from '../src';
// const { ThaiBaht: thaiBahtText } = require('thai-baht-text-ts');
// import thaiBahtText from "thai-baht-text"
import { convert as bahtLatest } from 'baht';
const { ThaiBaht: thaiBahtText } = require('thai-baht-text-ts');

describe('convert', () => {
it('works', () => {
Expand All @@ -25,6 +25,9 @@ describe('convert', () => {
expect(convert(1000)).toBe('หนึ่งพันบาทถ้วน');
expect(convert(10000)).toBe('หนึ่งหมื่นบาทถ้วน');
expect(convert(100000)).toBe('หนึ่งแสนบาทถ้วน');
expect(convert(100001)).toBe('หนึ่งแสนเอ็ดบาทถ้วน');
expect(convert(100005)).toBe('หนึ่งแสนห้าบาทถ้วน');
expect(convert(100021)).toBe('หนึ่งแสนยี่สิบเอ็ดบาทถ้วน');

expect(convert(1000000)).toBe('หนึ่งล้านบาทถ้วน');
expect(convert(1002000)).toBe('หนึ่งล้านสองพันบาทถ้วน');
Expand Down Expand Up @@ -104,6 +107,10 @@ describe('convert', () => {
expect(convert(1000001)).toEqual('หนึ่งล้านเอ็ดบาทถ้วน');
expect(convert(11000001)).toEqual('สิบเอ็ดล้านเอ็ดบาทถ้วน');
expect(convert(11000000)).toEqual('สิบเอ็ดล้านบาทถ้วน');
expect(convert(21000000)).toEqual('ยี่สิบเอ็ดล้านบาทถ้วน');
expect(convert(21000010)).toEqual('ยี่สิบเอ็ดล้านสิบบาทถ้วน');
expect(convert(21000011)).toEqual('ยี่สิบเอ็ดล้านสิบเอ็ดบาทถ้วน');
expect(convert(121000011)).toEqual('หนึ่งร้อยยี่สิบเอ็ดล้านสิบเอ็ดบาทถ้วน');
});

it('should convert multiple million round to Baht', () => {
Expand Down Expand Up @@ -163,10 +170,10 @@ describe('convert', () => {
);
});

// it("equals to value from thai-baht-text-ts library", () => {
// console.log(10056518, convert(10056518), thaiBahtText(10056518))
// for (let i = 1; i < 20000000; i += 1) { // Math.floor(Math.random() * 10000)) {
// it('equals to value from other library (STRESS TEST)', () => {
// for (let i = 1; i < 20000000; i += 1) {
// expect(convert(i)).toEqual(bahtLatest(i));
// expect(convert(i)).toEqual(thaiBahtText(i));
// }
// })
// });
});
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
// noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative
"noUnusedLocals": true,
"noUnusedLocals": false,
"noUnusedParameters": true,
// use Node's module resolution algorithm, instead of the legacy TS one
"moduleResolution": "node",
Expand All @@ -30,6 +30,6 @@
// error out if import and file system have a casing mismatch. Recommended by TS
"forceConsistentCasingInFileNames": true,
// `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc`
"noEmit": true,
"noEmit": true
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2400,10 +2400,10 @@ babel-preset-jest@^26.6.2:
babel-plugin-jest-hoist "^26.6.2"
babel-preset-current-node-syntax "^1.0.0"

baht@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/baht/-/baht-0.3.1.tgz#96e555d6c07908a420d4ed740913d24ce30f1150"
integrity sha512-awrUawgctkHnDY6m4QUDDNAjPx0LI2ybsFBnOuS9sOPWx5yBoBDjH+3zKZn/dehWTcpQfSAAhA05bmS63SipKQ==
baht@0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/baht/-/baht-0.4.1.tgz#4ad15d161add807172e219c50084892c7a2bbc6b"
integrity sha512-QDe+RXdcjbbLW2FVKoTZM20q+t6D69k5n3hUCT/0y83gk7qVPaj1KCsF25nepy5S9lFb8asip7bq0SHHIUB01Q==

bahttext@^2.0.2:
version "2.0.2"
Expand Down

0 comments on commit 86c7882

Please sign in to comment.