Skip to content

Commit

Permalink
feat: view random snippet
Browse files Browse the repository at this point in the history
This closes: #224.
  • Loading branch information
sQVe committed Jun 12, 2019
1 parent 89da9c4 commit 66993f0
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 28 deletions.
42 changes: 31 additions & 11 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
import program from 'commander'
import { reduce } from 'ramda'

import {
getSnippet,
getSnippetsByTag,
randomSnippet,
searchSnippets,
} from './handler'
import snippets from '../lib/snippets.json'
import { getSnippet, getSnippetsByTag, searchSnippets } from './handler'
import { printSnippet } from './printer'
import { version } from '../package.json'

const isTest = process.env.NODE_ENV === 'test'
const actions = {
tag: (id, opts) => printSnippet (opts, getSnippetsByTag (snippets, id)),
view: (id, opts) => printSnippet (opts, getSnippet (snippets, id)),
search: (query, opts) => printSnippet (opts, searchSnippets (snippets, query)),
random: opts => printSnippet (opts, randomSnippet (snippets)),
search: (opts, query) => printSnippet (opts, searchSnippets (snippets, query)),
tag: (opts, id) => printSnippet (opts, getSnippetsByTag (snippets, id)),
view: (opts, id) => printSnippet (opts, getSnippet (snippets, id)),
}
const addCommand = settings =>
reduce ((acc, [key, ...args]) => acc[key] (...args), program, settings)
Expand All @@ -31,9 +37,12 @@ const addAction = action => [
console.log (
JSON.stringify ([action, input, !!opts.cp, !!opts.json, opts.layout])
)
: actions[action] (input, opts)
: action === 'random'
? actions[action] (input)
: actions[action] (opts, input)
},
]

const commonOptions = [
['option', '-c, --cp', 'copy code to clipboard', false],
['option', '-j, --json', 'output in json format', false],
Expand All @@ -42,6 +51,14 @@ const commonOptions = [

program.version (version)

addCommand ([
['command', 'r'],
['alias', 'random'],
['description', ['view random snippet']],
...commonOptions,
addAction ('random'),
])

addCommand ([
['command', 's [query]'],
['alias', 'search'],
Expand Down Expand Up @@ -71,14 +88,17 @@ program.on ('--help', () =>
[
'',
'Examples:',
' v head',
' view head',
' 30s v head',
' 30s view head',
'',
' 30s r',
' 30s random',
'',
' s -j flatten',
' search --json flatten',
' 30s s -j flatten',
' 30s search --json flatten',
'',
' t -l ic array',
' tag --layout ic array',
' 30s t -l ic array',
' 30s tag --layout ic array',
].join ('\n')
)
)
Expand Down
5 changes: 4 additions & 1 deletion src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ import {
sort,
} from 'ramda'

import { startsOrEndsWith } from './helpers'
import { randomNumberInRange, startsOrEndsWith } from './helpers'

const fuzzyMatch = query => x => new RegExp (query).test (x.id)
export const getSnippet = (snippets, id) => find (x => x.id === id, snippets)
export const getSnippetsByTag = (snippets, id) =>
filter (x => any (y => y === id, x.tags), snippets)

export const randomSnippet = snippets =>
snippets[randomNumberInRange (0, snippets.length - 1)]

const computeRelevance = (id, query) => {
const base = query.length * 10
const diffInChars = id.replace (query, '').length + 1
Expand Down
8 changes: 6 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { anyPass, compose, endsWith, is, replace, startsWith } from 'ramda'

export const startsOrEndsWith = (val, query) =>
anyPass ([startsWith, endsWith]) (query, val)
export const enforceSingleNewLine = x =>
is (String, x)
? compose (
replace (/[\r\n]+/g, '\n'),
replace (/[\r\n]*$/, '')
) (x)
: x

export const randomNumberInRange = (min, max) =>
Math.floor (Math.random () * (max - min + 1)) + min

export const startsOrEndsWith = (val, query) =>
anyPass ([startsWith, endsWith]) (query, val)
15 changes: 14 additions & 1 deletion test/handler.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
global.Math.random = () => 0.6

const snippets = [
{ id: 'bar', tags: ['array'] },
{ id: 'foobar', tags: ['array'] },
Expand All @@ -6,7 +8,12 @@ const snippets = [
{ id: 'bazfoo', tags: ['object'] },
]

import { getSnippet, getSnippetsByTag, searchSnippets } from '../src/handler'
import {
getSnippet,
getSnippetsByTag,
randomSnippet,
searchSnippets,
} from '../src/handler'

describe('Handler', () => {
describe('getSnippet', () => {
Expand All @@ -21,6 +28,12 @@ describe('Handler', () => {
})
})

describe('randomSnippet', () => {
it('should get random snippet', () => {
expect(randomSnippet(snippets)).toEqual(snippets[3])
})
})

describe('searchSnippets', () => {
it('should find snippets fuzzy matching query', () => {
expect(searchSnippets(snippets, 'foo')).toMatchSnapshot()
Expand Down
56 changes: 44 additions & 12 deletions test/integration/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ Options:
-h, --help output usage information
Commands:
r|random [options] view random snippet
s|search [options] [query] fuzzy search snippets by id
t|tag [options] [id] view snippets by tag
v|view [options] [id] view snippet with id
Examples:
v head
view head
30s v head
30s view head
s -j flatten
search --json flatten
30s r
30s random
t -l ic array
tag --layout ic array"
30s s -j flatten
30s search --json flatten
30s t -l ic array
30s tag --layout ic array"
`;

exports[`Cli help option should print help page 2`] = `
Expand All @@ -31,19 +35,47 @@ Options:
-h, --help output usage information
Commands:
r|random [options] view random snippet
s|search [options] [query] fuzzy search snippets by id
t|tag [options] [id] view snippets by tag
v|view [options] [id] view snippet with id
Examples:
v head
view head
30s v head
30s view head
30s r
30s random
30s s -j flatten
30s search --json flatten
30s t -l ic array
30s tag --layout ic array"
`;

exports[`Cli help option should print help page for random command 1`] = `
"Usage: r|random [options]
view random snippet
Options:
-c, --cp copy code to clipboard
-j, --json output in json format
-l, --layout <layout> print in specified layout (default: \\"itced\\")
-h, --help output usage information"
`;
exports[`Cli help option should print help page for random command 2`] = `
"Usage: r|random [options]
s -j flatten
search --json flatten
view random snippet
t -l ic array
tag --layout ic array"
Options:
-c, --cp copy code to clipboard
-j, --json output in json format
-l, --layout <layout> print in specified layout (default: \\"itced\\")
-h, --help output usage information"
`;
exports[`Cli help option should print help page for search command 1`] = `
Expand Down
2 changes: 1 addition & 1 deletion test/integration/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const callCli = (...args) =>
spawnSync('node', [cliPath, ...args])
.stdout.toString()
.trim()
const commands = ['search', 'view', 'tag']
const commands = ['random', 'search', 'view', 'tag']

describe('Cli', () => {
describe('version option', () => {
Expand Down

0 comments on commit 66993f0

Please sign in to comment.