quantible 0.1.12-alpha → 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 rinaldowouterson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,97 +1,179 @@
1
- # quantible
1
+ # Quantible - Convert Numbers and Quantities to Spoken Words
2
2
 
3
- A library for converting quantities, numbers, and simple expressions to spoken words.
3
+ [![NPM Version](https://img.shields.io/npm/v/quantible)](https://www.npmjs.com/package/quantible)
4
+ [![bundlephobia](https://badgen.net/bundlephobia/minzip/quantible)](https://bundlephobia.com/package/quantible)
4
5
 
5
- ## Examples:
6
+ **Quantible** is a versatile JavaScript library designed to convert numbers, quantities, and simple mathematical expressions into easily understandable spoken words. Whether you're building applications that require text-to-speech functionality, accessibility enhancements, or simply need to present numerical data in a more human-readable format, Quantible provides a robust and flexible solution.
6
7
 
7
- - 5 - 10 = five minus ten
8
- - -5 + 10 = negative five plus ten
9
- - 10m/s = ten meters per second
10
- - 15m/s² = fifteen meters per second squared
11
- - 15² || 15^2 = fifteen to the power of two
12
- - 2^2m^2 = two to the power of two meters squared
13
- - 2^2m² = two to the power of two square meters
14
- - 5E-10 = five times ten to the power of negative ten
15
- - -5E10 = negative five times ten to the power of ten
16
- - 5E10 = five times ten to the power of ten
17
- - 5 USD = five dollars
18
- - $5 = five dollars
19
- - 5.25 USD = five dollars and twenty five cents
20
- - $5.25 = five dollars and twenty five cents
8
+ ## Key Features
21
9
 
22
- When using the autoReplaceAllMatches function, it will convert all quantities in a string to spoken words. For example, it transformed this sentence:
10
+ * **Comprehensive Quantity Conversion:** Handles a wide range of numerical formats, including integers, decimals, negative numbers, currencies, units (e.g., meters, seconds), scientific expressions, and simple mathematical operations.
11
+ * **Flexible Usage:** Supports multiple consumption methods including:
12
+ * **CDN:** Directly include in HTML for quick and easy use in web browsers.
13
+ * **CommonJS (CJS):** Integrate into Node.js projects and older JavaScript module systems.
14
+ * **ECMAScript Modules (ESM):** Utilize modern JavaScript module imports for optimized bundling and performance.
15
+ * **String Auto-Replacement:** Effortlessly convert all or the first detected quantities within a string, simplifying text manipulation and dynamic content generation.
16
+ * **Extensive Examples:** Clear and concise examples to quickly grasp the library's capabilities and integration methods.
17
+ * **Well-Documented and Tested:** Thorough documentation and comprehensive test suite ensure reliability and ease of maintenance.
18
+ * **Lightweight and Performant:** Optimized for minimal bundle size and efficient execution, ensuring a smooth user experience.
23
19
 
24
- ###### "We made $500 more this year than last years projections of 25%"
20
+ ## Examples
25
21
 
26
- into this sentence:
22
+ Quantible excels at transforming various numerical expressions into their spoken word equivalents:
27
23
 
28
- ###### We made five hundred dollars more this year than last years projections of twenty five percent
24
+ - `5 - 10` -> "five minus ten"
25
+ - `-5 + 10` -> "negative five plus ten"
26
+ - `10m/s` -> "ten meters per second"
27
+ - `15m/s²` -> "fifteen meters per second squared"
28
+ - `15²` or `15^2` -> "fifteen to the power of two"
29
+ - `2^2m^2` -> "two to the power of two meters squared"
30
+ - `2^2m²` -> "two to the power of two square meters"
31
+ - `5E-10` -> "five times ten to the power of negative ten"
32
+ - `-5E10` -> "negative five times ten to the power of ten"
33
+ - `5E10` -> "five times ten to the power of ten"
34
+ - `5 USD` -> "five dollars"
35
+ - `$5` -> "five dollars"
36
+ - `5.25 USD` -> "five dollars and twenty-five cents"
37
+ - `$5.25` -> "five dollars and twenty-five cents"
38
+
39
+ The `autoReplaceAllMatches` function demonstrates the power of Quantible by converting all quantities within a text:
40
+
41
+ **Input Sentence:**
42
+
43
+ > "We made $500 more this year than last years projections of 25%"
44
+
45
+ **Output Sentence (after `autoReplaceAllMatches`):**
46
+
47
+ > "We made five hundred dollars more this year than last years projections of twenty-five percent"
29
48
 
30
49
  ## Installation
31
50
 
51
+ ### npm
52
+
53
+ For projects using npm or yarn:
54
+
32
55
  ```bash
33
56
  npm install quantible
34
57
  ```
35
58
 
36
- ## Example Usage
59
+ ## How to Consume Quantible
60
+
61
+ Quantible is designed to be versatile and easily integrated into various JavaScript environments. You can consume it in three primary ways: directly in HTML via CDN, using CommonJS `require`, or with modern ECMAScript `import` statements.
62
+
63
+ ### 1. Directly in HTML via CDN
64
+
65
+ For the quickest integration into web pages, you can use a Content Delivery Network (CDN) to include Quantible directly in your HTML file. This method is ideal for simple projects or when you want to avoid complex build processes.
66
+
67
+ ```html
68
+ <!DOCTYPE html>
69
+ <html>
70
+ <head>
71
+ <title>Quantible Example</title>
72
+ </head>
73
+ <body>
74
+ <div id="output"></div>
75
+ <script src="https://unpkg.com/quantible/dist/index.umd.js"></script>
76
+ <script>
77
+ const inputString = "The price is $12.50 USD.";
78
+ const spokenText = quantible.convertQuantities.autoReplaceAllMatches(inputString);
79
+ document.getElementById('output').textContent = spokenText;
80
+ </script>
81
+ </body>
82
+ </html>
83
+ ```
84
+
85
+ In this example:
37
86
 
38
- ### Extracting and translating a quantity
87
+ * We include Quantible by referencing the UMD build from unpkg CDN: `https://unpkg.com/quantible/dist/index.umd.js`.
88
+ * The library is then globally accessible as `quantible`.
89
+ * We use `quantible.convertQuantities.autoReplaceAllMatches` to process a string and display the spoken word output in the `div#output` element.
90
+
91
+ ### 2. CommonJS (CJS) - `require`
92
+
93
+ For Node.js environments or projects using CommonJS modules, you can import Quantible using `require`.
39
94
 
40
95
  ```javascript
41
- import { convertQuantities, extractQuantities } from "quantible";
96
+ const { convertQuantities, extractQuantities } = require('quantible');
42
97
 
43
- const input = "123.45 USD";
98
+ const input = "25 EUR";
44
99
  const extractedData = extractQuantities.firstMatch(input);
100
+ const spokenWord = convertQuantities.translateMatch(extractedData);
45
101
 
46
- console.log(extractedData);
47
- // Output: {
48
- // integer: '123',
49
- // decimal: '45',
50
- // negativeInt: false,
51
- // currency: 'USD',
52
- // matchType: 'codeCurrency',
53
- // input: '123.45 USD',
54
- // index: 0
55
- // }
102
+ console.log(spokenWord); // Output: twenty-five euros
103
+ ```
104
+
105
+ ### 3. ECMAScript Modules (ESM) - `import`
106
+
107
+ For modern JavaScript projects and applications using ECMAScript modules, you can import Quantible using the `import` statement. This is the recommended approach for most modern JavaScript development.
108
+
109
+ ```javascript
110
+ import { convertQuantities, extractQuantities } from 'quantible';
56
111
 
112
+ const input = "100 km/h";
113
+ const extractedData = extractQuantities.firstMatch(input);
57
114
  const spokenWord = convertQuantities.translateMatch(extractedData);
58
115
 
59
- console.log(spokenWord);
60
- // Output:
61
- // one hundred twenty-three dollars and forty-five cents
116
+ console.log(spokenWord); // Output: one hundred kilometers per hour
62
117
  ```
63
118
 
64
- ### Extracting all quanities and do something with them
119
+ Choose the consumption method that best fits your project's environment and build process. Quantible is designed to be flexible and adaptable to your needs.
120
+
121
+ ## Example Usage
122
+
123
+ The following examples demonstrate key functions of Quantible.
124
+
125
+ ### Extracting and Translating a Quantity
65
126
 
66
127
  ```javascript
67
- import { convertQuantities, extractQuantities } from "quantible";
68
-
69
- const input = "The price is $5.25 and the quantity is 10.";
70
- const extractedData2 = extractQuantities.allMatches(input);
71
- extractedData2.forEach((match) => {
72
- console.log(convertQuantities.translateMatch(match));
73
- });
74
- // Output: The price is five dollars and twenty five cents and the quantity is ten.
128
+ import { convertQuantities, extractQuantities } from 'quantible';
129
+
130
+ const input = "123.45 USD";
131
+ const extractedData = extractQuantities.firstMatch(input);
132
+ const spokenWord = convertQuantities.translateMatch(extractedData);
133
+
134
+ console.log(spokenWord);
135
+ // Output: one hundred twenty-three dollars and forty-five cents
75
136
  ```
76
137
 
77
- ### Auto-replacing quantities in a string
138
+ This example shows how to extract and translate the first quantity found in a string.
139
+
140
+ ### Auto-replacing all quantities in a string
78
141
 
79
142
  ```javascript
80
- // Auto-replacing the first match
81
- const inputFirstMatch = "I have 5 USD and 10 EUR.";
82
- const replacedString1 = convertQuantities.autoReplaceFirstMatch(inputFirstMatch);
83
- console.log(replacedString1); // Output: I have five dollars and 10 EUR.
143
+ import { convertQuantities } from 'quantible';
84
144
 
85
- // Auto-replacing all matches
86
145
  const inputReplaceAllMatches = "I have 5 USD and 10 EUR.";
87
- const replacedString2 = convertQuantities.autoReplaceAllMatches(inputReplaceAllMatches);
88
- console.log(replacedString2); // Output: I have five dollars and ten euros.
146
+ const replacedString = convertQuantities.autoReplaceAllMatches(inputReplaceAllMatches);
147
+ console.log(replacedString); // Output: I have five dollars and ten euros.
89
148
  ```
90
149
 
150
+ This example demonstrates automatically replacing all quantities within a string.
151
+
91
152
  ## Contributing
92
153
 
93
- We welcome contributions to quantible! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute.
154
+ We welcome contributions to Quantible! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute.
94
155
 
95
156
  ## License
96
157
 
97
- ISC
158
+ MIT License
159
+
160
+ Copyright (c) 2025 rinaldowouterson
161
+
162
+ Permission is hereby granted, free of charge, to any person obtaining a copy
163
+ of this software and associated documentation files (the "Software"), to deal
164
+ in the Software without restriction, including without limitation the rights
165
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
166
+ copies of the Software, and to permit persons to whom the Software is
167
+ furnished to do so, subject to the following conditions:
168
+
169
+ The above copyright notice and this permission notice shall be included in all
170
+ copies or substantial portions of the Software.
171
+
172
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
173
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
175
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
176
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
177
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
178
+ SOFTWARE.
179
+ ```
package/dist/index.cjs CHANGED
@@ -1081,6 +1081,7 @@ var convertQuantities = {
1081
1081
  return output.replace(/\s+/g, " ").trim();
1082
1082
  }
1083
1083
  };
1084
+ console.log(convertQuantities.autoReplaceAllMatches("25 EUR"));
1084
1085
 
1085
1086
  exports.convertQuantities = convertQuantities;
1086
1087
  exports.extractQuantities = extractQuantities;