@cocreate/calculate 1.16.0 → 1.16.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/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ## [1.16.2](https://github.com/CoCreate-app/CoCreate-calculate/compare/v1.16.1...v1.16.2) (2025-05-01)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * update [@cocreate](https://github.com/cocreate) dependencies ([2e64a5a](https://github.com/CoCreate-app/CoCreate-calculate/commit/2e64a5a94f95a223e398a8a8cb92e1a2ce647ece))
7
+
8
+ ## [1.16.1](https://github.com/CoCreate-app/CoCreate-calculate/compare/v1.16.0...v1.16.1) (2025-04-30)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * added comments ([7ef054f](https://github.com/CoCreate-app/CoCreate-calculate/commit/7ef054f95558036f5d62b00676bf99a58fb425c4))
14
+ * added css-loader ([4649096](https://github.com/CoCreate-app/CoCreate-calculate/commit/46490965d3738e685afd41c44bc78e1e34fe1968))
15
+ * getAttribute ([2a79157](https://github.com/CoCreate-app/CoCreate-calculate/commit/2a7915740101e34923c499c9ebaac29e6b91c9fa))
16
+ * unified observer array of object ([8a525fa](https://github.com/CoCreate-app/CoCreate-calculate/commit/8a525fa0e0ab7a02b77783b2f0fae6a04851ef70))
17
+ * update query attributes ([38a77ff](https://github.com/CoCreate-app/CoCreate-calculate/commit/38a77ffb054b479f5ac926152e32980337ddbfe8))
18
+ * updated cocreate modules versions ([bf2795b](https://github.com/CoCreate-app/CoCreate-calculate/commit/bf2795b72ea5e7c7cbded8da66caa06e615353ee))
19
+ * webpack.config and devdependencies ([5f97fca](https://github.com/CoCreate-app/CoCreate-calculate/commit/5f97fcad7165a5332a0371b469f687d1fd7caa07))
20
+
1
21
  # [1.16.0](https://github.com/CoCreate-app/CoCreate-calculate/compare/v1.15.0...v1.16.0) (2025-04-11)
2
22
 
3
23
 
package/docs/index.html CHANGED
@@ -46,7 +46,7 @@
46
46
  id="menuL"
47
47
  class="position:fixed top:0px left:0px overflow:hidden background:whitesmoke height:100vh width:0px width:300px@xl"
48
48
  resizable
49
- resize-selector="[content_id='content']"
49
+ resize-query="[content_id='content']"
50
50
  resize-property="margin-left"
51
51
  resize-value="width">
52
52
  <menu
@@ -75,7 +75,7 @@
75
75
  share-width="700"
76
76
  share-media="https://cdn.cocreate.app/docs/calculate.png"
77
77
  hover="display:block!important"
78
- hover-selector=".social-networks">
78
+ hover-query=".social-networks">
79
79
  <div class="display:none social-networks">
80
80
  <a
81
81
  class="margin-right:15px"
@@ -134,11 +134,11 @@
134
134
  class="border-bottom:1px_solid_lightgrey"
135
135
  scroll
136
136
  scroll-intersect="color:dodgerblue"
137
- scroll-selector="#calculate-install-section">
137
+ scroll-query="#calculate-install-section">
138
138
  <span
139
139
  class="display:flex align-items:center width:fit-content"
140
140
  hover="display:block!important"
141
- hover-selector="[href='#calculate-install']">
141
+ hover-query="[href='#calculate-install']">
142
142
  <h2 class="padding:5px_0px">Install</h2>
143
143
  <a
144
144
  class="margin-left:10px display:none"
@@ -159,11 +159,11 @@
159
159
  class="margin-top:80px border-bottom:1px_solid_lightgrey"
160
160
  scroll
161
161
  scroll-intersect="color:dodgerblue"
162
- scroll-selector="#calculate-usage-section">
162
+ scroll-query="#calculate-usage-section">
163
163
  <span
164
164
  class="display:flex align-items:center width:fit-content"
165
165
  hover="display:block!important"
166
- hover-selector="[href='#calculate-usage']">
166
+ hover-query="[href='#calculate-usage']">
167
167
  <h2 class="padding:5px_0px">Usage</h2>
168
168
  <a
169
169
  class="margin-left:10px display:none"
@@ -193,11 +193,11 @@
193
193
  class="margin-top:80px border-bottom:1px_solid_lightgrey"
194
194
  scroll
195
195
  scroll-intersect="color:dodgerblue"
196
- scroll-selector="#calculate-attributes-section">
196
+ scroll-query="#calculate-attributes-section">
197
197
  <span
198
198
  class="display:flex align-items:center width:fit-content"
199
199
  hover="display:block!important"
200
- hover-selector="[href='#calculate-attributes']">
200
+ hover-query="[href='#calculate-attributes']">
201
201
  <h2 class="padding:5px_0px">Attributes</h2>
202
202
  <a
203
203
  class="margin-left:10px display:none"
@@ -244,11 +244,11 @@
244
244
  class="border-bottom:1px_solid_lightgrey"
245
245
  scroll
246
246
  scroll-intersect="color:dodgerblue"
247
- scroll-selector="#calculate-demo-section">
247
+ scroll-query="#calculate-demo-section">
248
248
  <span
249
249
  class="display:flex align-items:center width:fit-content"
250
250
  hover="display:block!important"
251
- hover-selector="[href='#calculate-demo']">
251
+ hover-query="[href='#calculate-demo']">
252
252
  <h2 class="padding:5px_0px">Demo</h2>
253
253
  <a
254
254
  class="margin-left:10px display:none"
@@ -295,7 +295,7 @@
295
295
  show="#eye-slash"
296
296
  hide="#eye, #demo-preview"
297
297
  toggle="code-height"
298
- toggle-selector="#demo-code"
298
+ toggle-query="#demo-code"
299
299
  ><i
300
300
  class="height:18px"
301
301
  src="/assets/svg/eye.svg"></i
@@ -307,7 +307,7 @@
307
307
  show="#eye, #demo-preview"
308
308
  hide="#eye-slash"
309
309
  toggle="code-height"
310
- toggle-selector="#demo-code"
310
+ toggle-query="#demo-code"
311
311
  ><i src="/assets/svg/eye-slash.svg"></i
312
312
  ></a>
313
313
  <a
@@ -330,7 +330,7 @@
330
330
  <a
331
331
  class="margin-right:5px"
332
332
  fullscreen
333
- fullscreen-selector="#playground"></a>
333
+ fullscreen-query="#playground"></a>
334
334
  </div>
335
335
  </div>
336
336
  <!-- End SandBox -->
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/calculate",
3
- "version": "1.16.0",
3
+ "version": "1.16.2",
4
4
  "description": "A handy vanilla JavaScript calculator, concatenate multiple elements containing integers & execute calculates. Can be used for creating invoices,making payments & any kind of complex calculate. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "calculate",
@@ -46,21 +46,17 @@
46
46
  },
47
47
  "main": "./src/index.js",
48
48
  "devDependencies": {
49
- "@babel/core": "^7.9.6",
50
- "@babel/preset-env": "^7.9.6",
51
- "babel-loader": "^8.1.0",
52
- "clean-webpack-plugin": "^3.0.0",
53
- "file-loader": "^6.2.0",
49
+ "css-loader": "^5.1.3",
50
+ "esbuild": "^0.25.2",
51
+ "esbuild-loader": "^4.3.0",
54
52
  "mini-css-extract-plugin": "^1.5.0",
55
- "style-loader": "^3.3.1",
56
- "terser-webpack-plugin": "^5.1.1",
57
53
  "webpack": "^5.24.4",
58
54
  "webpack-cli": "^4.5.0",
59
55
  "webpack-log": "^3.0.1"
60
56
  },
61
57
  "dependencies": {
62
- "@cocreate/element-prototype": "^1.23.0",
63
- "@cocreate/observer": "^1.16.1",
64
- "@cocreate/utils": "^1.33.7"
58
+ "@cocreate/element-prototype": "^1.29.1",
59
+ "@cocreate/observer": "^1.18.1",
60
+ "@cocreate/utils": "^1.39.0"
65
61
  }
66
62
  }
package/src/index.js CHANGED
@@ -1,44 +1,59 @@
1
- import observer from "@cocreate/observer";
2
- import { queryElements } from "@cocreate/utils";
3
- // import { renderValue } from '@cocreate/render';
4
- import "@cocreate/element-prototype";
1
+ import observer from "@cocreate/observer"; // Module for observing DOM mutations.
2
+ import { queryElements } from "@cocreate/utils"; // Utility for querying DOM elements.
3
+ import "@cocreate/element-prototype"; // Include custom element prototype extensions.
5
4
 
5
+ // Initializes the calculation elements within the document.
6
6
  function init() {
7
+ // Select all elements in the document with a "calculate" attribute.
7
8
  let calculateElements = document.querySelectorAll("[calculate]");
9
+ // Initialize each of the selected elements.
8
10
  initElements(calculateElements);
9
11
  }
10
12
 
13
+ // Initialize multiple elements by invoking initElement for each.
11
14
  function initElements(elements) {
15
+ // Iterate through the collection of elements and initialize each one.
12
16
  for (let el of elements) initElement(el);
13
17
  }
14
18
 
19
+ // Asynchronously initializes an individual element with setup for calculations.
15
20
  async function initElement(element) {
21
+ // Fetch the calculate string from the element's attribute.
16
22
  let calculate = element.getAttribute("calculate");
23
+ // Return early if the calculate string contains placeholders or template syntax.
17
24
  if (calculate.includes("{{") || calculate.includes("{[")) return;
18
25
 
19
- let selectors = getSelectors(calculate);
26
+ // Extract selectors from the calculate attribute value.
27
+ let selectors = getSelectors(element.attributes["calculate"].value);
20
28
 
29
+ // Iterate through each selector and set up elements impacted by them.
21
30
  for (let i = 0; i < selectors.length; i++) {
31
+ // Find input elements based on the selector criteria.
22
32
  let inputs = queryElements({
23
33
  element,
24
34
  selector: selectors[i],
25
35
  type: "selector"
26
36
  });
27
37
 
38
+ // Set up events for each found input element.
28
39
  for (let input of inputs) {
29
40
  initEvent(element, input);
30
41
  }
31
42
 
43
+ // Initialize an observer to monitor newly added nodes that match the selector.
32
44
  observer.init({
33
45
  name: "calculateSelectorInit",
34
46
  types: ["addedNodes"],
35
47
  selector: selectors[i],
48
+ // Callback function to run when nodes matching the selector are added.
36
49
  callback(mutation) {
50
+ // Initialize events for the new element and update calculation.
37
51
  initEvent(element, mutation.target);
38
52
  setCalcationValue(element);
39
53
  }
40
54
  });
41
55
  }
56
+ // Set initial calculation value when an element is being initialized.
42
57
  setCalcationValue(element);
43
58
  }
44
59
 
@@ -110,34 +125,47 @@ function initEvent(element, input) {
110
125
  });
111
126
  }
112
127
 
128
+ // Asynchronously set the calculated value for the given element.
113
129
  async function setCalcationValue(element) {
130
+ // Get the expression or formula from the element's "calculate" attribute.
114
131
  let calString = await getValues(element);
132
+ // Evaluate the formula and set the calculated value back to the element.
115
133
  element.setValue(calculate(calString));
116
134
  }
117
135
 
136
+ // Asynchronously retrieve values necessary for computing the calculation attribute of an element.
118
137
  async function getValues(element) {
138
+ // Get the expression that needs to be evaluated from the "calculate" attribute.
119
139
  let calculate = element.getAttribute("calculate");
120
140
 
121
- let selectors = getSelectors(calculate);
141
+ // Parse the expression to extract any selectors which values need to contribute to calculation.
142
+ let selectors = getSelectors(element.attributes["calculate"].value);
122
143
 
144
+ // For each selector, retrieve and calculate the respective value.
123
145
  for (let i = 0; i < selectors.length; i++) {
124
- let value = 0; // Default to 0 for missing inputs
146
+ let value = 0; // Default value in case no input is found for the selector.
147
+
148
+ // Query DOM elements based on selector.
125
149
  let inputs = queryElements({
126
- // Ensure queryElements can be awaited if needed
127
150
  element,
128
151
  selector: selectors[i],
129
152
  type: "selector"
130
153
  });
131
154
 
155
+ // Iterate through inputs/elements matched by the selector.
132
156
  for (let input of inputs) {
157
+ // Initialize event listeners on inputs so that changes can update the calculation.
133
158
  initEvent(element, input);
134
159
  let val = null;
160
+
161
+ // Attempt to get the value from the input element, if it can provide it.
135
162
  if (input.getValue) {
136
163
  val = Number(await input.getValue());
137
164
  }
138
165
 
166
+ // Only accumulate valid numeric values.
139
167
  if (!Number.isNaN(val)) {
140
- value += val; // Accumulate valid numeric values
168
+ value += val;
141
169
  } else {
142
170
  console.warn(
143
171
  `Invalid value for selector "${selectors[i]}". Defaulting to 0.`
@@ -145,29 +173,29 @@ async function getValues(element) {
145
173
  }
146
174
  }
147
175
 
176
+ // Replace the placeholder in the calculation expression with the accumulated value.
148
177
  calculate = calculate.replaceAll(`(${selectors[i]})`, value);
149
178
  }
150
-
151
- return calculate;
179
+ return calculate; // Return the resolved calculation expression.
152
180
  }
153
181
 
154
182
  // Defines mathematical constants available in expressions.
155
183
  const constants = { PI: Math.PI, E: Math.E };
156
184
 
157
- // Defines allowed mathematical functions and maps them to their JavaScript Math counterparts.
185
+ // Defines allowed mathematical functions and maps them to their respective JavaScript Math counterparts.
158
186
  const functions = {
159
- abs: Math.abs,
160
- ceil: Math.ceil,
161
- floor: Math.floor,
162
- round: Math.round,
163
- max: Math.max, // Note: RPN evaluator assumes arity 2 for max/min
164
- min: Math.min, // Note: RPN evaluator assumes arity 2 for max/min
165
- pow: Math.pow,
166
- sqrt: Math.sqrt,
187
+ abs: Math.abs, // Absolute value
188
+ ceil: Math.ceil, // Ceiling function
189
+ floor: Math.floor, // Floor function
190
+ round: Math.round, // Round to nearest integer
191
+ max: Math.max, // Maximum value (assumes arity 2 in RPN)
192
+ min: Math.min, // Minimum value (assumes arity 2 in RPN)
193
+ pow: Math.pow, // Exponentiation
194
+ sqrt: Math.sqrt, // Square root
167
195
  log: Math.log, // Natural logarithm
168
- sin: Math.sin,
169
- cos: Math.cos,
170
- tan: Math.tan
196
+ sin: Math.sin, // Sine function
197
+ cos: Math.cos, // Cosine function
198
+ tan: Math.tan // Tangent function
171
199
  };
172
200
 
173
201
  /**
@@ -924,24 +952,25 @@ function calculate(expression) {
924
952
  }
925
953
  }
926
954
 
927
- observer.init({
928
- name: "CoCreateCalculateChangeValue",
929
- types: ["attributes"],
930
- attributeFilter: ["calculate"],
931
- callback(mutation) {
932
- setCalcationValue(mutation.target);
933
- }
934
- });
935
-
936
- observer.init({
937
- name: "CoCreateCalculateInit",
938
- types: ["addedNodes"],
939
- selector: "[calculate]",
940
- callback(mutation) {
941
- initElement(mutation.target);
955
+ observer.init([
956
+ {
957
+ name: "CoCreateCalculateChangeValue",
958
+ types: ["attributes"],
959
+ attributeFilter: ["calculate"],
960
+ callback(mutation) {
961
+ setCalcationValue(mutation.target);
962
+ }
963
+ },
964
+ {
965
+ name: "CoCreateCalculateInit",
966
+ types: ["addedNodes"],
967
+ selector: "[calculate]",
968
+ callback(mutation) {
969
+ initElement(mutation.target);
970
+ }
942
971
  }
943
- });
972
+ ]);
944
973
 
945
- init();
974
+ init(); //
946
975
 
947
976
  export default { initElements, initElement, calculate };
package/webpack.config.js CHANGED
@@ -1,90 +1,65 @@
1
- const path = require("path")
2
- const TerserPlugin = require("terser-webpack-plugin")
3
- const MiniCssExtractPlugin = require("mini-css-extract-plugin")
4
- const { CleanWebpackPlugin } = require("clean-webpack-plugin")
5
-
6
- module.exports = (env, argv) => {
7
- let isProduction = false
8
- if (argv.mode === 'production')
9
- isProduction = true
10
-
11
- const config = {
12
- entry: {
13
- "CoCreate-calculate": "./src/index.js",
14
- },
15
- output: {
16
- path: path.resolve(__dirname, "dist"),
17
- filename: isProduction ? "[name].min.js" : "[name].js",
18
- libraryTarget: "umd",
19
- libraryExport: "default",
20
- library: ["CoCreate", "calculate"],
21
- globalObject: "this",
22
- },
23
-
24
- plugins: [
25
- new CleanWebpackPlugin(),
26
- new MiniCssExtractPlugin({
27
- filename: "[name].css",
28
- }),
29
- ],
30
- // Default mode for Webpack is production.
31
- // Depending on mode Webpack will apply different things
32
- // on final bundle. For now we don't need production's JavaScript
33
- // minifying and other thing so let's set mode to development
34
- mode: isProduction ? "production" : "development",
35
- module: {
36
- rules: [
37
- {
38
- test: /.js$/,
39
- exclude: /(node_modules)/,
40
- use: {
41
- loader: "babel-loader",
42
- options: {
43
- plugins: ["@babel/plugin-transform-modules-commonjs"],
44
- },
45
- },
46
- },
47
- {
48
- test: /.css$/i,
49
- use: [
50
- { loader: "style-loader", options: { injectType: "linkTag" } },
51
- "file-loader",
52
- ],
53
- },
54
- ],
55
- },
56
-
57
- // add source map
58
- ...(isProduction ? {} : { devtool: "eval-source-map" }),
59
-
60
- optimization: {
61
- minimize: true,
62
- minimizer: [
63
- new TerserPlugin({
64
- extractComments: true,
65
- // cache: true,
66
- parallel: true,
67
- // sourceMap: true, // Must be set to true if using source-maps in production
68
- terserOptions: {
69
- // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
70
- // extractComments: 'all',
71
- compress: {
72
- drop_console: true,
73
- },
74
- },
75
- }),
76
- ],
77
- splitChunks: {
78
- chunks: "all",
79
- minSize: 200,
80
- // maxSize: 99999,
81
- //minChunks: 1,
82
-
83
- cacheGroups: {
84
- defaultVendors: false,
85
- },
86
- },
87
- },
88
- }
89
- return config
90
- }
1
+ const path = require("path");
2
+ const MiniCssExtractPlugin = require("mini-css-extract-plugin");
3
+ const { EsbuildPlugin } = require("esbuild-loader");
4
+ const { FileUploader } = require("@cocreate/webpack");
5
+
6
+ module.exports = async (env, argv) => {
7
+ const isProduction = argv && argv.mode === "production";
8
+ const config = {
9
+ entry: {
10
+ "CoCreate-calculate": "./src/index.js"
11
+ },
12
+ output: {
13
+ path: path.resolve(__dirname, "dist"),
14
+ filename: isProduction ? "[name].min.js" : "[name].js",
15
+ libraryExport: "default",
16
+ library: ["CoCreate", "calculate"],
17
+ clean: true
18
+ },
19
+ plugins: [
20
+ new MiniCssExtractPlugin({
21
+ filename: isProduction ? "[name].min.css" : "[name].css"
22
+ }),
23
+ new FileUploader(env, argv)
24
+ ],
25
+ mode: isProduction ? "production" : "development",
26
+ devtool: isProduction ? "source-map" : "eval-source-map",
27
+ module: {
28
+ rules: [
29
+ {
30
+ test: /.js$/,
31
+ exclude: /node_modules/,
32
+ use: {
33
+ loader: "esbuild-loader",
34
+ options: {
35
+ loader: "js",
36
+ target: "es2017"
37
+ }
38
+ }
39
+ },
40
+ {
41
+ test: /.css$/i,
42
+ use: [MiniCssExtractPlugin.loader, "css-loader"]
43
+ }
44
+ ]
45
+ },
46
+ optimization: {
47
+ minimize: isProduction,
48
+ minimizer: [
49
+ new EsbuildPlugin({
50
+ target: "es2017",
51
+ css: true
52
+ })
53
+ ],
54
+ splitChunks: {
55
+ cacheGroups: {
56
+ defaultVendors: false
57
+ }
58
+ }
59
+ },
60
+ performance: {
61
+ hints: isProduction ? "warning" : false
62
+ }
63
+ };
64
+ return config;
65
+ };