tiny-ja-money 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/API.md CHANGED
@@ -98,6 +98,8 @@ A rate string uses an optional ASCII sign, one or more ASCII digits, and an opti
98
98
 
99
99
  All listed functions return `bigint`. Amount arguments are `YenAmount`. Tax rates must be nonnegative. A tax conversion rounds the tax component once so tax plus net always equals gross. For example, with `"0.1"` and `floor`, the tax included in `1101n` is `100n` and the derived net is `1001n`. Applying the same policy to that net yields a gross of `1101n`. Other inputs and modes can lose information in the first rounding step, so reversing a conversion can give a different value. Applications choose rates and rounding for each transaction and decide when to aggregate amounts.
100
100
 
101
+ At a rate of `"0.1"` with `floor`, a gross amount of `10n` contains `0n` of tax and yields a net amount of `10n`. Computing the gross from that net with the same policy gives `11n`. When the gross is the input, keep that amount alongside the net and included tax so the breakdown adds up to the original total.
102
+
101
103
  | Mode | Rule | `2.5` | `-2.5` | `-1.5` |
102
104
  | --- | --- | ---: | ---: | ---: |
103
105
  | `floor` | Toward negative infinity | `2` | `-3` | `-2` |
@@ -109,16 +111,21 @@ All listed functions return `bigint`. Amount arguments are `YenAmount`. Tax rate
109
111
  ## Splitting and allocation
110
112
 
111
113
  ```ts
112
- function splitAmount(value: YenAmount, count: number): bigint[]
114
+ interface SplitAmountOptions {
115
+ maxCount?: number
116
+ }
117
+ function splitAmount(value: YenAmount, count: number, options?: SplitAmountOptions): bigint[]
113
118
  function allocateAmount(value: YenAmount, ratios: readonly YenAmount[]): bigint[]
114
119
  ```
115
120
 
116
- - `splitAmount` requires a positive integer `count` within JavaScript's array-length range and raises `RangeError` outside it. Each result differs from the others by at most one yen. Extra yen go to the earliest entries. The function returns `count` entries, so choose a count suitable for the available memory.
121
+ - `splitAmount` requires `count` to be an integer from 1 to 4,294,967,295 and returns that many entries. Set `maxCount` to an integer in the same range to cap the number of entries. An invalid count, an invalid limit, or a count above the limit raises `RangeError` before allocating the array. Omitting `maxCount` uses the full array-length range. Each share differs from the others by at most one yen. Extra yen go to the earliest entries.
117
122
  - `allocateAmount` requires a nonempty array of nonnegative integer weights with a positive total. To represent fractional weights, scale them to a common integer denominator. The algorithm assigns the whole-yen portion of each exact quota, distributes leftover yen to the largest fractional remainders, and resolves ties in input order. A zero weight always gets `0n`. Invalid array or weight types raise `TypeError`. Negative weights, unsafe integer weights, or a zero total raise `RangeError`.
118
123
  - Both algorithms distribute the magnitude, then restore the original sign. The result sums exactly to the original amount and preserves the input order of recipients.
119
124
 
120
125
  ```ts
121
126
  splitAmount(-1000, 3) // [-334n, -333n, -333n]
127
+ splitAmount(1000, 3, { maxCount: 3 }) // [334n, 333n, 333n]
128
+ splitAmount(1000, 4, { maxCount: 3 }) // throws RangeError
122
129
  allocateAmount(2, [2, 3, 4]) // [0n, 1n, 1n]
123
130
  allocateAmount(-2, [2, 3, 4]) // [0n, -1n, -1n]
124
131
  ```
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.2 (unreleased)
4
+
5
+ - Added an optional `maxCount` to `splitAmount` to limit the number of entries before allocating the result.
6
+ - Added a tax rounding example that shows how to preserve the original gross amount alongside its net and tax components.
7
+ - Fixed package verification when npm's dry-run setting is inherited and added CI coverage for both settings.
8
+
3
9
  ## 0.1.1 (unreleased)
4
10
 
5
11
  - Added GitHub and live demo links for `ja-char-check`, `ja-ime-utils`, `ja-input-mask`, and `ja-zengin-utils` to both READMEs and the demo.
package/README.ja.md CHANGED
@@ -14,21 +14,21 @@ npm install tiny-ja-money
14
14
 
15
15
  名前付き ESM エクスポートと TypeScript 型定義を提供します。BigInt をサポートする ES2020 対応ブラウザと Node.js で利用でき、ランタイム依存はありません。
16
16
 
17
- ソースから使う場合は、このディレクトリで `npm ci`、`npm run build`、`npm pack` を実行し、生成された `tiny-ja-money-0.1.1.tgz` を利用側のプロジェクトにインストールします。
17
+ ソースから使う場合は、このディレクトリで `npm ci`、`npm run build`、`npm pack` を実行し、生成された `tiny-ja-money-0.1.2.tgz` を利用側のプロジェクトにインストールします。
18
18
 
