@kikuchan/bigdate 0.0.1-alpha.0
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/README.md +219 -0
- package/index.cjs +1 -0
- package/index.d.cts +75 -0
- package/index.d.ts +75 -0
- package/index.js +1 -0
- package/package.json +30 -0
package/README.md
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# @kikuchan/decimal
|
|
2
|
+
|
|
3
|
+
Arbitrary precision decimal arithmetic for TypeScript and JavaScript. Avoids binary floating-point rounding errors.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @kikuchan/decimal
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { Decimal } from '@kikuchan/decimal';
|
|
15
|
+
|
|
16
|
+
const price = Decimal('12.345');
|
|
17
|
+
const quantity = Decimal(3);
|
|
18
|
+
const total = price.mul(quantity).round(2);
|
|
19
|
+
|
|
20
|
+
console.log(total.toString()); // "37.04"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Creating Decimals
|
|
24
|
+
|
|
25
|
+
The `Decimal()` constructor accepts numbers, strings, bigints, or existing Decimal instances:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
Decimal(100) // from number
|
|
29
|
+
Decimal('12.345') // from string
|
|
30
|
+
Decimal('1.5e2') // scientific notation
|
|
31
|
+
Decimal(12345n) // from bigint
|
|
32
|
+
Decimal(existing) // from another Decimal
|
|
33
|
+
|
|
34
|
+
// Nullable values pass through unchanged
|
|
35
|
+
Decimal(null) // returns null
|
|
36
|
+
Decimal(undefined) // returns undefined
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## API Overview
|
|
40
|
+
|
|
41
|
+
### Arithmetic Operations
|
|
42
|
+
|
|
43
|
+
All operations are **immutable by default** and return new Decimal instances:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
const a = Decimal('10.5');
|
|
47
|
+
const b = Decimal('2.3');
|
|
48
|
+
|
|
49
|
+
a.add(b) // addition
|
|
50
|
+
a.sub(b) // subtraction
|
|
51
|
+
a.mul(b) // multiplication
|
|
52
|
+
a.div(b) // division (default 18 decimal places)
|
|
53
|
+
a.div(b, 5) // division with custom precision
|
|
54
|
+
|
|
55
|
+
a.mod(b) // modulo (same sign rules as JavaScript %)
|
|
56
|
+
a.modPositive(b) // always non-negative remainder
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Mutable Operations
|
|
60
|
+
|
|
61
|
+
Methods ending with `$` modify the value in place and return `this`:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
const value = Decimal('1.2345');
|
|
65
|
+
value.round$(2); // value is now 1.23
|
|
66
|
+
|
|
67
|
+
// Both styles support chaining
|
|
68
|
+
value.add$(1).mul$(2).round$(0);
|
|
69
|
+
Decimal('10').add(5).mul(2).round(0); // 30
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Rounding & Precision
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const num = Decimal('12.3456');
|
|
76
|
+
|
|
77
|
+
num.round(2) // 12.35 (half away from zero)
|
|
78
|
+
num.floor(2) // 12.34
|
|
79
|
+
num.ceil(2) // 12.35
|
|
80
|
+
num.trunc(2) // 12.34
|
|
81
|
+
|
|
82
|
+
// Negative precision rounds to powers of 10
|
|
83
|
+
Decimal('1234').round(-2) // 1200 (nearest hundred)
|
|
84
|
+
|
|
85
|
+
// Round to specific step sizes
|
|
86
|
+
Decimal('12.7').roundBy('0.5') // 13.0
|
|
87
|
+
Decimal('47').roundBy(10) // 50
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**Precision control:**
|
|
91
|
+
- Positive values: digits after decimal point
|
|
92
|
+
- Zero: round to whole units
|
|
93
|
+
- Negative values: powers of ten (e.g., `-2` = hundreds)
|
|
94
|
+
|
|
95
|
+
### Sign & Absolute Value
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const num = Decimal('-5.5');
|
|
99
|
+
|
|
100
|
+
num.neg() // 5.5
|
|
101
|
+
num.abs() // 5.5
|
|
102
|
+
num.isNegative() // true
|
|
103
|
+
num.isPositive() // false
|
|
104
|
+
num.isZero() // false
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Comparison
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
const a = Decimal('10');
|
|
111
|
+
const b = Decimal('20');
|
|
112
|
+
|
|
113
|
+
a.cmp(b) // -1 (less), 0 (equal), or 1 (greater)
|
|
114
|
+
a.eq(b) // false
|
|
115
|
+
a.lt(b) // true
|
|
116
|
+
a.le(b) // true
|
|
117
|
+
a.gt(b) // false
|
|
118
|
+
a.ge(b) // false
|
|
119
|
+
|
|
120
|
+
a.between(5, 15) // true
|
|
121
|
+
a.between(15, 25) // false
|
|
122
|
+
a.isCloseTo(10.001, 0.01) // true
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Advanced Math
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const num = Decimal('100');
|
|
129
|
+
|
|
130
|
+
num.sqrt() // square root
|
|
131
|
+
num.root(3) // cube root
|
|
132
|
+
num.pow(2) // exponentiation
|
|
133
|
+
num.pow('0.5') // fractional exponents (non-negative bases only)
|
|
134
|
+
num.log(10) // logarithm (base 10)
|
|
135
|
+
num.inverse() // 1 / num
|
|
136
|
+
|
|
137
|
+
// All accept optional precision argument
|
|
138
|
+
num.sqrt(10) // 10 decimal places
|
|
139
|
+
num.log(10, 20) // 20 decimal places
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Decimal Point Manipulation
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
const num = Decimal('12.345');
|
|
146
|
+
|
|
147
|
+
num.shift10(2) // 1234.5 (shift right = multiply by 10²)
|
|
148
|
+
num.shift10(-1) // 1.2345 (shift left = divide by 10)
|
|
149
|
+
|
|
150
|
+
// Split into integer and fractional parts
|
|
151
|
+
const [whole, frac] = num.split(2); // [12.34, 0.005]
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Conversion
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
const num = Decimal('12.345');
|
|
158
|
+
|
|
159
|
+
num.toString() // "12.345"
|
|
160
|
+
num.toFixed(2) // "12.35"
|
|
161
|
+
num.number() // 12.345 (native number)
|
|
162
|
+
num.integer() // 12n (bigint, truncated)
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Utilities
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
Decimal('12.3000').rescale() // "12.3" (remove trailing zeros)
|
|
169
|
+
|
|
170
|
+
const a = Decimal('5.5');
|
|
171
|
+
a.clamp(0, 10) // 5.5 (bounded)
|
|
172
|
+
a.clamp(6, 10) // 6 (clamped to minimum)
|
|
173
|
+
|
|
174
|
+
// Exported utility functions
|
|
175
|
+
import { min, max, minmax, pow10, isDecimal } from '@kikuchan/decimal';
|
|
176
|
+
|
|
177
|
+
min(1, 2, 3) // Decimal(1)
|
|
178
|
+
max('1.5', '2.5', '3.5') // Decimal(3.5)
|
|
179
|
+
minmax(5, 1, 3) // [Decimal(1), Decimal(5)]
|
|
180
|
+
pow10(3) // Decimal(1000)
|
|
181
|
+
isDecimal(value) // type guard
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Rounding Modes
|
|
185
|
+
|
|
186
|
+
All rounding operations support these modes:
|
|
187
|
+
|
|
188
|
+
- `'round'` — half away from zero (default)
|
|
189
|
+
- `'floor'` — toward negative infinity
|
|
190
|
+
- `'ceil'` — toward positive infinity
|
|
191
|
+
- `'trunc'` — toward zero
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
const num = Decimal('12.75');
|
|
195
|
+
num.round(1, 'round') // 12.8
|
|
196
|
+
num.round(1, 'floor') // 12.7
|
|
197
|
+
num.round(1, 'ceil') // 12.8
|
|
198
|
+
num.round(1, 'trunc') // 12.7
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Important Notes
|
|
202
|
+
|
|
203
|
+
- **Immutability**: Operations return new instances unless using `$` methods
|
|
204
|
+
- **Precision**: Division and advanced math default to 18 decimal places
|
|
205
|
+
- **No silent overflow**: All operations maintain arbitrary precision
|
|
206
|
+
- **Null-safe**: `Decimal(null)` and `Decimal(undefined)` pass through unchanged
|
|
207
|
+
|
|
208
|
+
## Development
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
pnpm test # Run tests
|
|
212
|
+
pnpm build # Build package
|
|
213
|
+
pnpm lint # Lint source
|
|
214
|
+
pnpm format # Check formatting
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## License
|
|
218
|
+
|
|
219
|
+
See LICENSE file for details.
|
package/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
let e=require(`@kikuchan/decimal`);const t=86400n,n=3600n,r=60n,i=(0,e.Decimal)(-8640000000000000n),a=(0,e.Decimal)(8640000000000000n);function o(t,n){if(typeof t==`bigint`)return t;if(typeof t==`number`){if(!Number.isFinite(t)||!Number.isInteger(t))throw Error(`${n} must be an integer`);return BigInt(t)}let r=(0,e.Decimal)(t),i=r.integer();if(!r.eq(i))throw Error(`${n} must be an integer`);return i}function s(e,t){let n=e/t;return e%t===0n||e<0n==t<0n?n:n-1n}function c(e,t){let n=t-1n,r=s(n,12n),i=n-r*12n+1n;return{year:e+r,month:i}}function l(e,t,n){return h(m(e,t,n))}function u(e,t,n=0n){if(!t)return e;if(Array.isArray(t))return t.map(e=>BigInt(e)).filter(t=>t<=e).toSorted((e,t)=>e<t?-1:e>t?1:0).at(-1)??e;let r=BigInt(t);return(e-n)/r*r+n}function d(e,t,n=0n){if(!t)return e;if(Array.isArray(t))return t.map(e=>BigInt(e)).filter(t=>e<t).toSorted((e,t)=>e<t?-1:e>t?1:0).at(0);let r=BigInt(t);return((e-n)/r+1n)*r+n}function f(e,t,n){if(!t)return e;if(!n?.era)return u(e,t,0n);if(e>0n){let n=u(e,t,0n);return n===0n?1n:n}let r=d(-e+1n-1n,t,0n);return r===void 0?e:-(r-1n)}function p(e,t,n){if(!t)return e;if(!n?.era||e>0n)return d(e,t,0n);let r=u(-e+1n-1n,t,0n);return Array.isArray(t)&&!t.map(e=>BigInt(e)).some(e=>e===r)||r<=0n?1n:-(r-1n)}function m(e,t,n){let{year:r,month:i}=c(e,t),a=n-1n,o=r,l=i;o-=l<=2n?1n:0n;let u=s(o,400n),d=o-u*400n,f=s(153n*(l+(l>2n?-3n:9n))+2n,5n),p=d*365n+s(d,4n)-s(d,100n)+f;return u*146097n+p-719468n+a}function h(e){let t=e+719468n,n=s(t,146097n),r=t-n*146097n,i=s(r-s(r,1460n)+s(r,36524n)-s(r,146096n),365n),a=i+n*400n,o=r-(365n*i+s(i,4n)-s(i,100n)),c=s(5n*o+2n,153n),l=o-s(153n*c+2n,5n)+1n,u=c+(c<10n?3n:-9n);a+=u<=2n?1n:0n;let d=Number(((e+4n)%7n+7n)%7n);return{year:a,month:u,day:l,weekday:d}}function g(e){let t=g.cache??new Map;g.cache=t;let n=t.get(e);return n||(n=new Intl.DateTimeFormat(`en-US`,{timeZone:e,year:`numeric`,month:`2-digit`,day:`2-digit`,hour:`2-digit`,minute:`2-digit`,second:`2-digit`,hourCycle:`h23`}),t.set(e,n)),n}g.cache=void 0;function _(o,s){if(s.toLowerCase()===`utc`)return 0;let c=o.mul(1e3).clamp(i,a),l=new Date(c.number());if(s.toLowerCase()===`local`)return l.getTimezoneOffset();let u=new Map(g(s).formatToParts(l).map(e=>[e.type,e.value])),d=BigInt(u.get(`year`)??`0`),f=BigInt(u.get(`month`)??`0`),p=BigInt(u.get(`day`)??`0`),h=BigInt(u.get(`hour`)??`0`),_=BigInt(u.get(`minute`)??`0`),v=BigInt(u.get(`second`)??`0`),y=(0,e.Decimal)(m(d,f,p)).mul(t).add(h*n+_*r+v).add((0,e.Decimal)(l.getUTCMilliseconds()).div(1e3));return c.div(1e3).sub(y).div(60).trunc(0,!0).number()}function v(i,a){let o=_(i,a),c=o===0?i:i.sub((0,e.Decimal)(o).mul(r)),l=s(c.floor(0).integer(),t),u=c.sub((0,e.Decimal)(l*t)),d=u.floor(0).integer(),f=u.sub((0,e.Decimal)(d)),p=d/n,m=d%n/r,g=d%r,{year:v,month:y,day:b,weekday:x}=h(l);return{year:v,month:y,day:b,hour:p,minutes:m,seconds:(0,e.Decimal)(g).add(f),weekday:x}}function y(i,a){let o=(0,e.Decimal)(m(i.year,i.month,i.day)).mul(t).add((0,e.Decimal)(i.hour*n+i.minutes*r).add(i.seconds));if(a.toLowerCase()===`utc`)return o;let s=o;for(let t=0;t<4;t++){let t=_(s,a),n=o.add((0,e.Decimal)(t).mul(r));if(n.eq(s))return n;s=n}return s}function b(t){let n=o(t.year,`year`),r=o(t.month,`month`),i=o(t.day,`day`);return{year:n,month:r,day:i,hour:t.hour==null?0n:o(t.hour,`hour`),minutes:t.minutes==null?0n:o(t.minutes,`minutes`),seconds:t.seconds==null?(0,e.Decimal)(0):(0,e.Decimal)(t.seconds),weekday:h(m(n,r,i)).weekday}}function x(e){return e||`utc`}var S=class t{#e;#t;constructor(...t){if(t.length===0){this.#e=(0,e.Decimal)(Date.now()).div(1e3),this.#t=`utc`;return}let n=(t.length===7||t.length===2)&&typeof t[t.length-1]==`string`?x(t[t.length-1]):`utc`;if(t.length>=3&&typeof t[0]!=`object`){let[e,r,i,a,o,s]=t;this.#e=y(b({year:e,month:r,day:i,hour:a,minutes:o,seconds:s}),n),this.#t=n;return}if(this.#t=n,t[0]instanceof Date){this.#e=(0,e.Decimal)(t[0].getTime()).div(1e3);return}this.#e=(0,e.Decimal)(t[0])}static fromEpoch(e,n=`utc`){return new t(e,n)}static fromDate(e,n=`utc`){return new t(e,n)}static fromCalendar(e,n=`utc`){return new t(y(b(e),n),n)}clone(){return new t(this.#e,this.#t)}zone(e){return e===void 0?this.#t:new t(this.#e,e)}zone$(e){return this.#t=e,this}utc(){return this.zone(`utc`)}utc$(){return this.zone$(`utc`)}local(){return this.zone(`local`)}local$(){return this.zone$(`local`)}epoch(e){return e===void 0?this.#e.clone():new t(e,this.#t)}epoch$(t){return this.#e=(0,e.Decimal)(t),this}object(){return v(this.#e,this.#t)}#n(e,n){let r=this.object(),i=y({year:e.year??r.year,month:e.month??r.month,day:e.day??r.day,hour:e.hour??r.hour,minutes:e.minutes??r.minutes,seconds:e.seconds??r.seconds,weekday:r.weekday},this.#t);return n?(this.#e=i,this):new t(i,this.#t)}#r(n){let r=this.object();return new t(y({...n(r),hour:0n,minutes:0n,seconds:(0,e.Decimal)(0),weekday:r.weekday},this.#t),this.#t)}year(e){return e===void 0?this.object().year:this.#n({year:o(e,`year`)},!1)}year$(e){return this.#n({year:o(e,`year`)},!0)}month(e){return e===void 0?this.object().month:this.#n({month:o(e,`month`)},!1)}month$(e){return this.#n({month:o(e,`month`)},!0)}day(e){return e===void 0?this.object().day:this.#n({day:o(e,`day`)},!1)}day$(e){return this.#n({day:o(e,`day`)},!0)}hour(e){return e===void 0?this.object().hour:this.#n({hour:o(e,`hour`)},!1)}hour$(e){return this.#n({hour:o(e,`hour`)},!0)}minutes(e){return e===void 0?this.object().minutes:this.#n({minutes:o(e,`minutes`)},!1)}minutes$(e){return this.#n({minutes:o(e,`minutes`)},!0)}seconds(t){return t===void 0?this.object().seconds.clone():this.#n({seconds:(0,e.Decimal)(t)},!1)}seconds$(t){return this.#n({seconds:(0,e.Decimal)(t)},!0)}weekday(){return this.object().weekday}alignToDay(e){return this.#r(t=>l(t.year,t.month,u(t.day,e,1n)))}nextDay(e){return this.#r(t=>{let n=d(t.day,e,1n);if(n===void 0){let{year:e,month:n}=c(t.year,t.month+1n);return{year:e,month:n,day:1n}}return l(t.year,t.month,n)})}alignToMonth(e){return this.#r(t=>{let{year:n,month:r}=c(t.year,u(t.month,e,1n));return{year:n,month:r,day:1n}})}nextMonth(e){return this.#r(t=>{let{year:n,month:r}=c(t.year,d(t.month,e,1n));return{year:n,month:r,day:1n}})}alignToYear(e,t){return this.#r(n=>({year:f(n.year,e,t),month:1n,day:1n}))}nextYear(e,t){return this.#r(n=>({year:p(n.year,e,t),month:1n,day:1n}))}alignToSecond(n){let r=this.alignToDay(1n);return new t(this.#e.sub(r.epoch()).floorBy((0,e.Decimal)(n)).add(r.epoch()),this.#t)}format(e){let t=this.object(),n=t.year.toString(),r=t.month.toString(),i=t.day.toString(),a=t.hour.toString(),o=t.minutes.toString(),s=t.seconds,c=s.toString(),l=c.indexOf(`.`),u=l>=0?c.slice(l+1):``,d=t.year<=0?`BC `:``,f=t.year>0?` AD`:``,p=t.year<=0?(1n-t.year).toString():t.year.toString(),m={gggg:`${d}${p.padStart(4,`0`)}`,GGGG:`${d}${p.padStart(4,`0`)}${f}`,g:`${d}${p}`,G:`${d}${p}${f}`,yyyy:n.padStart(4,`0`),YYYY:n.padStart(4,`0`),y:n,MM:r.padStart(2,`0`),DD:i.padStart(2,`0`),hh:a.padStart(2,`0`),mm:o.padStart(2,`0`),ss:s.floor().toString().padStart(2,`0`),SSSSSS:u.padEnd(6,`0`).slice(0,6),SSS:u.padEnd(3,`0`).slice(0,3),SS:u.padEnd(2,`0`).slice(0,2),S:u.padEnd(1,`0`).slice(0,1)},h=e;for(let[e,t]of Object.entries(m))h=h.replace(e,t);return h}};exports.BigDate=S;
|
package/index.d.cts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Decimal, DecimalLike } from "@kikuchan/decimal";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
type Zone = string;
|
|
5
|
+
type CalendarInput = {
|
|
6
|
+
year: bigint | number | DecimalLike;
|
|
7
|
+
month: bigint | number | DecimalLike;
|
|
8
|
+
day: bigint | number | DecimalLike;
|
|
9
|
+
hour?: bigint | number | DecimalLike;
|
|
10
|
+
minutes?: bigint | number | DecimalLike;
|
|
11
|
+
seconds?: DecimalLike;
|
|
12
|
+
};
|
|
13
|
+
type CalendarComponents = {
|
|
14
|
+
year: bigint;
|
|
15
|
+
month: bigint;
|
|
16
|
+
day: bigint;
|
|
17
|
+
hour: bigint;
|
|
18
|
+
minutes: bigint;
|
|
19
|
+
seconds: Decimal;
|
|
20
|
+
weekday: number;
|
|
21
|
+
};
|
|
22
|
+
type Step = bigint | number | (bigint | number)[] | undefined;
|
|
23
|
+
type YearStepOptions = {
|
|
24
|
+
era?: boolean;
|
|
25
|
+
};
|
|
26
|
+
declare class BigDate {
|
|
27
|
+
#private;
|
|
28
|
+
constructor();
|
|
29
|
+
constructor(dateLike: DecimalLike | Date, zone?: Zone);
|
|
30
|
+
constructor(year: bigint | number, month: bigint | number, day: bigint | number, hour?: bigint | number, minutes?: bigint | number, seconds?: DecimalLike, zone?: Zone);
|
|
31
|
+
static fromEpoch(epochSeconds: DecimalLike, zone?: Zone): BigDate;
|
|
32
|
+
static fromDate(date: Date, zone?: Zone): BigDate;
|
|
33
|
+
static fromCalendar(input: CalendarInput, zone?: Zone): BigDate;
|
|
34
|
+
clone(): BigDate;
|
|
35
|
+
zone(): Zone;
|
|
36
|
+
zone(value: Zone): BigDate;
|
|
37
|
+
zone$(value: Zone): this;
|
|
38
|
+
utc(): BigDate;
|
|
39
|
+
utc$(): this;
|
|
40
|
+
local(): BigDate;
|
|
41
|
+
local$(): this;
|
|
42
|
+
epoch(): Decimal;
|
|
43
|
+
epoch(value: DecimalLike): BigDate;
|
|
44
|
+
epoch$(value: DecimalLike): this;
|
|
45
|
+
object(): CalendarComponents;
|
|
46
|
+
year(): bigint;
|
|
47
|
+
year(value: bigint | number | DecimalLike): BigDate;
|
|
48
|
+
year$(value: bigint | number | DecimalLike): BigDate;
|
|
49
|
+
month(): bigint;
|
|
50
|
+
month(value: bigint | number | DecimalLike): BigDate;
|
|
51
|
+
month$(value: bigint | number | DecimalLike): BigDate;
|
|
52
|
+
day(): bigint;
|
|
53
|
+
day(value: bigint | number | DecimalLike): BigDate;
|
|
54
|
+
day$(value: bigint | number | DecimalLike): BigDate;
|
|
55
|
+
hour(): bigint;
|
|
56
|
+
hour(value: bigint | number | DecimalLike): BigDate;
|
|
57
|
+
hour$(value: bigint | number | DecimalLike): BigDate;
|
|
58
|
+
minutes(): bigint;
|
|
59
|
+
minutes(value: bigint | number | DecimalLike): BigDate;
|
|
60
|
+
minutes$(value: bigint | number | DecimalLike): BigDate;
|
|
61
|
+
seconds(): Decimal;
|
|
62
|
+
seconds(value: DecimalLike): BigDate;
|
|
63
|
+
seconds$(value: DecimalLike): BigDate;
|
|
64
|
+
weekday(): number;
|
|
65
|
+
alignToDay(step?: Step): BigDate;
|
|
66
|
+
nextDay(step?: Step): BigDate;
|
|
67
|
+
alignToMonth(step?: Step): BigDate;
|
|
68
|
+
nextMonth(step?: Step): BigDate;
|
|
69
|
+
alignToYear(step?: Step, options?: YearStepOptions): BigDate;
|
|
70
|
+
nextYear(step?: Step, options?: YearStepOptions): BigDate;
|
|
71
|
+
alignToSecond(step: DecimalLike): BigDate;
|
|
72
|
+
format(fmt: string): string;
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
export { BigDate };
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Decimal, DecimalLike } from "@kikuchan/decimal";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
type Zone = string;
|
|
5
|
+
type CalendarInput = {
|
|
6
|
+
year: bigint | number | DecimalLike;
|
|
7
|
+
month: bigint | number | DecimalLike;
|
|
8
|
+
day: bigint | number | DecimalLike;
|
|
9
|
+
hour?: bigint | number | DecimalLike;
|
|
10
|
+
minutes?: bigint | number | DecimalLike;
|
|
11
|
+
seconds?: DecimalLike;
|
|
12
|
+
};
|
|
13
|
+
type CalendarComponents = {
|
|
14
|
+
year: bigint;
|
|
15
|
+
month: bigint;
|
|
16
|
+
day: bigint;
|
|
17
|
+
hour: bigint;
|
|
18
|
+
minutes: bigint;
|
|
19
|
+
seconds: Decimal;
|
|
20
|
+
weekday: number;
|
|
21
|
+
};
|
|
22
|
+
type Step = bigint | number | (bigint | number)[] | undefined;
|
|
23
|
+
type YearStepOptions = {
|
|
24
|
+
era?: boolean;
|
|
25
|
+
};
|
|
26
|
+
declare class BigDate {
|
|
27
|
+
#private;
|
|
28
|
+
constructor();
|
|
29
|
+
constructor(dateLike: DecimalLike | Date, zone?: Zone);
|
|
30
|
+
constructor(year: bigint | number, month: bigint | number, day: bigint | number, hour?: bigint | number, minutes?: bigint | number, seconds?: DecimalLike, zone?: Zone);
|
|
31
|
+
static fromEpoch(epochSeconds: DecimalLike, zone?: Zone): BigDate;
|
|
32
|
+
static fromDate(date: Date, zone?: Zone): BigDate;
|
|
33
|
+
static fromCalendar(input: CalendarInput, zone?: Zone): BigDate;
|
|
34
|
+
clone(): BigDate;
|
|
35
|
+
zone(): Zone;
|
|
36
|
+
zone(value: Zone): BigDate;
|
|
37
|
+
zone$(value: Zone): this;
|
|
38
|
+
utc(): BigDate;
|
|
39
|
+
utc$(): this;
|
|
40
|
+
local(): BigDate;
|
|
41
|
+
local$(): this;
|
|
42
|
+
epoch(): Decimal;
|
|
43
|
+
epoch(value: DecimalLike): BigDate;
|
|
44
|
+
epoch$(value: DecimalLike): this;
|
|
45
|
+
object(): CalendarComponents;
|
|
46
|
+
year(): bigint;
|
|
47
|
+
year(value: bigint | number | DecimalLike): BigDate;
|
|
48
|
+
year$(value: bigint | number | DecimalLike): BigDate;
|
|
49
|
+
month(): bigint;
|
|
50
|
+
month(value: bigint | number | DecimalLike): BigDate;
|
|
51
|
+
month$(value: bigint | number | DecimalLike): BigDate;
|
|
52
|
+
day(): bigint;
|
|
53
|
+
day(value: bigint | number | DecimalLike): BigDate;
|
|
54
|
+
day$(value: bigint | number | DecimalLike): BigDate;
|
|
55
|
+
hour(): bigint;
|
|
56
|
+
hour(value: bigint | number | DecimalLike): BigDate;
|
|
57
|
+
hour$(value: bigint | number | DecimalLike): BigDate;
|
|
58
|
+
minutes(): bigint;
|
|
59
|
+
minutes(value: bigint | number | DecimalLike): BigDate;
|
|
60
|
+
minutes$(value: bigint | number | DecimalLike): BigDate;
|
|
61
|
+
seconds(): Decimal;
|
|
62
|
+
seconds(value: DecimalLike): BigDate;
|
|
63
|
+
seconds$(value: DecimalLike): BigDate;
|
|
64
|
+
weekday(): number;
|
|
65
|
+
alignToDay(step?: Step): BigDate;
|
|
66
|
+
nextDay(step?: Step): BigDate;
|
|
67
|
+
alignToMonth(step?: Step): BigDate;
|
|
68
|
+
nextMonth(step?: Step): BigDate;
|
|
69
|
+
alignToYear(step?: Step, options?: YearStepOptions): BigDate;
|
|
70
|
+
nextYear(step?: Step, options?: YearStepOptions): BigDate;
|
|
71
|
+
alignToSecond(step: DecimalLike): BigDate;
|
|
72
|
+
format(fmt: string): string;
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
export { BigDate };
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Decimal as e}from"@kikuchan/decimal";const t=86400n,n=3600n,r=60n,i=e(-8640000000000000n),a=e(8640000000000000n);function o(t,n){if(typeof t==`bigint`)return t;if(typeof t==`number`){if(!Number.isFinite(t)||!Number.isInteger(t))throw Error(`${n} must be an integer`);return BigInt(t)}let r=e(t),i=r.integer();if(!r.eq(i))throw Error(`${n} must be an integer`);return i}function s(e,t){let n=e/t;return e%t===0n||e<0n==t<0n?n:n-1n}function c(e,t){let n=t-1n,r=s(n,12n),i=n-r*12n+1n;return{year:e+r,month:i}}function l(e,t,n){return h(m(e,t,n))}function u(e,t,n=0n){if(!t)return e;if(Array.isArray(t))return t.map(e=>BigInt(e)).filter(t=>t<=e).toSorted((e,t)=>e<t?-1:e>t?1:0).at(-1)??e;let r=BigInt(t);return(e-n)/r*r+n}function d(e,t,n=0n){if(!t)return e;if(Array.isArray(t))return t.map(e=>BigInt(e)).filter(t=>e<t).toSorted((e,t)=>e<t?-1:e>t?1:0).at(0);let r=BigInt(t);return((e-n)/r+1n)*r+n}function f(e,t,n){if(!t)return e;if(!n?.era)return u(e,t,0n);if(e>0n){let n=u(e,t,0n);return n===0n?1n:n}let r=d(-e+1n-1n,t,0n);return r===void 0?e:-(r-1n)}function p(e,t,n){if(!t)return e;if(!n?.era||e>0n)return d(e,t,0n);let r=u(-e+1n-1n,t,0n);return Array.isArray(t)&&!t.map(e=>BigInt(e)).some(e=>e===r)||r<=0n?1n:-(r-1n)}function m(e,t,n){let{year:r,month:i}=c(e,t),a=n-1n,o=r,l=i;o-=l<=2n?1n:0n;let u=s(o,400n),d=o-u*400n,f=s(153n*(l+(l>2n?-3n:9n))+2n,5n),p=d*365n+s(d,4n)-s(d,100n)+f;return u*146097n+p-719468n+a}function h(e){let t=e+719468n,n=s(t,146097n),r=t-n*146097n,i=s(r-s(r,1460n)+s(r,36524n)-s(r,146096n),365n),a=i+n*400n,o=r-(365n*i+s(i,4n)-s(i,100n)),c=s(5n*o+2n,153n),l=o-s(153n*c+2n,5n)+1n,u=c+(c<10n?3n:-9n);a+=u<=2n?1n:0n;let d=Number(((e+4n)%7n+7n)%7n);return{year:a,month:u,day:l,weekday:d}}function g(e){let t=g.cache??new Map;g.cache=t;let n=t.get(e);return n||(n=new Intl.DateTimeFormat(`en-US`,{timeZone:e,year:`numeric`,month:`2-digit`,day:`2-digit`,hour:`2-digit`,minute:`2-digit`,second:`2-digit`,hourCycle:`h23`}),t.set(e,n)),n}g.cache=void 0;function _(o,s){if(s.toLowerCase()===`utc`)return 0;let c=o.mul(1e3).clamp(i,a),l=new Date(c.number());if(s.toLowerCase()===`local`)return l.getTimezoneOffset();let u=new Map(g(s).formatToParts(l).map(e=>[e.type,e.value])),d=BigInt(u.get(`year`)??`0`),f=BigInt(u.get(`month`)??`0`),p=BigInt(u.get(`day`)??`0`),h=BigInt(u.get(`hour`)??`0`),_=BigInt(u.get(`minute`)??`0`),v=BigInt(u.get(`second`)??`0`),y=e(m(d,f,p)).mul(t).add(h*n+_*r+v).add(e(l.getUTCMilliseconds()).div(1e3));return c.div(1e3).sub(y).div(60).trunc(0,!0).number()}function v(i,a){let o=_(i,a),c=o===0?i:i.sub(e(o).mul(r)),l=s(c.floor(0).integer(),t),u=c.sub(e(l*t)),d=u.floor(0).integer(),f=u.sub(e(d)),p=d/n,m=d%n/r,g=d%r,{year:v,month:y,day:b,weekday:x}=h(l);return{year:v,month:y,day:b,hour:p,minutes:m,seconds:e(g).add(f),weekday:x}}function y(i,a){let o=e(m(i.year,i.month,i.day)).mul(t).add(e(i.hour*n+i.minutes*r).add(i.seconds));if(a.toLowerCase()===`utc`)return o;let s=o;for(let t=0;t<4;t++){let t=_(s,a),n=o.add(e(t).mul(r));if(n.eq(s))return n;s=n}return s}function b(t){let n=o(t.year,`year`),r=o(t.month,`month`),i=o(t.day,`day`);return{year:n,month:r,day:i,hour:t.hour==null?0n:o(t.hour,`hour`),minutes:t.minutes==null?0n:o(t.minutes,`minutes`),seconds:t.seconds==null?e(0):e(t.seconds),weekday:h(m(n,r,i)).weekday}}function x(e){return e||`utc`}var S=class t{#e;#t;constructor(...t){if(t.length===0){this.#e=e(Date.now()).div(1e3),this.#t=`utc`;return}let n=(t.length===7||t.length===2)&&typeof t[t.length-1]==`string`?x(t[t.length-1]):`utc`;if(t.length>=3&&typeof t[0]!=`object`){let[e,r,i,a,o,s]=t;this.#e=y(b({year:e,month:r,day:i,hour:a,minutes:o,seconds:s}),n),this.#t=n;return}if(this.#t=n,t[0]instanceof Date){this.#e=e(t[0].getTime()).div(1e3);return}this.#e=e(t[0])}static fromEpoch(e,n=`utc`){return new t(e,n)}static fromDate(e,n=`utc`){return new t(e,n)}static fromCalendar(e,n=`utc`){return new t(y(b(e),n),n)}clone(){return new t(this.#e,this.#t)}zone(e){return e===void 0?this.#t:new t(this.#e,e)}zone$(e){return this.#t=e,this}utc(){return this.zone(`utc`)}utc$(){return this.zone$(`utc`)}local(){return this.zone(`local`)}local$(){return this.zone$(`local`)}epoch(e){return e===void 0?this.#e.clone():new t(e,this.#t)}epoch$(t){return this.#e=e(t),this}object(){return v(this.#e,this.#t)}#n(e,n){let r=this.object(),i=y({year:e.year??r.year,month:e.month??r.month,day:e.day??r.day,hour:e.hour??r.hour,minutes:e.minutes??r.minutes,seconds:e.seconds??r.seconds,weekday:r.weekday},this.#t);return n?(this.#e=i,this):new t(i,this.#t)}#r(n){let r=this.object();return new t(y({...n(r),hour:0n,minutes:0n,seconds:e(0),weekday:r.weekday},this.#t),this.#t)}year(e){return e===void 0?this.object().year:this.#n({year:o(e,`year`)},!1)}year$(e){return this.#n({year:o(e,`year`)},!0)}month(e){return e===void 0?this.object().month:this.#n({month:o(e,`month`)},!1)}month$(e){return this.#n({month:o(e,`month`)},!0)}day(e){return e===void 0?this.object().day:this.#n({day:o(e,`day`)},!1)}day$(e){return this.#n({day:o(e,`day`)},!0)}hour(e){return e===void 0?this.object().hour:this.#n({hour:o(e,`hour`)},!1)}hour$(e){return this.#n({hour:o(e,`hour`)},!0)}minutes(e){return e===void 0?this.object().minutes:this.#n({minutes:o(e,`minutes`)},!1)}minutes$(e){return this.#n({minutes:o(e,`minutes`)},!0)}seconds(t){return t===void 0?this.object().seconds.clone():this.#n({seconds:e(t)},!1)}seconds$(t){return this.#n({seconds:e(t)},!0)}weekday(){return this.object().weekday}alignToDay(e){return this.#r(t=>l(t.year,t.month,u(t.day,e,1n)))}nextDay(e){return this.#r(t=>{let n=d(t.day,e,1n);if(n===void 0){let{year:e,month:n}=c(t.year,t.month+1n);return{year:e,month:n,day:1n}}return l(t.year,t.month,n)})}alignToMonth(e){return this.#r(t=>{let{year:n,month:r}=c(t.year,u(t.month,e,1n));return{year:n,month:r,day:1n}})}nextMonth(e){return this.#r(t=>{let{year:n,month:r}=c(t.year,d(t.month,e,1n));return{year:n,month:r,day:1n}})}alignToYear(e,t){return this.#r(n=>({year:f(n.year,e,t),month:1n,day:1n}))}nextYear(e,t){return this.#r(n=>({year:p(n.year,e,t),month:1n,day:1n}))}alignToSecond(n){let r=this.alignToDay(1n);return new t(this.#e.sub(r.epoch()).floorBy(e(n)).add(r.epoch()),this.#t)}format(e){let t=this.object(),n=t.year.toString(),r=t.month.toString(),i=t.day.toString(),a=t.hour.toString(),o=t.minutes.toString(),s=t.seconds,c=s.toString(),l=c.indexOf(`.`),u=l>=0?c.slice(l+1):``,d=t.year<=0?`BC `:``,f=t.year>0?` AD`:``,p=t.year<=0?(1n-t.year).toString():t.year.toString(),m={gggg:`${d}${p.padStart(4,`0`)}`,GGGG:`${d}${p.padStart(4,`0`)}${f}`,g:`${d}${p}`,G:`${d}${p}${f}`,yyyy:n.padStart(4,`0`),YYYY:n.padStart(4,`0`),y:n,MM:r.padStart(2,`0`),DD:i.padStart(2,`0`),hh:a.padStart(2,`0`),mm:o.padStart(2,`0`),ss:s.floor().toString().padStart(2,`0`),SSSSSS:u.padEnd(6,`0`).slice(0,6),SSS:u.padEnd(3,`0`).slice(0,3),SS:u.padEnd(2,`0`).slice(0,2),S:u.padEnd(1,`0`).slice(0,1)},h=e;for(let[e,t]of Object.entries(m))h=h.replace(e,t);return h}};export{S as BigDate};
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kikuchan/bigdate",
|
|
3
|
+
"description": "Arbitrary precision date and time library using decimal numbers.",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"decimal",
|
|
6
|
+
"arbitrary",
|
|
7
|
+
"date",
|
|
8
|
+
"bigint"
|
|
9
|
+
],
|
|
10
|
+
"version": "0.0.1-alpha.0",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./index.js",
|
|
13
|
+
"author": "kikuchan <kikuchan98@gmail.com>",
|
|
14
|
+
"homepage": "https://github.com/kikuchan/utils-on-npm#readme",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@kikuchan/decimal": "^0.1.0-alpha.5"
|
|
18
|
+
},
|
|
19
|
+
"types": "./index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./index.d.ts",
|
|
23
|
+
"import": "./index.js",
|
|
24
|
+
"require": "./index.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"url": "https://github.com/kikuchan/utils-on-npm"
|
|
29
|
+
}
|
|
30
|
+
}
|