@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.
- package/README.md +3 -3
- package/dist/cjs/Mouse.js +6 -1
- package/dist/cjs/Translator.js +7 -1
- package/dist/cjs/array.js +7 -25
- package/dist/cjs/dom.js +819 -210
- package/dist/cjs/eventDispatcher.js +6 -1
- package/dist/cjs/index.js +6 -13
- package/dist/cjs/is.js +49 -6
- package/dist/cjs/math.js +56 -31
- package/dist/cjs/onOff.js +2 -2
- package/dist/cjs/random.js +6 -6
- package/dist/cjs/string.js +19 -17
- package/dist/cjs/traversal.js +1 -2
- package/dist/cjs/utils.js +155 -38
- package/dist/esm/Mouse.js +8 -2
- package/dist/esm/Translator.js +9 -3
- package/dist/esm/array.js +10 -29
- package/dist/esm/dom.js +821 -211
- package/dist/esm/eventDispatcher.js +9 -3
- package/dist/esm/index.js +7 -8
- package/dist/esm/is.js +49 -6
- package/dist/esm/math.js +60 -34
- package/dist/esm/onOff.js +6 -6
- package/dist/esm/random.js +6 -6
- package/dist/esm/string.js +23 -21
- package/dist/esm/stringPrototype.js +2 -2
- package/dist/esm/traversal.js +2 -3
- package/dist/esm/utils.js +158 -41
- package/dist/umd/Translator.umd.js +1 -0
- package/dist/umd/dom.umd.js +1 -0
- package/dist/umd/eventDispatcher.umd.js +1 -0
- package/dist/umd/mouse.umd.js +1 -0
- package/dist/umd/webf.umd.js +1 -0
- package/docs/array.md +90 -0
- package/docs/dom.md +1477 -0
- package/docs/eventDispatcher.md +15 -0
- package/docs/is.md +259 -0
- package/docs/math.md +127 -0
- package/docs/mouse.md +58 -0
- package/docs/random.md +15 -0
- package/docs/string.md +145 -0
- package/docs/translator.md +95 -0
- package/docs/traversal.md +159 -0
- package/docs/utils.md +228 -0
- package/package.json +37 -5
- package/src/Mouse.js +73 -0
- package/src/Translator.js +148 -0
- package/src/array.js +136 -0
- package/src/dom.js +1553 -0
- package/src/eventDispatcher.js +118 -0
- package/src/index.js +106 -0
- package/src/is.js +201 -0
- package/src/math.js +113 -0
- package/src/onOff.js +313 -0
- package/src/random.js +38 -0
- package/src/string.js +662 -0
- package/src/stringPrototype.js +16 -0
- package/src/traversal.js +236 -0
- package/src/utils.js +242 -0
- package/types/Translator.d.ts +6 -5
- package/types/array.d.ts +1 -2
- package/types/dom.d.ts +763 -204
- package/types/index.d.ts +23 -22
- package/types/is.d.ts +3 -0
- package/types/math.d.ts +6 -5
- package/types/utils.d.ts +4 -4
- 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
|
+
}
|