@wisemen/wise-date 0.0.4 → 0.0.5

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.
Files changed (2) hide show
  1. package/README.md +8 -288
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,302 +1,22 @@
1
- # Wisemen Monetary Package for TypeScript
1
+ # WiseDate Package for TypeScript
2
2
 
3
3
  ## Features
4
4
 
5
- Accurate Financial Calculations `Monetary` provides the tools to accurately perform moneraty operations \
6
- Persistence Support Seamlessly store and retrieve monetary values with extenstions for `TypeORM`. \
7
- DTO Validation – Ensure data integrity with built-in validation for monetary objects. \
8
- ✔ Immutability – Monetary is fully immutable, meaning no state bugs. \
5
+ dayjs and Moment inspired interface for dealing with calendar dates (no time component) \
6
+ support for +infinity and -infinity dates \
7
+ support for typeorm \
8
+ ✔ Immutability – Wisedate is fully immutable, meaning no state bugs. \
9
9
  ✔ TypeScript Support – Fully typed for a smooth developer experience.
10
10
 
11
11
 
12
12
  ## Philosophy
13
13
 
14
- Wisemen Monetary places the responsibility of accurately dealing with monetary values with the developer, but helps where needed by providing the necessary tools.
14
+ Wisedate fills the need of a dayjs like tool but for calendar dates without a time component.
15
15
 
16
16
  ## Example
17
17
 
18
- ```typescript
19
- // 10 euros with precision 4
20
- import { Monetary } from './monetary'
21
-
22
- const price = new Monetary({
23
- amount: 10_0000,
24
- currency: Currency.EUR,
25
- precision: 4
26
- })
27
-
28
- // 2 euros discount with precision 0
29
- const flatDiscount = new Monetary({
30
- amount: 2,
31
- currency: Currency.EUR,
32
- precision: 0
33
- })
34
-
35
- const bonusDiscountRate = 0.90 // 90 percent of discounted price
36
-
37
- const discountedPrice = price
38
- .subtract(flatDiscount) // subtract 2 euros
39
- .multiply(bonusDiscountRate) // take 90 percent of price
40
- .toPrecision(2) // only store up to cents
41
- .floor()
42
-
43
- discountedPrice.toString() // -> "7,20 euros"
44
- ```
45
-
18
+ coming soon...
46
19
 
47
20
  ## Deep Dive
48
21
 