19
19
  ### CDN からブラウザで使う
20
20
 
21
- `0.1.1` の npm 公開後は、`<script type="module">` から ESM を直接読み込めます。URL には利用する公開版のバージョンを指定します。
21
+ `0.1.2` の npm 公開後は、`<script type="module">` から ESM を直接読み込めます。URL には利用する公開版のバージョンを指定します。
22
22
 
23
23
  ```html
24
24
  <script type="module">
25
- import { parseYen, formatYen } from "https://unpkg.com/tiny-ja-money@0.1.1/dist/index.js"
25
+ import { parseYen, formatYen } from "https://unpkg.com/tiny-ja-money@0.1.2/dist/index.js"
26
26
 
27
27
  console.log(formatYen(parseYen("三万五千円"))) // "35,000円"
28
28
  </script>
29
29
  ```
30
30
 
31
- jsDelivr から読み込む場合は `https://cdn.jsdelivr.net/npm/tiny-ja-money@0.1.1/dist/index.js` を指定します。
31
+ jsDelivr から読み込む場合は `https://cdn.jsdelivr.net/npm/tiny-ja-money@0.1.2/dist/index.js` を指定します。
32
32
 
33
33
  ## 使用例
34
34
 
@@ -52,7 +52,7 @@ formatJapaneseAmount(125000000) // "1億2500万円"
52
52
  formatJapaneseAmount(125000000n, { digits: "kanji" }) // "一億二千五百万円"
53
53
  calculateTaxInclusive(1005, "0.1", { rounding: "floor" }) // 1105n
54
54
  calculateTaxExclusive(1101, "0.1", { rounding: "floor" }) // 1001n
55
- splitAmount(1000, 3) // [334n, 333n, 333n]
55
+ splitAmount(1000, 3, { maxCount: 1000 }) // [334n, 333n, 333n]
56
56
  allocateAmount(1000, [1, 2, 3]) // [167n, 333n, 500n]
57
57
  ```
58
58
 
@@ -62,7 +62,7 @@ allocateAmount(1000, [1, 2, 3]) // [167n, 333n, 500n]
62
62
  - 既定で厳密に解析します。全角数字、円記号、漢数字、単位による表記、負数を扱います。`{ trim: true }` で文字列の前後の空白を許容できます。文法と失敗理由は [解析 API](API.md#parsing) に記載しています。
63
63
  - `formatYen` はすべての桁を保持します。`formatJapaneseAmount` は絶対値が `10^16` 円未満の金額を万・億・兆で表します。区切りと記号を固定し、実行環境のロケールによる表記の変化を避けています。
64
64
  - 税率・割引率は `"0.1"` のような十進文字列か `{ numerator: 1n, denominator: 10n }` のような整数の分数で指定します。丸め方式の指定は必須です。税額は、渡された率・金額・丸め方式に従って計算します。[端数処理と算式](API.md#arithmetic-tax-and-discounts)
65
- - 均等分割では先頭から余りを配り、比率按分では余りの大きい順に配ります。同順位は入力順です。負の金額でも配分後の合計は元の金額に一致します。
65
+ - 均等分割では先頭から余りを配り、比率按分では余りの大きい順に配ります。同順位は入力順です。負の金額でも配分後の合計は元の金額に一致します。分割数をユーザーから受け取る場合は、`splitAmount` の `maxCount` で上限を指定できます。
66
66
 
67
67
  ## ライブデモ
68
68
 
package/README.md CHANGED
@@ -14,21 +14,21 @@ npm install tiny-ja-money
14
14
 
15
15
  The package supplies named ESM exports and TypeScript declarations for BigInt-capable browser and Node.js runtimes. It targets ES2020 and has zero runtime dependencies.
16
16
 
17
- To build from source, run `npm ci`, `npm run build`, and `npm pack` in this checkout. Install the resulting `tiny-ja-money-0.1.1.tgz` in the consuming project.
17
+ To build from source, run `npm ci`, `npm run build`, and `npm pack` in this checkout. Install the resulting `tiny-ja-money-0.1.2.tgz` in the consuming project.
18
18
 
19
19
  ### Browser ESM via CDN
20
20
 
21
- Once `0.1.1` is on npm, import its ESM entry point from a `<script type="module">`. Pin the published version you want to load.
21
+ Once `0.1.2` is on npm, import its ESM entry point from a `<script type="module">`. Pin the published version you want to load.
22
22
 
23
23
  ```html
24
24
  <script type="module">
25
- import { parseYen, formatYen } from "https://unpkg.com/tiny-ja-money@0.1.1/dist/index.js"
25
+ import { parseYen, formatYen } from "https://unpkg.com/tiny-ja-money@0.1.2/dist/index.js"
26
26
 
27
27
  console.log(formatYen(parseYen("三万五千円"))) // "35,000円"
28
28
  </script>
