-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (130 loc) · 4.48 KB
/
index.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
136
137
138
139
140
141
142
143
144
145
const defaults = require('defaults');
const objectPrettify = require('object-prettify');
const TEMPLATE = {
defindex: 0,
quality: 0,
craftable: true,
killstreak: 0,
australium: false,
festive: false,
effect: null,
paintkit: null,
wear: null,
quality2: null,
target: null,
craftnumber: null,
crateseries: null,
output: null,
outputQuality: null
};
/**
* Format items as strings or objects
*/
class SKU {
/**
* Convert SKU to item object
* @param {String} sku SKU string
* @return {Object} item Item object
*/
static fromString (sku) {
const attributes = {};
const parts = sku.split(';');
if (parts.length > 0) {
if (isNum(parts[0])) {
attributes.defindex = parseInt(parts[0]);
}
parts.shift();
}
if (parts.length > 0) {
if (isNum(parts[0])) {
attributes.quality = parseInt(parts[0]);
}
parts.shift();
}
for (let i = 0; i < parts.length; i++) {
const attribute = parts[i].replace('-', '');
if (attribute === 'uncraftable') {
attributes.craftable = false;
} else if (attribute === 'australium') {
attributes.australium = true;
} else if (attribute === 'festive') {
attributes.festive = true;
} else if (attribute === 'strange') {
attributes.quality2 = 11;
} else if (attribute.startsWith('kt') && isNum(attribute.substring(2))) {
attributes.killstreak = parseInt(attribute.substring(2));
} else if (attribute.startsWith('u') && isNum(attribute.substring(1))) {
attributes.effect = parseInt(attribute.substring(1));
} else if (attribute.startsWith('pk') && isNum(attribute.substring(2))) {
attributes.paintkit = parseInt(attribute.substring(2));
} else if (attribute.startsWith('w') && isNum(attribute.substring(1))) {
attributes.wear = parseInt(attribute.substring(1));
} else if (attribute.startsWith('td') && isNum(attribute.substring(2))) {
attributes.target = parseInt(attribute.substring(2));
} else if (attribute.startsWith('n') && isNum(attribute.substring(1))) {
attributes.craftnumber = parseInt(attribute.substring(1));
} else if (attribute.startsWith('c') && isNum(attribute.substring(1))) {
attributes.crateseries = parseInt(attribute.substring(1));
} else if (attribute.startsWith('od') && isNum(attribute.substring(2))) {
attributes.output = parseInt(attribute.substring(2));
} else if (attribute.startsWith('oq') && isNum(attribute.substring(2))) {
attributes.outputQuality = parseInt(attribute.substring(2));
}
}
const item = objectPrettify(defaults(attributes, TEMPLATE), TEMPLATE);
return item;
}
/**
* Convert item object to SKU
* @param {Object} item Item object
* @return {String} sku SKU string
*/
static fromObject (item) {
item = objectPrettify(defaults(item, TEMPLATE), TEMPLATE);
let sku = `${item.defindex};${item.quality}`;
if (item.effect) {
sku += `;u${item.effect}`;
}
if (item.australium === true) {
sku += ';australium';
}
if (item.craftable === false) {
sku += ';uncraftable';
}
if (item.wear) {
sku += `;w${item.wear}`;
}
if (item.paintkit) {
sku += `;pk${item.paintkit}`;
}
if (item.quality2 == 11) {
sku += ';strange';
}
if (typeof item.killstreak === 'number' && item.killstreak !== 0) {
sku += `;kt-${item.killstreak}`;
}
if (item.target) {
sku += `;td-${item.target}`;
}
if (item.festive === true) {
sku += ';festive';
}
if (item.craftnumber) {
sku += `;n${item.craftnumber}`;
}
if (item.crateseries) {
sku += `;c${item.crateseries}`;
}
if (item.output) {
sku += `;od-${item.output}`;
}
if (item.outputQuality) {
sku += `;oq-${item.outputQuality}`;
}
return sku;
}
}
function isNum (test) {
return /^-{0,1}\d+$/.test(test);
}
module.exports = SKU;