@dakkiin/eida 0.1.0 → 0.1.1
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 +244 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
# @dakkiin/eida
|
|
2
|
+
|
|
3
|
+
Utility helpers for common Enjaz/Eida frontend formatting tasks:
|
|
4
|
+
|
|
5
|
+
- date formatting with consistent placeholders
|
|
6
|
+
- currency conversion with direct rates or base-currency quotes
|
|
7
|
+
- Arabic number-to-words spelling
|
|
8
|
+
|
|
9
|
+
The package is distributed as ESM and typed with TypeScript declarations.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @dakkiin/eida
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`vue` is declared as a peer dependency.
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { useFormatDate, useCurrency, useNumbersToWords } from '@dakkiin/eida'
|
|
23
|
+
|
|
24
|
+
const { toDDMMYYYY, toDateTime12h } = useFormatDate()
|
|
25
|
+
const { convertCurrency, roundMoney } = useCurrency()
|
|
26
|
+
const { spell } = useNumbersToWords()
|
|
27
|
+
|
|
28
|
+
console.log(toDDMMYYYY('2026-06-16')) // 16/06/2026
|
|
29
|
+
console.log(toDateTime12h('2026-06-16T13:45:00')) // 16/06/2026 01:45 PM
|
|
30
|
+
|
|
31
|
+
const iqd = convertCurrency({
|
|
32
|
+
amount: 100,
|
|
33
|
+
from: 'USD',
|
|
34
|
+
to: 'IQD',
|
|
35
|
+
directRate: 1480
|
|
36
|
+
})
|
|
37
|
+
console.log(iqd) // 148000
|
|
38
|
+
console.log(roundMoney(iqd, 0)) // 148000
|
|
39
|
+
|
|
40
|
+
console.log(spell(1250)) // Arabic words for 1250
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Exports
|
|
44
|
+
|
|
45
|
+
The package exports:
|
|
46
|
+
|
|
47
|
+
- `useFormatDate`
|
|
48
|
+
- `useCurrency`
|
|
49
|
+
- `useNumbersToWords`
|
|
50
|
+
- utility functions from currency module, including `convertCurrency`, `convertCurrencyWithDirectRate`, `convertCurrencyViaBase`, `roundMoney`, and `sameCurrency`
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Date Formatting (`useFormatDate`)
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { useFormatDate } from '@dakkiin/eida'
|
|
58
|
+
|
|
59
|
+
const { toDDMMYY, toDDMMYYYY, toDateTime, toDateTime12h } = useFormatDate()
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### `toDDMMYY(date)`
|
|
63
|
+
|
|
64
|
+
- Input: `string | Date | null | undefined`
|
|
65
|
+
- Output format: `DD/MM/YY`
|
|
66
|
+
- Fallback when input is empty: `--/--/--`
|
|
67
|
+
|
|
68
|
+
Example:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
toDDMMYY('2026-06-16') // 16/06/26
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### `toDDMMYYYY(date)`
|
|
75
|
+
|
|
76
|
+
- Input: `string | Date | null | undefined`
|
|
77
|
+
- Output format: `DD/MM/YYYY`
|
|
78
|
+
- Fallback when input is empty: `--/--/----`
|
|
79
|
+
|
|
80
|
+
Example:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
toDDMMYYYY('2026-06-16') // 16/06/2026
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### `toDateTime(date)`
|
|
87
|
+
|
|
88
|
+
- Input: `string | Date | null | undefined`
|
|
89
|
+
- Output format: `DD/MM/YYYY HH:mm` (24-hour)
|
|
90
|
+
- Fallback when input is empty: `--/--/---- --:--`
|
|
91
|
+
|
|
92
|
+
Example:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
toDateTime('2026-06-16T13:45:00') // 16/06/2026 13:45
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### `toDateTime12h(date)`
|
|
99
|
+
|
|
100
|
+
- Input: `string | Date | null | undefined`
|
|
101
|
+
- Output format: `DD/MM/YYYY hh:mm AM|PM` (12-hour)
|
|
102
|
+
- Fallback when input is empty: `--/--/---- --:-- --`
|
|
103
|
+
|
|
104
|
+
Example:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
toDateTime12h('2026-06-16T13:45:00') // 16/06/2026 01:45 PM
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Notes
|
|
111
|
+
|
|
112
|
+
- Date parsing relies on JavaScript `Date` behavior.
|
|
113
|
+
- Invalid date strings can produce implementation-dependent output, so prefer ISO date inputs where possible.
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Currency Utilities (`useCurrency`)
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import { useCurrency } from '@dakkiin/eida'
|
|
121
|
+
|
|
122
|
+
const {
|
|
123
|
+
convertCurrency,
|
|
124
|
+
convertCurrencyWithDirectRate,
|
|
125
|
+
convertCurrencyViaBase,
|
|
126
|
+
roundMoney
|
|
127
|
+
} = useCurrency()
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 1) Direct rate conversion
|
|
131
|
+
|
|
132
|
+
Use `convertCurrencyWithDirectRate(amount, from, to, directRate)` when you already have one cross rate:
|
|
133
|
+
|
|
134
|
+
- Formula: `amount * directRate`
|
|
135
|
+
- `directRate` means: **units of `to` per 1 unit of `from`**
|
|
136
|
+
- If `from` and `to` are the same (case-insensitive), original amount is returned.
|
|
137
|
+
|
|
138
|
+
Example:
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
convertCurrencyWithDirectRate(100, 'USD', 'IQD', 1480) // 148000
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 2) Base-currency quote conversion
|
|
145
|
+
|
|
146
|
+
Use `convertCurrencyViaBase(amount, from, to, baseCurrency, quotes)` when rates are defined against one base currency.
|
|
147
|
+
|
|
148
|
+
Quote rule:
|
|
149
|
+
|
|
150
|
+
- `quotes[currency] = units of currency per 1 baseCurrency`
|
|
151
|
+
|
|
152
|
+
Example with base `USD`:
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
const quotes = { IQD: 1480, EUR: 0.92 }
|
|
156
|
+
|
|
157
|
+
convertCurrencyViaBase(100, 'USD', 'IQD', 'USD', quotes) // 148000
|
|
158
|
+
convertCurrencyViaBase(1480, 'IQD', 'USD', 'USD', quotes) // 1
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### 3) Unified API
|
|
162
|
+
|
|
163
|
+
Use `convertCurrency(options)` with either:
|
|
164
|
+
|
|
165
|
+
- direct shape: `{ amount, from, to, directRate }`
|
|
166
|
+
- base shape: `{ amount, from, to, baseCurrency, quotes }`
|
|
167
|
+
|
|
168
|
+
Examples:
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
convertCurrency({
|
|
172
|
+
amount: 100,
|
|
173
|
+
from: 'USD',
|
|
174
|
+
to: 'IQD',
|
|
175
|
+
directRate: 1480
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
convertCurrency({
|
|
179
|
+
amount: 1480,
|
|
180
|
+
from: 'IQD',
|
|
181
|
+
to: 'USD',
|
|
182
|
+
baseCurrency: 'USD',
|
|
183
|
+
quotes: { IQD: 1480 }
|
|
184
|
+
})
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### `roundMoney(value, fractionDigits = 2)`
|
|
188
|
+
|
|
189
|
+
Rounds using `Math.round`.
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
roundMoney(10.236) // 10.24
|
|
193
|
+
roundMoney(10.236, 1) // 10.2
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Validation behavior
|
|
197
|
+
|
|
198
|
+
- Non-finite amounts return `NaN`.
|
|
199
|
+
- Invalid or missing rates/quotes throw `RangeError`.
|
|
200
|
+
- Calling `convertCurrency` without a valid shape throws `TypeError`.
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
## Arabic Number Spelling (`useNumbersToWords`)
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
import { useNumbersToWords } from '@dakkiin/eida'
|
|
208
|
+
|
|
209
|
+
const { spell } = useNumbersToWords()
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### `spell(value)`
|
|
213
|
+
|
|
214
|
+
- Input: `number`
|
|
215
|
+
- Output: Arabic text representation of the integer value
|
|
216
|
+
- Uses `Math.abs` + `Math.round` internally
|
|
217
|
+
- Returns empty string for `0` and other falsy input values
|
|
218
|
+
- Supports large numbers up to trillions based on current implementation rules
|
|
219
|
+
|
|
220
|
+
Examples:
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
spell(1) // "واحد"
|
|
224
|
+
spell(12) // "اثنى عشر"
|
|
225
|
+
spell(1250) // Arabic phrase for 1250
|
|
226
|
+
spell(-42.4) // Arabic phrase for 42 (rounded absolute value)
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## TypeScript
|
|
232
|
+
|
|
233
|
+
Types are shipped from `dist/*.d.ts`, so imported APIs are strongly typed by default.
|
|
234
|
+
|
|
235
|
+
## Build
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
npm run build
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## License
|
|
242
|
+
|
|
243
|
+
`UNLICENSED`
|
|
244
|
+
|