Browse Source

make sure more formats of parameters are vali

fix-broken-scrape
Jørgen Lien Sellæg 6 years ago
parent
commit
8640f0d9ce
  1. 27
      src/logic.js
  2. 33
      tests/parse_args.test.js

27
src/logic.js

@ -24,16 +24,32 @@ const gm = require('gm').subClass({ imageMagick: true });
const puppeteer = require('puppeteer');
const flatten_string = (page_id) => {
if (page_id.startsWith('"') && page_id.endsWith('"')) {
return page_id.slice(1, page_id.length - 1);
}
if (page_id.startsWith("'") && page_id.endsWith("'")) {
return page_id.slice(1, page_id.length - 1);
}
return page_id;
};
const parse_output = (argv) => {
const [res = null] = props(['output', 'o'], argv).filter(
let [res = ''] = props(['output', 'o'], argv).filter(
(item) => item !== undefined,
);
res = flatten_string(res);
if (res === '') {
res = null;
}
return res;
};
const parse_args = (args) => {
const argv = parseArgs(args);
const has_help_param =
hasPath(['h'], argv) || hasPath(['help'], argv) || hasPath(['?'], argv);
if (has_help_param) {
@ -43,12 +59,15 @@ const parse_args = (args) => {
const away_empty_strings = (str) => str.length !== 0;
const page_id_to_page_events_url = page_events_url;
const parse_param = (param) =>
pathOr('', [param], argv)
flatten_string(pathOr('', [param], argv))
.split(',')
.filter(away_empty_strings)
.map(page_id_to_page_events_url);
const events = pathOr(null, ['events'], argv);
let events = flatten_string(pathOr('', ['events'], argv));
if (events === '') {
events = null;
}
const output = parse_output(argv);
return {

33
tests/parse_args.test.js

@ -9,11 +9,21 @@ describe('test parse args', () => {
expect(res.page_ids).toEqual(['https://www.facebook.com/foo/events/']);
});
it('parses a single page id with -p', () => {
const res = parse_args(['-p', '"foo"']);
expect(res.page_ids).toEqual(['https://www.facebook.com/foo/events/']);
});
it('parses a single page id with --page', () => {
const res = parse_args(['--page=foo']);
expect(res.page_ids).toEqual(['https://www.facebook.com/foo/events/']);
});
it('parses a single page id with --page', () => {
const res = parse_args(['--page="foo"']);
expect(res.page_ids).toEqual(['https://www.facebook.com/foo/events/']);
});
it('parses multiple page ids with --pages', () => {
const res = parse_args(['--pages=foo,bar']);
expect(res.page_ids).toEqual([
@ -22,6 +32,14 @@ describe('test parse args', () => {
]);
});
it('parses multiple page ids with --pages', () => {
const res = parse_args(['--pages="foo,bar"']);
expect(res.page_ids).toEqual([
'https://www.facebook.com/foo/events/',
'https://www.facebook.com/bar/events/',
]);
});
['-?', '--help', '-h'].forEach((param) => {
it('parses help options', () => {
const res = parse_args([param]);
@ -34,6 +52,11 @@ describe('test parse args', () => {
expect(res.events).toEqual('events.json');
});
it('parses event option', () => {
const res = parse_args(['--events="events.json"']);
expect(res.events).toEqual('events.json');
});
it('sets events to null if no option is passed', () => {
const res = parse_args(['']);
expect(res.events).toEqual(null);
@ -49,8 +72,18 @@ describe('test parse args', () => {
expect(res.output).toEqual('jacobi');
});
it('passes output if it is set with --output', () => {
const res = parse_args(['--output="jacobi"']);
expect(res.output).toEqual('jacobi');
});
it('passes output if it is set with -o', () => {
const res = parse_args(['-o', 'jacobi']);
expect(res.output).toEqual('jacobi');
});
it('passes output if it is set with -o', () => {
const res = parse_args(['-o', '"jacobi"']);
expect(res.output).toEqual('jacobi');
});
});

Loading…
Cancel
Save