@webalternatif/js-core 1.6.0 → 1.6.4

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 (67) hide show
  1. package/README.md +3 -3
  2. package/dist/cjs/Mouse.js +6 -1
  3. package/dist/cjs/Translator.js +7 -1
  4. package/dist/cjs/array.js +7 -25
  5. package/dist/cjs/dom.js +819 -210
  6. package/dist/cjs/eventDispatcher.js +6 -1
  7. package/dist/cjs/index.js +6 -13
  8. package/dist/cjs/is.js +49 -6
  9. package/dist/cjs/math.js +56 -31
  10. package/dist/cjs/onOff.js +2 -2
  11. package/dist/cjs/random.js +6 -6
  12. package/dist/cjs/string.js +19 -17
  13. package/dist/cjs/traversal.js +1 -2
  14. package/dist/cjs/utils.js +155 -38
  15. package/dist/esm/Mouse.js +8 -2
  16. package/dist/esm/Translator.js +9 -3
  17. package/dist/esm/array.js +10 -29
  18. package/dist/esm/dom.js +821 -211
  19. package/dist/esm/eventDispatcher.js +9 -3
  20. package/dist/esm/index.js +7 -8
  21. package/dist/esm/is.js +49 -6
  22. package/dist/esm/math.js +60 -34
  23. package/dist/esm/onOff.js +6 -6
  24. package/dist/esm/random.js +6 -6
  25. package/dist/esm/string.js +23 -21
  26. package/dist/esm/stringPrototype.js +2 -2
  27. package/dist/esm/traversal.js +2 -3
  28. package/dist/esm/utils.js +158 -41
  29. package/dist/umd/Translator.umd.js +1 -0
  30. package/dist/umd/dom.umd.js +1 -0
  31. package/dist/umd/eventDispatcher.umd.js +1 -0
  32. package/dist/umd/mouse.umd.js +1 -0
  33. package/dist/umd/webf.umd.js +1 -0
  34. package/docs/array.md +90 -0
  35. package/docs/dom.md +1477 -0
  36. package/docs/eventDispatcher.md +15 -0
  37. package/docs/is.md +259 -0
  38. package/docs/math.md +127 -0
  39. package/docs/mouse.md +58 -0
  40. package/docs/random.md +15 -0
  41. package/docs/string.md +145 -0
  42. package/docs/translator.md +95 -0
  43. package/docs/traversal.md +159 -0
  44. package/docs/utils.md +228 -0
  45. package/package.json +37 -5
  46. package/src/Mouse.js +73 -0
  47. package/src/Translator.js +148 -0
  48. package/src/array.js +136 -0
  49. package/src/dom.js +1553 -0
  50. package/src/eventDispatcher.js +118 -0
  51. package/src/index.js +106 -0
  52. package/src/is.js +201 -0
  53. package/src/math.js +113 -0
  54. package/src/onOff.js +313 -0
  55. package/src/random.js +38 -0
  56. package/src/string.js +662 -0
  57. package/src/stringPrototype.js +16 -0
  58. package/src/traversal.js +236 -0
  59. package/src/utils.js +242 -0
  60. package/types/Translator.d.ts +6 -5
  61. package/types/array.d.ts +1 -2
  62. package/types/dom.d.ts +763 -204
  63. package/types/index.d.ts +23 -22
  64. package/types/is.d.ts +3 -0
  65. package/types/math.d.ts +6 -5
  66. package/types/utils.d.ts +4 -4
  67. package/types/i18n.d.ts +0 -4
