From 4b9bf307edcc3a217c979be593b8dd7027c72599 Mon Sep 17 00:00:00 2001 From: Aung Myo Kyaw Date: Mon, 16 Feb 2026 02:42:01 +0700 Subject: [PATCH] chore: improve code quality and documentation - Enhanced README.md with comprehensive API documentation, table of contents, and usage examples - Added .gitignore entries for IDE files, OS files, logs, coverage, and temp files - Added TypeScript type definitions (index.d.ts) for better TypeScript support - Added JSDoc comments to all functions for improved code documentation - Added input validation to all functions with appropriate error messages - Improved package.json with keywords, types field, files array, and homepage - Enhanced all test files with additional test cases for edge cases and validation - Added new test files for chineseZodiac and zodiac functions - Improved code consistency across all modules --- .gitignore | 39 +++++++ README.md | 233 +++++++++++++++++++++++++++++++------ gulpfile.js | 62 +++++++--- index.d.ts | 127 ++++++++++++++++++++ package.json | 27 ++++- src/baydin.js | 37 +++--- src/lib/chineseZodiac.js | 26 ++++- src/lib/maharbote.js | 31 ++++- src/lib/nayNum.js | 77 +++++++----- src/lib/numFormat.js | 68 ++++++++--- src/lib/numerology.js | 43 ++++++- src/lib/tatyout.js | 39 +++++-- src/lib/zodiac.js | 32 ++++- test/chineseZodiac.test.js | 40 +++++++ test/maharbote.test.js | 22 +++- test/nayNum.test.js | 21 +++- test/numFormat.test.js | 25 +++- test/numerology.test.js | 26 ++++- test/tatyout.test.js | 32 ++++- test/zodiac.test.js | 46 ++++++++ webpack.config.js | 7 +- 21 files changed, 913 insertions(+), 147 deletions(-) create mode 100644 index.d.ts create mode 100644 test/chineseZodiac.test.js create mode 100644 test/zodiac.test.js diff --git a/.gitignore b/.gitignore index c2658d7..9892067 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,40 @@ +# Dependencies node_modules/ + +# Build output +dist/ + +# IDE +.idea/ +.vscode/ +*.swp +*.swo + +# OS files +.DS_Store +Thumbs.db + +# Logs +logs/ +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Coverage +coverage/ +.nyc_output/ + +# Environment +.env +.env.local +.env.*.local + +# Temporary files +tmp/ +temp/ +*.tmp + +# Package locks (keep one) +# Uncomment if you prefer yarn.lock +# package-lock.json diff --git a/README.md b/README.md index 021edda..0c4faaf 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,31 @@ # BayDinJS -> Myanmar Astrology +> Myanmar Astrology Library for JavaScript [![code style: prettier][prettier]][prettier-url] [![npm][npm-download]][npm-dl-url] [![contributions welcome][contri]][contri-url] [![License: MIT][license]][license-url] +A JavaScript library for Myanmar traditional astrology calculations including maharbote, numerology, zodiac signs, and more. + +## Table of Contents + +- [Installation](#installation) +- [Usage](#usage) +- [API Reference](#api-reference) + - [maharbote](#maharboteyear-day) + - [nayNum](#naynumstr) + - [numerology](#numerologynum) + - [numFormat](#numformatnum) + - [tatyout](#tatyoutage-day) + - [chineseZodiac](#chinesezodiacyear) + - [zodiac](#zodiacday-month) +- [Development](#development) +- [Testing](#testing) +- [Contributing](#contributing) +- [License](#license) + ## Installation ### via npm @@ -15,84 +34,224 @@ npm install --save baydin ``` -### via web +### via yarn + +```shell +yarn add baydin +``` + +### via web (CDN) ```html ``` -## Test +## Usage -```shell -npm test +### Node.js + +```javascript +const baydin = require("baydin"); + +// Calculate maharbote +const maharboteResult = baydin.maharbote(1356, 1); +console.log(maharboteResult); // "မရဏ" + +// Get numerology number +const numerologyResult = baydin.numerology(11); +console.log(numerologyResult); // 2 ``` -## Function List +### Browser -- [maharbote](#maharbote) -- [nayNum](#nayNum) -- [numerology](#numerology) -- [numFormat](#numFormat) -- [tatyout](#tatyout) +```html + + +``` -## maharbote +## API Reference -ပုတိ ၊ ဘင်္ဂ ၊ မရဏ ၊ အထွန်း ၊ သိုက် ၊ ရာဇာ ၊ အဓိပတိ ဆိုတဲ့ အဖွား ခုနှစ်ခု ကိုတွက်ထုတ်ပေးမှာဖြစ်ပါတယ်။ ခုနှစ် သက္ကရာဇ်ရယ် မွေးနံ ရယ်ကို ထည့်သွင်းဖို့ လိုအပ်ပါတယ်။ +### maharbote(year, day) -### Example Code +Calculates the maharbote (astrological influence) based on Myanmar calendar year and birth day of the week. +ပုတိ ၊ ဘင်္ဂ ၊ မရဏ ၊ အထွန်း ၊ သိုက် ၊ ရာဇာ ၊ အဓိပတိ ဆိုတဲ့ အဖွား ခုနှစ်ခု ကိုတွက်ထုတ်ပေးမှာဖြစ်ပါတယ်။ + +**Parameters:** +- `year` (number): Myanmar calendar year (e.g., 1356) +- `day` (number): Day of the week (1-7, where 1 = Sunday, 7 = Saturday) + +**Returns:** (string) The maharbote name in Myanmar + +**Example:** ```javascript -var baydin = require("baydin"); -var mahb = baydin.maharbote(1356, 1); //မရဏ +baydin.maharbote(1356, 1); // "မရဏ" ``` -## nayNum +--- + +### nayNum(str) -ထည့်သွင်းလိုက်တဲ့ စာသားကို နေ့သင့်နံသင်ဂဏန်းအဖြစ်ပြောင်းမှာဖြစ်ပါတယ်။ ဥပမာ-nayNum("မင်းဖြူမှန်မှန်ပြော")//55555 +Converts Myanmar text to its numerological equivalent (nay num). -### Example Code +ထည့်သွင်းလိုက်တဲ့ စာသားကို နေ့သင့်နံသင့်ဂဏန်းအဖြစ်ပြောင်းမှာဖြစ်ပါတယ်။ +**Parameters:** +- `str` (string): Myanmar text string + +**Returns:** (number) The numerological number + +**Example:** ```javascript -var baydin = require("baydin"); -var nay = baydin.nayNum("မင်းဖြူမှန်မှန်ပြော"); //55555 +baydin.nayNum("မင်းဖြူမှန်မှန်ပြော"); // 55555 ``` -## numerology +--- + +### numerology(num) + +Calculates the single-digit numerology number from any number. + +၁ ဂဏန်း သမား ၂ ဂဏန်း သမား ၉ ဂဏန်းသမား ဆိုပြီးတော့ အမျိုးအစား ၉ မျိုး ရှိပါတယ်။ တွက်နည်းက မွေးရက် ကို တစ်လုံးတည်းရအောင် ပေါင်းရမှာပါ။ -၁ ဂဏန်း သမား ၂ ဂဏန်း သမား ၉ ဂဏန်းသမား ဆိုပြီးတော့ အမျိုးအစား ၉ မျိုး ရှိပါတယ်။ တွက်နည်းက မွေးရက် ကို တစ်လုံးတည်းရအောင် ပေါင်းရမှာပါ။ ဥပမာ-၁၁ ဆိုရင် ၂ ဂဏန်းသမားဖြစ်ပါတယ် +**Parameters:** +- `num` (number): Any positive number -### Example Code +**Returns:** (number) Single digit number (1-9) +**Example:** ```javascript -var baydin = require("baydin"); -var numero = baydin.numerology(11); //2 +baydin.numerology(11); // 2 (1 + 1 = 2) +baydin.numerology(29); // 2 (2 + 9 = 11, 1 + 1 = 2) ``` -## numFormat +--- -ထည့်သွင်းလိုက်တဲ့ ကိန်းဂဏန်းကို စာသားအနေနဲ့ ပြန်ထုတ်ပေးမှာဖြစ်ပါတယ်။ ဥပမာ-numFormat(152)//တစ်ရာငါးဆယ်နှစ် +### numFormat(num) -### Example Code +Converts a number to Myanmar words. +ထည့်သွင်းလိုက်တဲ့ ကိန်းဂဏန်းကို စာသားအနေနဲ့ ပြန်ထုတ်ပေးမှာဖြစ်ပါတယ်။ + +**Parameters:** +- `num` (number): Number to convert (supports up to 14 digits) + +**Returns:** (string) Number in Myanmar words + +**Example:** ```javascript -var baydin = require("baydin"); -var numF = baydin.numFormat(152); //တစ်ရာငါးဆယ်နှစ် +baydin.numFormat(152); // "တစ်ရာငါးဆယ်နှစ်" ``` -## tatyout +--- + +### tatyout(age, day) -အသက် နဲ့ နေ့နံ ကို ထည့်သွင်းလိုက်မယ် ဆိုရင် သက်ရောက်ကို တွက်ထုတ်ပေးပါတယ်။ စနေ့အတွက် ကိုတော့ သုည မဟုတ်ပဲ 7 ကိုပဲထည့်သွင်းပေးစေလိုပါတယ်။ ဥပမာ-သက်ရောက်(1,1)//1 လို့ တွက်ထုတ်ပေးပါတယ်။ +Calculates the tatyout (life influence) based on age and birth day of the week. -### Example Code +အသက် နဲ့ နေ့နံ ကို ထည့်သွင်းလိုက်မယ် ဆိုရင် သက်ရောက်ကို တွက်ထုတ်ပေးပါတယ်။ +**Parameters:** +- `age` (number): Age in years +- `day` (number): Day of the week (1-7, where 7 = Saturday) + +**Returns:** (number) The tatyout number (1-7) + +**Example:** ```javascript -var baydin = require("baydin"); -var ty = baydin.tatyout(1, 1); //1 +baydin.tatyout(1, 1); // 1 ``` +--- + +### chineseZodiac(year) + +Gets the Chinese zodiac sign for a given year. + +**Parameters:** +- `year` (number): Gregorian calendar year (positive integer) + +**Returns:** (Object) Object containing `sign` (English) and `signInBurmese` (Myanmar) + +**Example:** +```javascript +baydin.chineseZodiac(2024); +// { sign: "Dragon", signInBurmese: "နဂါး" } +``` + +--- + +### zodiac(day, month) + +Gets the Western zodiac sign based on birth date. + +**Parameters:** +- `day` (number): Day of birth (1-31) +- `month` (number): Month of birth (1-12) + +**Returns:** (Object) Object containing `sign` (English) and `sign_mm` (Myanmar) + +**Throws:** Error if invalid date or month input + +**Example:** +```javascript +baydin.zodiac(15, 3); +// { sign: "Pisces", sign_mm: "မိန်" } +``` + +## Development + +### Prerequisites + +- Node.js (v14 or higher recommended) +- npm or yarn + +### Setup + +```shell +git clone https://github.com/AungMyoKyaw/BayDin.git +cd BayDin +npm install +``` + +### Build + +```shell +npm run build +``` + +This will create: +- `dist/node/` - Node.js compatible build +- `dist/client/` - Browser compatible minified build + +## Testing + +```shell +npm test +``` + +## Contributing + +Contributions are welcome! Please feel free to submit a Pull Request. + +1. Fork the repository +2. Create your feature branch (`git checkout -b feature/amazing-feature`) +3. Commit your changes (`git commit -m 'feat: add amazing feature'`) +4. Push to the branch (`git push origin feature/amazing-feature`) +5. Open a Pull Request + +Please make sure to update tests as appropriate and follow the existing code style. + ## License -MIT © [Aung Myo Kyaw](https://github.com/AungMyoKyaw) +MIT License - see the [LICENSE](LICENSE) file for details. + +## Author + +Aung Myo Kyaw - [GitHub](https://github.com/AungMyoKyaw) [contri]: https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat-square [contri-url]: https://github.com/AungMyoKyaw/BayDin/issues diff --git a/gulpfile.js b/gulpfile.js index dbaa98d..53e7c5e 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,30 +1,62 @@ -const gulp = require("gulp"); -const babel = require("gulp-babel"); -const mocha = require("gulp-mocha"); -const webpack = require("webpack-stream"); -const path = require("path"); -const webpackConfig = require("./webpack.config.js"); +/** + * @fileoverview Gulp build configuration for BayDinJS + * @description Build tasks for Node.js and browser distributions + */ -gulp.task("build-node", () => - gulp +var gulp = require("gulp"); +var babel = require("gulp-babel"); +var mocha = require("gulp-mocha"); +var webpack = require("webpack-stream"); +var path = require("path"); +var webpackConfig = require("./webpack.config.js"); + +/** + * Build task for Node.js distribution + * Transpiles source files using Babel for Node.js compatibility + * @task build-node + */ +gulp.task("build-node", function() { + return gulp .src("src/**/*.js") .pipe( babel({ presets: ["@babel/preset-env"] }) ) - .pipe(gulp.dest("dist/node")) -); + .pipe(gulp.dest("dist/node")); +}); -gulp.task("build-browser", () => - gulp +/** + * Build task for browser distribution + * Bundles and minifies source files using Webpack + * @task build-browser + */ +gulp.task("build-browser", function() { + return gulp .src("src/**/*.js") .pipe(webpack(webpackConfig)) - .pipe(gulp.dest("dist/client")) -); + .pipe(gulp.dest("dist/client")); +}); -gulp.task("test", () => gulp.src("test/**/*.js").pipe(mocha())); +/** + * Test task + * Runs all tests using Mocha + * @task test + */ +gulp.task("test", function() { + return gulp.src("test/**/*.js").pipe(mocha()); +}); +/** + * Combined build task + * Runs both Node.js and browser builds in parallel + * @task build + */ gulp.task("build", gulp.parallel("build-node", "build-browser")); +/** + * Default task + * Runs build and then tests + * @task default + */ gulp.task("default", gulp.series("build", "test")); diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..921c2d1 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,127 @@ +/** + * BayDinJS - Myanmar Astrology Library + * TypeScript type definitions + * @packageDocumentation + */ + +/** + * Result object for zodiac sign functions + */ +export interface ZodiacSignResult { + /** Zodiac sign name in English */ + sign: string; + /** Zodiac sign name in Myanmar */ + sign_mm: string; +} + +/** + * Result object for Chinese zodiac sign function + */ +export interface ChineseZodiacResult { + /** Zodiac sign name in English */ + sign: string; + /** Zodiac sign name in Burmese */ + signInBurmese: string; +} + +/** + * Calculates the maharbote (astrological influence) based on Myanmar calendar year + * and birth day of the week. + * + * @param year - Myanmar calendar year (e.g., 1356) + * @param day - Day of the week (1-7, where 1 = Sunday, 7 = Saturday) + * @returns The maharbote name in Myanmar + * @throws Error if year or day are invalid + * + * @example + * maharbote(1356, 1); // "မရဏ" + */ +export function maharbote(year: number, day: number): string; + +/** + * Converts Myanmar text to its numerological equivalent (nay num). + * + * @param str - Myanmar text string + * @returns The numerological number + * @throws Error if input is not a string or is empty + * + * @example + * nayNum("မင်းဖြူမှန်မှန်ပြော"); // 55555 + */ +export function nayNum(str: string): number; + +/** + * Calculates the single-digit numerology number from any number. + * + * @param num - Any positive number + * @returns Single digit number (1-9, or 0 if input is 0) + * @throws Error if input is not a number or is negative + * + * @example + * numerology(11); // 2 + * numerology(29); // 2 + */ +export function numerology(num: number): number; + +/** + * Converts a number to Myanmar words. + * + * @param num - Number to convert (supports up to 14 digits) + * @returns Number in Myanmar words, or original number if too large + * @throws Error if input is not a number + * + * @example + * numFormat(152); // "တစ်ရာငါးဆယ်နှစ်" + */ +export function numFormat(num: number): string; + +/** + * Calculates the tatyout (life influence) based on age and birth day of the week. + * + * @param age - Age in years (positive integer) + * @param day - Day of the week (1-7, where 7 = Saturday) + * @returns The tatyout number (1-7) + * @throws Error if age or day are invalid + * + * @example + * tatyout(1, 1); // 1 + */ +export function tatyout(age: number, day: number): number; + +/** + * Gets the Chinese zodiac sign for a given year. + * + * @param year - Gregorian calendar year (positive integer) + * @returns Object containing zodiac information + * @throws Error if year is not a valid positive integer + * + * @example + * chineseZodiac(2024); // { sign: "Dragon", signInBurmese: "နဂါး" } + */ +export function chineseZodiac(year: number): ChineseZodiacResult; + +/** + * Gets the Western zodiac sign based on birth date. + * + * @param day - Day of birth (1-31) + * @param month - Month of birth (1-12) + * @returns Object containing zodiac information + * @throws Error if invalid date or month input + * + * @example + * zodiac(15, 3); // { sign: "Pisces", sign_mm: "မိန်" } + */ +export function zodiac(day: number, month: number): ZodiacSignResult; + +/** + * Main module export containing all astrology functions + */ +export interface BayDin { + maharbote: typeof maharbote; + nayNum: typeof nayNum; + numerology: typeof numerology; + numFormat: typeof numFormat; + tatyout: typeof tatyout; + chineseZodiac: typeof chineseZodiac; + zodiac: typeof zodiac; +} diff --git a/package.json b/package.json index 629b961..ffae028 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,38 @@ { "name": "baydin", "version": "1.0.1", - "description": "Bay Din JS", + "description": "Bay Din JS - Myanmar Astrology Library for JavaScript", "main": "dist/node/baydin.js", + "types": "index.d.ts", + "files": [ + "dist/", + "index.d.ts" + ], "scripts": { "test": "gulp test", - "build": "gulp" + "build": "gulp", + "prepublishOnly": "npm run build" }, "repository": { "type": "git", "url": "https://github.com/AungMyoKyaw/BayDin" }, + "keywords": [ + "myanmar", + "astrology", + "baydin", + "maharbote", + "numerology", + "zodiac", + "chinese-zodiac", + "burmese" + ], "author": "Aung Myo Kyaw", "license": "MIT", + "bugs": { + "url": "https://github.com/AungMyoKyaw/BayDin/issues" + }, + "homepage": "https://github.com/AungMyoKyaw/BayDin#readme", "devDependencies": { "@babel/core": "^7.18.6", "@babel/preset-env": "^7.18.6", @@ -24,5 +44,8 @@ "webpack": "^5.72.0", "webpack-cli": "^4.9.2", "webpack-stream": "^7.0.0" + }, + "engines": { + "node": ">=12.0.0" } } diff --git a/src/baydin.js b/src/baydin.js index c76e094..dc08e7f 100644 --- a/src/baydin.js +++ b/src/baydin.js @@ -1,17 +1,26 @@ -const maharbote = require("./lib/maharbote"); -const nayNum = require("./lib/nayNum"); -const numerology = require("./lib/numerology"); -const numFormat = require("./lib/numFormat"); -const tatyout = require("./lib/tatyout"); -const chineseZodiac = require("./lib/chineseZodiac"); -const zodiac = require("./lib/zodiac"); +/** + * @fileoverview BayDinJS - Myanmar Astrology Library + * @description A JavaScript library for Myanmar traditional astrology calculations + * including maharbote, numerology, zodiac signs, and more. + * @author Aung Myo Kyaw + * @license MIT + * @see {@link https://github.com/AungMyoKyaw/BayDin|GitHub Repository} + */ + +var maharbote = require("./lib/maharbote"); +var nayNum = require("./lib/nayNum"); +var numerology = require("./lib/numerology"); +var numFormat = require("./lib/numFormat"); +var tatyout = require("./lib/tatyout"); +var chineseZodiac = require("./lib/chineseZodiac"); +var zodiac = require("./lib/zodiac"); module.exports = { - maharbote, - nayNum, - numerology, - numFormat, - tatyout, - chineseZodiac, - zodiac + maharbote: maharbote, + nayNum: nayNum, + numerology: numerology, + numFormat: numFormat, + tatyout: tatyout, + chineseZodiac: chineseZodiac, + zodiac: zodiac }; diff --git a/src/lib/chineseZodiac.js b/src/lib/chineseZodiac.js index fb8a19c..15ef32f 100644 --- a/src/lib/chineseZodiac.js +++ b/src/lib/chineseZodiac.js @@ -1,4 +1,22 @@ -// Function to get the Chinese Zodiac sign for a given year +/** + * Gets the Chinese zodiac sign for a given year. + * + * The Chinese zodiac is a repeating 12-year cycle where each year + * is represented by an animal. This function calculates the zodiac + * sign based on the Gregorian calendar year. + * + * @param {number} year - Gregorian calendar year (positive integer) + * @returns {Object} Object containing zodiac information + * @returns {string} returns.sign - Zodiac sign name in English + * @returns {string} returns.signInBurmese - Zodiac sign name in Burmese + * @throws {Error} If year is not a valid positive integer + * @example + * chineseZodiac(2024); + * // { sign: "Dragon", signInBurmese: "နဂါး" } + * + * chineseZodiac(1990); + * // { sign: "Horse", signInBurmese: "မြင်း" } + */ function getChineseZodiac(year) { // Validate the input if (typeof year !== "number" || !Number.isInteger(year) || year <= 0) { @@ -6,7 +24,7 @@ function getChineseZodiac(year) { } // Array of Chinese Zodiac signs in the order they repeat - const zodiacSigns = [ + var zodiacSigns = [ "Rat", "Ox", "Tiger", @@ -21,7 +39,7 @@ function getChineseZodiac(year) { "Pig" ]; - const zodiacSignsInBurmese = [ + var zodiacSignsInBurmese = [ "ကြွက်", "နွား", "ကျား", @@ -37,7 +55,7 @@ function getChineseZodiac(year) { ]; // Calculate the index for the zodiac sign - const index = (year - 4) % 12; + var index = ((year - 4) % 12 + 12) % 12; // Handle negative modulo // Return the corresponding zodiac sign return { diff --git a/src/lib/maharbote.js b/src/lib/maharbote.js index 7d64874..c0d25b3 100644 --- a/src/lib/maharbote.js +++ b/src/lib/maharbote.js @@ -1,7 +1,31 @@ +/** + * Calculates the maharbote (astrological influence) based on Myanmar calendar year + * and birth day of the week. + * + * Maharbote refers to seven astrological influences: + * ပုတိ, ဘင်္ဂ, မရဏ, အထွန်း, သိုက်, ရာဇာ, အဓိပတိ + * + * @param {number} year - Myanmar calendar year (e.g., 1356) + * @param {number} day - Day of the week (1-7, where 1 = Sunday, 7 = Saturday) + * @returns {string} The maharbote name in Myanmar + * @throws {Error} If year or day are invalid + * @example + * maharbote(1356, 1); // "မရဏ" + */ function maharbote(year, day) { - //Aung Myo Kyaw + // Validate inputs + if (typeof year !== "number" || typeof day !== "number") { + throw new Error("Year and day must be numbers"); + } + if (day < 1 || day > 7) { + throw new Error("Day must be between 1 and 7"); + } + if (year <= 0) { + throw new Error("Year must be a positive number"); + } + var အကြွင်း = year % 7; - if (အကြွင်း == 0) { + if (အကြွင်း === 0) { အကြွင်း = 7; } var တိုင်စဉ် = []; @@ -9,16 +33,13 @@ function maharbote(year, day) { တိုင်စဉ်.push(အကြွင်း); for (var i = 0; i < 6; i++) { var ထည့်ရန် = တိုင်စဉ်[i] + 3; - //console.log(ထည့်ရန်); if (ထည့်ရန် > 7) { တိုင်စဉ်.push(ထည့်ရန် - 7); } else { တိုင်စဉ်.push(ထည့်ရန်); } } - // console.log(တိုင်စဉ်); return အဖွား[တိုင်စဉ်.indexOf(day)]; - //console.log(အကြွင်း); } module.exports = maharbote; diff --git a/src/lib/nayNum.js b/src/lib/nayNum.js index ea5a81e..5578694 100644 --- a/src/lib/nayNum.js +++ b/src/lib/nayNum.js @@ -1,41 +1,60 @@ +/** + * Converts Myanmar text to its numerological equivalent (nay num). + * + * This function extracts consonants from Myanmar text and converts them + * to their corresponding numerological values based on traditional + * Myanmar astrology. + * + * @param {string} str - Myanmar text string + * @returns {number} The numerological number + * @throws {Error} If input is not a string or is empty + * @example + * nayNum("မင်းဖြူမှန်မှန်ပြော"); // 55555 + */ function nayNum(str) { - // to filter vowel - var vowel = /[က-အ](?!်|္)/g; //thanks to ko camhill(mysteryzillion.org) for this regex. + // Validate input + if (typeof str !== "string") { + throw new Error("Input must be a string"); + } + if (str.length === 0) { + throw new Error("Input string cannot be empty"); + } + + // Regex to filter vowel characters in Myanmar script + var vowel = /[က-အ](?!်|္)/g; + + /** + * Returns a regex pattern for the given numerological group + * @param {number} i - Group number (1-7) + * @returns {RegExp} Regex pattern for the group + */ function setRex(i) { - var s1 = /[အ]/; - var s2 = /[က-င]/; - var s3 = /[စ-ည]/; - var s4 = /[ယ-ဝ]/; - var s5 = /[ပ-မ]/; - var s6 = /[သဟ]/; - var s7 = /[တ-န]/; - switch (i) { - case 1: - return s1; - case 2: - return s2; - case 3: - return s3; - case 4: - return s4; - case 5: - return s5; - case 6: - return s6; - case 7: - return s7; - } + var patterns = { + 1: /[အ]/, + 2: /[က-င]/, + 3: /[စ-ည]/, + 4: /[ယ-ဝ]/, + 5: /[ပ-မ]/, + 6: /[သဟ]/, + 7: /[တ-န]/ + }; + return patterns[i]; + } + + var matchedChars = str.match(vowel); + if (!matchedChars || matchedChars.length === 0) { + return 0; } - var str = str.match(vowel); + var ans = ""; - str.forEach(function (a) { + matchedChars.forEach(function (a) { var test = false; var i = 1; - while (test === false) { + while (test === false && i <= 7) { var patt = setRex(i); if (patt.test(a) === true) { test = true; - if (i == 7) { + if (i === 7) { ans += 0; } else { ans += i; diff --git a/src/lib/numFormat.js b/src/lib/numFormat.js index 5c5b785..a7110e2 100644 --- a/src/lib/numFormat.js +++ b/src/lib/numFormat.js @@ -1,4 +1,37 @@ +/** + * Converts a number to Myanmar words. + * + * This function takes a numeric value and returns its representation + * in Myanmar language words, supporting various magnitudes including + * ဆယ် (ten), ရာ (hundred), ထောင် (thousand), သောင်း (ten thousand), + * သိန်း (hundred thousand), သန်း (million), and ကုဋေ (ten million). + * + * @param {number} num - Number to convert (supports up to 14 digits) + * @returns {string} Number in Myanmar words, or original number if too large + * @throws {Error} If input is not a number + * @example + * numFormat(152); // "တစ်ရာငါးဆယ်နှစ်" + * numFormat(1000); // "တစ်ထောင်" + */ function numFormat(num) { + // Validate input + if (typeof num !== "number" || !Number.isFinite(num)) { + throw new Error("Input must be a valid number"); + } + + // Handle negative numbers + if (num < 0) { + return "အနုတ်" + numFormat(Math.abs(num)); + } + + // Handle zero + if (num === 0) { + return "သုည"; + } + + // Ensure we work with integer + num = Math.floor(num); + var value = [ "တစ်", "နှစ်", @@ -11,9 +44,28 @@ function numFormat(num) { "ကိုး" ]; var format = ["ကုဋေ", "သန်း", "သိန်း", "သောင်း", "ထောင်", "ရာ", "ဆယ်", ""]; + + /** + * Formats a number segment into Myanmar words + * @param {number} numStr - Number to format + * @returns {string} Formatted string in Myanmar + */ + function formatter(numStr) { + var result = ""; + var numString = numStr.toString(); + if (numString.length) { + numString.split("").forEach(function (element, index, arr) { + if (Number(element) !== 0) { + result += value[element - 1] + format[8 + index - arr.length]; + } + }); + } + return result; + } + var ans = ""; var numlength = num.toString().length; - // console.log(numlength); + if (numlength > 7) { var kuday = num.toString().slice(0, numlength - 7); if (kuday.length < 8) { @@ -27,19 +79,7 @@ function numFormat(num) { } else { ans += formatter(num); } - function formatter(num) { - var ans = ""; - if (num.toString().length) - num - .toString() - .split("") - .forEach(function (element, index, arr) { - if (Number(element) !== 0) { - ans += value[element - 1] + format[8 + index - arr.length]; - } - }); - return ans; - } + return ans; } diff --git a/src/lib/numerology.js b/src/lib/numerology.js index 235bcd9..6d85635 100644 --- a/src/lib/numerology.js +++ b/src/lib/numerology.js @@ -1,16 +1,53 @@ +/** + * Calculates the single-digit numerology number from any number. + * + * This function reduces any number to a single digit (1-9) by repeatedly + * summing its digits. In Myanmar astrology, this is used to determine + * a person's numerology group based on their birth date. + * + * @param {number} num - Any positive number + * @returns {number} Single digit number (1-9, or 0 if input is 0) + * @throws {Error} If input is not a number or is negative + * @example + * numerology(11); // 2 (1 + 1 = 2) + * numerology(29); // 2 (2 + 9 = 11, 1 + 1 = 2) + * numerology(0); // 0 + */ function numberbaydin(num) { - //Aung Myo Kyaw + // Validate input + if (typeof num !== "number" || !Number.isFinite(num)) { + throw new Error("Input must be a valid number"); + } + if (num < 0) { + throw new Error("Input must be a non-negative number"); + } + + // Handle 0 case + if (num === 0) { + return 0; + } + + // Ensure we work with integer + num = Math.floor(Math.abs(num)); + + /** + * Reducer function to sum digits + * @param {string} a - First value + * @param {string} b - Second value + * @returns {number} Sum of the two values + */ function numsplitreduce(a, b) { return Number(a) + Number(b); } + var numsplit = num.toString().split(""); var sum = numsplit.reduce(numsplitreduce); - //console.log(sum); + while (sum > 9) { numsplit = sum.toString().split(""); sum = numsplit.reduce(numsplitreduce); } - //console.log(sum); + return sum; } diff --git a/src/lib/tatyout.js b/src/lib/tatyout.js index 2d4f20b..7875aed 100644 --- a/src/lib/tatyout.js +++ b/src/lib/tatyout.js @@ -1,17 +1,42 @@ -function သက်ရောက်(အသက်, နေ့) { +/** + * Calculates the tatyout (life influence) based on age and birth day of the week. + * + * This function determines the astrological influence (သက်ရောက်) for a given + * age and day of birth, returning a number from 1 to 7. + * + * @param {number} age - Age in years (positive integer) + * @param {number} day - Day of the week (1-7, where 7 = Saturday) + * @returns {number} The tatyout number (1-7) + * @throws {Error} If age or day are invalid + * @example + * tatyout(1, 1); // 1 + * tatyout(25, 3); // 5 + */ +function tatyout(age, day) { + // Validate inputs + if (typeof age !== "number" || typeof day !== "number") { + throw new Error("Age and day must be numbers"); + } + if (!Number.isInteger(age) || age <= 0) { + throw new Error("Age must be a positive integer"); + } + if (day < 1 || day > 7) { + throw new Error("Day must be between 1 and 7"); + } + var တိုင်စဉ် = []; - var အကြွင်း = အသက် % 7; + var အကြွင်း = age % 7; + for (var i = 0; i < 7; i++) { - var ထည့်ရန် = နေ့ + i; + var ထည့်ရန် = day + i; if (ထည့်ရန် > 7) { တိုင်စဉ်.push(ထည့်ရန် - 7); } else { တိုင်စဉ်.push(ထည့်ရန်); } } - //console.log(တိုင်စဉ်); - //console.log(အကြွင်း); - return တိုင်စဉ်[အကြွင်း - 1]; + + return တိုင်စဉ်[အကြွင်း - 1 < 0 ? 6 : အကြွင်း - 1]; } -module.exports = သက်ရောက်; +module.exports = tatyout; diff --git a/src/lib/zodiac.js b/src/lib/zodiac.js index 8406a50..124ac7c 100644 --- a/src/lib/zodiac.js +++ b/src/lib/zodiac.js @@ -1,8 +1,21 @@ /** - * Function to calculate the Zodiac sign based on birth date and month. - * @param {number} day - The day of the birth. - * @param {number} month - The month of the birth. - * @returns {string} - The Zodiac sign. + * Gets the Western zodiac sign based on birth date. + * + * This function determines the zodiac sign based on the day and month + * of birth, following the tropical zodiac system used in Western astrology. + * + * @param {number} day - Day of birth (1-31) + * @param {number} month - Month of birth (1-12) + * @returns {Object} Object containing zodiac information + * @returns {string} returns.sign - Zodiac sign name in English + * @returns {string} returns.sign_mm - Zodiac sign name in Myanmar + * @throws {Error} If invalid date or month input + * @example + * zodiac(15, 3); + * // { sign: "Pisces", sign_mm: "မိန်" } + * + * zodiac(25, 12); + * // { sign: "Capricorn", sign_mm: "မကာရ" } */ function getZodiacSign(day, month) { // Validate input @@ -17,8 +30,14 @@ function getZodiacSign(day, month) { throw new Error("Invalid date or month input"); } + // Additional validation for days in month + var daysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + if (day > daysInMonth[month - 1]) { + throw new Error("Invalid day for the given month"); + } + // Array of zodiac signs and their date ranges - const zodiacSigns = [ + var zodiacSigns = [ { sign: "Capricorn", sign_mm: "မကာရ", @@ -100,7 +119,8 @@ function getZodiacSign(day, month) { ]; // Determine zodiac sign based on date and month - for (const zodiac of zodiacSigns) { + for (var i = 0; i < zodiacSigns.length; i++) { + var zodiac = zodiacSigns[i]; if ( (month === zodiac.start.month && day >= zodiac.start.day) || (month === zodiac.end.month && day <= zodiac.end.day) diff --git a/test/chineseZodiac.test.js b/test/chineseZodiac.test.js new file mode 100644 index 0000000..cee029c --- /dev/null +++ b/test/chineseZodiac.test.js @@ -0,0 +1,40 @@ +var expect = require("chai").expect; +var baydin = require("../dist/node/baydin.js"); + +describe("chineseZodiac", function() { + it("should return Dragon for year 2024", function() { + var result = baydin.chineseZodiac(2024); + expect(result.sign).to.equal("Dragon"); + expect(result.signInBurmese).to.equal("နဂါး"); + }); + + it("should return Rat for year 2020", function() { + var result = baydin.chineseZodiac(2020); + expect(result.sign).to.equal("Rat"); + expect(result.signInBurmese).to.equal("ကြွက်"); + }); + + it("should return Ox for year 2021", function() { + var result = baydin.chineseZodiac(2021); + expect(result.sign).to.equal("Ox"); + expect(result.signInBurmese).to.equal("နွား"); + }); + + it("should throw error for non-number input", function() { + expect(function() { + baydin.chineseZodiac("2024"); + }).to.throw("Please provide a valid year as a positive integer"); + }); + + it("should throw error for negative year", function() { + expect(function() { + baydin.chineseZodiac(-100); + }).to.throw("Please provide a valid year as a positive integer"); + }); + + it("should throw error for zero year", function() { + expect(function() { + baydin.chineseZodiac(0); + }).to.throw("Please provide a valid year as a positive integer"); + }); +}); diff --git a/test/maharbote.test.js b/test/maharbote.test.js index 3965ee2..9fdf23b 100644 --- a/test/maharbote.test.js +++ b/test/maharbote.test.js @@ -1,9 +1,27 @@ var expect = require("chai").expect; var baydin = require("../dist/node/baydin.js"); -describe("maharbote", () => { - it("should equal to မရဏ", () => { +describe("maharbote", function() { + it("should equal to မရဏ", function() { var result = baydin.maharbote(1356, 1); expect(result).to.equal("မရဏ"); }); + + it("should throw error for invalid day", function() { + expect(function() { + baydin.maharbote(1356, 8); + }).to.throw("Day must be between 1 and 7"); + }); + + it("should throw error for non-number inputs", function() { + expect(function() { + baydin.maharbote("1356", 1); + }).to.throw("Year and day must be numbers"); + }); + + it("should throw error for negative year", function() { + expect(function() { + baydin.maharbote(-100, 1); + }).to.throw("Year must be a positive number"); + }); }); diff --git a/test/nayNum.test.js b/test/nayNum.test.js index 01bf38a..374cd60 100644 --- a/test/nayNum.test.js +++ b/test/nayNum.test.js @@ -1,9 +1,26 @@ var expect = require("chai").expect; var baydin = require("../dist/node/baydin.js"); -describe("nayNum", () => { - it("should equal to 55555", () => { +describe("nayNum", function() { + it("should equal to 55555", function() { var result = baydin.nayNum("မင်းဖြူမှန်မှန်ပြော"); expect(result).to.equal(55555); }); + + it("should throw error for non-string input", function() { + expect(function() { + baydin.nayNum(123); + }).to.throw("Input must be a string"); + }); + + it("should throw error for empty string", function() { + expect(function() { + baydin.nayNum(""); + }).to.throw("Input string cannot be empty"); + }); + + it("should return 0 for non-Myanmar characters", function() { + var result = baydin.nayNum("hello"); + expect(result).to.equal(0); + }); }); diff --git a/test/numFormat.test.js b/test/numFormat.test.js index 7f4f2e3..08c56a8 100644 --- a/test/numFormat.test.js +++ b/test/numFormat.test.js @@ -1,9 +1,30 @@ var expect = require("chai").expect; var baydin = require("../dist/node/baydin.js"); -describe("numFormat", () => { - it("should equal to တစ်ရာငါးဆယ်နှစ်", () => { +describe("numFormat", function() { + it("should equal to တစ်ရာငါးဆယ်နှစ်", function() { var result = baydin.numFormat(152); expect(result).to.equal("တစ်ရာငါးဆယ်နှစ်"); }); + + it("should handle single digit numbers", function() { + expect(baydin.numFormat(1)).to.equal("တစ်"); + expect(baydin.numFormat(5)).to.equal("ငါး"); + expect(baydin.numFormat(9)).to.equal("ကိုး"); + }); + + it("should handle zero", function() { + expect(baydin.numFormat(0)).to.equal("သုည"); + }); + + it("should throw error for non-number input", function() { + expect(function() { + baydin.numFormat("123"); + }).to.throw("Input must be a valid number"); + }); + + it("should handle negative numbers", function() { + var result = baydin.numFormat(-10); + expect(result).to.contain("အနုတ်"); + }); }); diff --git a/test/numerology.test.js b/test/numerology.test.js index 8414836..cc750bd 100644 --- a/test/numerology.test.js +++ b/test/numerology.test.js @@ -1,9 +1,31 @@ var expect = require("chai").expect; var baydin = require("../dist/node/baydin.js"); -describe("Numerology", () => { - it("should equal to 2", () => { +describe("Numerology", function() { + it("should equal to 2", function() { var result = baydin.numerology(11); expect(result).to.equal(2); }); + + it("should reduce multi-digit numbers to single digit", function() { + expect(baydin.numerology(29)).to.equal(2); // 2+9=11, 1+1=2 + expect(baydin.numerology(123)).to.equal(6); // 1+2+3=6 + expect(baydin.numerology(999)).to.equal(9); // 9+9+9=27, 2+7=9 + }); + + it("should return 0 for input 0", function() { + expect(baydin.numerology(0)).to.equal(0); + }); + + it("should throw error for non-number input", function() { + expect(function() { + baydin.numerology("123"); + }).to.throw("Input must be a valid number"); + }); + + it("should throw error for negative input", function() { + expect(function() { + baydin.numerology(-11); + }).to.throw("Input must be a non-negative number"); + }); }); diff --git a/test/tatyout.test.js b/test/tatyout.test.js index 120787a..f774d7b 100644 --- a/test/tatyout.test.js +++ b/test/tatyout.test.js @@ -1,9 +1,37 @@ var expect = require("chai").expect; var baydin = require("../dist/node/baydin.js"); -describe("tatyout", () => { - it("should equal to 1", () => { +describe("tatyout", function() { + it("should equal to 1", function() { var result = baydin.tatyout(1, 1); expect(result).to.equal(1); }); + + it("should throw error for non-number inputs", function() { + expect(function() { + baydin.tatyout("1", 1); + }).to.throw("Age and day must be numbers"); + }); + + it("should throw error for invalid day", function() { + expect(function() { + baydin.tatyout(25, 8); + }).to.throw("Day must be between 1 and 7"); + }); + + it("should throw error for non-positive age", function() { + expect(function() { + baydin.tatyout(0, 1); + }).to.throw("Age must be a positive integer"); + + expect(function() { + baydin.tatyout(-5, 1); + }).to.throw("Age must be a positive integer"); + }); + + it("should throw error for non-integer age", function() { + expect(function() { + baydin.tatyout(25.5, 1); + }).to.throw("Age must be a positive integer"); + }); }); diff --git a/test/zodiac.test.js b/test/zodiac.test.js new file mode 100644 index 0000000..e8a90e3 --- /dev/null +++ b/test/zodiac.test.js @@ -0,0 +1,46 @@ +var expect = require("chai").expect; +var baydin = require("../dist/node/baydin.js"); + +describe("zodiac", function() { + it("should return Pisces for March 15", function() { + var result = baydin.zodiac(15, 3); + expect(result.sign).to.equal("Pisces"); + expect(result.sign_mm).to.equal("မိန်"); + }); + + it("should return Capricorn for January 1", function() { + var result = baydin.zodiac(1, 1); + expect(result.sign).to.equal("Capricorn"); + expect(result.sign_mm).to.equal("မကာရ"); + }); + + it("should return Leo for August 1", function() { + var result = baydin.zodiac(1, 8); + expect(result.sign).to.equal("Leo"); + expect(result.sign_mm).to.equal("သိဟ်"); + }); + + it("should throw error for invalid day", function() { + expect(function() { + baydin.zodiac(32, 1); + }).to.throw("Invalid date or month input"); + }); + + it("should throw error for invalid month", function() { + expect(function() { + baydin.zodiac(15, 13); + }).to.throw("Invalid date or month input"); + }); + + it("should throw error for invalid day for February", function() { + expect(function() { + baydin.zodiac(31, 2); + }).to.throw("Invalid day for the given month"); + }); + + it("should throw error for non-number input", function() { + expect(function() { + baydin.zodiac("15", 3); + }).to.throw("Invalid date or month input"); + }); +}); diff --git a/webpack.config.js b/webpack.config.js index a69ed81..f40ced8 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,4 +1,9 @@ -const path = require("path"); +/** + * @fileoverview Webpack configuration for BayDinJS browser bundle + * @description Configures the build for browser-compatible minified output + */ + +var path = require("path"); module.exports = { entry: {