-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (103 loc) · 6.06 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
const displayNameMappings = require('./data/displayName')
const idMappings = require('./data/id')
const nameMappings = require('./data/name')
const imageMappings = require('./data/images')
const joinImages = require('join-images')
const mergedMappings = { ...displayNameMappings, ...nameMappings, ...idMappings }
const listedValues = Object.values(idMappings).map(el => el)
const stringSimilarity = require('string-similarity')
function idCompare (id, query) {
id = id.toString()
if (!Number.isInteger(Number(query.replace(':', '')))) return 0
else if (query === id) return 1
else if (id.split(':')[0] === query.split(':')[0]) return 0.6
else if (query.includes(':') && id.includes(':')) { if (id.includes(query)) return 0.7 } else if (query.includes(':') && !id.includes(':')) { if (id.split(':')[0].includes(query)) return 0.4 } else if (!query.includes(':') && id.includes(':')) { if (id.split(':')[0].includes(query)) return 0.5 } else if (id.split(':')[0].includes(query)) { return 0.3 } else return 0
}
module.exports.getBlockImageID = async function getBlockImageID (ids = []) {
const imgList = []
ids.forEach((element) => {
try {
imgList.push(Buffer.from(imageMappings[element].image.split(';base64,').pop(), 'base64'))
} catch {
throw new Error('Invalid Block ID present')
}
})
if (imgList.length === 1) return ('data:image/png;base64,' + imgList[0].toString('base64'))
if (imgList.length === 0) return ''
const bufferimg = await (await joinImages.joinImages(imgList, { direction: 'vertical' })).png().toBuffer()
return ('data:image/png;base64,' + bufferimg.toString('base64'))
}
module.exports.getBlockImageObject = async function getBlockImageID (objects = []) {
const imgList = []
objects.forEach((element) => {
if (imageMappings[element.id]) imgList.push(Buffer.from(imageMappings[element.id].image.split(';base64,').pop(), 'base64'))
})
if (imgList.length === 1) return ('data:image/png;base64,' + imgList[0].toString('base64'))
if (imgList.length === 0) return ''
const bufferimg = await (await joinImages.joinImages(imgList, { direction: 'vertical' })).png().toBuffer()
return ('data:image/png;base64,' + bufferimg.toString('base64'))
}
module.exports.isValidBlockID = function isValidBlockID (id) {
if (idMappings[id]) return true
else return false
}
module.exports.isValidBlockName = function isValidBlockName (name) {
if (nameMappings[name]) return true
else return false
}
module.exports.isValidBlockDisplayName = function isValidBlockDisplayName (displayName) {
if (displayNameMappings[displayName]) return true
else return false
}
module.exports.getBlockInfoFromIDs = async function getBlockInfoFromIDs (ids = []) {
const exportList = []
ids.forEach((element) => {
if (idMappings[element]) exportList.push(idMappings[element])
else throw new Error('Invalid Block ID present')
})
return [...new Set([...new Map(exportList.map(item => [item.id, item])).values()])]
}
module.exports.getBlockInfoFromNames = async function getBlockInfoFromNames (names = []) {
const exportList = []
names.forEach((element) => {
if (nameMappings[element]) exportList.push(nameMappings[element])
else throw new Error('Invalid Block Name present')
})
return [...new Set([...new Map(exportList.map(item => [item.id, item])).values()])]
}
module.exports.getBlockInfoFromDisplayNames = async function getBlockInfoFromDisplayNames (displayNames = []) {
const exportList = []
displayNames.forEach((element) => {
if (displayNameMappings[element]) exportList.push(displayNameMappings[element])
else throw new Error('Invalid Block Display Name present')
})
return [...new Set([...new Map(exportList.map(item => [item.id, item])).values()])]
}
module.exports.searchAbsolute = async function searchAbsolute (queryList = []) {
const exportList = []
queryList.forEach((element) => {
if (Object.keys(mergedMappings).find(key => key.toLowerCase() === element.toString().toLowerCase())) exportList.push(mergedMappings[Object.keys(mergedMappings).find(key => key.toLowerCase() === element.toString().toLowerCase())])
})
return [...new Set([...new Map(exportList.map(item => [item.id, item])).values()])]
}
module.exports.search = async function search (query) {
let exportList = []
listedValues.forEach((element) => {
if (element.displayName.toLowerCase().includes(query.toLowerCase())) exportList.push(element)
else if (element.name.toLowerCase().includes(query.toLowerCase())) exportList.push(element)
else if (element.id.toString().toLowerCase().includes(query.toLowerCase())) exportList.push(element)
})
exportList = [...new Set(exportList)].sort((firstEl, secondEl) => {
if ((idCompare(firstEl.id.toString().toLowerCase(), query.toLowerCase()) + stringSimilarity.compareTwoStrings(firstEl.name.toLowerCase(), query.toLowerCase()) + stringSimilarity.compareTwoStrings(firstEl.displayName.toLowerCase(), query.toLowerCase())) > (idCompare(secondEl.id.toString().toLowerCase(), query.toLowerCase()) + stringSimilarity.compareTwoStrings(secondEl.name.toLowerCase(), query.toLowerCase()) + stringSimilarity.compareTwoStrings(secondEl.displayName.toLowerCase(), query.toLowerCase()))) return -1
else if ((idCompare(firstEl.id.toString().toLowerCase(), query.toLowerCase()) + stringSimilarity.compareTwoStrings(firstEl.name.toLowerCase(), query.toLowerCase()) + stringSimilarity.compareTwoStrings(firstEl.displayName.toLowerCase(), query.toLowerCase())) < (idCompare(secondEl.id.toString().toLowerCase(), query.toLowerCase()) + stringSimilarity.compareTwoStrings(secondEl.name.toLowerCase(), query.toLowerCase()) + stringSimilarity.compareTwoStrings(secondEl.displayName.toLowerCase(), query.toLowerCase()))) return 1
else return 0
})
return [...new Set([...new Map(exportList.map(item => [item.id, item])).values()])]
}
module.exports.filterInvalid = async function filterInvalid (queryList = []) {
const exportList = []
queryList.forEach((element) => {
if (Object.keys(mergedMappings).find(key => key.toLowerCase() === element.toString().toLowerCase())) exportList.push(element)
})
return [...new Set(exportList)]
}