@@ -0,0 +1,148 @@
1
+ import { isFunction, isPlainObject } from './is.js'
2
+ import { each, extend } from './traversal.js'
3
+
4
+ /**
5
+ * @typedef {string|(()=>string)} TranslatorValue
6
+ */
7
+
8
+ /**
9
+ * @typedef {Record<string,Record<string,Record<string,TranslatorValue>>>} TranslatorNsMapping
10
+ */
11
+
12
+ /**
13
+ * @typedef {Record<string,Record<string,TranslatorValue>>} TranslatorCoreMapping
14
+ */
15
+
16
+ /**
17
+ * @typedef {TranslatorNsMapping | TranslatorCoreMapping} TranslatorMapping
18
+ */
19
+
20
+ class Translator {
21
+ /** @type string */
22
+ #lang
23
+
24
+ /** @type TranslatorMapping */
25
+ #mapping
26
+
27
+ /**
28
+ * @param {TranslatorMapping} mapping
29
+ * @param {string} [defaultLang]
30
+ */
31
+ constructor(mapping, defaultLang) {
32
+ this.#setMapping(mapping)
33
+ this.setLang(defaultLang)
34
+ }
35
+
36
+ #setMapping(mapping) {
37
+ let nsMapping = {},
38
+ coreMapping = {}
39
+
40
+ each(mapping, (ns, langs) => {
41
+ each(langs, (lang, trads) => {
42
+ if (isPlainObject(trads)) {
43
+ if (undefined === nsMapping[ns]) nsMapping[ns] = {}
44
+ nsMapping[ns][lang] = trads
45
+ } else {
46
+ if (undefined === coreMapping[ns]) coreMapping[ns] = {}
47
+ coreMapping[ns][lang] = trads
48
+ }
49
+ })
50
+ })
51
+
52
+ this.#mapping = extend(true, nsMapping, {
53
+ core: extend({}, nsMapping.core || {}, coreMapping),
54
+ })
55
+ }
56
+
57
+ /**
58
+ * @param {string} label
59
+ * @param {string} [lang]
60
+ * @param {string} [namespace='core']
61
+ * @returns {string}
62
+ */
63
+ translate(label, lang, namespace = 'core') {
64
+ lang = undefined === lang ? this.getLang() : lang
65
+
66
+ const translationExists =
67
+ undefined !== this.#mapping[namespace] &&
68
+ undefined !== this.#mapping[namespace][lang] &&
69
+ undefined !== this.#mapping[namespace][lang][label]
70
+
71
+ if (translationExists) {
72
+ const entry = this.#mapping[namespace][lang][label]
73
+ return this.#resolve(entry)
74
+ }
75
+
76
+ return 'en' !== lang ? this.translate(label, 'en', namespace) : label
77
+ }
78
+
79
+ /**
80
+ * @param {string} label
81
+ * @param {string} from - Language from.
82
+ * @param {string} to - Language to.
83
+ * @param {string} [namespace='core']
84
+ */
85
+ translateFrom(label, from, to, namespace = 'core') {
86
+ if (!label) return label
87
+
88
+ const mapNs = this.#mapping?.[namespace]
89
+ if (!mapNs) return label
90
+
91
+ const mapFrom = mapNs?.[from]
92
+ const mapTo = mapNs?.[to]
93
+ if (!mapFrom || !mapTo) return label
94
+
95
+ const key = this.#findKeyByValue(mapFrom, label)
96
+ if (!key) return label
97
+
98
+ const entryTo = mapTo[key]
99
+ const resolvedTo = this.#resolve(entryTo)
100
+
101
+ return resolvedTo ?? label
102
+ }
103
+
104
+ _(...args) {
105
+ return this.translate(...args)
106
+ }
107
+
108
+ getLang() {
109
+ return this.#lang
110
+ }
111
+
112
+ setLang(lang) {
113
+ if (!lang) {
114
+ if (typeof navigator !== 'undefined' && navigator.language) {
115
+ lang = navigator.language
116
+ } else if (typeof process !== 'undefined' && process.env) {
117
+ lang = process.env.LANG || process.env.LC_ALL || process.env.LC_MESSAGES
118
+ }
119
+ }
120
+
121
+ this.#lang = (lang || 'en').trim().toLowerCase().slice(0, 2)
122
+ }
123
+
124
+ #findKeyByValue(entries, label) {
125
+ for (const key in entries) {
126
+ const resolved = this.#resolve(entries[key])
127
+ if (resolved === label) return key
128
+ }
129
+
130
+ return null
131
+ }
132
+
133
+ #resolve(entry) {
134
+ if (isFunction(entry)) {
135
+ return entry()
136
+ }
137
+
138
+ return entry
139
+ }
140
+ }
141
+
142
+ export default Translator
143
+
144
+ /* istanbul ignore next */
145
+ if (typeof window !== 'undefined') {
146
+ window.webf = window.webf || {}
147
+ window.webf.Translator = Translator
148
+ }
package/src/array.js ADDED
@@ -0,0 +1,136 @@
1
+ import { each, map } from './traversal.js'
2
+ import { isArray, isInteger, isObject, isString, isUndefined } from './is.js'
3
+ import { round } from './math.js'
4
+ import { equals } from './utils.js'
5
+
6
+ /**
7
+ * Checks if a value exists in an array or an object
8
+ *
9
+ * @param {any} value the searched value
10
+ * @param {Object|Array} arr the array
11
+ * @param {number} [index=0] if provided, search from this index
12
+ * @param {boolean} [strict=false] if true, search is done with strict equality
13
+ * @returns {boolean}
14
+ *
15
+ * @example
16
+ * inArray(2, [1, 2, 3]) => true
17
+ *
18
+ * @example
19
+ * inArray({a: 1}, {a: 1, b: 2}) // => true
20
+ *
21
+ * @example
22
+ * inArray(5, [1, 2, 3]) // => false
23
+ */
24
+ export const inArray = function (value, arr, index = 0, strict = false) {
25
+ let ret = false
26
+
27
+ each(arr, (i, val) => {
28
+ if (i >= index) {
29
+ if (strict) {
30
+ if (val === value) {
31
+ ret = true
32
+ return false
33
+ }
34
+ } else {
35
+ if (isObject(value) && isObject(val)) {
36
+ ret = equals(val, value)
37
+ return false
38
+ } else if (isArray(value) && isObject(val)) {
39
+ ret = equals(val, value)
40
+ return false
41
+ // eslint-disable-next-line eqeqeq
42
+ } else if (val == value) {
43
+ ret = true
44
+ return false
45
+ }
46
+ }
47
+ }
48
+ })
49
+
50
+ return ret
51
+ }
52
+
53
+ /**
54
+ * Returns the first index at which a given element can be found in an array or a string.
55
+ * or -1 if it is not present.
56
+ *
57
+ * @param {Array<any>|string} arr - The array to search in
58
+ * @param {any} elt - The element to search for
59
+ * @param {number} [from] - The index to start the search from. Can be negative.
60
+ * @returns {number} - The index of the element, or -1 if not found
61
+ */
62
+ export const indexOf = function (arr, elt, from = 0) {
63
+ const a = isString(arr) ? map(arr, (_, a) => a) : arr
64
+
65
+ from = from < 0 ? Math.ceil(from) + a.length : Math.floor(from)
66
+
67
+ for (; from < a.length; from++) {
68
+ if (from in a && a[from] === elt) {
69
+ return from
70
+ }
71
+ }
72
+
73
+ return -1
74
+ }
75
+
76
+ /**
77
+ * Returns the last index at which a given element can be found in an array or a string.
78
+ * or -1 if it is not present.
79
+ *
80
+ * @param {Array<any>|string} arr - The array to search in
81
+ * @param {any} elt - The element to search for
82
+ * @param {number} [from] - The index to start the search from. Can be negative.
83
+ * @returns {number} - The index of the element, or -1 if not found
84
+ */
85
+ export const lastIndexOf = function (arr, elt, from = -1) {
86
+ const a = isString(arr) ? map(arr, (_, a) => a) : arr
87
+
88
+ from = from < 0 ? a.length + Math.ceil(from) : Math.floor(from)
89
+
90
+ for (; from >= 0; from--) {
91
+ if (from in a && a[from] === elt) {
92
+ return from
93
+ }
94
+ }
95
+
96
+ return -1
97
+ }
98
+
99
+ export const arrayUnique = function (arr) {
100
+ return arr.filter((el, index, arr) => index === indexOf(arr, el))
101
+ }
102
+
103
+ export const array_unique = arrayUnique
104
+
105
+ export const arrayDiff = (array1, array2, strict = false) => {
106
+ return array1.filter((item) => !inArray(item, array2, 0, strict))
107
+ }
108
+
109
+ export const array_diff = arrayDiff
110
+
111
+ export const range = function (size, startAt = 0, step = 1) {
112
+ size = round(size)
113
+ step = round(step)
114
+
115
+ const rng = []
116
+
117
+ if (isUndefined(startAt) || size < 1 || step === 0 || size < Math.abs(step)) {
118
+ return rng
119
+ }
120
+
121
+ const end = size * step
122
+
123
+ if (isString(startAt)) {
124
+ startAt = startAt.charCodeAt(0)
125
+
126
+ for (let i = 0; step > 0 ? i < end : i > end; i += step) {
127
+ rng.push(String.fromCharCode(startAt + i))
128
+ }
129
+ } else if (isInteger(startAt)) {
130
+ for (let i = 0; step > 0 ? i < end : i > end; i += step) {
131
+ rng.push(startAt + i)
132
+ }
133
+ }
134
+
135
+ return rng
136
+ }