@webalternatif/js-core 1.5.5 → 1.6.3

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 CHANGED
@@ -1,7 +1,59 @@
1
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
2
+ ![Tests](https://img.shields.io/github/actions/workflow/status/webalternatif/js-core/ci.yml?label=Tests)
3
+ [![codecov](https://codecov.io/github/webalternatif/js-core/branch/main/graph/badge.svg?token=MLPFU51XJH)](https://codecov.io/github/webalternatif/js-core)
4
+ ![npm](https://img.shields.io/npm/v/@webalternatif/js-core)
5
+
1
6
  # js-core
2
- A set of Javascript utility functions.
3
7
 
4
- ## Functions
8
+ Modular JavaScript utilities for modern applications.
9
+
10
+ Lightweight, framework-agnostic helpers for data manipulation, DOM handling, events and internationalization.
11
+
12
+ ---
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @webalternatif/js-core
18
+ ```
19
+
20
+ ```bash
21
+ yarn add @webalternatif/js-core
22
+ ```
23
+
24
+ ## Basic Usage
25
+
26
+ The default export provides pure JavaScript utilities only.
27
+
28
+ ```js
29
+ import webf from '@webalternatif/js-core'
30
+
31
+ webf.unique([1, 2, 2]) // [1, 2]
32
+ webf.numberFormat(12345.6789, 2, true, '.', ',') // 12.345,68
33
+ webf.isTouchDevice() // true or false
34
+ ```
35
+
36
+ ## Documentation
37
+
38
+ ### Core modules
39
+ - [String utilities](docs/string.md)
40
+ - [Array utilities](docs/array.md)
41
+ - [Type checking](docs/is.md)
42
+ - [Random utilities](docs/random.md)
43
+ - [Traversal utilities](docs/traversal.md)
44
+ - [Math utilities](docs/math.md)
45
+ - [General utilities](docs/utils.md)
46
+
47
+ ### Additional modules
48
+ - [DOM utilities](docs/dom.md)
49
+ - [Event Dispatcher](docs/eventDispatcher.md)
50
+ - [Translator (i18n)](docs/translator.md)
51
+ - [Mouse utilities](docs/mouse.md)
5
52
 
6
53
  ## Tests
7
54
 
55
+ This project uses Jest for unit testing.
56
+
57
+ ```bash
58
+ yarn test
59
+ ```
package/dist/cjs/Mouse.js CHANGED
@@ -18,7 +18,13 @@ var Mouse = /*#__PURE__*/function () {
18
18
  }
19
19
  return _createClass(Mouse, null, [{
20
20
  key: "getPosition",
21
- value: function getPosition(ev, element) {
21
+ value:
22
+ /**
23
+ * @param {Event} ev
24
+ * @param {Element} element
25
+ * @returns {{x: number, y: number}}
26
+ */
27
+ function getPosition(ev, element) {
22
28
  ev = _assertClassBrand(Mouse, this, _getEvent).call(this, ev);
23
29
  var rect = {
24
30
  left: 0,
@@ -32,10 +38,15 @@ var Mouse = /*#__PURE__*/function () {
32
38
  };
33
39
  }
34
40
  return {
35
- x: ev.pageX !== undefined ? ev.pageX - rect.left : rect.left,
36
- y: ev.pageY !== undefined ? ev.pageY - rect.top : rect.top
41
+ x: ev.pageX - rect.left,
42
+ y: ev.pageY - rect.top
37
43
  };
38
44
  }
45
+
46
+ /**
47
+ * @param {Event} ev
48
+ * @returns {{x: number, y: number}}
49
+ */
39
50
  }, {
40
51
  key: "getViewportPosition",
41
52
  value: function getViewportPosition(ev) {
@@ -51,18 +62,24 @@ var Mouse = /*#__PURE__*/function () {
51
62
  ev = _assertClassBrand(Mouse, this, _getEvent).call(this, ev);
52
63
  return window.document.elementFromPoint(ev.clientX, ev.clientY);
53
64
  }
65
+
66
+ /**
67
+ * @param {Event|{originalEvent?: Event}} ev
68
+ * @returns {Event}
69
+ */
54
70
  }]);
55
71
  }();
56
72
  function _getEvent(ev) {
57
73
  var _ev$originalEvent;
74
+ ev = (_ev$originalEvent = ev.originalEvent) !== null && _ev$originalEvent !== void 0 ? _ev$originalEvent : ev;
58
75
  if ((0, _is.isTouchDevice)()) {
59
- var orgEvent = ev.originalEvent ? ev.originalEvent : ev;
60
- if (orgEvent.changedTouches && orgEvent.changedTouches.length) {
61
- ev = orgEvent.changedTouches[0];
62
- } else if (orgEvent.touches) {
63
- ev = orgEvent.touches[0];
64
- }
76
+ var _ev$changedTouches, _ev$touches;
77
+ var touch = ((_ev$changedTouches = ev.changedTouches) === null || _ev$changedTouches === void 0 ? void 0 : _ev$changedTouches[0]) || ((_ev$touches = ev.touches) === null || _ev$touches === void 0 ? void 0 : _ev$touches[0]);
78
+ ev.clientX = touch.clientX;
79
+ ev.clientY = touch.clientY;
80
+ ev.pageX = touch.pageX;
81
+ ev.pageY = touch.pageY;
65
82
  }
66
- return (_ev$originalEvent = ev.originalEvent) !== null && _ev$originalEvent !== void 0 ? _ev$originalEvent : ev;
83
+ return ev;
67
84
  }
68
85
  var _default = exports["default"] = Mouse;
@@ -19,13 +19,13 @@ function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
19
19
  function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
20
20
  function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
21
21
  /**
22
- * @typedef {string | (() => string)} TranslatorValue
22
+ * @typedef {string|(()=>string)} TranslatorValue
23
23
  */
24
24
  /**
25
- * @typedef {Record<string, Record<string, Record<string, TranslatorValue>>>} TranslatorNsMapping
25
+ * @typedef {Record<string,Record<string,Record<string,TranslatorValue>>>} TranslatorNsMapping
26
26
  */
27
27
  /**
28
- * @typedef {Record<string, Record<string, TranslatorValue>>} TranslatorCoreMapping
28
+ * @typedef {Record<string,Record<string,TranslatorValue>>} TranslatorCoreMapping
29
29
  */
30
30
  /**
31
31
  * @typedef {TranslatorNsMapping | TranslatorCoreMapping} TranslatorMapping
@@ -65,7 +65,7 @@ var Translator = exports["default"] = /*#__PURE__*/function () {
65
65
  var entry = _classPrivateFieldGet(_mapping, this)[namespace][lang][label];
66
66
  return _assertClassBrand(_Translator_brand, this, _resolve).call(this, entry);
67
67
  }
68
- return 'en' !== lang ? this.translate(label, namespace, 'en') : label;
68
+ return 'en' !== lang ? this.translate(label, 'en', namespace) : label;
69
69
  }
70
70
 
71
71
  /**
@@ -142,11 +142,7 @@ function _findKeyByValue(entries, label) {
142
142
  }
143
143
  function _resolve(entry) {
144
144
  if ((0, _is.isFunction)(entry)) {
145
- try {
146
- return entry();
147
- } catch (e) {
148
- return null;
149
- }
145
+ return entry();
150
146
  }
151
147
  return entry;
152
148
  }
package/dist/cjs/array.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.range = exports.indexOf = exports.inArray = exports.compareArray = exports.array_unique = exports.array_diff = exports.arrayUnique = exports.arrayDiff = void 0;
6
+ exports.range = exports.lastIndexOf = exports.indexOf = exports.inArray = exports.compareArray = exports.array_unique = exports.array_diff = exports.arrayUnique = exports.arrayDiff = void 0;
7
7
  var _traversal = require("./traversal.js");
8
8
  var _is = require("./is.js");
9
9
  var _math = require("./math.js");
@@ -11,7 +11,7 @@ var _utils = require("./utils.js");
11
11
  /**
12
12
  * Checks if a value exists in an array or an object
13
13
  *
14
- * @param {*} value the searched value
14
+ * @param {any} value the searched value
15
15
  * @param {Object|Array} arr the array
16
16
  * @param {number} [index=0] if provided, search from this index
17
17
  * @param {boolean} [strict=false] if true, search is done with strict equality
@@ -26,8 +26,7 @@ var _utils = require("./utils.js");
26
26
  * // → true
27
27
  *
28
28
  * @example
29
- * inArray(5, [1, 2, 3])
30
- * // → false
29
+ * inArray(5, [1, 2, 3]) // → false
31
30
  */
32
31
  var inArray = exports.inArray = function inArray(value, arr) {
33
32
  var index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
@@ -47,6 +46,7 @@ var inArray = exports.inArray = function inArray(value, arr) {
47
46
  } else if ((0, _is.isArray)(value) && (0, _is.isObject)(val)) {
48
47
  ret = _compareArray(val, value);
49
48
  return false;
49
+ // eslint-disable-next-line eqeqeq
50
50
  } else if (val == value) {
51
51
  ret = true;
52
52
  return false;
@@ -56,16 +56,63 @@ var inArray = exports.inArray = function inArray(value, arr) {
56
56
  });
57
57
  return ret;
58
58
  };
59
+
60
+ /**
61
+ * Returns the first index at which a given element can be found in an array or a string.
62
+ * or -1 if it is not present.
63
+ *
64
+ * @param {Array<any>|string} arr - The array to search in
65
+ * @param {any} elt - The element to search for
66
+ * @param {number} [from] - The index to start the search from. Can be negative.
67
+ * @returns {number} - The index of the element, or -1 if not found
68
+ */
59
69
  var indexOf = exports.indexOf = function indexOf(arr, elt) {
60
70
  var from = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
61
- from = from < 0 ? Math.ceil(from) + arr.length : Math.floor(from);
62
- for (; from < arr.length; from++) {
63
- if (from in arr && arr[from] === elt) {
71
+ var a = (0, _is.isString)(arr) ? (0, _traversal.map)(arr, function (_, a) {
72
+ return a;
73
+ }) : arr;
74
+ from = from < 0 ? Math.ceil(from) + a.length : Math.floor(from);
75
+ for (; from < a.length; from++) {
76
+ if (from in a && a[from] === elt) {
64
77
  return from;
65
78
  }
66
79
  }
67
80
  return -1;
68
81
  };
82
+
83
+ /**
84
+ * Returns the last index at which a given element can be found in an array or a string.
85
+ * or -1 if it is not present.
86
+ *
87
+ * @param {Array<any>|string} arr - The array to search in
88
+ * @param {any} elt - The element to search for
89
+ * @param {number} [from] - The index to start the search from. Can be negative.
90
+ * @returns {number} - The index of the element, or -1 if not found
91
+ */
92
+ var lastIndexOf = exports.lastIndexOf = function lastIndexOf(arr, elt) {
93
+ var from = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : -1;
94
+ var a = (0, _is.isString)(arr) ? (0, _traversal.map)(arr, function (_, a) {
95
+ return a;
96
+ }) : arr;
97
+ from = from < 0 ? a.length + Math.ceil(from) : Math.floor(from);
98
+ for (; from >= 0; from--) {
99
+ if (from in a && a[from] === elt) {
100
+ return from;
101
+ }
102
+ }
103
+ return -1;
104
+ };
105
+
106
+ /**
107
+ * Returns true if given arrays are equals
108
+ *
109
+ * @example
110
+ *
111
+ *
112
+ * @param {Array} a1
113
+ * @param {Array} a2
114
+ * @returns {boolean}
115
+ */
69
116
  var _compareArray = exports.compareArray = function compareArray(a1, a2) {
70
117
  if (a1.length !== a2.length) {
71
118
  return false;
@@ -95,7 +142,7 @@ var arrayDiff = exports.arrayDiff = function arrayDiff(array1, array2) {
95
142
  return !inArray(item, array2, 0, strict);
96
143
  });
97
144
  };
98
- var array_diff = exports.array_diff = arrayUnique;
145
+ var array_diff = exports.array_diff = arrayDiff;
99
146
  var range = exports.range = function range(size) {
100
147
  var startAt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
101
148
  var step = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;