UNPKG

wikibase-edit

Version:

Edit Wikibase from NodeJS

36 lines 1.86 kB
import { flatten, values } from 'lodash-es'; import { newError } from '../error.js'; import { normalizeDatatype } from '../properties/datatypes_to_builder_datatypes.js'; import { forceArray, mapValues } from '../utils.js'; import { validatePropertyId, validateSnakValue } from '../validate.js'; import { entityEditBuilders as builders } from './builders.js'; export function buildSnak(property, datatype, value, instance) { const datavalueValue = (typeof value === 'object' && 'value' in value) ? value.value : value; if (typeof value === 'object' && 'snaktype' in value && value?.snaktype && value.snaktype !== 'value') { return { snaktype: value.snaktype, property }; } const builderDatatype = normalizeDatatype(datatype); return builders[builderDatatype](property, datavalueValue, instance).mainsnak; } export function buildReferenceFactory(properties, instance) { return function buildReference(reference) { if (typeof reference !== 'object') throw newError('expected reference object', 500, { reference }); const hash = 'hash' in reference ? reference.hash : undefined; const referenceSnaks = 'snaks' in reference ? reference.snaks : reference; const snaksPerProperty = mapValues(referenceSnaks, buildPropSnaksFactory(properties, instance)); const snaks = flatten(values(snaksPerProperty)); return { snaks, hash }; }; } export function buildPropSnaksFactory(properties, instance) { return function buildPropSnaks(prop, propSnakValues) { validatePropertyId(prop); return forceArray(propSnakValues).map(snakValue => { const datatype = properties[prop]; validateSnakValue(prop, datatype, snakValue); return buildSnak(prop, datatype, snakValue, instance); }); }; } //# sourceMappingURL=snak.js.map