49
- ### Monetary operations
50
-
51
- #### Rounding
52
-
53
- Monetary stores the current amount internally as a regular javascript `number` which means it can be both an `integer` or a `float`. Monetary objects will never perform any type of rounding on the internal value automatically. The point at which rounding of the monetary value takes place and the specific rounding operation is determined by the developer. Rounding always respects the set precision.
54
-
55
- Monetary supports 3 rounding operations:
56
- - `round()`: rounds half up to the nearest integer
57
- - `ceil()`: rounds up to the nearest integer
58
- - `floor()`: rounds down to the nearest integer
59
-
60
- ```typescript
61
- // 10 euros with precsion 2
62
- const price = new Monetary({
63
- amount: 1000,
64
- currency: Currency.EUR,
65
- precision: 2
66
- })
67
-
68
- price.isRounded() // -> true
69
-
70
- const discountedPrice = price.multiply(.9999) // -> 9.999 euros
71
- discountedPrice.isRounded() // -> false
72
-
73
- discountedPrice.round() // -> 10 euros
74
- discountedPrice.floor() // -> 9.99 euros
75
- discountedPrice.ceil() // -> 10 euros
76
- ```
77
-
78
- #### Adding and subtracting
79
-
80
- Monetary only allows values with the same currency to be added or subtracted from each other.
81
-
82
- ```typescript
83
- // 10 euros with precsion 2
84
- const euros = new Monetary({
85
- amount: 1000,
86
- currency: Currency.EUR,
87
- precision: 2
88
- })
89
-
90
- // 10 USD with precsion 2
91
- const dollars = new Monetary({
92
- amount: 1000,
93
- currency: Currency.USD,
94
- precision: 2
95
- })
96
-
97
- euros.add(dollars) // -> throws IllegalMonetaryOperationError
98
- euros.subtract(dollars) // -> throws IllegalMonetaryOperationError
99
- ```
100
-
101
- Adding or subtracting values with different precisions will use the highest precision of the two in the resulting value. The amount of the value with the lowest precision is increased to the higher precision before the operation is performed.
102
-
103
- ```typescript
104
- // 10 euros with precsion 2
105
- const euros = new Monetary({
106
- amount: 1000,
107
- currency: Currency.EUR,
108
- precision: 2
109
- })
110
-
111
- // 10 euros with precsion 4
112
- const morePreciseEuros = new Monetary({
113
- amount: 1000000,
114
- currency: Currency.EUR,
115
- precision: 4
116
- })
117
-
118
- euros.add(morePreciseEuros) // -> 20 euros with precision 4
119
- euros.subtract(morePreciseEuros) // -> 0 euros with precision 4
120
- ```
121
-
122
- #### Changing precision
123
-
124
- Changing the precision of a monetary value adjusts the internal amount. The value is multiplied by 10<sup>(newPrecision - oldPrecision)</sup>. A decrease in precision can result in an unrounded value.
125
-
126
- ```typescript
127
- // 9,50 euros with precsion 2
128
- const euros = new Monetary({
129
- amount: 950,
130
- currency: Currency.EUR,
131
- precision: 2
132
- })
133
-
134
- euros.toPrecision(4) // -> amount of 95000
135
- euros.toPrecision(0) // -> amount of 9,50 (unrounded)
136
- ```
137
-
138
- ### Persistence
139
-
140
- Storing arbitrary precision is not possible in Monetary. Monetary storing mechanisms require a set precision. All stored values will have the set precision. Values with a lower precision are normalized to the set precision. Values with a higher precision will trigger an error when stored.
141
-
142
- ```typescript
143
- class Entity {
144
- @MonetaryColumn({defaultPrecision: 4})
145
- amount: Monetary
146
- }
147
-
148
- // 9,50 euros with precsion 2
149
- const euros = new Monetary({
150
- amount: 950,
151
- currency: Currency.EUR,
152
- precision: 2
153
- })
154
-
155
- // 9,50 euros with precsion 5
156
- const highPrecisionEuros = new Monetary({
157
- amount: 950000,
158
- currency: Currency.EUR,
159
- precision: 5
160
- })
161
-
162
- await entityRepository.insert({amount: euros}) // -> stored as 95000
163
- await entityRepository.insert({amount: highPrecisionEuros}) // -> throws PrecisionLossError
164
- ```
165
-
166
- Monetary provides 2 storing mechanisms for `TypeORM`.
167
-
168
- #### Storing only the amount
169
-
170
- When the currency of a monetary value is static (i.e. only EUR is supported) monetary values can be stored as just the amount. The amount is stored as an `int4` column.
171
-
172
- ```typescript
173
- class Entity {
174
- @MonetaryAmountColumn({
175
- monetaryPrecision: 4,
176
- currency: Currency.EUR // the static currency
177
- })
178
- amount: Monetary
179
- }
180
-
181
- // 9,50 dollars with precision 2
182
- const dollars = new Monetary({
183
- amount: 950,
184
- currency: Currency.USD,
185
- precision: 2
186
- })
187
-
188
- await entityRepository.insert({amount: dollars}) // -> throws UnsupportedCurrencyError
189
- ```
190
-
191
- #### Storing the amount and currency
192
-
193
- Storing multiple currencies in a single field will store the monetary values as `jsonb`. It's possible to define a precision per currency. If a precision is not set for a currency, the default precision is used.
194
-
195
- ```typescript
196
- class Entity {
197
- @MonetaryColumn({
198
- defaultPrecision: 2,
199
- currencyPrecisions: {
200
- [Currency.EUR]: 4
201
- }
202
- })
203
- amount: Monetary
204
- }
205
-
206
- // 9,50 dollars with precsion 2
207
- const dollars = new Monetary({
208
- amount: 950,
209
- currency: Currency.USD,
210
- precision: 2
211
- })
212
-
213
- // 9,50 euros with precsion 2
214
- const euros = new Monetary({
215
- amount: 950,
216
- currency: Currency.EUR,
217
- precision: 2
218
- })
219
-
220
-
221
- await entityRepository.insert({amount: dollars}) // -> stored with precision 2 as 950
222
- await entityRepository.insert({amount: euros}) // -> stored with precision 4 as 95000
223
- ```
224
-
225
- ### DTO
226
-
227
- Monetary provides a DTO `MonetaryDto` which defines `ApiProperty`s for open api documentation in `Nestjs` and validation for `class-validator`.
228
-
229
- ```json
230
- {
231
- "amount": 950,
232
- "currency": "EUR",
233
- "precision": 2
234
- }
235
- ```
236
-
237
- #### Validation
238
-
239
- The DTO validates it's internal properties. When you need to validate the DTO as a nested property you can use `IsMonetary`. This validator applies `IsObject`, `ValidateNested` and `Type` internally. You can optionally define allowed currencies and a maximum precision.
240
-
241
- ```typescript
242
- class EntityDto {
243
- @IsMonetary({
244
- maxPrecision: 4, // optional max precision
245
- allowedCurrencies: new Set([Currency.EUR]) // optional allowed currencies
246
- })
247
- amount: MonetaryDto
248
- }
249
- ```
250
-
251
- #### Creating DTOs
252
-
253
- A Monetary DTO can be made through a static method:
254
-
255
- ```typescript
256
- const euros = new Monetary({
257
- amount: 950,
258
- currency: Currency.EUR,
259
- precision: 2
260
- })
261
-
262
- const dto = MonetaryDto.from(euros)
263
- ```
264
-
265
- Or with a builder which allows invalid values for testing:
266
- ```typescript
267
- const dto = new MonetaryDtoBuilder()
268
- .withAmount(950)
269
- .withCurrency(Currency.EUR)
270
- .withPrecision(2)
271
- .build()
272
- ```
273
-
274
- #### Parsing DTOs
275
-
276
- A Monetary DTO can be parsed by passing it to the constructor:
277
-
278
- ```typescript
279
- const dto = new MonetaryDtoBuilder()
280
- .withAmount(950)
281
- .withCurrency(Currency.EUR)
282
- .withPrecision(2)
283
- .build()
284
-
285
- const amount = new Monetary(dto)
286
- ```
287
-
288
- or with a convenience method on the dto:
289
-
290
- ```typescript
291
- const dto = new MonetaryDtoBuilder()
292
- .withAmount(950)
293
- .withCurrency(Currency.EUR)
294
- .withPrecision(2)
295
- .build()
296
-
297
- const amount = dto.parse()
298
- ```
299
-
300
-
301
-
302
-
22
+ coming soon...
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wisemen/wise-date",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",