Skip to content

Commit

Permalink
✨ add UMD lib to support pure js invoking #87
Browse files Browse the repository at this point in the history
  • Loading branch information
SylarLong committed Nov 7, 2023
1 parent fa3b9ba commit 4c4f6d5
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
lib
dist
coverage
.vscode
*.log
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
- 🛠️ 修复(fix)
- 🧹 琐事(Chore)

## v2.0.5

- ✨ 改进(enhancement)

🇨🇳

- 废弃 `astrolabeByLunarDate``astrolabeBySolarDate` 方法
- 增加umd编译以便支持纯js库CDN调用 #87

🇺🇸

- deprecate `astrolabeByLunarDate` and `astrolabeBySolarDate` function
- add UMD lib to support pure js invoking #87

## v2.0.4

- 🛠️ 修复(fix)
Expand Down
8 changes: 4 additions & 4 deletions README-en_US.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ This is a very simple example to show how to use `iztro` to get the astrolabe da
import { astro } from 'iztro';
// get astrolabe data by solar date
const astrolabe = astro.astrolabeBySolarDate('2000-8-16', 2, 'male', true, 'en-US');
const astrolabe = astro.bySolar('2000-8-16', 2, 'male', true, 'en-US');
// get astrolabe data by lunar date
const astrolabe = astro.astrolabeByLunarDate('2000-7-17', 2, 'male', false, true, 'en-US');
const astrolabe = astro.byLunar('2000-7-17', 2, 'male', false, true, 'en-US');
```

- CommonJS
Expand All @@ -129,10 +129,10 @@ This is a very simple example to show how to use `iztro` to get the astrolabe da
var iztro = require('iztro');
// get astrolabe data by solar date
var astrolabe = iztro.astro.astrolabeBySolarDate('2000-8-16', 2, 'male', true, 'en-US');
var astrolabe = iztro.astro.bySolar('2000-8-16', 2, 'male', true, 'en-US');
// get astrolabe data by lunar date
var astrolabe = iztro.astro.astrolabeByLunarDate('2000-7-17', 2, 'male', false, true, 'en-US');
var astrolabe = iztro.astro.byLunar('2000-7-17', 2, 'male', false, true, 'en-US');
```

### [CONTRIBUTING](https://github.com/SylarLong/iztro/blob/main/CONTRIBUTING.md)
Expand Down
8 changes: 4 additions & 4 deletions README-zh_TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@
import { astro } from 'iztro';
// 通過陽歷獲取星盤信息
const astrolabe = astro.astrolabeBySolarDate('2000-8-16', 2, '女', true, 'zh-CN');
const astrolabe = astro.bySolar('2000-8-16', 2, '女', true, 'zh-CN');
// 通過農歷獲取星盤信息
const astrolabe = astro.astrolabeByLunarDate('2000-7-17', 2, '女', false, true, 'zh-CN');
const astrolabe = astro.byLunar('2000-7-17', 2, '女', false, true, 'zh-CN');
```

- CommonJS
Expand All @@ -130,10 +130,10 @@
var iztro = require('iztro');
// 通過陽歷獲取星盤信息
var astrolabe = iztro.astro.astrolabeBySolarDate('2000-8-16', 2, '女', true, 'zh-CN');
var astrolabe = iztro.astro.bySolar('2000-8-16', 2, '女', true, 'zh-CN');
// 通過農歷獲取星盤信息
var astrolabe = iztro.astro.astrolabeByLunarDate('2000-7-17', 2, '女', false, true, 'zh-CN');
var astrolabe = iztro.astro.byLunar('2000-7-17', 2, '女', false, true, 'zh-CN');
```

### [貢獻指南](https://github.com/SylarLong/iztro/blob/main/CONTRIBUTING.md)
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@

> `v2.0.4+` 版本才提供独立js库。

`iztro-{version}.min.js`script标签引入html文件使用。
`iztro.min.js`script标签引入html文件使用。

