UNPKG

@fontoxml/fontoxml-development-tools

Version:

Development tools for Fonto.

85 lines (71 loc) 1.91 kB
import symbols from './symbols.js'; function getValueFromPath(nameParts, obj) { return nameParts.reduce( (o, part) => (o && o[part] ? o[part] : undefined), obj ); } // TECHNICAL DEBT function assignValueToPath(nameParts, resultObj, value) { const name = nameParts.shift(); resultObj[name] = nameParts.length ? assignValueToPath(nameParts, resultObj[name] || {}, value) : value; return resultObj; } export default class DeepSyntaxPart { static [symbols.spliceInputFromParts](parts, _tiers) { let deepName = parts.shift(); deepName = deepName.substr(deepName.indexOf('.') + 1); // if value is a dash, set actual value to TRUE if (parts[0] === '-') { parts.shift(); return [ deepName, getValueFromPath(deepName.split('.'), this.cloneDefault()) || true, ]; } return parts[0] && parts[0].indexOf('-') !== 0 ? [deepName, parts.shift()] : [ deepName, getValueFromPath( deepName.split('.'), this.cloneDefault() ) || true, ]; } static [symbols.createContributionToRequestObject]( propertyName, accumulated, value ) { const accumulatedProperty = accumulated && accumulated[propertyName] && accumulated[propertyName][this.name]; const contribution = { [propertyName]: { [this.name]: accumulatedProperty || this.cloneDefault() || {}, }, }; // console.log(contribution[propertyName][this.name]); if (value === undefined) { return contribution; } // if (!value[0]?.split) { // console.log(value, accumulated); // } if (typeof value[0] === 'string') { assignValueToPath( value[0].split('.'), contribution[propertyName][this.name], value[1] ); } // Object.assign(contribution[propertyName][this.name], contributedProp); // console.log('Contributed DeepSyntaxPath response ' + propertyName + '/' + this.name, contribution); return contribution; } }