4 changed files with 72 additions and 53 deletions
@ -1,6 +1,36 @@
|
||||
const { pathOr, hasPath } = require('ramda'); |
||||
const parseArgs = require('minimist'); |
||||
const process = require('process'); |
||||
|
||||
export const event_url = (event_id) => |
||||
`https://www.facebook.com/events/${event_id}`; |
||||
|
||||
const page_url = (page_id) => `https://www.facebook.com/${page_id}`; |
||||
|
||||
export const page_events_url = (page_id) => page_url(page_id) + '/events/'; |
||||
|
||||
export const parse_args = (args) => { |
||||
const argv = parseArgs(args); |
||||
|
||||
const has_help_param = |
||||
hasPath(['h'], argv) || hasPath(['help'], argv) || hasPath(['?'], argv); |
||||
if (has_help_param) { |
||||
process.exit(1); |
||||
} |
||||
|
||||
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) |
||||
.split(',') |
||||
.filter(away_empty_strings) |
||||
.map(page_id_to_page_events_url); |
||||
|
||||
return { |
||||
page_ids: [ |
||||
...parse_param('page'), |
||||
...parse_param('p'), |
||||
...parse_param('pages'), |
||||
], |
||||
}; |
||||
}; |
||||
|
||||
@ -0,0 +1,32 @@
|
||||
import { parse_args } from '../src/logic'; |
||||
|
||||
const process = require('process'); |
||||
const process_mock = jest.spyOn(process, 'exit').mockImplementation(() => true); |
||||
|
||||
describe('test parse args', () => { |
||||
it('parses a single page id with -p', () => { |
||||
const res = parse_args(['-p', 'foo']); |
||||
expect(res).toEqual({ page_ids: ['https://www.facebook.com/foo/events/'] }); |
||||
}); |
||||
|
||||
it('parses a single page id with --page', () => { |
||||
const res = parse_args(['--page=foo']); |
||||
expect(res).toEqual({ page_ids: ['https://www.facebook.com/foo/events/'] }); |
||||
}); |
||||
|
||||
it('parses multiple page ids with --pages', () => { |
||||
const res = parse_args(['--pages=foo,bar']); |
||||
expect(res).toEqual({ |
||||
page_ids: [ |
||||
'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]); |
||||
expect(process_mock).toHaveBeenCalledWith(1); |
||||
}); |
||||
}); |
||||
}); |
||||
Loading…
Reference in new issue