parseCommon: resolve operator references

This commit is contained in:
Jannis R 2019-08-23 15:59:38 +02:00
parent 4270125bf7
commit 3eae7ab169
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
4 changed files with 45 additions and 8 deletions

36
lib/resolve-idx-refs.js Normal file
View file

@ -0,0 +1,36 @@
'use strict'
const scanner = require('object-scan')
const toPath = require('lodash/toPath')
const get = require('lodash/get')
// For all items in `tree` matching a path selector specified
// in `selector`, call `onRef` with the item, its parent and
// the path to the item.
// Example:
// tree: {foo: [{bar: 1}], hey: {there: [{bar: 2}]}}
// selector: **[*].bar
const findIdxRefs = (tree, selector, onRef) => {
const scan = scanner([selector])
for (const pathStr of scan(tree)) {
const path = toPath(pathStr)
const val = get(tree, path)
const parent = get(tree, path.slice(0, -1), {})
onRef(val, parent, path)
}
}
// For all items in `tree` matching a path selector specified
// in `selector`:
// - interpret the item as an index in `source`
// - set the parent's field `prop` to `source[item]`
const resolveIdxRefs = (tree, selector, source, prop) => {
findIdxRefs(tree, selector, (idx, parent) => {
if ('number' === typeof idx) parent[prop] = source[idx]
})
}
module.exports = {
findIdxRefs,
resolveIdxRefs
}

View file

@ -41,6 +41,7 @@
"gps-distance": "0.0.4", "gps-distance": "0.0.4",
"lodash": "^4.17.5", "lodash": "^4.17.5",
"luxon": "^1.3.0", "luxon": "^1.3.0",
"object-scan": "^10.0.9",
"p-retry": "^4.1.0", "p-retry": "^4.1.0",
"p-throttle": "^3.1.0", "p-throttle": "^3.1.0",
"pinkie-promise": "^2.0.1", "pinkie-promise": "^2.0.1",

View file

@ -1,5 +1,8 @@
'use strict' 'use strict'
const get = require('lodash/get')
const {findIdxRefs, resolveIdxRefs} = require('../lib/resolve-idx-refs')
// todo: move to separate file // todo: move to separate file
const parseIcon = (i) => { const parseIcon = (i) => {
const res = { const res = {
@ -11,13 +14,13 @@ const parseIcon = (i) => {
return res return res
} }
const parseCommonData = (profile, opt, raw) => { const parseCommonData = (profile, opt, res) => {
const res = Object.assign({}, raw) const c = res.common || {}
const c = raw.common || {}
res.operators = [] res.operators = []
if (Array.isArray(c.opL)) { if (Array.isArray(c.opL)) {
res.operators = c.opL.map(op => profile.parseOperator(profile, op)) res.operators = c.opL.map(op => profile.parseOperator(profile, op))
resolveIdxRefs(res, '**.oprX', res.operators, 'operator')
} }
res.icons = [] res.icons = []

View file

@ -2,7 +2,7 @@
const slugg = require('slugg') const slugg = require('slugg')
const createParseLine = (profile, opt, {operators}) => { const createParseLine = (profile, opt, _) => {
const byBitmask = [] const byBitmask = []
for (let product of profile.products) { for (let product of profile.products) {
for (let bitmask of product.bitmasks) { for (let bitmask of product.bitmasks) {
@ -37,10 +37,7 @@ const createParseLine = (profile, opt, {operators}) => {
res.product = product && product.id || null res.product = product && product.id || null
} }
if ('number' === typeof p.oprX) { if (p.operator) res.operator = p.operator // todo: move up
res.operator = operators[p.oprX] || null
}
return res return res
} }
return parseLine return parseLine