UNPKG

microdata-tooling

Version:

Provides tooling to help using micro data from TypeScript/JavaScript.

3 lines (2 loc) 1.36 kB
import{pushMany}from"https://unpkg.com/microdata-tooling@1.0.4/dist/utils.min.js";import{jsonify}from"https://unpkg.com/microdata-tooling@1.0.4/dist/jsonify.min.js";import{MultiTypeError,NoTypeError}from"https://unpkg.com/microdata-tooling@1.0.4/dist/errors.min.js";export function parse(t,o={}){const r=findTopLevel(t),n=jsonify(r),e=[];for(const t of n.items){const r=parseMicroJsonThing(t,o);null!==r&&e.push(r)}return e}function parseMicroJsonThing(t,o){if(void 0===t.type||0==t.type.length){if(o.skipOnNoType)return null;throw new NoTypeError("No Type")}if(t.type.length>1){if(o.skipOnMultiType)return null;throw new MultiTypeError("More than one Type")}const r={"@type":t.type[0]};t.id&&(r.identifier=t.id);for(const n in t.properties){const e=t.properties[n];switch(e.length){case 0:break;case 1:r[n]=getThing(e[0],o);break;default:const t=[];for(const r of e){const n=getThing(r,o);null!==n&&t.push(n)}r[n]=t}}return r}function getThing(t,o){return"string"==typeof t?t:parseMicroJsonThing(t,o)}function findTopLevel(t){if(isTopLevel(t))return[t];const o=[],r=[];for(pushMany(r,Array.from(t.children));r.length>0;){const t=r.pop();isTopLevel(t)?o.push(t):pushMany(r,Array.from(t.children))}return o}function isTopLevel(t){return t.hasAttribute("itemscope")&&t.hasAttribute("itemtype")&&!t.hasAttribute("itemprop")} //# sourceMappingURL=parse.min.js.map