Skip to content

Commit

Permalink
spaces between functions
Browse files Browse the repository at this point in the history
  • Loading branch information
derduher committed Aug 31, 2019
1 parent 2d1108f commit 9678baf
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 67 deletions.
3 changes: 3 additions & 0 deletions lib/sitemap-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export async function parseSitemap (xml: Readable): Promise<ISitemapOptions> {
currentItem.news = newsTemplate();
}
})

saxStream.on('opentag', (tag): void => {
switch (tag.name) {
case "url":
Expand Down Expand Up @@ -155,6 +156,7 @@ export async function parseSitemap (xml: Readable): Promise<ISitemapOptions> {
break;
}
})

saxStream.on('text', (text): void => {
switch (currentTag) {
case "mobile:mobile":
Expand Down Expand Up @@ -327,6 +329,7 @@ export async function parseSitemap (xml: Readable): Promise<ISitemapOptions> {
break;
}
})

saxStream.on('attribute', (attr): void => {
switch (currentTag) {
case "urlset":
Expand Down
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"eslintConfig": {
"env": {
"es6": true,
"jasmine": true,
"jest": true,
"node": true
},
Expand All @@ -66,6 +65,21 @@
"no-unused-vars": 0,
"indent": "off",
"no-dupe-class-members": "off",
"lines-between-class-members": [
"error",
"always",
{
"exceptAfterSingleLine": true
}
],
"padding-line-between-statements": [
"error",
{
"blankLine": "always",
"prev": "multiline-expression",
"next": "multiline-expression"
}
],
"@typescript-eslint/indent": [
"error",
2
Expand Down
141 changes: 75 additions & 66 deletions tests/sitemap-index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import 'babel-polyfill';
import {
buildSitemapIndex,
createSitemapIndex
} from '../index'
import 'babel-polyfill'
import { buildSitemapIndex, createSitemapIndex } from '../index'
import { tmpdir } from 'os'
import { existsSync, unlinkSync } from 'fs'
/* eslint-env jest, jasmine */
function removeFilesArray (files) {
function removeFilesArray(files): void {
if (files && files.length) {
files.forEach(function (file) {
files.forEach(function(file) {
if (existsSync(file)) {
unlinkSync(file)
}
Expand All @@ -19,16 +16,17 @@ function removeFilesArray (files) {
const xmlDef = '<?xml version="1.0" encoding="UTF-8"?>'
describe('sitemapIndex', () => {
it('build sitemap index', () => {
const expectedResult = xmlDef +
'<?xml-stylesheet type="text/xsl" href="https://test.com/style.xsl"?>' +
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' +
'<sitemap>' +
'<loc>https://test.com/s1.xml</loc>' +
'</sitemap>' +
'<sitemap>' +
'<loc>https://test.com/s2.xml</loc>' +
'</sitemap>' +
'</sitemapindex>'
const expectedResult =
xmlDef +
'<?xml-stylesheet type="text/xsl" href="https://test.com/style.xsl"?>' +
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' +
'<sitemap>' +
'<loc>https://test.com/s1.xml</loc>' +
'</sitemap>' +
'<sitemap>' +
'<loc>https://test.com/s2.xml</loc>' +
'</sitemap>' +
'</sitemapindex>'

const result = buildSitemapIndex({
urls: ['https://test.com/s1.xml', 'https://test.com/s2.xml'],
Expand All @@ -37,16 +35,18 @@ describe('sitemapIndex', () => {

expect(result).toBe(expectedResult)
})

it('build sitemap index with custom xmlNS', () => {
const expectedResult = xmlDef +
'<sitemapindex xmlns="http://www.example.org/schemas/sitemap/0.9">' +
'<sitemap>' +
'<loc>https://test.com/s1.xml</loc>' +
'</sitemap>' +
'<sitemap>' +
'<loc>https://test.com/s2.xml</loc>' +
'</sitemap>' +
'</sitemapindex>'
const expectedResult =
xmlDef +
'<sitemapindex xmlns="http://www.example.org/schemas/sitemap/0.9">' +
'<sitemap>' +
'<loc>https://test.com/s1.xml</loc>' +
'</sitemap>' +
'<sitemap>' +
'<loc>https://test.com/s2.xml</loc>' +
'</sitemap>' +
'</sitemapindex>'

const result = buildSitemapIndex({
urls: ['https://test.com/s1.xml', 'https://test.com/s2.xml'],
Expand All @@ -55,22 +55,24 @@ describe('sitemapIndex', () => {

expect(result).toBe(expectedResult)
})

it('build sitemap index with lastmodISO', () => {
const expectedResult = xmlDef +
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' +
'<sitemap>' +
'<loc>https://test.com/s1.xml</loc>' +
'<lastmod>2018-11-26T00:00:00.000Z</lastmod>' +
'</sitemap>' +
'<sitemap>' +
'<loc>https://test.com/s2.xml</loc>' +
'<lastmod>2018-11-27T00:00:00.000Z</lastmod>' +
'</sitemap>' +
'<sitemap>' +
'<loc>https://test.com/s3.xml</loc>' +
'<lastmod>2019-07-01T00:00:00.000Z</lastmod>' +
'</sitemap>' +
'</sitemapindex>'
const expectedResult =
xmlDef +
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' +
'<sitemap>' +
'<loc>https://test.com/s1.xml</loc>' +
'<lastmod>2018-11-26T00:00:00.000Z</lastmod>' +
'</sitemap>' +
'<sitemap>' +
'<loc>https://test.com/s2.xml</loc>' +
'<lastmod>2018-11-27T00:00:00.000Z</lastmod>' +
'</sitemap>' +
'<sitemap>' +
'<loc>https://test.com/s3.xml</loc>' +
'<lastmod>2019-07-01T00:00:00.000Z</lastmod>' +
'</sitemap>' +
'</sitemapindex>'

const result = buildSitemapIndex({
urls: [
Expand All @@ -92,14 +94,16 @@ describe('sitemapIndex', () => {

expect(result).toBe(expectedResult)
})

it('build sitemap index with lastmod', () => {
const expectedResult = xmlDef +
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' +
'<sitemap>' +
'<loc>https://test.com/s1.xml</loc>' +
'<lastmod>2018-11-26T00:00:00.000Z</lastmod>' +
'</sitemap>' +
'</sitemapindex>'
const expectedResult =
xmlDef +
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' +
'<sitemap>' +
'<loc>https://test.com/s1.xml</loc>' +
'<lastmod>2018-11-26T00:00:00.000Z</lastmod>' +
'</sitemap>' +
'</sitemapindex>'

const result = buildSitemapIndex({
urls: [
Expand All @@ -113,6 +117,7 @@ describe('sitemapIndex', () => {

expect(result).toBe(expectedResult)
})

it('simple sitemap index', async () => {
const tmp = tmpdir()
const url1 = 'http://ya.ru'
Expand All @@ -123,40 +128,41 @@ describe('sitemapIndex', () => {
tmp + '/sm-test-index.xml'
]

expect(
function () {
createSitemapIndex({
cacheTime: 600000,
hostname: 'https://www.sitemap.org',
sitemapName: 'sm-test',
sitemapSize: 1,
targetFolder: '/tmp2',
urls: [url1, url2]
})
}
).toThrowError(/Target folder must exist/)
expect(function() {
createSitemapIndex({
cacheTime: 600000,
hostname: 'https://www.sitemap.org',
sitemapName: 'sm-test',
sitemapSize: 1,
targetFolder: '/tmp2',
urls: [url1, url2]
})
}).toThrowError(/Target folder must exist/)

// Cleanup before run test
removeFilesArray(expectedFiles)

const [err, result] = await new Promise(resolve => {
const [err, result] = await new Promise((resolve): void => {
createSitemapIndex({
cacheTime: 600000,
hostname: 'https://www.sitemap.org',
sitemapName: 'sm-test',
sitemapSize: 1,
targetFolder: tmp,
urls: [url1, url2],
callback: (error, result) => { resolve([error, result]) }
callback: (error, result) => {
resolve([error, result])
}
})
})

expect(err).toBeFalsy()
expect(result).toBe(true)
expectedFiles.forEach(function (expectedFile) {
expectedFiles.forEach(function(expectedFile) {
expect(existsSync(expectedFile)).toBe(true)
})
})

it('sitemap without callback', () => {
createSitemapIndex({
cacheTime: 600000,
Expand All @@ -167,6 +173,7 @@ describe('sitemapIndex', () => {
urls: ['http://ya.ru', 'http://ya2.ru']
})
})

it('sitemap with gzip files', async () => {
const tmp = tmpdir()
const url1 = 'http://ya.ru'
Expand All @@ -180,7 +187,7 @@ describe('sitemapIndex', () => {
// Cleanup before run test
removeFilesArray(expectedFiles)

const [err, result] = await new Promise(resolve => {
const [err, result] = await new Promise((resolve): void => {
createSitemapIndex({
cacheTime: 600000,
hostname: 'http://www.sitemap.org',
Expand All @@ -189,12 +196,14 @@ describe('sitemapIndex', () => {
targetFolder: tmp,
gzip: true,
urls: [url1, url2],
callback: (error, result) => { resolve([error, result]) }
callback: (error, result) => {
resolve([error, result])
}
})
})
expect(err).toBeFalsy()
expect(result).toBe(true)
expectedFiles.forEach(function (expectedFile) {
expectedFiles.forEach(function(expectedFile) {
expect(existsSync(expectedFile)).toBe(true)
})
})
Expand Down
3 changes: 3 additions & 0 deletions tests/sitemap-item.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('sitemapItem', () => {
xmlLoc = '<loc>http://ya.ru/</loc>'
xmlPriority = '<priority>0.9</priority>'
})

it('default values && escape', () => {
const url = 'http://ya.ru/view?widget=3&count>2'
const smi = new SitemapItem({ ...itemTemplate, 'url': url })
Expand All @@ -25,6 +26,7 @@ describe('sitemapItem', () => {
'<loc>http://ya.ru/view?widget=3&amp;count&gt;2</loc>' +
'</url>')
})

it('properly handles url fragments', () => {
const url = 'http://ya.ru/#!/home'
const smi = new SitemapItem({ ...itemTemplate, 'url': url })
Expand All @@ -41,6 +43,7 @@ describe('sitemapItem', () => {
function () { new SitemapItem(undefined, undefined, ErrorLevel.THROW) }
).toThrowError(/SitemapItem requires a configuration/)
})

it('throws an error for url absence', () => {
/* eslint-disable no-new */
expect(() => new SitemapItem({} as SitemapItemOptions, undefined, ErrorLevel.THROW))
Expand Down
Loading

0 comments on commit 9678baf

Please sign in to comment.