```html
Expand All @@ -135,7 +135,7 @@
<title>iztro-紫微斗数开源库</title>
</head>
<body>
<script src="./iztro-2.0.4.min.js"></script>
<script src="./iztro.min.js"></script>
<script>
// 获取一张星盘数据
var astrolabe = iztro.asto.astrolabeBySolarDate('2000-8-16', 2, '男', true, 'zh-CN');
Expand All @@ -157,10 +157,10 @@
import { astro } from 'iztro';
// 通过阳历获取星盘信息
const astrolabe = astro.astrolabeBySolarDate('2000-8-16', 2, '女', true, 'zh-CN');
const astrolabe = astro.bySolar('2000-8-16', 2, '女', true, 'zh-CN');
// 通过农历获取星盘信息
const astrolabe = astro.astrolabeByLunarDate('2000-7-17', 2, '女', false, true, 'zh-CN');
const astrolabe = astro.byLunar('2000-7-17', 2, '女', false, true, 'zh-CN');
```

- CommonJS
Expand All @@ -169,10 +169,10 @@
var iztro = require('iztro');
// 通过阳历获取星盘信息
var astrolabe = iztro.astro.astrolabeBySolarDate('2000-8-16', 2, '女', true, 'zh-CN');
var astrolabe = iztro.astro.bySolar('2000-8-16', 2, '女', true, 'zh-CN');
// 通过农历获取星盘信息
var astrolabe = iztro.astro.astrolabeByLunarDate('2000-7-17', 2, '女', false, true, 'zh-CN');
var astrolabe = iztro.astro.byLunar('2000-7-17', 2, '女', false, true, 'zh-CN');
```

### [贡献指南](https://github.com/SylarLong/iztro/blob/main/CONTRIBUTING.md)
Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iztro",
"version": "2.0.4",
"version": "2.0.5",
"description": "轻量级紫微斗数星盘生成库。可以通过出生年月日获取到紫微斗数星盘信息、生肖、星座等信息。A lightweight kit to astrolabe generator of The Purple Star Astrology (Zi Wei Dou Shu). The Purple Star Astrology(Zi Wei Dou Shu) is a Chinese ancient astrology. You're able to get your horoscope and personality from the astrolabe",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand All @@ -11,14 +11,16 @@
"format": "prettier --write \"src/**/*.ts\"",
"lint": "eslint . --ext .ts",
"test": "jest --config jestconfig.json --coverage",
"prepare": "npm run build",
"prepublishOnly": "npm test && npm run lint",
"preversion": "npm run lint",
"version": "npm run format && git add -A src",
"postversion": "git push && git push --tags"
"prepare": "yarn build",
"prepublishOnly": "npm test && yarn lint",
"preversion": "yarn lint",
"version": "yarn format && git add -A src",
"postversion": "git push && git push --tags",
"pub": "yarn build:umd && yarn publish"
},
"files": [
"lib/**/*"
"lib/**/*",
"dist/**/*"
],
"publishConfig": {
"access": "public"
Expand Down
40 changes: 20 additions & 20 deletions src/__tests__/astro/astro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { astro } from '../../index';
describe('Astrolabe', () => {
afterEach(() => setLanguage('zh-CN'));

test('astrolabeBySolarDate()', () => {
const result = astro.astrolabeBySolarDate('2000-8-16', 2, '女', true);
test('bySolar()', () => {
const result = astro.bySolar('2000-8-16', 2, '女', true);

expect(result).toHaveProperty('solarDate', '2000-8-16');
expect(result).toHaveProperty('lunarDate', '二〇〇〇年七月十七');
Expand Down Expand Up @@ -233,8 +233,8 @@ describe('Astrolabe', () => {
expect(agePalace2).toHaveProperty('earthlyBranch', '亥');
});

test('astrolabeBySolarDate() Korean', () => {
const result = astro.astrolabeBySolarDate('2000-8-16', 2, '女', true, 'ko-KR');
test('bySolar() Korean', () => {
const result = astro.bySolar('2000-8-16', 2, '女', true, 'ko-KR');

expect(result).toHaveProperty('solarDate', '2000-8-16');
expect(result).toHaveProperty('lunarDate', '二〇〇〇年七月十七');
Expand Down Expand Up @@ -399,8 +399,8 @@ describe('Astrolabe', () => {
expect(horoscope.hourly).toHaveProperty('mutagen', ['천동', '천기', '문창', '염정']);
});

test('astrolabeBySolarDate() Vietnamese', () => {
const result = astro.astrolabeBySolarDate('2000-8-16', 2, '女', true, 'vi-VN');
test('bySolar() Vietnamese', () => {
const result = astro.bySolar('2000-8-16', 2, '女', true, 'vi-VN');

expect(result).toHaveProperty('solarDate', '2000-8-16');
expect(result).toHaveProperty('lunarDate', '二〇〇〇年七月十七');
Expand Down Expand Up @@ -565,8 +565,8 @@ describe('Astrolabe', () => {
expect(horoscope.hourly).toHaveProperty('mutagen', ['Thiên Đồng', 'Thiên Cơ', 'Văn Xương', 'Liêm Trinh']);
});

test('astrolabeByLunarDate()', () => {
const result = astro.astrolabeByLunarDate('2000-7-17', 2, '女', true, true);
test('byLunar()', () => {
const result = astro.byLunar('2000-7-17', 2, '女', true, true);

expect(result).toHaveProperty('solarDate', '2000-8-16');
expect(result).toHaveProperty('lunarDate', '二〇〇〇年七月十七');
Expand All @@ -584,8 +584,8 @@ describe('Astrolabe', () => {
expect(result.palaces[11].decadal).toStrictEqual({ range: [53, 62], heavenlyStem: '己', earthlyBranch: '丑' });
});

test('astrolabeBySolarDate() fix leap month', () => {
const result = astro.astrolabeBySolarDate('2023-4-10', 4, '女', true);
test('bySolar() fix leap month', () => {
const result = astro.bySolar('2023-4-10', 4, '女', true);

expect(result).toHaveProperty('earthlyBranchOfSoulPalace', '子');
expect(result).toHaveProperty('earthlyBranchOfBodyPalace', '申');
Expand All @@ -595,8 +595,8 @@ describe('Astrolabe', () => {
expect(result.star('紫微').palace()).toHaveProperty('name', '迁移');
});

test('astrolabeBySolarDate() use default fixLeap', () => {
const result = astro.astrolabeBySolarDate('2023-4-10', 4, '女');
test('bySolar() use default fixLeap', () => {
const result = astro.bySolar('2023-4-10', 4, '女');

expect(result).toHaveProperty('earthlyBranchOfSoulPalace', '子');
expect(result).toHaveProperty('earthlyBranchOfBodyPalace', '申');
Expand All @@ -606,8 +606,8 @@ describe('Astrolabe', () => {
expect(result.star('紫微').palace()).toHaveProperty('name', '迁移');
});

test('astrolabeBySolarDate() do not fix leap month', () => {
const result = astro.astrolabeBySolarDate('2023-4-10', 4, '女', false);
test('bySolar() do not fix leap month', () => {
const result = astro.bySolar('2023-4-10', 4, '女', false);

expect(result).toHaveProperty('earthlyBranchOfSoulPalace', '亥');
expect(result).toHaveProperty('earthlyBranchOfBodyPalace', '未');
Expand All @@ -617,8 +617,8 @@ describe('Astrolabe', () => {
expect(result.star('紫微').palace()).toHaveProperty('name', '命宫');
});

test('astrolabeByLunarDate() fix leap month', () => {
const result = astro.astrolabeByLunarDate('2023-2-20', 4, '女', true, true);
test('byLunar() fix leap month', () => {
const result = astro.byLunar('2023-2-20', 4, '女', true, true);

expect(result).toHaveProperty('earthlyBranchOfSoulPalace', '子');
expect(result).toHaveProperty('earthlyBranchOfBodyPalace', '申');
Expand All @@ -628,8 +628,8 @@ describe('Astrolabe', () => {
expect(result.star('紫微').palace()).toHaveProperty('name', '迁移');
});

test('astrolabeByLunarDate() use default isLeapMonth', () => {
const result = astro.astrolabeByLunarDate('2023-2-20', 4, '女');
test('byLunar() use default isLeapMonth', () => {
const result = astro.byLunar('2023-2-20', 4, '女');

expect(result).toHaveProperty('earthlyBranchOfSoulPalace', '亥');
expect(result).toHaveProperty('earthlyBranchOfBodyPalace', '未');
Expand All @@ -639,8 +639,8 @@ describe('Astrolabe', () => {
expect(result.star('紫微').palace()).toHaveProperty('name', '命宫');
});

test('astrolabeByLunarDate() do not fix leap month', () => {
const result = astro.astrolabeByLunarDate('2023-2-20', 4, '女', true, false);
test('byLunar() do not fix leap month', () => {
const result = astro.byLunar('2023-2-20', 4, '女', true, false);

expect(result).toHaveProperty('earthlyBranchOfSoulPalace', '亥');
expect(result).toHaveProperty('earthlyBranchOfBodyPalace', '未');
Expand Down
48 changes: 47 additions & 1 deletion src/astro/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { getPalaceNames, getSoulAndBody, getHoroscope, getFiveElementsClass } fr
/**
* 通过阳历获取星盘信息
*
* @deprecated 此方法已在`v2.0.5`废弃,请用 `bySolar` 方法替换,参数不变
*
* @param solarDateStr 阳历日期【YYYY-M-D】
* @param timeIndex 出生时辰序号【0~12】
* @param gender 性别【男|女】
Expand All @@ -31,6 +33,26 @@ export const astrolabeBySolarDate = (
gender: GenderName,
fixLeap: boolean = true,
language?: Language,
) => {
return bySolar(solarDateStr, timeIndex, gender, fixLeap, language);
};

/**
* 通过阳历获取星盘信息
*
* @param solarDateStr 阳历日期【YYYY-M-D】
* @param timeIndex 出生时辰序号【0~12】
* @param gender 性别【男|女】
* @param fixLeap 是否调整闰月情况【默认 true】,假入调整闰月,则闰月的前半个月算上个月,后半个月算下个月
* @param language 输出语言
* @returns 星盘信息
*/
export const bySolar = (
solarDateStr: string,
timeIndex: number,
gender: GenderName,
fixLeap: boolean = true,
language?: Language,
) => {
language && setLanguage(language);

Expand Down Expand Up @@ -111,6 +133,8 @@ export const astrolabeBySolarDate = (
/**
* 通过农历获取星盘信息
*
* @deprecated 此方法已在`v2.0.5`废弃,请用 `byLunar` 方法替换,参数不变
*
* @param lunarDateStr 农历日期【YYYY-M-D】,例如2000年七月十七则传入 2000-7-17
* @param timeIndex 出生时辰序号【0~12】
* @param gender 性别【男|女】
Expand All @@ -126,10 +150,32 @@ export const astrolabeByLunarDate = (
isLeapMonth: boolean = false,
fixLeap: boolean = true,
language?: Language,
) => {
return byLunar(lunarDateStr, timeIndex, gender, isLeapMonth, fixLeap, language);
};

/**
* 通过农历获取星盘信息
*
* @param lunarDateStr 农历日期【YYYY-M-D】,例如2000年七月十七则传入 2000-7-17
* @param timeIndex 出生时辰序号【0~12】
* @param gender 性别【男|女】
* @param isLeapMonth 是否闰月【默认 false】,当实际月份没有闰月时该参数不生效
* @param fixLeap 是否调整闰月情况【默认 true】,假入调整闰月,则闰月的前半个月算上个月,后半个月算下个月
* @param language 输出语言
* @returns 星盘数据
*/
export const byLunar = (
lunarDateStr: string,
timeIndex: number,
gender: GenderName,
isLeapMonth: boolean = false,
fixLeap: boolean = true,
language?: Language,
) => {
const solarDate = lunar2solar(lunarDateStr, isLeapMonth);

return astrolabeBySolarDate(solarDate.toString(), timeIndex, gender, fixLeap, language);
return bySolar(solarDate.toString(), timeIndex, gender, fixLeap, language);
};

/**
Expand Down
3 changes: 1 addition & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const package = require('./package.json');

module.exports = {
mode: 'none',
devtool: 'source-map', // 输出sourcemap
entry: './src/index.ts', // 指定入口文件
output: {
path: path.resolve(__dirname, 'dist'), // 指定打包文件的目录
filename: `iztro-${package.version}.min.js`, // 打包后文件的名称
filename: `iztro.min.js`, // 打包后文件的名称
library: 'iztro', // 将打包后的代码作为一个全局变量可直接调用
libraryTarget: 'umd', // 将代码打包为通用模块定义
umdNamedDefine: true, // 为UMD模块命名
Expand Down

0 comments on commit 4c4f6d5

Please sign in to comment.