Skip to content

Commit

Permalink
break up tests, add back lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
derduher committed Jul 5, 2019
1 parent 0822339 commit 9f1a8c0
Show file tree
Hide file tree
Showing 11 changed files with 8,384 additions and 1,786 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dist
coverage/*
.nyc_output/

package-lock.json
/yarn.lock
/.eslintrc.json.tpl
/.browserslistrc
Expand Down
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Makefile
*.old
*.log
tsconfig.json
package-lock.json
test
.github
.gitkeep
Expand Down
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package-lock=false
package-lock=true
19 changes: 10 additions & 9 deletions lib/sitemap-item.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ut from './utils';
import fs from 'fs';
import builder from 'xmlbuilder';
import { create, XMLElement } from 'xmlbuilder';
import isArray from 'lodash/isArray';
import {
ChangeFreqInvalidError,
Expand Down Expand Up @@ -77,8 +77,8 @@ class SitemapItem {
mobile?: SitemapItemOptions["mobile"];
video?: SitemapItemOptions["video"];
ampLink?: SitemapItemOptions["ampLink"];
root: builder.XMLElement;
url: builder.XMLElement;
root: XMLElement;
url: XMLElement;

constructor (conf: SitemapItemOptions) {
this.conf = conf
Expand Down Expand Up @@ -148,7 +148,7 @@ class SitemapItem {
this.mobile = conf.mobile
this.video = conf.video
this.ampLink = conf.ampLink
this.root = conf.root || builder.create('root')
this.root = conf.root || create('root')
this.url = this.root.element('url')
}

Expand Down Expand Up @@ -249,7 +249,7 @@ class SitemapItem {
}
}

buildXML (): builder.XMLElement {
buildXML (): XMLElement {
this.url.children = []
// @ts-ignore
this.url.attribs = {}
Expand All @@ -266,7 +266,7 @@ class SitemapItem {

if (this.img && p === 'img') {
// Image handling
if (typeof (this.img) !== 'object' || this.img.length === undefined) {
if (!Array.isArray(this.img)) {
// make it an array
this.img = [this.img]
}
Expand All @@ -275,10 +275,11 @@ class SitemapItem {
if (typeof (image) !== 'object') {
// it’s a string
// make it an object
xmlObj['image:loc'] = image
} else if (image.url) {
xmlObj['image:loc'] = image.url
image = {url: image}
}

xmlObj['image:loc'] = image.url

if (image.caption) {
xmlObj['image:caption'] = {'#cdata': image.caption}
}
Expand Down
24 changes: 13 additions & 11 deletions lib/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

import * as errors from './errors';
import fs from 'fs';
import builder from 'xmlbuilder';
import { create, XMLElement } from 'xmlbuilder';
import SitemapItem from './sitemap-item';
import chunk from 'lodash/chunk';
import { Profiler } from 'inspector';
import { ICallback, ISitemapImg, SitemapItemOptions } from './types';
import { ICallback, SitemapItemOptions } from './types';
import zlib from 'zlib';
// remove once we drop node 8
import { URL } from 'whatwg-url'
Expand All @@ -32,7 +32,7 @@ export const version = '2.2.0'
* @return {Sitemap}
*/
export function createSitemap(conf: {
urls: string | Sitemap["urls"];
urls?: string | Sitemap["urls"];
hostname?: string;
cacheTime?: number;
xslUrl?: string;
Expand All @@ -55,7 +55,7 @@ export class Sitemap {
cacheResetPeriod: number;
cache: string;
xslUrl?: string;
root: builder.XMLElement;
root: XMLElement;


/**
Expand Down Expand Up @@ -85,7 +85,7 @@ export class Sitemap {
this.cache = '';

this.xslUrl = xslUrl;
this.root = builder.create('urlset', {encoding: 'UTF-8'})
this.root = create('urlset', {encoding: 'UTF-8'})
if (xmlNs) {
this.xmlNs = xmlNs;
const ns = this.xmlNs.split(' ')
Expand Down Expand Up @@ -167,7 +167,7 @@ export class Sitemap {
* Create sitemap xml
* @param {Function} callback Callback function with one argument — xml
*/
toXML (callback: ICallback<Error, string>): string|void {
toXML (callback?: ICallback<Error, string>): string|void {
if (typeof callback === 'undefined') {
return this.toString();
}
Expand Down Expand Up @@ -219,14 +219,16 @@ export class Sitemap {
if (smi.img) {
if (typeof smi.img === 'string') {
// string -> array of objects
smi.img = [{ url: smi.img as string }];
}
if (typeof smi.img === 'object' && smi.img.length === undefined) {
smi.img = [{ url: smi.img }];
} else if (!Array.isArray(smi.img)) {
// object -> array of objects
smi.img = [smi.img as ISitemapImg];
smi.img = [smi.img];
}
// prepend hostname to all image urls
(smi.img as ISitemapImg[]).forEach((img): void => {
smi.img.forEach((img): void => {
if (typeof img === 'string') {
img = {url: img}
}
img.url = (new URL(img.url, this.hostname)).toString();
});
}
Expand Down
15 changes: 7 additions & 8 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ export interface INewsItem {

export interface ISitemapImg {
url: string;
caption: string;
title: string;
geoLocation: string;
license: string;
length?: never;
caption?: string;
title?: string;
geoLocation?: string;
license?: string;
}

export interface IVideoItem {
Expand All @@ -61,7 +60,7 @@ export interface IVideoItem {
description: string;
content_loc?: string;
player_loc?: string;
'player_loc:autoplay': boolean;
'player_loc:autoplay'?: string;
duration?: number;
expiration_date?: string;
rating?: string | number;
Expand All @@ -71,7 +70,7 @@ export interface IVideoItem {
tag?: string | string[];
category?: string;
restriction?: string;
'restriction:relationship': string;
'restriction:relationship'?: string;
gallery_loc?: string;
'gallery_loc:title'?: string;
price?: string;
Expand Down Expand Up @@ -100,7 +99,7 @@ export interface SitemapItemOptions {
fullPrecisionPriority?: boolean;
priority?: number;
news?: INewsItem;
img?: Partial<ISitemapImg> | Partial<ISitemapImg>[];
img?: string | ISitemapImg | (string | ISitemapImg)[];
links?: ILinkItem[];
expires?: string;
androidLink?: string;
Expand Down
Loading

0 comments on commit 9f1a8c0

Please sign in to comment.