-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.js
135 lines (116 loc) · 3.28 KB
/
utils.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"use strict";
const util = require('util');
const stream = require('stream');
const fullTierList = ['Uber', 'OU', 'BL', 'UU', 'BL2', 'RU', 'BL3', 'NU', 'BL4', 'PU'];
const tierPositions = Object.create(null);
for (let i = 0; i < fullTierList.length; i++) {
tierPositions[fullTierList[i]] = i;
}
exports.tiers = fullTierList;
exports.getTierIndex = function (tier) {
return tierPositions[tier.replace(/[\(\)]/g, '')];
};
function RangeValidator(start, end) {
return function (value) {
return value >= start && value <= end;
};
}
/* Assume integer inputs */
exports.isValidEV = new RangeValidator(0, 252);
exports.isValidIV = new RangeValidator(0, 32);
exports.isValidLevel = new RangeValidator(1, 100);
exports.isValidHappiness = new RangeValidator(0, 252);
exports.toDict = function (data) {
if (!Array.isArray(data)) throw new TypeError("toDict only accepts arrays as input");
const dict = Object.create(null);
for (let i = 0, len = data.length; i < len; i++) {
dict[data[i]] = 1;
}
return dict;
};
exports.inValues = function inValues(obj, val) {
for (let key in obj) {
if (obj[key] === val) return true;
}
return false;
};
const cloneObject = exports.clone = function clone(obj) {
const clonedObj = {};
for (let key in obj) {
clonedObj[key] = obj[key];
}
return clonedObj;
};
const setKeys = ['species', 'gender', 'item', 'ability', 'shiny', 'level', 'happiness', 'evs', 'ivs', 'nature', 'moves'];
exports.markConflict = function markConflict(set, conflict) {
return Object.defineProperty(set, 'conflict', {
value: conflict,
enumerable: false,
writable: true,
configurable: true,
});
};
const markClone = exports.markClone = function markClone(set) {
return Object.defineProperty(set, 'isClone', {
value: true,
enumerable: false,
writable: true,
configurable: true,
});
};
exports.copySet = function copySet(set) {
const clone = {};
for (let i = 0; i < setKeys.length; i++) {
let key = setKeys[i];
if (!(key in set)) continue;
if (typeof set[key] !== 'object') {
// Primitive; never a function (or symbol)
clone[key] = set[key];
} else if (!Array.isArray(set[key])) {
// Object with depth 1
clone[key] = cloneObject(set[key]);
} else {
// Array of arrays
clone[key] = Array(set[key].length);
for (let j = 0; j < set[key].length; j++) {
clone[key][j] = set[key][j].slice();
}
}
}
return markClone(clone);
};
exports.sumIterate = function (arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
};
exports.excludeKeys = function (obj, excludeSet) {
const result = Object.create(Object.getPrototypeOf(obj));
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
if (excludeSet.has(keys[i])) continue;
result[keys[i]] = obj[keys[i]];
}
return result;
};
// Notations supported by PS teambuilder
exports.statIDs = {
HP: 'hp', hp: 'hp',
Atk: 'atk', atk: 'atk',
Def: 'def', def: 'def',
SpA: 'spa', SAtk: 'spa', SpAtk: 'spa', spa: 'spa',
SpD: 'spd', SDef: 'spd', SpDef: 'spd', spd: 'spd',
Spe: 'spe', Spd: 'spe', spe: 'spe',
};
function OutputStream() {
stream.Writable.call(this);
this.setData = '';
}
util.inherits(OutputStream, stream.Writable);
OutputStream.prototype._write = function (data, encoding, cb) {
this.setData += data;
cb();
};
exports.OutputStream = OutputStream;