-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoreJs.mjs
110 lines (99 loc) · 3.45 KB
/
coreJs.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
export const TRUE = 1;
export const FALSE = 0;
export const toBoolean = value => value ? TRUE : FALSE;
// ================================================
export const UNDEFINED = undefined;
export const NULL = null;
// ================================================
export const isArray = tested => Array.isArray(tested);
export const isFunction = v => typeof v === 'function';
export const isObject = o => typeof o === 'object' && o !== null;
export const isString = (value) => typeof value === 'string';
export const isNumber = value => Number.isFinite(value);
export const isMap = object => object instanceof Map;
export const isCapitalLetter = letter => letter === letter.toUpperCase();
// ------------------------------------------------
export const makeArray = element => isArray(element) ? element : [element];
export const makeFirstCapital = text => text[0].toUpperCase() + text.substr(1);
export const makeEnumFromStringArray = arr => {
const result = {};
arr.forEach((e, i) => {
if (isArray(e)) {
e.forEach(el => result[el] = i + 1);
} else {
result[e] = i + 1;
}
});
return result;
}
export const makeCharList = (...list) => {
list = list.map(l => {
if (isString(l)) {
const newList = {};
for (let i = 0; i < l.length; i++) {
newList[l[i]] = 1;
}
return newList;
}
return l;
});
const result = Object.assign({}, ...list)
return result;
}
export const excludeFromCharList = (charList, ...listToExclude) => {
charList = makeCharList(charList);
listToExclude = makeCharList(...listToExclude);
for (let char in listToExclude) {
delete charList[char];
}
return charList;
}
// ================================================
// ################################################
// Events section
// ################################################
export const SoftEvent = (...fixedArgs) => {
// const list = new Map();
// return {
// list,
// add: (callback) => (list.set(callback, callback), callback),
// del: (callback) => list.delete(callback),
// raise: (...param) => list.forEach((callback) => callback( ...param, ...fixedArgs))
// }
const list = new Map();
list.add = (callback) => (list.set(callback, callback), callback);
list.del = list.delete;
list.raise = (...param) => list.forEach((callback) => callback( ...param, ...fixedArgs));
return list;
}
export const SoftEventSelective = (...fixedArgs) => {
const map = new Map();
return {
add: (obj, callback) => {
let list
if (map.has(obj)) {
list = map.get(obj);
} else {
list = new Map();
map.set(obj, list);
}
list.set(callback, callback)
return callback;
},
del: (obj, callback) => {
if (map.has(obj)) {
const list = map.get(obj);
list.delete(callback);
if (!list.length) map.delete(obj);
}
},
delAll: (obj) => map.delete(obj),
raise: (obj, ...param) => {
if (map.has(obj)) {
const list = map.get(obj);
list.forEach((callback) => callback(obj, ...param, ...fixedArgs))
}
}
}
}
// ------------------------------------------------