Skip to content

Commit

Permalink
add thai numeral package
Browse files Browse the repository at this point in the history
  • Loading branch information
amerharb committed May 9, 2024
1 parent d11c89d commit d559722
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/thai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Thai Changelog
<!-- https://keepachangelog.com/en/1.0.0/ -->

## [0.0.1] 2024-05-09
### Added
- Initial project covert number into Thai numerals
12 changes: 12 additions & 0 deletions packages/thai/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ISC License

Copyright 2024 Amer Harb numerals@amerharb.com

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 changes: 28 additions & 0 deletions packages/thai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# @numerals/thai

[![Version](https://img.shields.io/badge/version-0.0.1-blue.svg)](https://github.com/amerharb/numerals/tree/thai/version/0.0.1)
[![License: GPLv3](https://img.shields.io/badge/License-ISC-blue.svg)](https://opensource.org/licenses/ISC)
![Coverage](https://raw.githubusercontent.com/amerharb/numerals/thai/version/0.0.1/packages/thai/badges/coverage.svg)
![Github workflow](https://github.com/amerharb/numerals/actions/workflows/lint-test.yaml/badge.svg?branch=thai/version/0.0.1)

**@numerals/thai** is a package for converting number into Thai numeral.

`123 -> ๑๒๓`

## How to use
npm:
```shell
npm i @numerals/thai
```

yarn:
```shell
yarn add @numerals/thai
```

Type Script:
```js
import { convert } from '@numerals/thai';
console.log(convert(123)); // ๑๒๓
console.log(convert(123.45)); // ๑๒๓.๔๕
```
1 change: 1 addition & 0 deletions packages/thai/badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/thai/doc/U0E00.pdf
Binary file not shown.
14 changes: 14 additions & 0 deletions packages/thai/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true,
coverageDirectory: 'coverage',
coverageProvider: 'v8',
testMatch: [
'**/?(*.)+(test.ts)',
],
coverageReporters: [
'json-summary',
],
};
38 changes: 38 additions & 0 deletions packages/thai/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@numerals/thai",
"version": "0.0.1",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"files": [
"dist/src/**/*.d.ts",
"dist/src/**/*.js",
"LICENSE",
"CHANGELOG.md",
"README.md"
],
"author": "Amer Harb",
"repository": {
"type": "git",
"url": "git+https://github.com/amerharb/numerals.git"
},
"homepage": "https://github.com/amerharb/numerals#readme",
"url": "https://github.com/amerharb/numerals/issues",
"email": "numerals@amerharb.com",
"keywords": [
"thai-numerals",
"thai-numbers"
],
"license": "ISC",
"scripts": {
"build": "rm -rf dist && tsc",
"start": "node dist/src/index.js",
"test": "jest",
"make-coverage-badge": "npx make-coverage-badge --report-path coverage/coverage-summary.json --output-path badges/coverage.svg",
"dev": "ts-node src/index.ts",
"lint": "npx eslint . --max-warnings 0",
"lint:fix": "npx eslint . --fix"
},
"bugs": {
"url": "https://github.com/amerharb/numerals/issues"
}
}
35 changes: 35 additions & 0 deletions packages/thai/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Th } from './numerals'

const map = new Map<string, string>(
[
['0', Th['0']],
['1', Th['1']],
['2', Th['2']],
['3', Th['3']],
['4', Th['4']],
['5', Th['5']],
['6', Th['6']],
['7', Th['7']],
['8', Th['8']],
['9', Th['9']],
['-', '-'],
['.', '.'],
]
)

export function convert(source: number): string {
if (!Number.isFinite(source)) {
throw new Error('Source is not a finite number')
}
const sourceString = source.toString()
let result = ''
for (const letter of sourceString) {
const u = map.get(letter) ?? throwNoNumeralFor(letter)
result += u
}
return result
}

function throwNoNumeralFor(letter: string): never {
throw new Error(`No numeral for "${letter}"`)
}
25 changes: 25 additions & 0 deletions packages/thai/src/numerals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const numbers = [
String.fromCodePoint(0x0E50), /** #0 ๐ U+0E50 number 0*/
String.fromCodePoint(0x0E51), /** #1 ๑ U+0E51 number 1*/
String.fromCodePoint(0x0E52), /** #2 ๒ U+0E52 number 2*/
String.fromCodePoint(0x0E53), /** #3 ๓ U+0E53 number 3*/
String.fromCodePoint(0x0E54), /** #4 ๔ U+0E54 number 4*/
String.fromCodePoint(0x0E55), /** #5 ๕ U+0E55 number 5*/
String.fromCodePoint(0x0E56), /** #6 ๖ U+0E56 number 6*/
String.fromCodePoint(0x0E57), /** #7 ๗ U+0E57 number 7*/
String.fromCodePoint(0x0E58), /** #8 ๘ U+0E58 number 8*/
String.fromCodePoint(0x0E59), /** #9 ๙ U+0E59 number 9*/
] as const

export const Th = {
'0': numbers[0],
'1': numbers[1],
'2': numbers[2],
'3': numbers[3],
'4': numbers[4],
'5': numbers[5],
'6': numbers[6],
'7': numbers[7],
'8': numbers[8],
'9': numbers[9],
}
26 changes: 26 additions & 0 deletions packages/thai/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { convert } from '../src'
import { describe, expect, it } from '@jest/globals'

describe('convert()', () => {
it('convert 1234567890 into ๑๒๓๔๕๖๗๘๙๐', () => {
const actual = convert(1234567890)
expect(actual).toEqual('๑๒๓๔๕๖๗๘๙๐')
})
it('convert 123.456 into ๑๒๓.๔๕๖', () => {
const actual = convert(123.456)
expect(actual).toEqual('๑๒๓.๔๕๖')
})
it('convert -1 into -๑', () => {
const actual = convert(-1)
expect(actual).toEqual('-๑')
})
it('convert 0 into ๐', () => {
const actual = convert(0)
expect(actual).toEqual('๐')
})
it.each([NaN, Infinity, -Infinity])
('throw Error for non number [%s]', (it) => {
const actual = () => convert(it)
expect(actual).toThrowError('Source is not a finite number')
})
})
6 changes: 6 additions & 0 deletions packages/thai/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
}
}

0 comments on commit d559722

Please sign in to comment.