Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information...
zrrrzzt committed Jul 28, 2015
1 parent 7faa3e2 commit 5d702f71969d92e4f5450b90c0b72e3dba9fdf83
Showing with 199 additions and 0 deletions.
  1. +7 −0 .gitignore
  2. +30 −0 index.js
  3. +44 −0 lib/convert-data.js
  4. +1 −0 lib/get-sitemap.js
  5. +40 −0 package.json
  6. +22 −0 test/data/sitemap.json
  7. +23 −0 test/data/sitemap.xml
  8. +32 −0 test/sitemapToArray-test.js
@@ -1,3 +1,9 @@
# IDE
.idea

# OS X
.DS_Store

# Logs
logs
*.log
@@ -12,6 +18,7 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
@@ -0,0 +1,30 @@
'use strict';

var validUrl = require('valid-url');
var convertData = require('./lib/convert-data');

function sitemapToArray(options, callback) {
if (!options) {
return callback(new Error('Missing required input: options.'), null);
}

if (!options.data && !options.url) {
return callback(new Error('Missing required input: options.data or options.url.'), null);
}

if (options.url && !validUrl.isWebUri(options.url)) {
return callback(new Error('Invalid url: options.url.'), null);
}

if (options.data) {
convertData(options.data, function(error, data){
if (error) {
return callback(error, null);
} else {
return callback(null, data);
}
});
}
}

module.exports = sitemapToArray;
@@ -0,0 +1,44 @@
'use strict';

var xml2js = require('xml2js');
var parser = new xml2js.Parser();

/**
* unWrapItem
*
*@desc unwraps arrays to value
*
* @param {object} item - The object from
* @returns {object} newItem - The unwrapped item
*/
function unWrapItem(item) {
var newItem = {};

Object.keys(item).forEach(function(prop) {
newItem[prop] = item[prop][0];
});

return newItem;
}

/**
* convertData
*
* @desc converts sitemap.xml to an array of objects
*
* @param {string} data - The sitemap xmldata as string
* @param {callback} callback - The callback for handling the response
* @returns {object}
*/
function convertData(data, callback) {
if (!data) {
return callback(new Error('Missing required input: data'), null);
}

parser.parseString(data, function(err, result) {
return err ? callback(err, null) : callback(null, result.urlset.url.map(unWrapItem));
});

}

module.exports = convertData;
@@ -0,0 +1 @@
'use strict';
@@ -0,0 +1,40 @@
{
"name": "sitemap-to-array",
"description": "Convert sitemap.xml to an array of objects",
"version": "1.0.0",
"license": "MIT",
"author": {
"name": "Geir Gåsodden",
"email": "geir.gasodden@pythonia.no"
},
"keywords": [
"sitemap",
"convert",
"array"
],
"repository": {
"type": "git",
"url": "http://github.com/zrrrzzt/sitemap-to-array.git"
},
"bugs": {
"url": "http://github.com/zrrrzzt/sitemap-to-array/issues"
},
"engines": {
"node": ">=0.10.0"
},
"main": "index.js",
"scripts": {
"test": "tap --reporter=spec test/*.js",
"coverage": "tap test/*.js --coverage",
"coveralls": "tap --cov --coverage-report=lcov test/*.js && cat coverage/lcov.info | coveralls"
},
"devDependencies": {
"coveralls": "^2.11.3",
"jscs": "^2.0.0",
"tap": "^1.3.1"
},
"dependencies": {
"valid-url": "^1.0.9",
"xml2js": "^0.4.9"
}
}
@@ -0,0 +1,22 @@
[
{
"loc": "http://www.telemark.no/Vaare-tjenester",
"lastmod": "2015-05-06T10:51:03+00:00"
},
{
"loc": "http://www.telemark.no/Vaare-tjenester/Kurs-og-konferanser",
"lastmod": "2014-10-06T11:40:22+00:00"
},
{
"loc": "http://www.telemark.no/Vaare-tjenester/Folkehelse",
"lastmod": "2015-03-13T07:35:30+00:00"
},
{
"loc": "http://www.telemark.no/Vaare-tjenester/Folkehelse/Tilbud-HEFRES/Paa-farta-til-skolen",
"lastmod": "2015-05-24T15:56:56+00:00"
},
{
"loc": "http://www.telemark.no/Vaare-tjenester/Folkehelse/Tilbud-HEFRES/Alle-barn-sykler",
"lastmod": "2015-05-22T13:46:26+00:00"
}
]
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.telemark.no/Vaare-tjenester</loc>
<lastmod>2015-05-06T10:51:03+00:00</lastmod>
</url>
<url>
<loc>http://www.telemark.no/Vaare-tjenester/Kurs-og-konferanser</loc>
<lastmod>2014-10-06T11:40:22+00:00</lastmod>
</url>
<url>
<loc>http://www.telemark.no/Vaare-tjenester/Folkehelse</loc>
<lastmod>2015-03-13T07:35:30+00:00</lastmod>
</url>
<url>
<loc>http://www.telemark.no/Vaare-tjenester/Folkehelse/Tilbud-HEFRES/Paa-farta-til-skolen</loc>
<lastmod>2015-05-24T15:56:56+00:00</lastmod>
</url>
<url>
<loc>http://www.telemark.no/Vaare-tjenester/Folkehelse/Tilbud-HEFRES/Alle-barn-sykler</loc>
<lastmod>2015-05-22T13:46:26+00:00</lastmod>
</url>
</urlset>
@@ -0,0 +1,32 @@
'use strict';

var tap = require('tap');
var sitemapToArray = require('../index');


tap.test('It requires an options object.', function(test) {
var options = false;
var expectedErrorMessage = 'Missing required input: options.';
sitemapToArray(options, function (error, data){
tap.equal(error.message, expectedErrorMessage, expectedErrorMessage);
test.done();
});
});

tap.test('It requires options.data or options.url.', function(test) {
var options = {};
var expectedErrorMessage = 'Missing required input: options.data or options.url.';
sitemapToArray(options, function (error, data){
tap.equal(error.message, expectedErrorMessage, expectedErrorMessage);
test.done();
});
});

tap.test('It requires options.url to be valid.', function(test) {
var options = {url:'pysje'};
var expectedErrorMessage = 'Invalid url: options.url.';
sitemapToArray(options, function (error, data){
tap.equal(error.message, expectedErrorMessage, expectedErrorMessage);
test.done();
});
});

0 comments on commit 5d702f7

Please sign in to comment.
You can’t perform that action at this time.