29
29
  ```
30
30
 
31
- The equivalent jsDelivr URL is `https://cdn.jsdelivr.net/npm/tiny-ja-money@0.1.1/dist/index.js`.
31
+ The equivalent jsDelivr URL is `https://cdn.jsdelivr.net/npm/tiny-ja-money@0.1.2/dist/index.js`.
32
32
 
33
33
  ## Usage
34
34
 
@@ -51,7 +51,7 @@ formatJapaneseAmount(125000000) // "1億2500万円"
51
51
  formatJapaneseAmount(125000000n, { digits: "kanji" }) // "一億二千五百万円"
52
52
  calculateTax(1005, "0.1", { rounding: "floor" }) // 100n
53
53
  calculateTaxInclusive(1005, "0.1", { rounding: "floor" }) // 1105n
54
- splitAmount(1000, 3) // [334n, 333n, 333n]
54
+ splitAmount(1000, 3, { maxCount: 1000 }) // [334n, 333n, 333n]
55
55
  allocateAmount(1000, [1, 2, 3]) // [167n, 333n, 500n]
56
56
  ```
57
57
 
@@ -61,7 +61,7 @@ allocateAmount(1000, [1, 2, 3]) // [167n, 333n, 500n]
61
61
  - Parsing is strict by default. It accepts `¥` or `¥` before the numeral or `円` after it, fullwidth digits, kanji numerals, and strictly ordered units through `兆`. Set `{ trim: true }` to allow whitespace around the input. See the [grammar and error table](API.md#parsing).
62
62
  - `formatYen` always emits exact digits. `formatJapaneseAmount` supports magnitudes below `10^16` yen. Formatting uses fixed separators and symbols so the result is independent of runtime locale data.
63
63
  - Rates are exact decimal strings such as `"0.1"` or fractions such as `{ numerator: 1n, denominator: 10n }`. Callers explicitly supply the rate and rounding policy. Tax functions perform arithmetic for the given amount and policy. The [tax formulas](API.md#arithmetic-tax-and-discounts) describe which component is rounded.
64
- - Equal splits and weighted allocations preserve the original amount, including negative amounts. Weighted allocation uses largest remainders with ties resolved in input order.
64
+ - Equal splits and weighted allocations preserve the original amount, including negative amounts. Set `maxCount` on `splitAmount` to cap the number of entries when accepting user input. Weighted allocation uses largest remainders with ties resolved in input order.
65
65
 
66
66
  ## Live demo
67
67
 
@@ -1,3 +1,7 @@
1
1
  import type { YenAmount } from "./amount.js";
2
- export declare function splitAmount(value: YenAmount, count: number): bigint[];
2
+ export interface SplitAmountOptions {
3
+ /** Limit the number of entries before allocating the result array. */
4
+ maxCount?: number;
5
+ }
6
+ export declare function splitAmount(value: YenAmount, count: number, options?: SplitAmountOptions): bigint[];
3
7
  export declare function allocateAmount(value: YenAmount, ratios: readonly YenAmount[]): bigint[];
@@ -1,8 +1,17 @@
1
1
  import { abs, toBigInt } from "./amount.js";
2
- export function splitAmount(value, count) {
3
- if (!Number.isInteger(count) || count < 1 || count > 4294967295) {
2
+ const MAX_ARRAY_LENGTH = 4294967295;
3
+ export function splitAmount(value, count, options = {}) {
4
+ if (!Number.isInteger(count) || count < 1 || count > MAX_ARRAY_LENGTH) {
4
5
  throw new RangeError("Split count must be a positive array length");
5
6
  }
7
+ const { maxCount } = options;
8
+ if (maxCount !== undefined) {
9
+ if (!Number.isInteger(maxCount) || maxCount < 1 || maxCount > MAX_ARRAY_LENGTH) {
10
+ throw new RangeError("Split maxCount must be a positive array length");
11
+ }
12
+ if (count > maxCount)
13
+ throw new RangeError("Split count exceeds maxCount");
14
+ }
6
15
  const amount = toBigInt(value);
7
16
  const divisor = BigInt(count);
8
17
  const share = amount / divisor;
package/dist/index.d.ts CHANGED
@@ -7,3 +7,4 @@ export type { FormatJapaneseAmountOptions, FormatYenOptions, YenDisplayOptions }
7
7
  export { addYen, applyDiscount, calculateIncludedTax, calculateTax, calculateTaxExclusive, calculateTaxInclusive, multiplyYen, subtractYen, } from "./calculate.js";
8
8
  export type { Rate, RoundingMode, RoundingOptions } from "./calculate.js";
9
9
  export { allocateAmount, splitAmount } from "./distribute.js";
10
+ export type { SplitAmountOptions } from "./distribute.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-ja-money",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Tiny TypeScript utilities for parsing, formatting, and calculating Japanese yen.",
5
5
  "type": "module",
6
6
  "sideEffects": false,