microdata-node
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -6,12 +6,48 @@ 'use strict'; | ||
function createScope(spec) { | ||
function Item(spec) { | ||
var typeString = spec.type && spec.type.trim(); | ||
if (typeString) { | ||
this.type = typeString.split(/\s+/); | ||
} | ||
return { | ||
type: spec.type || null, | ||
id: spec.id || null, | ||
props: {} | ||
}; | ||
var idString = spec.id && spec.id.trim(); | ||
if (idString) { | ||
this.id = idString; | ||
} | ||
this.properties = {}; | ||
} | ||
Item.prototype.serialize = function serialize() { | ||
var item = { | ||
properties: {} | ||
}; | ||
if (this.type) { | ||
item.type = this.type; | ||
} | ||
if (this.id) { | ||
item.id = this.id; | ||
} | ||
Object.keys(this.properties).forEach(function (propName) { | ||
var values = this.properties[propName]; | ||
var serializedValues = values.map(function (value) { | ||
if (value instanceof Item) { | ||
return value.serialize(); | ||
} else { | ||
return value; | ||
} | ||
}, this); | ||
item.properties[propName] = serializedValues; | ||
}, this); | ||
return item; | ||
}; | ||
function parse($, config) { | ||
@@ -57,7 +93,7 @@ | ||
function walkNode(node, parentScope) { | ||
var currentScope = parentScope; | ||
var newScope = null; | ||
var currentItem = parentScope; | ||
var newItem = null; | ||
if (node.attr('itemscope') !== undefined) { | ||
newScope = createScope({ | ||
newItem = new Item({ | ||
type: node.attr('itemtype'), | ||
@@ -70,19 +106,15 @@ id: node.attr('itemid') | ||
if (prop === undefined) { | ||
if (newScope) { | ||
items.push(newScope); | ||
if (newItem) { | ||
items.push(newItem); | ||
} | ||
} else { | ||
var value = newScope || parseValue(node); | ||
if (currentScope.props[prop] === undefined) { | ||
currentScope.props[prop] = value; | ||
} else { | ||
if (!util.isArray(currentScope.props[prop])) { | ||
currentScope.props[prop] = [ currentScope.props[prop] ]; | ||
} | ||
currentScope.props[prop].push(value); | ||
} | ||
var value = newItem || parseValue(node); | ||
if (!currentItem.properties[prop]) { | ||
currentItem.properties[prop] = []; | ||
} | ||
currentItem.properties[prop].push(value); | ||
} | ||
node.children().each(function (i, child) { | ||
walkNode($(child), newScope || currentScope); | ||
walkNode($(child), newItem || currentItem); | ||
}); | ||
@@ -92,8 +124,10 @@ | ||
var rootScope = createScope({}); | ||
var rootScope = new Item({}); | ||
walkNode($.root(), rootScope); | ||
return items; | ||
return items.map(function (item) { | ||
return item.serialize(); | ||
}); | ||
} | ||
exports.parse = parse; |
{ | ||
"name": "microdata-node", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Cheerio based microdata parser", | ||
@@ -5,0 +5,0 @@ "main": "lib", |
@@ -44,19 +44,19 @@ microdata-node [![Build Status](https://travis-ci.org/Janpot/microdata-node.svg)](https://travis-ci.org/Janpot/microdata-node) | ||
{ | ||
"type": "http://schema.org/Person", | ||
"id": null, | ||
"props": { | ||
"name": "John Doe", | ||
"jobTitle": "graduate research assistant", | ||
"affiliation": "University of Dreams", | ||
"additionalName": "Johnny", | ||
"url": "http://www.johnnyd.com/", | ||
"address": { | ||
"type": "http://schema.org/PostalAddress", | ||
"id": null, | ||
"props": { | ||
"streetAddress": "1234 Peach Drive", | ||
"addressLocality": "Warner Robins", | ||
"addressRegion": "Georgia" | ||
"type": [ "http://schema.org/Person" ], | ||
"properties": { | ||
"name": [ "John Doe" ], | ||
"jobTitle": [ "graduate research assistant" ], | ||
"affiliation": [ "University of Dreams" ], | ||
"additionalName": [ "Johnny" ], | ||
"url": [ "http://www.johnnyd.com/" ], | ||
"address": [ | ||
{ | ||
"type": [ "http://schema.org/PostalAddress" ], | ||
"properties": { | ||
"streetAddress": [ "1234 Peach Drive" ], | ||
"addressLocality": [ "Warner Robins" ], | ||
"addressRegion": [ "Georgia" ] | ||
} | ||
} | ||
} | ||
] | ||
} | ||
@@ -63,0 +63,0 @@ } |
@@ -24,4 +24,4 @@ /* global describe, it */ | ||
assert.isObject(result[0]); | ||
assert.isNull(result[0].type); | ||
assert.isObject(result[0].props); | ||
assert.isUndefined(result[0].type); | ||
assert.isObject(result[0].properties); | ||
}); | ||
@@ -34,3 +34,3 @@ | ||
assert.lengthOf(result, 1); | ||
assert.strictEqual(result[0].type, 'http://schema.org/Person'); | ||
assert.deepEqual(result[0].type, [ 'http://schema.org/Person' ]); | ||
}); | ||
@@ -43,3 +43,3 @@ | ||
assert.lengthOf(result, 1); | ||
assert.strictEqual(result[0].type, 'http://schema.org/Person'); | ||
assert.deepEqual(result[0].type, ['http://schema.org/Person']); | ||
}); | ||
@@ -57,4 +57,4 @@ | ||
assert.lengthOf(result, 2); | ||
assert.strictEqual(result[0].type, 'http://schema.org/Person'); | ||
assert.strictEqual(result[1].type, 'http://schema.org/PostalAddress'); | ||
assert.deepEqual(result[0].type, ['http://schema.org/Person']); | ||
assert.deepEqual(result[1].type, ['http://schema.org/PostalAddress']); | ||
}); | ||
@@ -72,5 +72,5 @@ | ||
assert.lengthOf(result, 1); | ||
assert.deepEqual(result[0].props, { | ||
name: 'Jan', | ||
age: '29' | ||
assert.deepEqual(result[0].properties, { | ||
name: ['Jan'], | ||
age: ['29'] | ||
}); | ||
@@ -94,12 +94,10 @@ }); | ||
assert.deepEqual(result[0].props.address1, { | ||
type: 'http://schema.org/PostalAddress', | ||
id: null, | ||
props: { street: 'street1' } | ||
}); | ||
assert.deepEqual(result[0].props.address2, { | ||
type: 'http://schema.org/PostalAddress', | ||
id: null, | ||
props: { street: 'street2' } | ||
}); | ||
assert.deepEqual(result[0].properties.address1, [{ | ||
type: ['http://schema.org/PostalAddress'], | ||
properties: { street: ['street1'] } | ||
}]); | ||
assert.deepEqual(result[0].properties.address2, [{ | ||
type: ['http://schema.org/PostalAddress'], | ||
properties: { street: ['street2'] } | ||
}]); | ||
}); | ||
@@ -117,3 +115,3 @@ | ||
assert.lengthOf(result, 1); | ||
assert.deepEqual(result[0].props, { | ||
assert.deepEqual(result[0].properties, { | ||
name: ['Jan', 'Potoms'] | ||
@@ -120,0 +118,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9835
202