-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfindMatchingObjects.js
54 lines (49 loc) · 1.59 KB
/
findMatchingObjects.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
//Extracts object property values recursively
const extractProps = (item) => {
if (typeof item === 'object') {
return Array.isArray(item)
? item.map((val) => extractProps(val)).flat()
: Object.values(item)
.map((value) => extractProps(value))
.flat()
} else {
return item
}
}
//Matches an array of property values with a searchTerm,
//by default run as a filter (stopping at first positive).
//Can run through every possible value if required (for data-mining purposes)
const propMatch = (propValues, searchTerm, completeSearch = false) => {
const nonNumeric = propValues
.filter((prop) => isNaN(prop))
.map((prop) => prop.toLowerCase())
searchTerm = searchTerm.toLowerCase()
if (completeSearch) {
let result = false
nonNumeric.forEach((prop) => {
if (prop.includes(searchTerm)) {
matches.add(prop)
result = true
}
})
return result
} else {
return nonNumeric.some((prop) => {
if (prop.includes(searchTerm.toLowerCase())) {
matches.add(prop)
return true
}
})
}
}
//For keeping unique results
const matches = new Set()
//Finds objects in the space with any property value including a given string
function findMatchingObjects(searchTerm) {
const objects = game.filterObjectsInSpace((obj) => {
const props = extractProps(obj)
return propMatch(props, searchTerm)
})
console.log(matches, objects)
return objects
}